-
Notifications
You must be signed in to change notification settings - Fork 1
/
sprite.py
113 lines (84 loc) · 3.38 KB
/
sprite.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
import pygame as pg
from abc import ABC, abstractmethod
class Sprite(ABC, pg.sprite.Sprite):
"""
An abstract class used to represent a sprite (that later will be a type of sprite, such as food, box)
Recurring to Subclass Sandbox design pattern
"""
@abstractmethod
def __init__(self, pos_x, pos_y, image, rect):
"""
Parameters
----------
pos_x: int
the first coordinate of the position of the sprite
pos_y: int
the second coordinate of the position of the sprite
image: pygame.Surface
the variable that stores the loaded sprite
rect: pygame.Rect
it contains the sprite itself and makes possible to place the sprite in a new position and to
check collision between sprites
"""
super().__init__()
self.pos_x = pos_x
self.pos_y = pos_y
# create variable that will load the sprite itself
self.image = image
# create a rect to track the position of the object
self.rect = rect
@abstractmethod
def render(self):
raise NotImplemented
def get_rect(self):
return self.rect
def get_pos(self):
return self.pos_x, self.pos_y
def set_pos(self, new_pos):
self.pos_x = new_pos[0]
self.pos_y = new_pos[1]
def set_rect(self, new_rect):
self.rect = new_rect
class MoveableSprite(Sprite):
"""
A concrete type of sprite, one that is able to move (such as Player and Point sprites)
Recurring to Subclass Sandbox design pattern
Methods
----------
move_left(self, screen)
Updates the position of the sprite, making it move to the left
move_right(self, screen)
Updates the position of the sprite, making it move to the right
move_up(self, screen)
Updates the position of the sprite, making it to move up
move_down(self, screen)
Updates the position of the sprite, making it move to move down
render(self, screen)
Draws sprite on screen
"""
def __init__(self, pos_x, pos_y, image, rect):
# call the abstract parent class (Sprite) constructor
# initializing self.pos_x, self.pos_y, self.image and self.rect
super().__init__(pos_x, pos_y, image, image.get_rect(topleft=(pos_x, pos_y)))
def move_left(self, screen):
vector = (-5, 0)
self.get_rect().move_ip(-5, 0)
self.set_pos((self.get_pos()[0] + vector[0], self.get_pos()[1] + vector[1]))
self.get_rect().clamp_ip(screen.get_rect())
def move_right(self, screen):
vector = (5, 0)
self.get_rect().move_ip(5, 0)
self.set_pos((self.get_pos()[0] + vector[0], self.get_pos()[1] + vector[1]))
self.get_rect().clamp_ip(screen.get_rect())
def move_up(self, screen):
vector = (0, -5)
self.get_rect().move_ip(0, -5)
self.set_pos((self.get_pos()[0] + vector[0], self.get_pos()[1] + vector[1]))
self.get_rect().clamp_ip(screen.get_rect())
def move_down(self, screen):
vector = (0, 5)
self.get_rect().move_ip(0, 5)
self.set_pos((self.get_pos()[0] + vector[0], self.get_pos()[1] + vector[1]))
self.get_rect().clamp_ip(screen.get_rect())
def render(self, screen):
screen.blit(self.image, self.rect)