-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
170 lines (141 loc) · 5.95 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
"""
This module is used to hold the Player class. The Player represents the user-
controlled sprite on the screen.
"""
import pygame
import constants
from platforms import MovingPlatform
from spritesheet_functions import SpriteSheet
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# -- Attributes
# Set speed vector of player
change_x = 0
change_y = 0
# This holds all the images for the animated walk left/right
# of our player
walking_frames_l = []
walking_frames_r = []
# What direction is the player facing?
direction = "R"
# List of sprites we can bump against
level = None
# -- Methods
def __init__(self):
""" Constructor function """
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
sprite_sheet = SpriteSheet("p1_walk.png")
# Load all the right facing images into a list
image = sprite_sheet.get_image(0, 0, 66, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(66, 0, 66, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(132, 0, 67, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(0, 93, 66, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(66, 93, 66, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(132, 93, 72, 90)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(0, 186, 70, 90)
self.walking_frames_r.append(image)
# Load all the right facing images, then flip them
# to face left.
image = sprite_sheet.get_image(0, 0, 66, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(66, 0, 66, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(132, 0, 67, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(0, 93, 66, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(66, 93, 66, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(132, 93, 72, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(0, 186, 70, 90)
image = pygame.transform.flip(image, True, False)
self.walking_frames_l.append(image)
# Set the image the player starts with
self.image = self.walking_frames_r[0]
# Set a referance to the image rect.
self.rect = self.image.get_rect()
def update(self):
""" Move the player. """
# Gravity
self.calc_grav()
# Move left/right
self.rect.x += self.change_x
pos = self.rect.x + self.level.world_shift
if self.direction == "R":
frame = (pos // 30) % len(self.walking_frames_r)
self.image = self.walking_frames_r[frame]
else:
frame = (pos // 30) % len(self.walking_frames_l)
self.image = self.walking_frames_l[frame]
# See if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# If we are moving right,
# set our right side to the left side of the item we hit
if self.change_x > 0:
self.rect.right = block.rect.left
elif self.change_x < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
# Move up/down
self.rect.y += self.change_y
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self.change_y > 0:
self.rect.bottom = block.rect.top
elif self.change_y < 0:
self.rect.top = block.rect.bottom
# Stop our vertical movement
self.change_y = 0
if isinstance(block, MovingPlatform):
self.rect.x += block.change_x
def calc_grav(self):
""" Calculate effect of gravity. """
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .35
# See if we are on the ground.
if self.rect.y >= constants.SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
self.change_y = 0
self.rect.y = constants.SCREEN_HEIGHT - self.rect.height
def jump(self):
""" Called when user hits 'jump' button. """
# move down a bit and see if there is a platform below us.
# Move down 2 pixels because it doesn't work well if we only move down 1
# when working with a platform moving down.
self.rect.y += 2
platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
self.rect.y -= 2
# If it is ok to jump, set our speed upwards
if len(platform_hit_list) > 0 or self.rect.bottom >= constants.SCREEN_HEIGHT:
self.change_y = -10
# Player-controlled movement:
def go_left(self):
""" Called when the user hits the left arrow. """
self.change_x = -6
self.direction = "L"
def go_right(self):
""" Called when the user hits the right arrow. """
self.change_x = 6
self.direction = "R"
def stop(self):
""" Called when the user lets off the keyboard. """
self.change_x = 0