-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.py
53 lines (45 loc) · 2.04 KB
/
Character.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
import keyboard
class Character:
def __init__(self, canvas):
self.canvas = canvas
self.item_id = None
self.x_axis = 300
self.y_axis = 550
self.R = 50
self.speed = 5
self.jump_speed = 3
self.max_jump_height = 10
self.ground_position = self.max_jump_height * self.jump_speed
self.jump_position = self.max_jump_height * self.jump_speed
def create(self):
self.item_id = self.canvas.create_oval(self.x_axis - self.R, self.y_axis - self.R,
self.x_axis + self.R, self.y_axis + self.R,
fill='black')
def move(self):
if keyboard.is_pressed('a'):
if self.x_axis > 2 + self.R:
self.x_axis -= self.speed
self.canvas.move(self.item_id, -self.speed, 0)
if keyboard.is_pressed('d'):
if self.x_axis < 800 - self.R:
self.x_axis += self.speed
self.canvas.move(self.item_id, self.speed, 0)
def jump(self):
if keyboard.is_pressed('w'):
self.jump_position -= self.jump_speed
self.y_axis -= self.jump_position
self.canvas.move(self.item_id, 0, -self.jump_position)
if self.jump_position <= -(self.ground_position - self.jump_speed):
self.jump_position = self.ground_position
self.y_axis = 550
else:
if (self.jump_position >= -(self.ground_position - self.jump_speed * 2)
and self.jump_position != self.ground_position):
self.jump_position -= self.jump_speed
self.y_axis -= self.jump_position
self.canvas.move(self.item_id, 0, -self.jump_position)
else:
self.jump_position = self.ground_position
self.y_axis = 550
self.canvas.coords(self.item_id, self.x_axis - self.R, self.y_axis - self.R,
self.x_axis + self.R, self.y_axis + self.R)