-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpongturtle.py
More file actions
142 lines (111 loc) · 3.09 KB
/
pongturtle.py
File metadata and controls
142 lines (111 loc) · 3.09 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
#
#
# CODIGO FUENTE DEL JUEGO DE PONG EN PYTHON USANDO EL MÓDULO TURTLE.
# FAVOR DE LEER BIEN LOS COMENTARIOS PARA CONOCER BIEN EL CÓDIGO.
#
# NOTA: ES IMPORTANTE TENER UNA VERSIÓN DE PYTHON 3.6+ O LA PARTE DEL MARCADOR TE DARÁ PROBLEMAS.
#
#
#
import turtle
#Ventana
wn = turtle.Screen()
wn.title("Pong by Mundo Python")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
#Marcador
marcadorA = 0
marcadorB = 0
#JugadorA
jugadorA = turtle.Turtle()
jugadorA.speed(0)
jugadorA.shape("square")
jugadorA.color("white")
jugadorA.penup()
jugadorA.goto(-350, 0)
jugadorA.shapesize(stretch_wid=5, stretch_len=1)
#JugadorA
jugadorB = turtle.Turtle()
jugadorB.speed(0)
jugadorB.shape("square")
jugadorB.color("white")
jugadorB.penup()
jugadorB.goto(350, 0)
jugadorB.shapesize(stretch_wid=5, stretch_len=1)
#Pelota
pelota = turtle.Turtle()
pelota.speed(0)
pelota.shape("square")
pelota.color("white")
pelota.penup()
pelota.goto(0,0)
#Modificar estas variables para cambiar la velocidad de la pelota
pelota.dx = 3
pelota.dy = 3
#Pen para dibujar el marcador.
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Jugador A: 0 jugadorB: 0", align="center", font=("Courier", 25, "normal"))
#Funciones
def jugadorA_up():
y = jugadorA.ycor()
y += 20
jugadorA.sety(y)
def jugadorA_down():
y = jugadorA.ycor()
y -= 20
jugadorA.sety(y)
def jugadorB_up():
y = jugadorB.ycor()
y += 20
jugadorB.sety(y)
def jugadorB_down():
y = jugadorB.ycor()
y -= 20
jugadorB.sety(y)
#Teclado
wn.listen()
wn.onkeypress(jugadorA_up, "w")
wn.onkeypress(jugadorA_down, "s")
wn.onkeypress(jugadorB_up, "Up")
wn.onkeypress(jugadorB_down, "Down")
while True:
wn.update()
pelota.setx(pelota.xcor() + pelota.dx)
pelota.sety(pelota.ycor() + pelota.dy)
#Revisa colisiones con los bordes de la ventana
if pelota.ycor() > 290:
pelota.dy *= -1
if pelota.ycor() < -290:
pelota.dy *= -1
# Si la pelota sale por la izq o derecha, esta regresa al centro.
if pelota.xcor() > 390:
pelota.goto(0,0)
pelota.dx *= -1
marcadorA += 1
pen.clear()
#Esta línea de codigo vuelve a pintar el marcador, utilizo "format" de la versión 3.6 en adelante de python.
#Si tienes python menor a la versión 3.6 esta parte no te funcionará.
pen.write(f"Jugador A: {marcadorA} jugadorB: {marcadorB}", align="center", font=("Courier", 25, "normal"))
if pelota.xcor() < -390:
pelota.goto(0,0)
pelota.dx *= -1
marcadorB += 1
pen.clear()
#Esta línea de codigo vuelve a pintar el marcador, utilizo "format" de la versión 3.6 en adelante de python.
#Si tienes python menor a la versión 3.6 esta parte no te funcionará.
pen.write(f"Jugador A: {marcadorA} jugadorB: {marcadorB}", align="center", font=("Courier", 25, "normal"))
#Revisa las colisiones
if ((pelota.xcor() > 340 and pelota.xcor() < 350)
and (pelota.ycor() < jugadorB.ycor() + 50
and pelota.ycor() > jugadorB.ycor() - 50)):
pelota.dx *= -1
if ((pelota.xcor() < -340 and pelota.xcor() > -350)
and (pelota.ycor() < jugadorA.ycor() + 50
and pelota.ycor() > jugadorA.ycor() - 50)):
pelota.dx *= -1