-
Notifications
You must be signed in to change notification settings - Fork 0
/
turretslime.py
221 lines (207 loc) · 8.1 KB
/
turretslime.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
import pygame
import random
class Sprite:
def __init__(self, sprite_type, pos, double = False, flip_lengthwise = False, flip_heightwise = False, direction = 0, num_o_animation_cycles = 1):
self.orig_img = pygame.image.load(sprite_type).convert_alpha()
self.direction = direction
self.what_to_blit = img_splitter(num_o_animation_cycles, self.orig_img)
self.position = pos
self.flip_horizontally = flip_lengthwise
self.flip_vertically = flip_heightwise
self.animation_timer = 0
self.time_between_animations = 10
self.animation_index = 0
self.img = self.next_img()
for x in range(len(self.what_to_blit)):
if direction != 0:
self.what_to_blit[x] = pygame.transform.rotate(self.what_to_blit[x], direction)
if flip_lengthwise or flip_heightwise:
self.what_to_blit[x] = pygame.transform.flip(self.what_to_blit[x], self.flip_horizontally, self.flip_vertically)
if double:
self.what_to_blit[x] = pygame.transform.scale2x(self.what_to_blit[x])
def blitme(self, screen):
screen.blit(self.next_img(), self.position)
self.img = self.next_img()
img_rect = self.img.get_rect()
if self.get_health() is not None:
pygame.draw.line(screen, (255,0,0),(self.position[0], self.img.get_rect().height + self.position[1]), (self.position[0] + (self.get_health() * img_rect.width / 100), self.img.get_rect().height + self.position[1]))
def get_rect(self):
rect = self.img.get_rect()
return rect.move(self.position)
def off_screen(self, screen):
screen_rect = screen.get_rect()
my_rect = self.get_rect()
return not screen_rect.colliderect(my_rect)
def get_health(self):
return None
def next_img(self):
next_img = self.what_to_blit[self.animation_index]
if self.animation_timer == self.time_between_animations:
self.animation_index += 1
self.animation_timer = 0
if self.animation_index == len(self.what_to_blit):
self.animation_index = 0
self.animation_timer +=1
return next_img
class Turret(Sprite):
def __init__(self, pos):
super().__init__("assets/towers/matter/3_left.png", pos)
self.counter = 1
self.health = 100
def update(self):
if self.counter == 10:
bullet = Bullet((self.position[0], self.position[1] + 10), 0)
self.counter = 1
return bullet
else:
self.counter += 1
def get_health(self):
return self.health
def collide_taking_dmg(self, hurtful_obj):
if type(hurtful_obj) == EnemyBlob:
self.health -= 25
if self.health <= 0:
return [hurtful_obj, self]
else:
return [hurtful_obj]
class EnemyBlob(Sprite):
def __init__(self, pos):
super().__init__("assets/monster/slime1_side.png", pos, double = True, flip_lengthwise = True, num_o_animation_cycles = 4)
self.health = 100
def get_health(self):
return self.health
def collide_taking_dmg(self, hurtful_obj):
if type(hurtful_obj) == Bullet:
self.health -= 10
elif type(hurtful_obj) == Missile:
self.health -= 75
if self.health <= 0:
return [hurtful_obj,self]
else:
return [hurtful_obj]
def update(self):
self.position = (self.position[0] + 1, self.position[1])
class Bullet(Sprite):
def __init__(self, pos, direction):
super().__init__("assets/other/shockwave.png", pos, direction, num_o_animation_cycles = 9)
self.what_to_blit = append_reverse(self.what_to_blit)
def update(self):
self.position = (self.position[0] - 10, self.position[1])
class Missile(Sprite):
def __init__(self,pos):
super().__init__("assets/other/rocket.png",pos, direction = 180)
def update(self):
self.position = (self.position[0] - 1, self.position[1])
def collision_detection(obj1, obj2):
obj1_rect = obj1.get_rect()
obj2_rect = obj2.get_rect()
if obj1_rect.colliderect(obj2_rect):
return True
return False
def process_collision(list1, list2):
dellist = []
for obj1 in list1:
for obj2 in list2:
if collision_detection(obj1, obj2):
dellist.extend(obj1.collide_taking_dmg(obj2))
return dellist
def img_splitter(num_o_imgs, img):
if type(img) == str:
img = pygame.image.load(img).convert_alpha()
list_of_imgs = []
img_rect = img.get_rect()
H_many_pixels_to_go = img_rect.width / num_o_imgs
top_left_o_img = 0
for timer in range(num_o_imgs):
img_surface = pygame.Surface((H_many_pixels_to_go, img_rect.height), pygame.SRCALPHA, 32)
individual_img = pygame.Rect((top_left_o_img, 0), (H_many_pixels_to_go, img_rect.height))
img_surface.blit(img, (0,0), individual_img)
top_left_o_img += H_many_pixels_to_go
list_of_imgs.append(img_surface)
return list_of_imgs
def append_reverse(flippin_list):
other_list_nums = list(range(0, len(flippin_list)-2))
list_nums = list(range(1, len(flippin_list)-1))
completed_list = flippin_list
flipped_list_nums = list(reversed(list_nums))
for iterating in other_list_nums:
completed_list.append(flippin_list[flipped_list_nums[iterating]])
return completed_list
def draw_background_and_grid(background, gridbool):
"""args: background surface and if you want a grid or not"""
background_rect = background.get_rect()
return_this_surface = pygame.Surface((width,height))
for x in range (0, width, background_rect.width):
for y in range (0, height, background_rect.height):
return_this_surface.blit(background, (x,y))
lines = 8
(xw1, yw1, xw2, yw2) = (0, 0, width, 0)
(xh1, yh1, xh2, yh2) = (0, 0, 0, height)
if gridbool:
for a in range (lines):
pygame.draw.line(return_this_surface, (0,0,0), (xw1, yw1), (xw2, yw2))
yw1 += 75
yw2 += 75
for b in range (lines):
pygame.draw.line(return_this_surface, (0,0,0), (xh1, yh1), (xh2, yh2))
xh1 += 100
xh2 += 100
return return_this_surface
Clock = pygame.time.Clock()
(width, height) = (800, 600)
pixel_grass = pygame.image.load("assets/background/rPixel_Grass_mirror.png")
bg_surface = draw_background_and_grid(pixel_grass, True)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Tower Defense")
spritesOnScreen = []
running = True
enemyspawnrate = 50
enemyspawn = enemyspawnrate
while running:
screen.blit(bg_surface, (0,0))
new_sprites = []
bullets = []
enemies = []
turrets = []
delsprites = []
if enemyspawn == enemyspawnrate:
dada = EnemyBlob((10, 10))
spritesOnScreen.append(dada)
enemyspawn = 0
else:
enemyspawn += 1
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
pos = event.pos
missile = Missile(pos)
spritesOnScreen.append(missile)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
pos = event.pos
ethan = Turret(pos)
spritesOnScreen.append(ethan)
elif event.type == pygame.QUIT:
running = False
for a in spritesOnScreen:
b = a.update()
if not a.off_screen(screen):
if isinstance(a, Bullet) or isinstance(a, Missile):
bullets.append(a)
if isinstance(a, EnemyBlob):
enemies.append(a)
if isinstance(a, Turret):
turrets.append(a)
else:
delsprites.append(a)
if b:
new_sprites.append(b)
a.blitme(screen)
delsprites.extend(process_collision(enemies, bullets))
delsprites.extend(process_collision(turrets, enemies))
spritesOnScreen.extend(new_sprites)
for x in delsprites:
if x in spritesOnScreen:
spritesOnScreen.remove(x)
pygame.display.flip()
pygame.display.quit()
pygame.quit()