-
Notifications
You must be signed in to change notification settings - Fork 17
/
Movement.py
82 lines (64 loc) · 2.15 KB
/
Movement.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
import arcade
win_width = 800
win_height = 600
rec_width = 50
rec_height = 50
speed = 5
class Rectangle:
def __init__(self, x, y, rec_width, rec_height, angle, colour):
self.x = x
self.y = y
self.new_x = 0 # New position
self.new_y = 0
self.angle = angle
self.rec_width = rec_width
self.rec_height = rec_height
self.colour = colour
def draw(self):
arcade.draw_rectangle_filled(
self.x, self.y, self.rec_width, self.rec_height, self.colour, self.angle
)
def move(self):
self.x = self.x + self.new_x
if self.x < (rec_width // 2):
self.x = rec_width // 2
if self.x > win_width - (rec_width // 2):
self.x = win_width - (rec_width // 2)
self.y = self.y + self.new_y
if self.y < (rec_height // 2):
self.y = rec_height // 2
if self.y > win_height - (rec_height // 2):
self.y = win_height - (rec_height // 2)
class movement(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height, title="MOVEMENT")
def start(self):
x = win_width // 2
y = win_height // 2
self.player = Rectangle(
x, y, rec_width, rec_width, angle=0, colour=arcade.color.RED
)
def on_update(self, dt):
self.player.move()
def on_draw(self):
arcade.start_render()
self.player.draw()
def on_key_press(self, key, modif):
if key == arcade.key.UP:
self.player.new_y = speed
elif key == arcade.key.DOWN:
self.player.new_y = -speed
elif key == arcade.key.LEFT:
self.player.new_x = -speed
elif key == arcade.key.RIGHT:
self.player.new_x = speed
def on_key_release(self, key, modif):
if key == arcade.key.UP or key == arcade.key.DOWN:
self.player.new_y = 0
if key == arcade.key.LEFT or key == arcade.key.RIGHT:
self.player.new_x = 0
if __name__ == "__main__":
print("Use Arrow Keys to move the square")
move = movement(win_width, win_height)
move.start()
arcade.run()