-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird.py
50 lines (42 loc) · 1.64 KB
/
bird.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
import pygame as pg
class Bird(pg.sprite.Sprite):
def __init__(self,scale_factor):
super().__init__()
# convert_alpha to convert with transparency
self.scale_factor=scale_factor
self.img_list = [pg.transform.scale_by(pg.image.load("assets/birdup.png").convert_alpha(),self.scale_factor),
pg.transform.scale_by(pg.image.load("assets/birddown.png").convert_alpha(),self.scale_factor)]
self.img_idx=0 # 0 for up and 1 for down
self.image=self.img_list[self.img_idx]
self.rect = self.image.get_rect(center=(100,100))
self.y_velocity=0
self.gravity=10
self.flap_speed=250
self.anim_counter=0
self.update_on=False
def update(self, delta_time):
if self.update_on:
self.playAnimation()
self.applyGravity(delta_time)
if self.rect.y<=0:
self.rect.y=0
self.flap_speed=0
elif self.rect.y>=0 and self.flap_speed==0:
self.flap_speed=250
def applyGravity(self, delta_time):
self.y_velocity += self.gravity*delta_time
self.rect.y += self.y_velocity
# responsible for sending up on keypress
def flap(self, dt):
self.y_velocity =-self.flap_speed*dt
def playAnimation(self):
if(self.anim_counter==5):
self.image=self.img_list[self.img_idx]
if self.img_idx==0: self.img_idx=1
else: self.img_idx=0
self.anim_counter=0
self.anim_counter+=1
def resetPosition(self):
self.rect.center=(100,100)
self.y_velocity=0
self.anim_counter=0