-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
154 lines (125 loc) · 5.44 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
# Player
# Author: Caleb Kisby
# Date: Jun.3.15
#
# Represents a player event in a general adventure game.
from menu_manager import *
from event import *
from tkinter import NW
class Player(Event):
def __init__(self, init_root, init_canvas, image, animations, init_x1, init_y1, init_x2, init_y2):
"""
Initializes and draws the player. Sets key press checks.
"""
super().__init__(init_root, init_canvas, image, animations, init_x1, init_y1, init_x2, init_y2, tag="player")
# A simple attribute which toggles animation.
self._toggle = 0
self._animate_flag = True
# A simple attribute which controls the amount of time to change the frame.
self._wait = 500
# Attributes to monitor the player's health and points.
self._health = 3
self._health_events = []
self._points = 0
self._score_name = ""
self._points_display = self._canvas.create_text(896, 0,
text=str(self._points), fill="white", font=("Arial", 24), anchor=NW)
self.update_health(self._health)
self.update_points(self._points)
# These attributes keep track of direction keys being held.
self._hold_up = False
self._hold_down = False
self._hold_left = False
self._hold_right = False
# Bind key presses.
self._up_id = self._root.bind("<Up>", self.key_up)
self._down_id = self._root.bind("<Down>", self.key_down)
self._left_id = self._root.bind("<Left>", self.key_left)
self._right_id = self._root.bind("<Right>", self.key_right)
# Bind key releases.
self._root.bind("<KeyRelease-Up>", self.stop_up)
self._root.bind("<KeyRelease-Down>", self.stop_down)
self._root.bind("<KeyRelease-Left>", self.stop_left)
self._root.bind("<KeyRelease-Right>", self.stop_right)
# Schedule animation.
self._root.after(self._wait, self.change_frame)
# Change frame method is used to animate the player.
def change_frame(self):
if(self._animate_flag):
self.set_frame(self._animations[self._toggle])
self.draw_event()
# Update the next frame.
if(self._toggle >= 3):
self._toggle = 0
else:
self._toggle += 1
# Reschedule animation.
self._root.after(self._wait, self.change_frame)
def update_health(self, new_health):
"""
A helper function which updates the health given an amount of health new_health.
This function also redraws the health events.
"""
self._health = new_health
for event in self._health_events:
event.erase_event()
self._health_events = [Event(self._root, self._canvas, "graphics/spritesets/life1.gif", [],
0, 64*pos, 0, 64*(pos+1)) for pos in range(0, new_health)]
def update_points(self, new_points):
"""
A helper function which updates the points given an amount of points new_points.
This function also redraws the point count.
"""
self._points = new_points
if((self._points % 500 == 0) and (self._points != 0)):
self.update_health(self._health+1)
self._canvas.delete(self._points_display)
self._points_display = self._canvas.create_text(896, 0,
text=str(self._points), fill="white", font=("Arial", 24), anchor=NW)
# Moving methods move the player until they are interrupted.
def move_up(self):
if self._hold_up:
self.move(UP)
self._root.after(50, self.move_up)
def move_down(self):
if self._hold_down:
self.move(DOWN)
self._root.after(50, self.move_down)
def move_left(self):
if self._hold_left:
self.move(LEFT)
self._root.after(50, self.move_left)
def move_right(self):
if self._hold_right:
self.move(RIGHT)
self._root.after(50, self.move_right)
# Stop methods interrupt the move methods.
def stop_up(self, event):
self._hold_up = False
self._up_id = self._root.bind("<Up>", self.key_up)
def stop_down(self, event):
self._hold_down = False
self._down_id = self._root.bind("<Down>", self.key_down)
def stop_left(self, event):
self._hold_left = False
self._left_id = self._root.bind("<Left>", self.key_left)
def stop_right(self, event):
self._hold_right = False
self._right_id = self._root.bind("<Right>", self.key_right)
# Keypress methods respond to key presses.
def key_up(self, event):
self._hold_up = True
self._root.unbind("<Up>", self._up_id)
self.move_up()
def key_down(self, event):
self._hold_down = True
self._root.unbind("<Down>", self._down_id)
self.move_down()
def key_left(self, event):
self._hold_left = True
self._root.unbind("<Left>", self._left_id)
self.move_left()
def key_right(self, event):
self._hold_right = True
self._root.unbind("<Right>", self._right_id)
self.move_right()