-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.py
77 lines (56 loc) · 2.39 KB
/
player.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
import pygame as pg
from consts import Consts
from point import Point
from sprite import MoveableSprite
from states import PlayerState
class Player(MoveableSprite):
"""
A concrete moveable sprite class that represents the player itself
Recurring to State, Subclass Sandbox and Update Method design patterns
Methods
----------
render(self, screen)
Draws the player sprite
update(self, screen, food_list, shelter, score)
Where the player changes its state when catching food and dropping it in the shelter
This is also where the event to update the overall score is fired
"""
def __init__(self, pos_x, pos_y):
"""
Parameters
----------
pos_x: int
the first coordinate of the position of the player
pos_y: int
the second coordinate of the position of the player
"""
# initializing self.pos_x, self.pos_y, self.image and self.rect
super().__init__(pos_x, pos_y, Consts.SPRITE_PLAYER_WITHOUT_FOOD,
Consts.SPRITE_PLAYER_WITHOUT_FOOD
.get_rect(topleft=(pos_x, pos_y)))
# create a point to use it for the collision algorithm
self.ref_point = Point(self.pos_x + 15, self.pos_y + 15)
# initialize state of player
self.state = PlayerState.WITHOUT_FOOD
# initialize pointer to picked food
self.food = None
def update(self, screen, food_list, shelter, score):
food_hit = pg.sprite.spritecollideany(self, food_list)
shelter_hit = self.get_rect().colliderect(shelter.reference_rect)
if self.state == PlayerState.WITHOUT_FOOD and food_hit:
self.food = food_hit
self.state = PlayerState.WITH_FOOD
self.image = Consts.SPRITE_PLAYER_WITH_FOOD
food_hit.kill()
pg.display.update()
if self.state == PlayerState.WITH_FOOD is not None and shelter_hit:
# call update_score event
ev = pg.event.Event(Consts.CUSTOM_GAME_EVENT, {"name": Consts.UPDATE_SCORE, "points": self.food.get_score()})
pg.event.post(ev)
self.food = None
self.state = PlayerState.WITHOUT_FOOD
self.image = Consts.SPRITE_PLAYER_WITHOUT_FOOD
pg.display.update()
self.render(screen)
def render(self, screen):
super().render(screen)