-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (123 loc) · 3.85 KB
/
main.py
File metadata and controls
154 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
from power_bar import Bar
import time
game_is_on = True
power_pos1 = -880
power_pos2 = 380
full_bar1 = -280
full_bar2 = 980
energy_bar1 = []
energy_bar2 = []
my_screen = Screen()
my_screen.setup(height = 1500, width = 2500)
my_screen.bgcolor("black")
my_screen.title("Pong")
my_screen.tracer(0)
#Write the borderline
border_line = Turtle()
border_line.hideturtle()
border_line.pensize(10)
border_line.penup()
border_line.color("white")
border_line.goto(0, -1000)
border_line.setheading(90)
#Draw the power bar for each paddle (930, -740), (-930, -740)
def draw_bar(pos, width = 40, length = 600):
frame = Turtle()
frame.pensize()
frame.hideturtle()
frame.penup()
frame.color("white")
frame.goto(pos)
frame.setheading(90)
frame.pendown()
for line in range(2):
frame.forward(width)
frame.left(90)
frame.forward(length)
frame.left(90)
return frame
bar1 = draw_bar((930, -740))
bar2 = draw_bar((-330, -740))
while int(border_line.ycor()) < 1050:
border_line.pendown()
border_line.forward(40)
border_line.penup()
border_line.forward(40)
player1 = Paddle((-1220, 0))
player2 = Paddle((1220, 0))
ball = Ball()
player1_score = Scoreboard((-612, 690))
player2_score = Scoreboard((612, 690))
my_screen.listen()
my_screen.onkey(player1.go_up, "w")
my_screen.onkey(player1.go_down, "s")
my_screen.onkey(player2.go_up, "Up")
my_screen.onkey(player2.go_down, "Down")
my_screen.onkey(player1.smash, "d")
my_screen.onkey(player2.smash, "Left")
while game_is_on:
time.sleep(0.03)
ball.move()
if ball.ycor() > 720 or ball.ycor() < -720:
ball.bounce_y()
# if player2.xcor() - 25 < ball.xcor() < player2.xcor() + 25 and player2.ycor() - 120 < ball.ycor() < player2.ycor() + 120:
if 10 < ball.distance(player2) < 90 and ball.xcor() > 1180:
ball.bounce_x()
# elif player1.xcor() - 25 < ball.xcor() < player1.xcor() + 25 and player1.ycor() - 120 < ball.ycor() < player1.ycor() + 120:
elif 10 < ball.distance(player1) < 90 and ball.xcor() < -1180:
ball.bounce_x()
elif ball.xcor() > 1260:
player1_score.clear_score()
player1_score.score_update()
player1.energy_full = False
ball.move_x = 20
ball.goto(0, 0)
if power_pos1 < full_bar1:
power_bar1 = Bar((power_pos1, -720))
energy_bar1.append(power_bar1)
power_pos1 += 100
elif ball.xcor() < -1260:
player2_score.clear_score()
player2_score.score_update()
player2.energy_full = False
ball.move_x = -20
ball.goto(0, 0)
if power_pos2 < full_bar2:
power_bar2 = Bar((power_pos2, -720))
energy_bar2.append(power_bar2)
power_pos2 += 100
if power_pos1 == full_bar1:
player1.energy_full = True
if player1.xcor() > -1220:
if player1.energy_full:
ball.move_x = 100
power_pos1 = -880
for bar in energy_bar1:
bar.hideturtle()
player1.move_x = 1
player1.setx(player1.xcor() - player1.move_x)
else:
player1.move_x = 20
if power_pos2 == full_bar2:
player2.energy_full = True
if player2.xcor() < 1220:
if player2.energy_full:
ball.move_x = -100
power_pos2 = 380
for bar in energy_bar2:
bar.hideturtle()
player2.move_x = 1
player2.setx(player2.xcor() + player2.move_x)
else:
player2.move_x = 20
if player1_score.score > 21 or player2_score.score > 21:
game_is_on = False
game_over = Scoreboard((-20, -100))
game_over.clear()
game_over.write("Game Over", align= "center", font = ("Arial", 20, "normal"))
my_screen.update()
my_screen.exitonclick()