-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics.py
99 lines (81 loc) · 2.81 KB
/
generics.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
import pygame, random
from constants import *
# This module is for general classes and functions which I like to use
# Function which rotates an image about its center by given angle
def rotCenter(image, angle):
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
# Create a generic sprite to use in groups and collisions
class GenericSprite(pygame.sprite.Sprite):
def __init__(self, image, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Cut out pieces of sprite sheet to get individual sprites. Gets them in order, left to right, and bottom to top
def cutSpriteSheet(sprite_width, sprite_height, horiz_count, sheet_height, sheet_img, sprite_count):
sprite_list = []
i = 0
x = 0
y = sheet_height - sprite_height
height = sprite_height
width = sprite_width
while i <= sprite_count:
sheet_img.set_clip(pygame.Rect(x, y, width, height))
sprite_list.append(sheet_img.subsurface(sheet_img.get_clip()))
x += sprite_width
i += 1
if i % horiz_count == 0:
y -= sprite_height
x = 0
return sprite_list
# Make a rect to be added to dirty rect list--for showing things on the screen
def customRect(x, y, width, height):
return pygame.Rect(x, y, width, height)
# Get a random x and y for enemy to spawn
def randomHoriz(enemy_width):
return random.randint((0 - enemy_width), WINDOWWIDTH)
def randomVert(enemy_height):
return random.randint(0 - enemy_height, WINDOWHEIGHT)
def randSpawnPos(enemy_width, enemy_height):
rand1 = random.randint(0, 100)
rand2 = random.randint(0, 100)
if rand1 >= 50:
y = randomVert(enemy_height)
if rand2 >= 50:
x = 0 - enemy_width
elif rand2 < 50:
x = WINDOWWIDTH
elif rand1 < 50:
x = randomHoriz(enemy_width)
if rand2 >= 50:
y = 0 - enemy_height
elif rand2 < 50:
y = WINDOWHEIGHT
return x, y
def playExplosion(sound, animation):
sound.play()
animation.play()
# Set health to zero and remove groups from screen
def clearGroups(group1, group2, group3, group4, group5, blank_enemy):
# Remove the rest of the enemies
for obj in group1:
obj.image = blank_enemy
group1.empty()
for obj in group2:
obj.image = blank_enemy
group2.empty()
for obj in group3:
obj.image = blank_enemy
group3.empty()
for obj in group4:
obj.image = blank_enemy
group4.empty()
for obj in group5:
obj.image = blank_enemy
group5.empty()