-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGranny.py
58 lines (41 loc) · 1.9 KB
/
Granny.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
from Lifeform import *
"""
Usage!
1. instantiate granny
2. call walk to make her walk, she figures it out on her own.
3. call draw to bring her into existance.
"""
class Granny(Lifeform):
def __init__(self, window_width=750, window_height=750):
self.right_walk = [
pygame.image.load(f"Assets/granny/{x + 1}.png") for x in range(7)
]
self.left_walk = [pygame.transform.flip(x, True, False) for x in self.right_walk]
self.animation = self.right_walk
self.frame = 0
self.animationCounter = 0
self.window_width = window_width
self.direction = "right"
self.vel = 1
super().__init__(100, window_height - self.left_walk[0].get_height() - 20, self.right_walk[0], 1)
def walk(self):
self.move(2, self.vel) # Super function call to move in the desired direction.
if self.x > self.window_width - self.icon.get_width(): # Hits the left wall
self.direction = "left" # to query our dictionary of granny assets later.
self.vel = self.vel * (-1) # to switch the direction she walks
# self.icon = self.img[self.direction] # update the granny icon, to reflect change of direction.
self.animation = self.left_walk
elif self.x < self.icon.get_width() and self.direction == "left": # Hits the right wall
self.direction = "right"
self.vel = self.vel * (-1)
# self.icon = self.img[self.direction]
self.animation = self.right_walk
def draw(self, gamewindow):
if self.animationCounter == 0:
self.frame = (self.frame + 1) % 7
self.animationCounter += 7
else:
self.animationCounter -= 1
self.icon = self.animation[self.frame]
super().draw(gamewindow) # Draw the granny
self.status_bar(gamewindow) # Draw her health