forked from venom443/SpaceShip-Starwars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
258 lines (221 loc) · 9.93 KB
/
sprites.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Copyright (C) 2019 Andrés Segovia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import random
import pygame
from utils import load_image
from resources import *
# ------------------------------------------------------------------------------
# PLAYER sprite
# ------------------------------------------------------------------------------
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images = []
dir_images = os.path.join(os.path.dirname(
os.path.abspath(__file__)), "data", "images", "spaceship")
for name_img in os.listdir(dir_images):
current_img = load_image(
os.path.join(dir_images, name_img), False, (LENGTH_SPACESHIP, LENGTH_SPACESHIP))
self.images.append(current_img)
assert(len(self.images) == 6)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT)
self.x_speed = 0 # X Displacement
self.y_speed = 0 # Y Displacement
def update(self):
self.rect.move_ip((self.x_speed, self.y_speed))
if self.rect.left < 0: # In order not to exceed the limit of the window
self.rect.left = 0
elif self.rect.right > WINDOW_WIDTH:
self.rect.right = WINDOW_WIDTH
if self.rect.top <= WINDOW_HEIGHT / 2: # To determine that it does not exceed half of the screen
self.rect.top = WINDOW_HEIGHT / 2
elif self.rect.bottom >= WINDOW_HEIGHT:
self.rect.bottom = WINDOW_HEIGHT
# To determine the direction of movement
if self.x_speed < 0: # LEFT
if self.y_speed < 0: # UP + LEFT
self.image = self.images[5]
motor_channel.play(motor_on_sound, loops=0,
maxtime=0, fade_ms=0)
else: # only LEFT
self.image = self.images[4]
motor_channel.stop()
elif self.x_speed > 0: # RIGHT
if self.y_speed < 0: # UP + RIGHT
self.image = self.images[3]
motor_channel.play(motor_on_sound, loops=0,
maxtime=0, fade_ms=0)
else: # only RIGHT
self.image = self.images[2]
motor_channel.stop()
else: # UP / DOWN
if self.y_speed < 0: # UP
self.image = self.images[1]
motor_channel.play(motor_on_sound, loops=0,
maxtime=0, fade_ms=0)
else: # DOWN
self.image = self.images[0]
motor_channel.stop()
# ------------------------------------------------------------------------------
# ENEMY sprite
# ------------------------------------------------------------------------------
class Enemy(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
path_img = os.path.join(os.path.dirname(
os.path.abspath(__file__)), "data", "images", "resources", "droid.png")
self.image = load_image(path_img, False)
self.rect = self.image.get_rect()
# Random positioning
self.rect.centerx = random.randint(48, 752)
self.rect.centery = random.randint(70, 230)
self.x_speed = random.randint(-5, 5)
self.y_speed = random.randint(-5, 5)
if self.x_speed == 0:
self.x_speed = 1
elif self.y_speed == 0:
self.y_speed = 1
def update(self):
self.rect.move_ip((self.x_speed, self.y_speed))
if self.rect.left <= 0 or self.rect.right >= WINDOW_WIDTH:
self.x_speed = -(self.x_speed)
if self.rect.top <= 0 or self.rect.bottom >= WINDOW_HEIGHT/2:
self.y_speed = -(self.y_speed)
# This is to prevent the enemy from firing all the time.
is_shoot = random.randint(1, 80) == 1
if is_shoot == True: # The droid fires only if allowed to do so.
group_laser_enemy.add(LaserEnemy(self.rect.midbottom))
laser_droid_sound.play()
# ------------------------------------------------------------------------------
# ASTEROID sprite
# ------------------------------------------------------------------------------
class Asteroid(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# To set a variable asteroid size
self.size_asteroid = random.randint(
ASTEROID_MIN_SIZE, ASTEROID_MAX_SIZE)
# To randomly determine if the current asteroid provides power
self.is_energetic = (random.random() < PROB_ENERGETIC_ASTEROID)
# Energy level that will be provided by the asteroid. 0 is without energy
self.energy_lvl = 0
if self.is_energetic:
self.image = self.select_image(
os.path.join("resources", "energetic_asteroid.png"))
if self.size_asteroid <= 10:
self.energy_lvl = 3
elif self.size_asteroid <= 20:
self.energy_lvl = 5
elif self.size_asteroid <= 30:
self.energy_lvl = 7
elif self.size_asteroid > 30:
self.energy_lvl = 10
else: # Asteroid without energy
self.image = self.select_image(
os.path.join("resources", "asteroid.png"))
self.rect = pygame.Rect(random.randint(0, WINDOW_WIDTH - self.size_asteroid),
0 - self.size_asteroid, self.size_asteroid, self.size_asteroid)
self.rect.centerx = random.randint(48, WINDOW_WIDTH)
self.rect.centery = 0
self.x_speed = random.randint(-(ASTEROID_MAX_SPEED),
ASTEROID_MAX_SPEED)
self.y_speed = random.randint(ASTEROID_MIN_SPEED, ASTEROID_MAX_SPEED)
def update(self):
self.rect.move_ip((self.x_speed, self.y_speed))
if self.rect.left <= 0 or self.rect.right >= WINDOW_WIDTH or self.rect.bottom >= WINDOW_HEIGHT:
self.kill()
def select_image(self, archivo, is_energy_img=False):
path_img = os.path.join('data', 'images', archivo)
if is_energy_img is True: # Determine if it is to change the image to energy
image = load_image(path_img, False, (int(
ASTEROID_MAX_SIZE/2), int(ASTEROID_MAX_SIZE/2)))
else:
image = load_image(
path_img, False, (self.size_asteroid, self.size_asteroid))
return image
# ------------------------------------------------------------------------------
# LASER sprite
# ------------------------------------------------------------------------------
class PlayerLaser(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image = load_image(os.path.join(
'data', 'images', 'resources', 'laser1.png'), False)
self.rect = self.image.get_rect()
self.rect.center = pos
def update(self):
if self.rect.bottom <= 0:
self.kill()
else:
self.rect.move_ip((0, -10))
class LaserEnemy(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image = load_image(os.path.join(
'data', 'images', 'resources', 'laser3.png'), False)
self.rect = self.image.get_rect()
self.rect.midtop = pos
def update(self):
if self.rect.bottom >= WINDOW_HEIGHT:
self.kill()
else:
self.rect.move_ip((0, 6))
class TextBox(pygame.sprite.Sprite):
def __init__(self, text, font, pos_x, pos_y):
pygame.sprite.Sprite.__init__(self)
self.font = font
self.text = text
self.image = self.font.render(self.text, True, TEXTCOLOR)
self.rect = self.image.get_rect()
self.rect.x = pos_x
self.rect.y = pos_y
def update(self):
self.image = self.font.render(self.text, True, TEXTCOLOR)
class Explosion(pygame.sprite.Sprite):
def __init__(self, object_rect, type_explosion="explosion"):
pygame.sprite.Sprite.__init__(self)
self.index = 0
self.rate_image = 0
self.number_images = 6
self.lst_img_explosion = [] # Contains the sprites that compose the explosion
# We load the images depending on the type that is
for i in range(0, self.number_images):
path_img = os.path.join(
'data', 'images', 'animation', type_explosion + str(i + 1) + '.png')
self.lst_img_explosion.append(load_image(
path_img, False, object_rect.size))
self.image = self.lst_img_explosion[self.index]
self.rect = self.image.get_rect()
self.rect.x = object_rect.x
self.rect.y = object_rect.y
def update(self):
self.rate_image += 1
if self.rate_image >= DELAY_EXPLOSION:
self.index += 1
self.rate_image = 0
# We show each image of the animation
if self.index < len(self.lst_img_explosion):
self.image = self.lst_img_explosion[self.index]
else:
self.kill()
# ------------------------------------------------------------
# I have to create it in this module because a class needs it.
# ------------------------------------------------------------
group_laser_enemy = pygame.sprite.RenderUpdates()