-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPlayer.py
More file actions
67 lines (59 loc) · 3.14 KB
/
Player.py
File metadata and controls
67 lines (59 loc) · 3.14 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
__author__ = 'Batchu Vishal'
from Person import Person
'''
This class defines our player.
It inherits from the Person class since a Player is also a person.
We specialize the person by adding capabilities such as jump etc..
'''
class Player(Person):
def __init__(self, raw_image, position):
super(Player, self).__init__(raw_image, position)
self.isJumping = 0
self.onLadder = 0
self.currentJumpSpeed = 0
self.__gravity = 1 # Gravity affecting the jump velocity of the player
self.__speed = 5 # Movement speed of the player
# Getters and Setters
def getSpeed(self):
return self.__speed
def setSpeed(self):
return self.__speed
# This manages the players jump
def continuousUpdate(self, wallGroupList, ladderGroupList): # Only the player can jump (For the player's jump)
# Only gets run when the player is not on the ladder
if self.onLadder == 0:
wallsCollided = self.checkCollision(wallGroupList)
# If the player is not jumping
if self.isJumping == 0:
# We move down a little and check if we collide with anything
self.updateY(2)
laddersCollided = self.checkCollision(ladderGroupList)
wallsCollided = self.checkCollision(wallGroupList)
self.updateY(-2)
# If we are not colliding with anything below, then we start a jump with 0 speed so that we just fall down
if len(wallsCollided) == 0 and len(laddersCollided) == 0:
self.isJumping = 1
self.currentJumpSpeed = 0
# If the player is jumping
if self.isJumping:
if wallsCollided:
# If you collide a wall while jumping and its below you, then you stop the jump
if wallsCollided[0].getPosition()[1] > self.getPosition()[1]: # wallsize/2 and charsize/2 and +1
self.isJumping = 0
self.setPosition(((self.getPosition()[0], wallsCollided[0].getPosition()[
1] - 16))) # Wall size/2 and charactersize/2 and +1
#print "HIT FLOOR"
# If you collide a wall while jumping and its above you, then you hit the ceiling so you make jump speed 0 so he falls down
elif wallsCollided[0].getPosition()[1] < self.getPosition()[1]:
self.currentJumpSpeed = 0
self.setPosition((self.getPosition()[0], wallsCollided[0].getPosition()[1] + 16))
#print "HIT TOP"
self.setCenter(self.getPosition())
# If he is still jumping (ie. hasnt touched the floor yet)
if self.isJumping:
# We move him down by the currentJumpSpeed
self.updateY(-self.currentJumpSpeed)
self.setCenter(self.getPosition())
self.currentJumpSpeed -= self.__gravity # Affect the jump speed with gravity
if self.currentJumpSpeed < -8:
self.currentJumpSpeed = -8