forked from jucimarjr/mypong
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mypong.py
279 lines (246 loc) · 8.38 KB
/
mypong.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Jucimar Jr 2019
# pong em turtle python https://docs.python.org/3.3/library/turtle.html
# baseado em http://christianthompson.com/node/51
# fonte Press Start 2P https://www.fontspace.com/codeman38/press-start-2p
# som pontuação https://freesound.org/people/Kodack/sounds/258020/
# python mypong.py -1 para modo um jogador
# python mypong.py -2 para modo dois jogadores
import turtle
import os
import sys
import math
player = sys.argv[1]
alt_paddle = 5
larg_paddle = 1
# tamanho(altura) de um segmento da raquete
TAM_UM_SEG = (alt_paddle*10)/4
# desenhar tela
screen = turtle.Screen()
screen.title("My Pong")
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer()
# desenhar raquete 1
paddle_1 = turtle.Turtle()
paddle_1.speed(0)
paddle_1.shape("square")
paddle_1.color("green")
paddle_1.shapesize(stretch_wid=alt_paddle, stretch_len=larg_paddle)
paddle_1.penup()
paddle_1.goto(-350, 0)
# desenhar raquete 2
paddle_2 = turtle.Turtle()
paddle_2.speed(0)
paddle_2.shape("square")
paddle_2.color("blue")
paddle_2.shapesize(stretch_wid=alt_paddle, stretch_len=larg_paddle)
paddle_2.penup()
paddle_2.goto(350, 0)
# desenhar bola
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("purple")
ball.penup()
ball.goto(0, 0)
vel = 4
ball.dx = vel * math.cos(math.radians(45))
ball.dy = vel * math.sin(math.radians(45))
# pontuação
score_1 = 0
score_2 = 0
# fonte de vitória
win = turtle.Turtle()
win.speed(0)
win.shape("square")
win.color("white")
win.shapesize(stretch_wid=10, stretch_len=5)
win.penup()
win.hideturtle()
win.goto(0, 0)
# head-up display da pontuação
hud = turtle.Turtle()
hud.speed(0)
hud.shape("square")
hud.color("white")
hud.penup()
hud.hideturtle()
hud.goto(0, 260)
hud.write("0 : 0", align="center", font=("Press Start 2P", 24, "normal"))
# movimentação raquete 1
if player != '-1':
def paddle_1_up():
y = paddle_1.ycor()
if y < 250:
y += 20
else:
y = 250
paddle_1.sety(y)
def paddle_1_down():
y = paddle_1.ycor()
if y > -250:
y += -20
else:
y = -250
paddle_1.sety(y)
# movimentação raquete 2
def paddle_2_up():
y = paddle_2.ycor()
if y < 250:
y += 20
else:
y = 250
paddle_2.sety(y)
def paddle_2_down():
y = paddle_2.ycor()
if y > -250:
y += -20
else:
y = -250
paddle_2.sety(y)
# ângulo de direção da bola
def direction_angle(angle):
ball.dx = vel * math.cos(math.radians(angle))
ball.dy = vel * math.sin(math.radians(angle))
# reiniciando o jogo
def restart():
paddle_2.goto(350, 0)
paddle_1.goto(-350, 0)
ball.goto(0, 0)
global vel
vel = 4
ball.dx = vel * math.cos(math.radians(45))
ball.dy = vel * math.sin(math.radians(45))
direction_angle(45)
hud.clear()
global score_1
global score_2
score_1 = 0
score_2 = 0
hud.write("{} : {}".format(score_1, score_2),
align="center", font=("Press Start 2P", 24, "normal"))
# mapeando as teclas
screen.listen()
if player != '-1':
screen.onkeypress(paddle_1_up, "w")
screen.onkeypress(paddle_1_down, "s")
screen.onkeypress(paddle_2_up, "Up")
screen.onkeypress(paddle_2_down, "Down")
screen.onkeypress(restart, "space")
while True:
screen.update()
# definindo a posição da raquete2 no modo 1 jogador
if player == '-1':
paddle_1.sety(ball.ycor())
# movimentação da bola
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# colisão com parede superior
if ball.ycor() > 290:
os.system("aplay bounce.wav&")
ball.sety(290)
ball.dy *= -1
# colisão com parede inferior
if ball.ycor() < -280:
os.system("aplay bounce.wav&")
ball.sety(-280)
ball.dy *= -1
# colisão com parede esquerda
if ball.xcor() < -390:
score_2 += 1
vel = 4
hud.clear()
hud.write("{} : {}".format(score_1, score_2),
align="center", font=("Press Start 2P", 24, "normal"))
os.system("aplay 258020__kodack__arcade-bleep-sound.wav&")
ball.goto(0, 0)
direction_angle(45)
# colisão com parede direita
if ball.xcor() > 390:
score_1 += 1
vel = 4
hud.clear()
hud.write("{} : {}".format(score_1, score_2),
align="center", font=("Press Start 2P", 24, "normal"))
os.system("aplay 258020__kodack__arcade-bleep-sound.wav&")
ball.goto(0, 0)
direction_angle(45)
ball.dx *= -1
ball.dy *= -1
# colisão com raquete 1
if ball.xcor() < -330 and ball.ycor() < paddle_1.ycor() + 50 and \
ball.ycor() > paddle_1.ycor() - 50:
if ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 4 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * 3:
direction_angle(45)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 3 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * 2:
direction_angle(30)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 2 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * 1:
direction_angle(15)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 1 and \
ball.ycor() > paddle_1.ycor() + TAM_UM_SEG * 0:
direction_angle(5)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 1 and \
ball.ycor() == paddle_1.ycor() + TAM_UM_SEG * 0:
direction_angle(0)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * 0 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * -1:
direction_angle(355)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * -1 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * -2:
direction_angle(345)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * -2 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * -3:
direction_angle(330)
elif ball.ycor() < paddle_1.ycor() + TAM_UM_SEG * -3 and \
ball.ycor() >= paddle_1.ycor() + TAM_UM_SEG * -4:
direction_angle(315)
vel += 1
os.system("aplay bounce.wav&")
# colisão com raquete 2
if ball.xcor() > 330 and ball.ycor() < paddle_2.ycor() + 50 and \
ball.ycor() > paddle_2.ycor() - 50:
if ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 4 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * 3:
direction_angle(135)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 3 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * 2:
direction_angle(150)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 2 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * 1:
direction_angle(165)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 1 and \
ball.ycor() > paddle_2.ycor() + TAM_UM_SEG * 0:
direction_angle(175)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 1 and \
ball.ycor() == paddle_2.ycor() + TAM_UM_SEG * 0:
direction_angle(180)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * 0 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * -1:
direction_angle(185)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * -1 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * -2:
direction_angle(195)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * -2 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * -3:
direction_angle(210)
elif ball.ycor() < paddle_2.ycor() + TAM_UM_SEG * -3 and \
ball.ycor() >= paddle_2.ycor() + TAM_UM_SEG * -4:
direction_angle(225)
vel += 1
os.system("aplay bounce.wav&")
# condição de vitória
if score_1 == 5 or score_2 == 5:
winner = 'player 1' if score_1 > score_2 else 'player 2'
score_1 = score_2 = 0
win.write("Victory {}".format(winner), align="center",
font=("Press Start 2P", 24, "normal"))
screen.textinput("Victory {}".format(winner),
"Press [ENTER] to restart")
win.clear()
hud.clear()
hud.write("{} : {}".format(score_1, score_2), align="center", font=(
"Press Start 2P", 24, "normal"))
screen.listen()