This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
225 lines (200 loc) · 8.46 KB
/
game.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
import pygame, random, math
import ui, table
def deg(angle): # radianes a grados
while angle >= 2*math.pi:
angle -= 2*math.pi
return (180 * angle) / math.pi
def rad(angle): # grados a radianes
while angle >= 360:
angle -= 360
return (math.pi * angle) / 180
class Bullet:
def __init__(self, size, angle, pos):
self.img = pygame.image.load('assets/bullet.png').convert_alpha()
self.img = pygame.transform.scale(self.img, size)
self.rect = self.img.get_rect(center = pos)
self.speed = (10 * math.cos(angle), 10 * math.sin(angle))
def draw(self, screen):
screen.blit(self.img, self.rect)
self.rect.centerx += self.speed[0]
self.rect.centery += self.speed[1]
class Ship:
def __init__(self, size):
w,h = size
self.origin = pygame.image.load('assets/ship.png').convert_alpha()
self.origin = pygame.transform.scale(self.origin, (w//24, w//24))
#self.origin = pygame.transform.rotate(self.origin, -90)
self.bullet_size = (w//100, w//100)
self.img = self.origin
self.rect = self.img.get_rect(center = (w/2,h/2))
self.angle = 0.0
self.bullets = []
self.shoot_sound = pygame.mixer.Sound('sound/shoot.wav')
def rotate(self, pos):
x, y = pos[0] - self.rect.centerx, -pos[1] + self.rect.centery
if x == 0 and y == 0:
self.angle = 0
elif y > 0: # I
self.angle = math.acos(x / math.sqrt(x**2 + y**2))
else:
self.angle = -math.acos(x / math.sqrt(x**2 + y**2))
self.img = pygame.transform.rotate(self.origin, deg(self.angle))
self.rect = self.img.get_rect(center = self.rect.center)
def shoot(self, pos):
tmp = Bullet(self.bullet_size, -self.angle, self.rect.center)
self.bullets.append(tmp)
self.shoot_sound.play()
def draw(self, screen):
i = 0
while i < len(self.bullets):
if self.bullets[i].rect.center < (0,0) or self.bullets[i].rect.center > screen.get_size():
del self.bullets[i]
else:
self.bullets[i].draw(screen)
i += 1
screen.blit(self.img, self.rect)
class Asteroid:
def __init__(self, pos, angle, size):
self.origin = pygame.image.load('assets/asteroids.png').convert_alpha()
self.origin = pygame.transform.scale(self.origin, size)
self.img = self.origin
self.rect = self.img.get_rect(topleft = pos)
self.speed = (5 * math.cos(angle), 5 * math.sin(angle))
self.angle = 0
def draw(self, screen):
screen.blit(self.img, self.rect)
self.rect.centerx += self.speed[0]
self.rect.centery += self.speed[1]
# rotar asteroide
self.img = pygame.transform.rotate(self.origin, self.angle)
self.rect = self.img.get_rect(center = self.rect.center)
self.angle += 2
if self.angle >= 360:
self.angle = 0
class Explosion:
def __init__(self, size, pos):
self.imgs = []
for i in range(1, 13):
tmp = pygame.image.load(f'assets/explo-{i}.png').convert_alpha()
tmp = pygame.transform.scale(tmp, size)
self.imgs.append(tmp)
self.cnt = 0
self.pos = pos
self.end = False
def draw(self, screen):
screen.blit(self.imgs[self.cnt//2], self.pos)
self.cnt += 1
if self.cnt >= 24:
self.end = True
class State:
def __init__(self, size):
self.size = w, h = size
self.percentage = 0
self.timer = 0
self.timer2 = 0
self.text = ui.Label(str(self.percentage) + '%')
self.text.rect.center = (w/2, 7*h/8)
self.ship = Ship(size)
self.asteroids = []
self.asteroid_size = (w//12, w//12)
self.score = 0
self.explosions = []
self.sound = pygame.mixer.Sound('sound/expl2.wav')
def handle_input(self, input):
if self.timer >= 100 and self.percentage > 0:
self.percentage -= 1
self.timer = 0
tmp = ui.Label(str(self.percentage) + '%')
tmp.rect.center = self.text.rect.center
self.text = tmp
for event in input:
pos = pygame.mouse.get_pos()
self.ship.rotate(pos)
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
tmp = random.randrange(1, 100) # [1, 99]
for i in range(self.percentage):
if i == tmp:
return 'TABLE'
self.ship.shoot(pos)
self.score += self.percentage
self.percentage += 1
self.timer = 0
tmp = ui.Label(str(self.percentage) + '%')
tmp.rect.center = self.text.rect.center
self.text = tmp
# movimiento y bordes
key_input = pygame.key.get_pressed()
if key_input[pygame.K_a] and self.ship.rect.centerx > 0:
self.ship.rect.centerx -= 3
elif key_input[pygame.K_d] and self.ship.rect.centerx < self.size[0]:
self.ship.rect.centerx += 3
if key_input[pygame.K_w] and self.ship.rect.centery > 0:
self.ship.rect.centery -= 3
elif key_input[pygame.K_s] and self.ship.rect.centery < self.size[1]:
self.ship.rect.centery += 3
i = 0
# si un asteroid choca con la nave
while i < len(self.asteroids):
if self.ship.rect.inflate(-50, -50).colliderect(self.asteroids[i].rect.inflate(-50, -50)):
return 'TABLE'
i += 1
self.timer += 1
def draw(self, screen):
self.text.draw(screen)
self.ship.draw(screen)
i = 0
while i < len(self.explosions):
if self.explosions[i].end:
del self.explosions[i]
else:
self.explosions[i].draw(screen)
i += 1
if self.timer2 == 10:
tmp = random.randrange(0, 4)
self.timer2 = 0
# 0: arriba, 1: izq, 2: abajo, 3: der
if tmp == 0:
y = 0 - self.asteroid_size[1]
x = random.randrange(0, screen.get_size()[0])
angle = rad(random.randrange(225, 315))
self.asteroids.append(Asteroid((x, y), angle, self.asteroid_size))
elif tmp == 1:
x = 0 - self.asteroid_size[0]
y = random.randrange(0, screen.get_size()[1])
angle = rad(random.randrange(315, 405))
self.asteroids.append(Asteroid((x, y), angle, self.asteroid_size))
elif tmp == 2:
y = screen.get_size()[1]
x = random.randrange(0, screen.get_size()[0])
angle = rad(random.randrange(45, 135))
self.asteroids.append(Asteroid((x, y), angle, self.asteroid_size))
else:
x = screen.get_size()[0]
y = random.randrange(0, screen.get_size()[1])
angle = rad(random.randrange(135, 225))
self.asteroids.append(Asteroid((x, y), angle, self.asteroid_size))
i = 0
inf = (-self.asteroid_size[0], -self.asteroid_size[1])
sup = (screen.get_size()[0] + self.asteroid_size[0], screen.get_size()[1] + self.asteroid_size[1])
while i < len(self.asteroids):
if self.asteroids[i].rect.center < inf or self.asteroids[i].rect.center > sup:
del self.asteroids[i]
else:
self.asteroids[i].draw(screen)
i += 1
# si una bala choca con un asteroide
if len(self.ship.bullets) > 0:
tmp1 = self.ship.bullets[:]
tmp2 = self.asteroids[:]
i = 0
for bullet in tmp1:
for asteroid in tmp2:
if bullet.rect.colliderect(asteroid.rect):
self.score += 5
self.sound.play()
self.explosions.append(Explosion(self.asteroid_size, asteroid.rect.topleft))
tmp1.remove(bullet)
tmp2.remove(asteroid)
self.ship.bullets = tmp1[:]
self.asteroids = tmp2[:]
self.timer2 += 1