猫史档案馆


【Python作品分享】Flappy Bird【求助帖】

用户:只粉周深_只粉周深_查看:0 回复:1 评论:0 创建时间:2020-03-22T12:41:45


【作品展示】

center_image

 

【作品介绍】

嗷,哪位大佬能帮忙瞅瞅,第97行咋错了???

 

【作品源代码】

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/bgm2.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     #初始x坐标
        self.birdY=100     #初始y坐标
        self.jumpspeed=7   #跳跃速度
        self.gravity=0.4   #跳跃重力

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

    def birdUpdata(self):
        self.jumpspeed-=self.gravity
        self.birdY-=self.jumpspeed

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

        if self.jumpspeed<0:       #当y向量<0时,鸟下坠
            self.a=1  
        if self.jumpspeed > 0:     #当y向量>0时,鸟上升
            self.a = 2

    def birdcrash(self):
        global keep_going
        resultU=self.rect.colliderect(newwall.walluprect)
        resultD = self.rect.colliderect(newwall.walldownrect)
        
        if resultD or resultU or newbird.rect.top>512:
            keep_going=False
            hit = pygame.mixer.Sound('sound/hit.WAV')
            channel_3 = pygame.mixer.Channel(3)
            channel_3.play(bgm)

class Wall():
    def __init__(self):
        self.walldown = pygame.image.load('assets/top.png')
        self.wallup = pygame.image.load('assets/bottom.png')
        self.walluprect=self.wallup.get_rect()
        self.walldownrect = self.walldown.get_rect()

        self.wallx = 288
        self.gap=50                #管道间隙
        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 wallUpdata(self):
        self.wallx-=2      #速度为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

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

    screen.blit(background, (0,0))
    screen.blit(newbird.birdSprites[newbird.a],(newbird.birdX,newbird.birdY))
    screen.blit(newwall.wallup,(newwall.wallup,newwall.walluprect))
    screen.blit(newwall.walldown, (newwall.walldown, newwall.walldownrect))

    newbird.birdUpdata()
    newwall.wallUpdata()
    newbird.birdcrash()

    pygame.display.update()
    clock.tick(60)

pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
只粉周深_只粉周深_

center_image

点赞0


评论