-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
261 lines (193 loc) · 6.68 KB
/
main.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
import pygame, random, sys, os
from constantes import *
from pathlib import Path
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Galaxy Cody')
clock = pygame.time.Clock()
current_path = Path.cwd()
file_path = current_path / 'highscore.txt'
#texto
def draw_text(surface,text,size,color,x,y):
font = pygame.font.Font(current_path / 'assets/IBMPlexSans-Bold.ttf', size, bold=True)
text_surface = font.render(text,True,color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
surface.blit(text_surface,text_rect)
#shield en pantalla
def draw_shield_bar(surface,x,y,percentage):
BAR_LENGHT = 100
BAR_HEIGHT = 10
fill = (percentage / 100 ) * BAR_LENGHT
border = pygame.Rect(x,y,BAR_LENGHT,BAR_HEIGHT)
fill = pygame.Rect(x,y,fill,BAR_HEIGHT)
pygame.draw.rect(surface,GREEN,fill)
pygame.draw.rect(surface,WHITE,border,2)
# menu screen
def show_menu():
screen.blit(menu,(0,0))
draw_text(screen, 'Highscore: '+ str(highest_score),35,WHITE,WIDTH // 2, 320)
draw_text (screen, 'START [S]', 30,BLACK,WIDTH // 2, 433)
pygame.display.flip()
wait()
#game over screen
def show_game_over():
screen.blit(background,(0,0))
draw_text(screen, 'GAME OVER',60,WHITE,WIDTH // 2, HEIGHT // 5)
if highest_score <= score:
draw_text(screen, '¡Superaste el highscore!', 32,WHITE,WIDTH // 2, 280)
draw_text(screen, str(highest_score), 32,WHITE,WIDTH // 2, 330)
draw_text (screen, 'START [S]', 30,BLACK,WIDTH // 2, 436)
else:
draw_text(screen, 'Tu puntaje fue: '+ str(score), 30,WHITE,WIDTH // 2, 280)
draw_text (screen, 'START [S]', 30,BLACK,WIDTH // 2, 433)
pygame.display.flip()
wait()
def wait():
waiting = True
while waiting:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_s:
waiting = False
def get_high_score():
with open(file_path, 'r') as file:
return file.read()
class Player (pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load(os.path.join('assets/player2.png'))
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH // 2
self.rect.bottom = HEIGHT - 10
self.speed = 0
#barra de vida
self.shield = 100
def update(self):
self.speed_x = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speed_x = -5
if keystate[pygame.K_RIGHT]:
self.speed_x = 5
self.rect.x += self.speed_x
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
def shoot (self):
bullet = Bullet(self.rect.centerx,self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
laser_sound.play()
class Meteoro (pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('assets/meteorGrey_big4.png')
self.image.set_colorkey(BLACK) #quita lo negro de la img
self.rect = self.image.get_rect()
#donde aparecen los meteoritos
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(100 - 40)
self.speed_y = random.randrange(1,10)
self.speed_x = random.randrange(-5,2)
def update (self):
self.rect.y += self.speed_y
self.rect.x += self.speed_x
if self.rect.top > HEIGHT + 10 or self.rect.left < - 40 or self.rect.right > WIDTH + 25:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(140 - 100)
self.speed_y = random.randrange(1,5)
class Bullet(pygame.sprite.Sprite):
def __init__(self,x,y):
super().__init__()
self.image = pygame.image.load('assets/laser1.png')
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.centerx = x
self.speed_y = -10
def update(self):
self.rect.y += self.speed_y
if self.rect.bottom < 0:
self.kill()
background = pygame.image.load('assets/background.png').convert()
menu = pygame.image.load('assets/menu.jpg').convert()
#sonidos
laser_sound = pygame.mixer.Sound('assets/laser5.wav')
#pantalla game over
game_over = True
running = True
#### high score
try:
highest_score = int(get_high_score())
except:
highest_score = 0
#eventos
while running:
#para pantalla game over
if game_over:
show_menu()
game_over = False
#grupos
all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
#cantidad de meteoritos
for i in range(5):
meteor = Meteoro()
all_sprites.add(meteor)
meteor_list.add(meteor)
score = 0
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#disparos
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
player.shoot()
all_sprites.update()
#colisiones - meteoro + laser
hits = pygame.sprite.groupcollide(meteor_list,bullets,True,True)
for hit in hits:
score += 10
meteor = Meteoro()
all_sprites.add(meteor)
meteor_list.add(meteor)
#colisiones - jugador + meteoro
#mas shield
hits = pygame.sprite.spritecollide(player,meteor_list,True)
for hit in hits:
player.shield -= 25
#agregar meteoro desp de ser golpeado
meteor = Meteoro()
all_sprites.add(meteor)
meteor_list.add(meteor)
#termina el juego cuando la vida es 0
if player.shield <= 0:
game_over = True
show_game_over()
screen.blit(background, [0,0])
all_sprites.draw(screen)
#score en pantalla
draw_text(screen,'Score: ' + str(score),22,WHITE,48,15)
#escudo en pantalla
draw_shield_bar(screen,8,5,player.shield)
##checking high score
if (highest_score < score):
highest_score = score
#escribe el score en el archivo
with open(file_path, 'w') as file:
file.write(str(highest_score))
draw_text (screen, 'Highscore: '+ str(highest_score),22,WHITE,76,38)
pygame.display.flip()
pygame.quit()
sys.exit()