-
Notifications
You must be signed in to change notification settings - Fork 0
/
dino.py
81 lines (61 loc) · 2.28 KB
/
dino.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
import utils
import scene
gravity = 0.6
class Dino(scene.SpriteNode):
def __init__(self):
self.z_position = 2
self.is_jumping = False
self.is_ducking = False
self.is_blinking = False
self.is_dead = False
self.counter = 0
list_running_dinos = utils.load_sprite_sheet('dino.png', 5, 1, 44, 47)
list_ducking_dinos = utils.load_sprite_sheet('dino_ducking.png', 2, 1, 59, 47)
self.list_dinos = list_running_dinos + list_ducking_dinos
self.index = 0
self.left_buttom_coord = scene.Point(10, 10)
self.update_img()
self.velocity = [0, 0]
def check_bound(self):
if self.left_buttom_coord.y < 10:
self.left_buttom_coord.y = 10
self.is_jumping = False
def start_jump(self):
if not self.is_jumping:
self.is_jumping = True
self.is_blinking = False
self.velocity[1] = 10
def start_duck(self):
if not self.is_jumping:
self.is_ducking = True
self.index = 5
def end_duck(self):
self.is_ducking = False
self.index = 2
# 随index和左下端坐标改变texture、坐标等
def update_img(self):
self.texture, self.size = self.list_dinos[self.index]
self.position = self.size / 2 + self.left_buttom_coord
def update(self):
self.counter += 1
if self.is_jumping:
self.velocity[1] -= gravity
if self.is_jumping:
self.index = 0
elif self.is_blinking:
if self.index == 0:
if self.counter % 40 == 39:
self.index = 1
elif self.counter % 20 == 19:
self.index = 0
elif self.is_ducking:
if self.counter % 10 == 0:
self.index = (self.index) % 2 + 5
else:
if self.counter % 10 == 0:
self.index = (self.index + 1) % 2 + 2
if self.is_dead:
self.index = 4
self.left_buttom_coord += (self.velocity[0] * 1.5, self.velocity[1] * 1.5)
self.check_bound()
self.update_img()