/
juego.py
274 lines (250 loc) · 8.46 KB
/
juego.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
import pygame
import tkinter
from tkinter import messagebox
from barra import Barra
from bola import Bola
from barra_doble import Barra_doble
import time
import random
import os
import serial
#ser = serial.Serial('/dev/cu.usbmodem1411', 9600, timeout=0)
BLACK = (0,0,0)
WHITE = (255,255,255)
L = 20
# Clase Juego:
# Atributos:
# pantalla
# matriz: [[],[],...,[]]
# score: int
# bola: Class
# nivel: int
# barra1: Class
# barra2: Class
# modo: str
# versus: str
#######################
# Metodos:
# crearMatriz()
# dibujarMatriz()
# cpu()
# jugar()
# dibujar()
# Constantes
ANCHO = 800
LARGO = 500
TIEMPO_NIVEL1 = 0.1
TIEMPO_NIVEL2 = 0.07
TIEMPO_NIVEL3 = 0.04
TAMAÑO_BARRA_1 = 9
TAMAÑO_BARRA_2 = 6
TAMAÑO_BARRA_3 = 3
TAMAÑO_BARRA_PRACTICA = 25
# Clase Juego que controla las mecanicas de juego
class Juego:
def __init__(self, modo, nivel, versus, tamaño=None, time=TIEMPO_NIVEL1):
pygame.init()
self.pantalla = pygame.display.set_mode((ANCHO,LARGO))
pygame.display.set_caption("Pong")
self.FILAS = 30
self.COLUMNAS = 40
self.matriz = []
self.crearMatriz()
self.score = 0
self.bola = Bola(20,12, random.randrange(-1, 2), True)
self.nivel = nivel
self.modo = modo
self.versus = versus
self.CPU = 0
self.tamaño = tamaño
self.time = time
# Se define el tiempo, tamaño de barra, modo y versus de cada nivel
if self.nivel == 1:
if self.time == None:
self.tiempo = TIEMPO_NIVEL1
else:
self.tiempo = time
if self.modo == "Single":
if self.tamaño == None:
self.barra1 = Barra(1,2,TAMAÑO_BARRA_1)
self.barra2 = Barra(38,2,TAMAÑO_BARRA_1)
else:
# Si se le define un tamaño es porque está en modo práctica
self.barra1 = Barra(1,2,self.tamaño)
self.barra2 = Barra(38,0,TAMAÑO_BARRA_PRACTICA)
if self.versus == "humano":
self.CPU = 0
elif self.versus == "cpu":
self.CPU = 1
else:
# La primer barra es la de la izquiera, la otra la de la derecha
if self.tamaño == None:
self.barra1 = Barra_doble(1,3,9,13,TAMAÑO_BARRA_1)
self.barra2 = Barra_doble(38,12,32,3,TAMAÑO_BARRA_1)
else:
self.barra1 = Barra_doble(1,3,9,13,self.tamaño)
self.barra2 = Barra(38,0,TAMAÑO_BARRA_PRACTICA)
if self.versus == "humano":
# Si se escoje "humano" no se llama la función cpu()
self.CPU = 0
elif self.versus == "cpu":
self.CPU = 1
# Se crea auna matriz binaria(compuesta por ahora por 0s), de 25 filas x 40 columnas
def crearMatriz(self):
for i in range(self.FILAS):
fila = []
for j in range(self.COLUMNAS):
# Llena la lista de ceros
fila.append(0)
self.matriz.append(fila)
# Se va dibujando la matriz cuadro pr cuadro y se plasma en la pantalla
def dibujarMatriz(self):
for fila in range(self.FILAS):
for columna in range(self.COLUMNAS):
if self.matriz[fila][columna] == 0:
# Si el cierta posición de la matriz hay un 0, se pinta de color negro
pygame.draw.rect(self.pantalla, BLACK, [L* columna,L * fila,L,L])
else:
# Si el cierta posición de la matriz hay un 0, se pinta de color blanco
# Esto es para la bola y las barras
pygame.draw.rect(self.pantalla, WHITE, [L* columna,L * fila,L,L])
# Define cada cuánto tiempo se va a actualizar la matriz
time.sleep(self.tiempo)
# Sibuja una línea blanca en medio de la pantalla
pygame.draw.line(self.pantalla, WHITE, [ANCHO//2, 0], [ANCHO//2,LARGO], 4)
# Barra controlada por la computadora
def cpu(self):
# La posicion en x de la bola es mayor a 28
if self.bola.get_x() > 28:
if self.bola.get_y() > self.barra2.get_y():
# Si la posición en y de la bola es mayor que la de la barra
# se mueve la barra hacia arriba o abajo siguiendo la bola
self.barra2.mover(-1, self.matriz)
else:
self.barra2.mover(1, self.matriz)
def jugar(self):
fuera_juego = False
# Genera múltiples eventos pygame.KEYDOWN
pygame.key.set_repeat(50, 50)
while not fuera_juego:
# Si el score de alguno de los jugadores es igual a 5
if self.versus != "practica":
if self.bola.get_score1() == 5 or self.bola.get_score2() == 5:
# Se reinician los scores
self.bola.set_score1(0)
self.bola.set_score2(0)
# Se pasa de nivel
self.nivel += 1
# Se limpia la matriz para dibujar las barras del siguiente nivel
self.matriz = []
# Se vuelve a crear la matriz
self.crearMatriz()
# Se definen las condiciones de acuerdo con cada nivel
if self.nivel == 1:
self.tiempo = TIEMPO_NIVEL1
if self.modo == "Single":
self.barra1 = Barra(1,2,TAMAÑO_BARRA_1)
self.barra2 = Barra(38,2,TAMAÑO_BARRA_1)
else:
self.barra1 = Barra_doble(1,3,9,13,TAMAÑO_BARRA_1)
self.barra2 = Barra_doble(38,12,32,3,TAMAÑO_BARRA_1)
if self.nivel == 2:
self.tiempo = TIEMPO_NIVEL2
if self.modo == "Single":
self.barra1 = Barra(1,2,TAMAÑO_BARRA_2)
self.barra2 = Barra(38,2,TAMAÑO_BARRA_2)
else:
self.barra1 = Barra_doble(1,3,9,13,TAMAÑO_BARRA_2)
self.barra2 = Barra_doble(38,12,32,3,TAMAÑO_BARRA_2)
if self.nivel == 3:
self.tiempo = TIEMPO_NIVEL3
if self.modo == "Single":
self.barra1 = Barra(1,2,TAMAÑO_BARRA_3)
self.barra2 = Barra(38,2,TAMAÑO_BARRA_3)
else:
self.barra1 = Barra_doble(1,3,9,13,TAMAÑO_BARRA_3)
self.barra2 = Barra_doble(38,12,32,3,TAMAÑO_BARRA_3)
# Si pierde en el nivel 3, vuelve al nivel 1
if self.nivel == 4:
if self.bola.get_score1() == 5:
font = pygame.font.Font(None, 48)
texto = font.render("¡Felicidades!", "Has aprobado INTRO & TALLER.\
\nListo para ALGORITMOS Y ESTRUCTURAS DE DATOS I. :D", True, (WHITE))
self.pantalla.blit(texto, (0, 0))
self.nivel = 0
else:
if self.bola.get_score1() == 100:
# Se reinician los scores
self.bola.set_score1(0)
# Se pasa de nivel
self.nivel += 1
# Se limpia la matriz para dibujar las barras del siguiente nivel
self.matriz = []
# Se vuelve a crear la matriz
self.crearMatriz()
if self.nivel == 1:
self.tiempo = self.time
if self.tamaño != None:
if self.modo == "Single":
self.barra1 = Barra(1,2,self.tamaño)
self.barra2 = Barra(38,0,TAMAÑO_BARRA_PRACTICA)
else:
self.barra1 = Barra_doble(1,3,7,13,self.tamaño)
self.barra2 = Barra(38,0,TAMAÑO_BARRA_PRACTICA)
# Eventos de las teclas
for event in pygame.event.get():
if event.type == pygame.QUIT: #is le da X, cierra todo
pygame.quit()
quit()
if event.type == pygame.KEYDOWN: #al presionar una tecla
if event.key == pygame.K_UP:
self.barra2.mover(1,self.matriz)
elif event.key == pygame.K_DOWN:
self.barra2.mover(-1,self.matriz)
elif event.key == pygame.K_w:
self.barra1.mover(1,self.matriz)
elif event.key == pygame.K_s:
self.barra1.mover(-1,self.matriz)
elif event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
# Aquí se actualiza constántemente la matriz para que
# ocurra el movimiento de forma continua
self.dibujarMatriz()
self.dibujar()
# Lee los estimulos del Arduino
#self.leerArduino()
# se llama la función cpu solo si la variable CPU es igual a 1
if self.CPU == 1:
self.cpu()
def dibujar(self):
# Se defne un texto para poner en la pantalla
font = pygame.font.Font(None, 100)
score1 = self.bola.get_score1()
score_text = font.render(str(score1), True,
(WHITE))
# Se coloca el texto en la pantalla
self.pantalla.blit(score_text, (150, 0))
score2 = self.bola.get_score2()
score_text2 = font.render(str(score2), True,
(WHITE))
self.pantalla.blit(score_text2, (620, 0))
# Coloca la bola en la matriz
self.bola.mover(self.matriz)
# Posiciona las barras en la matriz
self.barra1.posicionar(self.matriz)
self.barra2.posicionar(self.matriz)
pygame.display.update()
def leerArduino(self):
try:
entrada = str(ser.readline())
datos = entrada[entrada.index("") + 1: entrada.index("\\")]
comando = datos[:datos.index("%")]
print(comando)
if comando == "'P1_UP":
self.barra1.mover(1, self.matriz)
elif comando == "'P1_DOWN":
self.barra1.mover(-1, self.matriz)
except:
print('NO INPUT')
time.sleep(0.0001)