generated from StRobertCHSCS/python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_files.py
279 lines (251 loc) · 10 KB
/
backup_files.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import arcade
import random
from player import Player
from tiledmap import TiledMap
from slime import Slime
class Main():
def __init__(self):
self.direction = None
self.player = None
self.enemies = []
self.character_list = None
self.physics_engine = None
self.tile_map = None
self.WINDOW_WIDTH = 800
self.WINDOW_HEIGHT = 800
self.main()
def on_draw(self) -> None:
"""
Holds all rendering code to screen
:return: none, draws to the window
"""
arcade.start_render()
self.tile_map.ground_list.draw()
self.tile_map.wall_list.draw()
self.character_list.draw()
def move_player(self, delta_time) -> None:
"""
Moves the player
:param delta_time: execution time
:return:
"""
output = self.player.move_player(delta_time, self.direction)
if output is not None:
self.on_key_release(output, None)
def move_enemy(self, delta_time) -> None:
"""
Moves enemies based on the position of the player
:param delta_time:
:return:
"""
for enemy in self.enemies:
enemy.follow(self.player, delta_time)
def on_update(self, delta_time) -> None:
"""
Update function called periodically
:param delta_time: time of execution
:return: none
"""
# updates the animation state of the player sprite
self.character_list.update_animation()
# move player
self.move_player(delta_time)
# move enemies
self.move_enemy(delta_time)
def on_key_press(self, symbol, modifiers) -> None:
'''
Moving the player based on the key pressed
:param symbol: keypressed
:param modifiers: Unused parameter
:return: none
'''
# player movement
if symbol == arcade.key.RIGHT:
self.direction = "RIGHT"
self.player.move_direction(self.direction)
elif symbol == arcade.key.LEFT:
self.direction = "LEFT"
self.player.move_direction(self.direction)
elif symbol == arcade.key.UP:
self.direction = "UP"
self.player.move_direction(self.direction)
elif symbol == arcade.key.DOWN:
self.direction = "DOWN"
self.player.move_direction(self.direction)
else:
print("invalid key press")
def on_key_release(self, symbol, modifiers) -> None:
"""
Handling when user releases keys (stops moving)
:param symbol: key released
:param modifiers: unused
:return: none
"""
# sets key direction back to None after key release, starts standing animation
if symbol == arcade.key.RIGHT and self.direction == "RIGHT":
self.player.face_direction(self.direction)
self.direction = None
elif symbol == arcade.key.LEFT and self.direction == "LEFT":
self.player.face_direction(self.direction)
self.direction = None
elif symbol == arcade.key.UP and self.direction == "UP":
self.player.face_direction(self.direction)
self.direction = None
elif symbol == arcade.key.DOWN and self.direction == "DOWN":
self.player.face_direction(self.direction)
self.direction = None
else:
print("invalid key release")
def create_enemies(self) -> None:
"""
temporary testing function that creates enemies
:return:
"""
for x in range(10):
self.enemies.append(Slime(self.WINDOW_WIDTH, self.WINDOW_HEIGHT, random.randint(75, 150)))
for enemy in self.enemies:
self.character_list.append(enemy)
def main(self):
# open window
arcade.open_window(self.WINDOW_WIDTH, self.WINDOW_HEIGHT, "Main")
arcade.schedule(self.on_update, 1 / 60)
arcade.set_background_color(arcade.color.BLACK)
# create character list
self.character_list = arcade.SpriteList()
# setting up player
self.player = Player(self.WINDOW_WIDTH, self.WINDOW_HEIGHT)
self.create_enemies()
# add player to the list of characters
self.character_list.append(self.player)
# Override arcade methods
self.physics_engine = None
self.tile_map = TiledMap()
window = arcade.get_window()
window.on_key_press = self.on_key_press
window.on_key_release = self.on_key_release
window.on_draw = self.on_draw
arcade.run()
Main()
"""------------------------------------------------------------------------"""
import arcade
class Player(arcade.AnimatedTimeSprite):
def __init__(self, window_width: int, window_height: int, player_speed=250, direction="DOWN"):
super().__init__()
"""Constructor of the Player class, that is the entity that the user will be moving controlling.
:param direction: default direction of player
:param player_speed: speed of player
:param window_width: width of game window
:param window_heigth: height of game window
"""
# setting speed and direction based on creation of Player object
self.player_speed = player_speed
self.direction = direction
# change animation rate
self.texture_change_frames = 30
# spawn facing forward
self.face_direction(direction)
# setting position of Player
self.center_x = window_width // 2
self.center_y = window_height // 2
# defining size of player for later use
self.width = None
self.height = None
# setting up window size
self.WINDOW_HEIGHT = window_height
self.WINDOW_WIDTH = window_width
# animation for the player to face when it is not moving
def face_direction(self, direction) -> None:
"""
Faces the character animation based on the direction.
:param direction: direction for the character to face
:return: None
"""
if direction == "LEFT":
self.textures = []
for i in range(3):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=104, width=96, height=104,
scale=0.5))
elif direction == "RIGHT":
self.textures = []
for i in range(3):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=312, width=96, height=104,
scale=0.5))
elif direction == "UP":
self.textures = []
for i in range(1):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=208, width=96, height=104,
scale=0.5))
elif direction == "DOWN":
self.textures = []
for i in range(3):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=0, width=96, height=104, scale=0.5))
else:
print("Invalid direction to face")
# animation for moving
def move_direction(self, direction) -> None:
"""
Sets animation to the direction of the player movement
:param direction: direction of player movement
:return: None
"""
if direction == "DOWN":
self.textures = []
for i in range(10):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=416, width=96, height=104,
scale=0.5))
elif direction == "LEFT":
self.textures = []
for i in range(10):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=520, width=96, height=104,
scale=0.5))
elif direction == "UP":
self.textures = []
for i in range(10):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=624, width=96, height=104,
scale=0.5))
elif direction == "RIGHT":
self.textures = []
for i in range(10):
self.textures.append(
arcade.load_texture("images/player.png", x=i * 96, y=728, width=96, height=104,
scale=0.5))
else:
print("Direction not valid to move")
def move_player(self, delta_time, direction):
self.direction = direction
if self.direction is not None:
self.texture_change_frames = 2.5
if self.direction == "RIGHT":
self.center_x += self.player_speed * delta_time
if self.center_x > self.WINDOW_WIDTH - 25:
self.center_x = self.WINDOW_WIDTH - 25
self.direction = None
return arcade.key.RIGHT
if self.direction == "LEFT":
self.center_x -= self.player_speed * delta_time
if self.center_x < 25:
self.center_x = 25
self.direction = None
return arcade.key.LEFT
if self.direction == "UP":
self.center_y += self.player_speed * delta_time
if self.center_y > self.WINDOW_HEIGHT - 25:
self.center_y = self.WINDOW_HEIGHT - 25
self.direction = None
return arcade.key.UP
if self.direction == "DOWN":
self.center_y -= self.player_speed * delta_time
if self.center_y < 25:
self.center_y = 25
self.direction = None
return arcade.key.DOWN
else:
self.texture_change_frames = 30
"""-----------------------------------------------------------------------------------------"""