猫史档案馆


【Python作品分享】FlappyBird

用户:伦敦西区肯辛顿富人区名富商大天才煊伦敦西区肯辛顿富人区名富商大天才煊查看:0 回复:0 评论:0 创建时间:2019-08-14T15:06:28


【作品展示】

center_image

 

【作品介绍】

Python 版Flappy Bird 出品!

 

【作品源代码】

import pygame
import random

pygame.init()
screen = pygame.display.set_mode([288, 512])

background = pygame.image.load('assets/background.png')
pygame.display.set_caption("Flappy Bird")

bgm = pygame.mixer.Sound('sound/bgm.wav')
channel_1 = pygame.mixer.Channel(1)
channel_1.play(bgm)

keep_going = True
clock = pygame.time.Clock()


class Bird (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.birdSprites = [pygame.image.load(
            'assets/0.png'), pygame.image.load('assets/1.png'), pygame.image.load('assets/2.png')]
        self.a = 0
        self.birdX = 50
        self.birdY = 100
        self.jumpSpeed = 7
        self.gravity = 0.4

        self.rect = self.birdSprites[self.a].get_rect()
        self.rect.center = (self.birdX, self.birdY)

    def BirdUpdate(self):
        self.jumpSpeed -= self.gravity
        self.birdY -= self.jumpSpeed

        self.rect.center = (self.birdX, self.birdY)

        if self.jumpSpeed == 0:
            self.a = 0
        if self.jumpSpeed > 0:
            self.a = 1
        if self.jumpSpeed < 0:
            self.a = 2

    def birdCrush(self):
        global keep_going
        resultU = self.rect.colliderect(newWall.wallUpRect)
        resultD = self.rect.colliderect(newWall.wallDownRect)
        if resultU or resultD or newBird.rect.top > 512:
            hit = pygame.mixer.Sound('sound/hit.wav')
            channel_3 = pygame.mixer.Channel(3)
            channel_3.play(hit)
            pygame.time.delay(1000)
            keep_going = False


class Wall():
    def __init__(self):
        self.wallUp = pygame.image.load('assets/bottom.png')
        self.wallDown = pygame.image.load('assets/top.png')
        self.wallUpRect = self.wallUp.get_rect()
        self.wallDownRect = self.wallDown.get_rect()

        self.gap = 50
        self.wallx = 288
        self.offset = random.randint(-50, 50)

        self.wallUpY = 360+self.gap-self.offset
        self.wallDownY = 0 - self.gap - self.offset
        self.wallUpRect.center = (self.wallx, self.wallUpY)
        self.wallDownRect.center = (self.wallx, self.wallDownY)

    def wallUpdate(self):
        self.wallx -= 2
        self.wallUpRect.center = (self.wallx, self.wallUpY)
        self.wallDownRect.center = (self.wallx, self.wallDownY)
        if self.wallx < -80:
            self.wallx = 288
            self.offset = random.randint(-50, 50)
            self.wallUpY = 360+self.gap-self.offset
            self.wallDownY = 0-self.gap-self.offset


newBird = Bird()
newWall = Wall()

while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        if (event.type == pygame.MOUSEBUTTONDOWN):
            newBird.jumpSpeed = 7

            channel_2 = pygame.mixer.Channel(2)
            fly = pygame.mixer.Sound('sound/fly.WAV')
            channel_2.play(fly)

    screen.blit(background, (0, 0))
    screen.blit(newBird.birdSprites[newBird.a], newBird.rect)
    screen.blit(newWall.wallUp, newWall.wallUpRect)
    screen.blit(newWall.wallDown, newWall.wallDownRect)

    newWall.wallUpdate()
    newBird.BirdUpdate()
    newBird.birdCrush()
    pygame.display.update()
    clock.tick(60)

pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页