-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
133 lines (110 loc) · 4.25 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pygame as pg
class Person(pg.sprite.Sprite):
"""
This class defines all 'living' objects in the game. These objects
are capable of climbing ladders, moving horizontally and falling.
Attributes:
image: A string representing the path to a png.
rect: A tuple of integers representing the width and height of
the person.
_position: A tuple of integers representing coordinates of the person.
"""
def __init__(self, image, position):
"""
Initiate a person and process their image into a square. Establish
their position.
Args:
image: A string representing the path to a png.
position: A tuple of integers representing coordinates.
"""
super().__init__()
# Create attributes for the image and the position
self._position = position
self.image = image
# Sets the rect attribute to be the image
self.rect = self.image.get_rect()
self.rect.center = self._position
def set_center(self, position):
"""
Set the center of the person's image to the position.
Args:
A tuple representing a position on the board
"""
self.rect.center = position
def get_position(self):
"""
Get the coordinates of the person.
"""
return self._position
def set_position(self, position):
"""
Set the position of the person.
Args:
A tuple representing a position on the board
"""
self._position = position
def update_position(self, raw_image, speed, direction):
"""
Update the players position based on the direction its
moving in.
Args:
raw_image: A string representing the path to a png.
speed: An integer representing the speed of the player.
direction: A string which is H or V depending on whether
the movement is horizantal or vertical respectively.
"""
# intializes the image again so that it can be changed
# based on whether player is moving right, left up or down
self.image = raw_image
# if the player is moving vertically, change the y value
# of the position based on the input value
if direction == 'V':
self._position = (self._position[0], self._position[1] - speed)
# if the player is moving horizantally, change the x value
# of the position based on the input value
if direction == 'H':
self._position = (self._position[0] - speed, self._position[1])
self.rect.center = self._position
def check_collision(self, collider_group):
"""
Check for collisions between the player and the input
collider group which can be ladders, platforms or fireballs.
Args:
collider_group: A pygame sprite group
"""
colliders = pg.sprite.spritecollide(self, collider_group, False)
return colliders
class Player(Person):
"""
A class that defines the player. A player is a person specialized with a
movement speed. Separation of the Person and Player classes allows for
more sustainable customization in the future.
Attributes:
image: A string representing the path to a png.
_position: A tuple of integers representing coordinates of the person.
_speed: An integer representing the movement speed of the player.
"""
def __init__(self, raw_image, position):
"""
Initiate a player that inherits from the Person class with a certain
movement speed.
Args:
raw_image: A string representing the path to a png.
position: A tuple of integers representing coordinates.
"""
super().__init__(raw_image, position)
self._speed = 5 # Movement speed of the player
def get_speed(self):
"""
Get the player speed.
Returns:
An integer representing the player's movement speed.
"""
return self._speed
def set_speed(self, speed):
"""
Set the player movement speed.
Args:
An intger representing the players movement speed.
"""
self._speed = speed