-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
44 lines (34 loc) · 1.36 KB
/
player.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
import pyxel
from collections import namedtuple
# location of the bird
Point = namedtuple("location", ['x', 'y'])
WIDTH = 255
HEIGHT = 255
GRAVITY = 1
class Bird:
def __init__(self):
self.location = Point(WIDTH//3, HEIGHT//2)
self._gravity = GRAVITY
self._velocity = 0
self.hit = False
def update_bird(self):
location = self.location
# change velocity according to gravity in order to make the fast fall
self._velocity = self._velocity + (self._gravity)
# change y location of the bird based on velocity
self.location = self.location._replace(y=location.y + self._velocity)
# if jumped - update velocity
if (pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_UP))and not self.hit:
self._velocity = -8
def draw_bird(self):
if self.location.y < HEIGHT - 20:
# save locations
bird_x = self.location.x
bird_y = self.location.y
# in order to animate, change v according to frames
bird_v = ((pyxel.frame_count) // 4) % 3 * 16
pyxel.blt(x=bird_x, y=bird_y, img=0, u=0,
v=bird_v, w=17, h=13, colkey=0)
else:
pyxel.blt(x=self.location.x, y=HEIGHT-27,
img=0, u=0, v=0, w=17, h=13, colkey=0)