-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
114 lines (103 loc) · 4.14 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
import pygame
import threading
import time
class Player():
def __init__(self, x, y):
self.maxHealth = 100
self.health = self.maxHealth
self.worldX = x
self.worldY = y
self.rect = pygame.Rect(x, y, 34, 64)
self.dx = 0
self.dy = 0
self.onGround = False
self.orientation = 1
self.time = 0
self.jumping = 0
self.vulnerable = False
self.vulnerability = 1
self.rocketDamage = 30
self.damageCooldown = False
self.damageCooldownTime = 0
self.sprite = "idle"
self.hoverSprite = 1
self.smallAttackFrame = 0
self.strongAttackFrame = 0
self.jumpFrame = 0
def updatePos(self, xOffset, yOffset):
newX = self.worldX - xOffset
newY = self.worldY - yOffset
self.rect = pygame.Rect(newX, newY, self.rect.width, self.rect.height)
def moveX(self, distance):
self.dx = distance
self.rect.x += self.dx
self.worldX += self.dx
def moveY(self, distance):
#Do kinematics calculations for gravity
self.dy = -distance + 0.02*self.time
self.worldY += self.dy
self.rect.y += self.dy
self.onGround = False
def damage(self, damage, enemy):
#Set the cooldown to a default value greater than the actual cooldown,
#This way if self.damageCooldown is false the second if won't throw an error and will execute
coolDown = 4
if(self.damageCooldown):
#Find the time elapsed since cooldown started
coolDown = int(time.time() - self.damageCooldownTime)
if(coolDown > 1): #Check time elapsed
#Do damage and start a new cooldown
self.health -= damage*self.vulnerability
self.damageCooldown = True
self.damageCooldownTime = time.time()
def drawPlayer(self, window):
#Draw the player
if(self.hoverSprite >= len(self.sprites[self.sprite])):
self.hoverSprite = 1
window.blit(self.sprites[self.sprite][0], (self.rect.x, self.rect.y))
if(self.jumping == 0):
window.blit(self.sprites[self.sprite][int(self.hoverSprite)], (self.rect.x, self.rect.y))
self.hoverSprite += .05
def checkObstacleCollisions(self, colliders, level):
"""
This function checks only against basic obstacles because much more needs
to be done to deal with a collision with these.
"""
#Check if the player has collided with 'collider'
if(self.dy > 0):
self.onGround = False
for collider in colliders:
if self.rect.colliderect(collider.rect):
if self.dx > 0:
self.rect.right = collider.rect.left
self.worldX = collider.rect.left - self.rect.width + level.x
elif self.dx < 0:
self.rect.left = collider.rect.right
self.worldX = collider.rect.right + level.x
if self.dy > 0:
#If the player is on top of an obstacle, set onGround to True
self.onGround = True
self.rect.bottom = collider.rect.top
self.worldY = collider.rect.top - self.rect.height + level.y
elif self.dy < 0:
self.rect.top = collider.rect.bottom
self.worldY = collider.rect.bottom + level.y
self.dx = 0
self.dy = 0
def checkCollisions(self, collider):
"""
This function checks collisions against anything and returns a boolean
value for more versatility than checkObstacleCollisions()
"""
collided = False
if self.rect.colliderect(collider.rect):
collided = True
return collided
def smallAttack(self):
while int(self.smallAttackFrame) < len(self.sprites["leftSmallAttack"]):
self.smallAttackFrame += .0004
self.smallAttackFrame = 0
def strongAttack(self):
while int(self.strongAttackFrame) < len(self.sprites["leftStrongAttack"]):
self.strongAttackFrame += .0004
self.strongAttackFrame = 0