-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
99 lines (79 loc) · 3.29 KB
/
objects.py
File metadata and controls
99 lines (79 loc) · 3.29 KB
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
import pygame
import random
def collide(obj1, obj2):
offset_x = obj2.x - obj1.x - obj1.img.get_width()/2
offset_y = obj2.y - obj1.y
return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) is not None # overlap based on offset?
# create character class that can be used for multiple things like enemy and player
class Character:
def __init__(self, x, y, img, powerList=None, health=100, lives=2):
self.x = x
self.y = y
self.health = health
self.max_health = health
self.powers = powerList
self.currentpower = None
self.img = img
self.mask = pygame.mask.from_surface(self.img)
self.shield = 3
self.dodge = 3
self.lives = lives
def draw(self, window):
# pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, 50, 50), 10) # test object
window.blit(self.img, (self.x, self.y))
self.healthbar(window)
if self.currentpower is not None:
self.currentpower.drawIt(window)
def healthbar(self, window):
percentHealth = self.health/self.max_health
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y + 4, self.img.get_width(), 10))
pygame.draw.rect(window, (0, 255, 0), (self.x, self.y + 4, self.img.get_width() * percentHealth, 10))
class Player(Character):
def __int__(self, x, y, img, powerList=None, health=100, lives=2):
super().__init__(x, y, img, powerList=powerList, health=health, lives=lives)
# self.mask = pygame.mask.from_surface(self.img)
def shoot(self, option):
p = self.powers[option]
p.x = self.x
p.y = self.y
self.currentpower = p
def movePower(self, vel_x, width, obj):
self.currentpower.move(vel_x)
if self.currentpower.off_screen(width):
self.currentpower = None
elif self.currentpower.collision(obj):
obj.health -= self.currentpower.damage
self.currentpower = None
class Enemy(Character):
def __init__(self, x, y, img, powerList=None, health=100, lives=2):
super().__init__(x, y, img, powerList=powerList, health=health, lives=lives)
# self.mask = pygame.mask.from_surface(self.img) # creates mask to create collisions perfect since only pixel
# # areas
def shoot(self):
p = self.powers[random.randint(0, len(self.powers) - 1)]
p.x = self.x
p.y = self.y
self.currentpower = p
def movePower(self, vel_x, width, obj):
self.currentpower.move(vel_x)
if self.currentpower.off_screen(width):
self.currentpower = None
elif self.currentpower.collision(obj):
obj.health -= self.currentpower.damage
self.currentpower = None
class Power:
def __init__(self, img, name, x, y, damage=10):
self.x = x
self.y = y
self.img = img
self.name = name # give out description when used
self.mask = pygame.mask.from_surface(self.img)
self.damage = damage
def drawIt(self, window):
window.blit(self.img, (self.x, self.y))
def move(self, vel_x):
self.x += vel_x
def off_screen(self, width):
return not (self.x <= width or self.x >= 0)
def collision(self, obj):
return collide(self, obj) # does power intersect with character