Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pg
- pg.init()
- SCREEN_WIDTH = 270
- SCREEN_HEIGHT = 620
- clock = pg.time.Clock()
- FPS = 60
- screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pg.display.set_caption('Flappy Bird', 'Assets/yellowbird-downflap.png')
- bg = pg.image.load('Assets/background-day.png')
- ground_img = pg.image.load('Assets/base.png')
- class Bird(pg.sprite.Sprite):
- def __init__(self, x, y):
- pg.sprite.Sprite.__init__(self)
- self.images = []
- self.index = 0
- self.counter = 0
- for i in range(1, 4):
- img = pg.image.load(f'Assets/red_bird_{i}.png')
- self.images.append(img)
- self.image = self.images[self.index]
- self.rect = self.image.get_rect()
- self.rect.center = [x, y]
- self.velocity = 0
- self.clicked = False
- def update(self):
- if game_running:
- self.velocity += 0.3
- if self.velocity > 8:
- self.velocity = 8
- if self.rect.bottom < 512:
- self.rect.y += int(self.velocity)
- if not game_over:
- if pg.mouse.get_pressed()[0] == 1 and not self.clicked:
- self.velocity -= 10
- self.clicked = True
- if pg.mouse.get_pressed()[0] == 0:
- self.clicked = False
- self.counter += 1
- flap_cooldown = 7
- if self.counter > flap_cooldown:
- self.counter = 0
- self.index += 1
- if self.index >= len(self.images):
- self.index = 0
- self.image = self.images[self.index]
- self.image = pg.transform.rotate(self.images[self.index], self.velocity * -2)
- else:
- self.image = pg.transform.rotate(self.images[self.index], -90)
- bird_group = pg.sprite.Group()
- player = Bird(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
- bird_group.add(player)
- ground_scroll = 0
- scroll_speed = 4
- is_active = True
- game_running = False
- game_over = False
- while is_active:
- clock.tick(FPS)
- screen.blit(bg, (0, 0))
- bird_group.draw(screen)
- bird_group.update()
- screen.blit(ground_img, (ground_scroll, 512))
- if player.rect.bottom > 512:
- game_over = True
- game_running = False
- if game_over == False:
- ground_scroll -= scroll_speed
- if abs(ground_scroll) > 35:
- ground_scroll = 0
- for event in pg.event.get():
- if event.type == pg.QUIT:
- is_active = False
- if event.type == pg.MOUSEBUTTONDOWN and not game_running and not game_over:
- game_running = True
- pg.display.update()
- pg.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement