-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
120 lines (101 loc) · 4.16 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
import pygame
from pidgeon import *
from config import *
from sound import *
# Player class. Keeps track of everything we need to know about a player.
class Player():
# Constructor. Initialize any player variables.
def __init__(self):
self.position = [0, Config.HEIGHT/2]
self.main_pidgeon = Pidgeon(0) # load starting pidgeon
self.status = 0
self.pidgeons = [self.main_pidgeon, Pidgeon(1), Pidgeon(2), Pidgeon(3), Pidgeon(4)]
self.speed = 200
self.found_bird = False
self.projectiles = []
self.lives = 3
self.dead = False
self.stun = -1
self.alpha = 255.0
def get_rect(self):
return pygame.Rect(self.position[0]+12, self.position[1]+12, 40, 40)
# Kill the player!
def die(self):
if not self.dead and (self.stun == -1):
print "Player died!"
self.lives -= 1
play_sound('assets/sounds/pidgeon_dying.wav')
self.dead = True
# Render the current state of the player.
def render(self):
self.main_pidgeon.render(self.position, int(self.alpha))
# Update the state of the player.
# delta: Time passed (in seconds) since the previous frame.
def update(self, delta, game_scene):
if self.found_bird is True:
if game_scene.reset_pending is False:
game_scene.counter = -3
game_scene.game.num_pigeons += 1
game_scene.reset_pending = True
elif game_scene.reset_pending is True and game_scene.counter > 0:
game_scene.__init__(game_scene.game)
# death animation
if self.dead:
if self.stun == -1:
self.stun = 3
pass
elif self.stun < delta:
self.dead = False
self.stun = -1
self.alpha = 255.0
else:
self.stun -= delta
if self.alpha > 10:
self.alpha -= delta * 255
else:
self.alpha = 255.0
keys_pressed = pygame.key.get_pressed()
# player movement
if (keys_pressed[pygame.K_w]):
self.position[1] = self.position[1] - delta * self.speed
if (self.position[1] < 0):
self.position[1] = 0
if (keys_pressed[pygame.K_s]):
self.position[1] = self.position[1] + delta * self.speed
if (self.position[1] + 64 > Config.HEIGHT):
self.position[1] = Config.HEIGHT - 64
if (keys_pressed[pygame.K_a]):
self.position[0] = self.position[0] - delta * self.speed
if (self.position[0] < 0):
self.position[0] = 0
if (keys_pressed[pygame.K_d]):
self.position[0] = self.position[0] + delta * self.speed
if (self.position[0] + 64 > Config.WIDTH):
self.position[0] = Config.WIDTH - 64
# pidgeon switching
if (keys_pressed[pygame.K_1]):
self.main_pidgeon = self.pidgeons[0]
self.status = 0
play_sound('assets/sounds/switching.wav')
elif (keys_pressed[pygame.K_2]):
if (game_scene.game.num_pigeons >= 2):
self.main_pidgeon = self.pidgeons[1]
self.status = 1
play_sound('assets/sounds/switching.wav')
elif (keys_pressed[pygame.K_3]):
if (game_scene.game.num_pigeons >= 3):
self.main_pidgeon = self.pidgeons[2]
self.status = 2
play_sound('assets/sounds/switching.wav')
elif (keys_pressed[pygame.K_4]):
if (game_scene.game.num_pigeons >= 4):
self.main_pidgeon = self.pidgeons[3]
self.status = 3
play_sound('assets/sounds/switching.wav')
elif (keys_pressed[pygame.K_5]):
if (game_scene.game.num_pigeons >= 5):
self.main_pidgeon = self.pidgeons[4]
self.status = 4
play_sound('assets/sounds/switching.wav')
# Update pidgeon
self.main_pidgeon.update(delta)