-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.py
105 lines (76 loc) · 2.6 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
# Player Sprite
import pygame
# Define some colors
black = ( 0, 0, 0)
white = (255, 255, 255)
green = ( 0, 255, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
class Player(pygame.sprite.Sprite):
# -- Attributes
change_x = 0
change_y = 0
# Facing directions
direction = "down"
frame = 5
# Constructor.
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images = []
for i in range(1, 17):
img = pygame.image.load("player"+str(i)+".png").convert()
img.set_colorkey(white)
self.images.append(img)
self.image = self.images[1]
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = 100
# Change the speed of the player
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# Find a new position for the player
def update(self,walls):
# Get the old position, in case we need to go back to it
old_x=self.rect.left
new_x=old_x+self.change_x
self.rect.left = new_x
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.left=old_x
old_y=self.rect.top
new_y=old_y+self.change_y
self.rect.top = new_y
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.top=old_y
# If we are moving left to right
if self.change_x > 0:
self.frame += 1
self.direction = "right"
if self.frame > (4 * 4) - 1:
self.frame = 0
self.image = self.images[self.frame//4+8]
if self.change_x < 0:
self.frame += 1
self.direction = "left"
if self.frame > (4 * 4) - 1:
self.frame = 0
self.image = self.images[self.frame//4+4]
# If we are moving up and down
if self.change_y > 0:
self.frame +=1
self.direction = "down"
if self.frame > (4 * 4) - 1:
self.frame = 0
self.image = self.images[self.frame//4]
if self.change_y < 0:
self.frame += 1
self.direction = "up"
if self.frame > (4 * 4) - 1:
self.frame = 0
self.image = self.images[self.frame//4 + 12]