-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboy.py
More file actions
93 lines (72 loc) · 3.14 KB
/
boy.py
File metadata and controls
93 lines (72 loc) · 3.14 KB
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
import random
from pico2d import *
class Boy:
PIXEL_PER_METER = (10.0 / 0.3) # 10 pixel 30 cm
RUN_SPEED_KMPH = 20.0 # Km / Hour
RUN_SPEED_MPM = (RUN_SPEED_KMPH * 1000.0 / 60.0)
RUN_SPEED_MPS = (RUN_SPEED_MPM / 60.0)
RUN_SPEED_PPS = (RUN_SPEED_MPS * PIXEL_PER_METER)
TIME_PER_ACTION = 0.5
ACTION_PER_TIME = 1.0 / TIME_PER_ACTION
FRAMES_PER_ACTION = 8
image = None
LEFT_RUN, RIGHT_RUN, LEFT_STAND, RIGHT_STAND,JUMP = 0, 1, 2, 3,4
def __init__(self):
self.x, self.y = 0, 90
self.frame = random.randint(0, 7)
self.life_time = 0.0
self.total_frames = 0.0
self.dir = 0
self.state = self.RIGHT_STAND
if Boy.image == None:
Boy.image = load_image('animation_sheet.png')
def update(self, frame_time):
def clamp(minimum, x, maximum):
return max(minimum, min(x, maximum))
self.life_time += frame_time
distance = Boy.RUN_SPEED_PPS * frame_time
self.total_frames += Boy.FRAMES_PER_ACTION * Boy.ACTION_PER_TIME * frame_time
self.frame = int(self.total_frames) % 8
self.x += (self.dir * distance)
if(self.state ==self.JUMP):
self.y += 4
if (90 <= self.y):
self.y -= 2
self.x = clamp(0, self.x, 800)
def draw_bb(self):
draw_rectangle(*self.get_bb())
def stop(self):
self.fall_speed = 0
def draw(self):
self.image.clip_draw(self.frame * 100, self.state * 100, 100, 100, self.x, self.y)
def get_bb(self):
return self.x -50, self.y - 50, self.x + 50, self.y + 50
def handle_event(self, event):
if (event.type, event.key) == (SDL_KEYDOWN, SDLK_LEFT):
if self.state in (self.RIGHT_STAND, self.LEFT_STAND, self.RIGHT_RUN):
self.state = self.LEFT_RUN
self.dir = -1
if (event.type, event.key) == (SDL_KEYDOWN, SDLK_SPACE):
if self.state in (self.RIGHT_STAND, self.LEFT_STAND, self.RIGHT_RUN):
self.state = self.JUMP
self.dir = 1
elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_RIGHT):
if self.state in (self.RIGHT_STAND, self.LEFT_STAND, self.LEFT_RUN):
self.state = self.RIGHT_RUN
self.dir = 1
elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_UP):
if self.state in (self.RIGHT_STAND, self.LEFT_STAND, self.LEFT_RUN):
self.state = self.RIGHT_UP
self.y += 1
self.dir = 0
elif (event.type, event.key) == (SDL_KEYUP, SDLK_LEFT):
if self.state in (self.LEFT_RUN,):
self.state = self.LEFT_STAND
self.dir = 0
elif (event.type, event.key) == (SDL_KEYUP, SDLK_RIGHT):
if self.state in (self.RIGHT_RUN,):
self.state = self.RIGHT_STAND
self.dir = 0
elif (event.type, event.key) == (SDL_KEYUP, SDLK_SPACE):
if self.state in (self.JUMP,):
self.state = self.RIGHT_STAND