Repository navigation
Expand file tree
/
Copy pathdodge_asteroids_game.py
More file actions
164 lines (116 loc) · 3.86 KB
/
Copy pathdodge_asteroids_game.py
File metadata and controls
164 lines (116 loc) · 3.86 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
154
155
156
157
158
159
160
161
162
163
164
import random
import pygame
from pygame import mixer
from personaje import Ship
from enemigos import Asteroid
from premios import Premio
# Iniciamos pygame
pygame.init()
# Personalizar el Icono y Titulo (superiores)
icono = pygame.image.load('icono.png')
pygame.display.set_icon(icono)
pygame.display.set_caption("Asteroid's Rain")
# Musica de fondo
mixer.music.load('assets/musica_fondo.mp3')
mixer.music.set_volume(0.5)
mixer.music.play(-1)
# Poner el fondo
fondo = pygame.image.load('assets/fondo.png')
# Definimos propiedades de la ventana de juego
ancho = 1000
alto = 800
ventana = pygame.display.set_mode([ancho, alto])
textos = pygame.font.SysFont('Comics Sans', 45)
fuente_final = pygame.font.SysFont('Comics Sans', 60)
# Estandarizamos la velocidad del juego sin importar que tan potente sea el ordenador
fps = 60
# Indicador para que no cierre la ventana
jugando = True
se_ejecuta = True
# Reloj para control de sucesos
reloj = pygame.time.Clock()
tiempo_transcurrido = 0
# Bloque de personaje
nave = Ship(500, 400)
vidas = 3
puntaje = 0
# Bloque de enemigos
asteroides = []
tiempo_entre_asteroides = 500
# Bloque de premios
premios = []
contador = 0
bonificador = 0
def gestionar_teclas(teclas):
if teclas[pygame.K_w] or teclas[pygame.K_UP]:
nave.y -= nave.velocidad
elif teclas[pygame.K_s] or teclas[pygame.K_DOWN]:
nave.y += nave.velocidad
elif teclas[pygame.K_a] or teclas[pygame.K_LEFT]:
nave.x -= nave.velocidad
elif teclas[pygame.K_d] or teclas[pygame.K_RIGHT]:
nave.x += nave.velocidad
# Funcion texto final del juego
def texto_final():
mensaje_final = fuente_final.render(' JUEGO TERMINADO', True, (255, 255, 255))
ventana.blit(mensaje_final, (300, 300))
ventana.blit(texto_puntos, (400, 350))
# Loop del juego
while jugando and vidas >= 0:
# Colocar la Imagen del fondo de pantalla
ventana.blit(fondo, (0, 0))
# Aparicion de asteroides
tiempo_transcurrido += reloj.tick(fps)
if tiempo_transcurrido > tiempo_entre_asteroides:
asteroides.append(Asteroid(random.randint(0, ancho), -80))
tiempo_transcurrido = 0
# Aparicion de las cajas de premios
contador += 1
if contador > 5:
premios.append(Premio(random.randint(0, ancho), -15))
contador = 0
# Aparicion de cajas Bonus
if bonificador > 5:
pass
eventos = pygame.event.get()
teclas = pygame.key.get_pressed()
gestionar_teclas(teclas)
texto_vidas = textos.render(f'Vidas: {vidas}', True, 'white')
texto_puntos = textos.render(f'Puntos: {puntaje}', True, 'white')
for evento in eventos:
if evento.type == pygame.QUIT:
jugando = False
nave.dibujar(ventana)
for premio in premios:
premio.dibujar(ventana)
premio.movimiento()
# captura de premios
if pygame.Rect.colliderect(nave.forma, premio.forma):
sonido_premio = mixer.Sound("assets/premio.mp3")
sonido_premio.play()
puntaje += 100
premios.remove(premio)
for asteroide in asteroides:
asteroide.dibujar(ventana)
asteroide.movimiento()
# daño de enemigos
if pygame.Rect.colliderect(nave.forma, asteroide.forma):
sonido_colision = mixer.Sound("assets/explosion.mp3")
sonido_colision.play()
vidas -= 1
asteroides.remove(asteroide)
# remover enemigos fuera de pantalla
if asteroide.alto + asteroide.y > alto + 80:
puntaje += 1
asteroides.remove(asteroide)
ventana.blit(texto_vidas, (10, 10))
ventana.blit(texto_puntos, (10, 50))
pygame.display.update()
# Fin del juego
while se_ejecuta:
eventos = pygame.event.get()
for evento in eventos:
if evento.type == pygame.QUIT:
se_ejecuta = False
texto_final()
pygame.display.update()