-
Notifications
You must be signed in to change notification settings - Fork 3
/
player.py
executable file
·247 lines (172 loc) · 6.68 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Pitfall v1.0
By Danilo Lekovic for Game Design 12
player.py
Main player
"""
import pygame
from pygame.locals import *
# Using Pygame's built-in vector system for coordinates
vec = pygame.math.Vector2
import os
class Player(pygame.sprite.Sprite):
# Initialize class
def __init__(self, x, y):
super().__init__()
self.pos = vec(x, y)
self.velocity = vec(0, 0)
self.acceleration = vec(0, 0)
# Various states the player can be in
# This is used to change the players sprite & behavior
self.STANCE = 0
self.RUNNING = 1
self.SWING = 2
self.JUMPING = 3
self.LEFT = 4
self.RIGHT = 5
self.CLIMBING = 6
self.currentState = self.STANCE
# Important values
self.health = 6
self.score = 0
self.coins = 0
# Double jump
self.doubled = False
# Player's orientation
# This is efficient because now we only have
# to have one direction for sprites
self.orientation = self.RIGHT
# Determines in which directions the player
# is allowed to move
self.canMoveRight = True
self.canMoveLeft = True
# Animation timers and indexes dictate
# the current frame and how long each frame
# should last
self.animationTimer = 0
self.animationIndex = 0
# Animations are loaded from the sprites folder
self.animations = {}
self.load()
# Current sprite is set and rectangle is created
# for collision detection purposes
self.image = self.animations['Idle'][0]
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
# Changes current sprite and edits rectangle properties
def changeSprite(self, kind, index=None):
if index is None:
index = self.animationIndex
self.image = self.animations[kind][index]
self.rect = self.image.get_rect()
self.rect.center = (self.image.get_width() / 2,
self.image.get_height() / 2)
# Handles jumping by changing the y acceleration to -25.0
# Also handles double jumping
def jump(self):
if self.pos.y < 600:
self.acceleration.y = -25.0
if self.acceleration.x > 0.0 and not self.doubled:
self.acceleration.x = self.acceleration.x * 1.5
if self.acceleration.x < 0.0 and not self.doubled:
self.acceleration.x = self.acceleration.x * 1.5
else:
self.currentState = self.STANCE
# Handles player going down ladders
# by changing the y acceleration to 10.0
def fall(self):
self.acceleration.y = 10.0
self.acceleration.x = 0.0
self.velocity.x = 0.0
self.currentState = self.CLIMBING
# Handles players going up ladders
# by changing the y acceleration to -5.0
def rise(self):
self.acceleration.y = -5.0
self.acceleration.x = 0.0
self.velocity.x = 0.0
self.currentState = self.CLIMBING
# Updates the player's position in the game loop
def update(self):
# Handles behavior for when player is underground
if self.pos.y > 600:
self.acceleration.y = 0.0
self.velocity.y = 0.0
self.pos.y = 600
self.currentState = self.STANCE
self.doubled = False
# Handles behavior for climbing ladders
if self.acceleration.y == -5.0 and self.pos.y <= 400:
self.acceleration.y = 0.0
self.velocity.y = 0.0
self.pos.y = 400
self.currentState = self.STANCE
self.doubled = False
# Equations of motion
self.pos += self.acceleration
# Does not let player leave left border
if self.pos.x < 0:
self.pos.x = 0
# Updates rectangle for collision purposes
self.rect.move_ip(self.pos.x, self.pos.y)
# Handles jumping and physics
if self.acceleration.y <= -25.0 and self.pos.y <= 300:
self.acceleration.y = 25.0
elif self.acceleration.y <= -25.0 and self.pos.y == 604.0:
self.acceleration.y = 25.0
if self.acceleration.y >= 25.0 and self.pos.y == 400.0:
self.acceleration.y = 0.0
if self.acceleration.x > 0 or self.acceleration.x < 0:
self.acceleration.x = self.acceleration.x / 1.5
self.currentState = self.RUNNING
else:
self.currentState = self.STANCE
if self.pos.y >= 690 - self.animations['Idle'][0].get_height() \
and self.acceleration.y <= -25.0:
self.currentState = self.STANCE
self.doubled = False
if self.pos.y == 400.0 and self.acceleration.y <= -25.0:
self.acceleration.y = 0.0
self.velocity.y = 0.0
self.doubled = False
if self.acceleration.y <= -25.0 and self.pos.y == 604.0:
self.acceleration.y = 0.0
self.velocity.y = 0.0
self.doubled = False
# Moves player left by setting x acceleration to -15
def left(self):
self.acceleration.x = -15
# Moves player right by setting x acceleration to +15
def right(self):
self.acceleration.x = 15
# Stops player by setting x acceleration to 0
def stop(self):
self.acceleration.x = 0
# Loads sprites & animations for the player
def load(self):
# Checks if player path exists (Should always exist)
if os.path.exists('Sprites/Player/'):
# Certain animations are found
animationNames = ['Climbing', 'Idle', 'Jumping', 'Running']
# Each sprite is converted to a pygame image and added to the animation dictionary
for name in animationNames:
if os.path.exists('Sprites/Player/{}'.format(name)):
(path, dirs, files) = \
os.walk('Sprites/Player/{}'.format(name)).__next__()
self.animations[name] = []
bufferArray = []
for i in files:
if i.endswith('.png'):
bufferArray.append(pygame.image.load('Sprites/Player/{}/{}'.format(name,
i)))
self.animations[name] = bufferArray
# Handles player orientation so we can use minimal
# sprites
def draw(self, screen, coords):
if self.orientation is self.LEFT:
screen.blit(pygame.transform.flip(self.image, True, False),
coords)
else:
screen.blit(self.image, coords)