-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.py
58 lines (47 loc) · 1.94 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
51
52
53
54
55
56
57
58
import pygame
import time
class Bird:
def __init__(self, width, height, screen, filename_left="./images/bird_left.png", filename_right="./images/bird_right.png"):
self.width = width
self.height = height
self.filename_right = filename_right
self.filename_left = filename_left
self.bird_image_right = pygame.transform.scale(
pygame.image.load(filename_right).convert_alpha(), (width, height))
self.bird_image_left = pygame.transform.scale(
pygame.image.load(filename_left).convert_alpha(), (width, height))
self.screen = screen
self.bird_pos = pygame.Vector2(screen.get_width() / 2, 0)
self.vel_y = 0 # Vertical velocity
self.gravity = 700 # Gravity value (adjust as needed)
self.last_jump_time = 0
self.points = 0
self.direction = "right"
self.just_jumped = False
def jump(self):
self.vel_y = -500
self.last_jump_time = time.time()
self.just_jumped = True
def move(self, keys, dt):
self.bird_pos.y += self.vel_y * dt
if keys[pygame.K_LEFT]:
self.bird_pos.x -= 300 * dt
self.direction = "left"
if keys[pygame.K_RIGHT]:
self.bird_pos.x += 300 * dt
self.direction = "right"
# Player can jump every 0.5 second
if keys[pygame.K_SPACE] and time.time() - self.last_jump_time >= 0.5 and self.bird_pos.y > 150:
self.jump()
# Apply gravity
self.vel_y += self.gravity * dt
# Screen edge handling
if self.bird_pos.x > self.screen.get_width():
self.bird_pos.x = 0
elif self.bird_pos.x < -self.width:
self.bird_pos.x = self.screen.get_width()
def display(self):
if self.direction == "right":
self.screen.blit(self.bird_image_right, self.bird_pos)
else:
self.screen.blit(self.bird_image_left, self.bird_pos)