猫史档案馆


【Python作品分享】飞翔的小鸟

用户:带大呱的皮卡丘带大呱的皮卡丘查看:0 回复:0 评论:0 创建时间:2020-08-31T21:14:23


【作品展示】

center_image

 

【作品介绍】

按鼠标,小鸟上升,否则,小鸟下降。看谁穿过的柱子最多~~~~~~(发在评论区)

 

【作品源代码】

import pygame
import random
import time
pygame.init()
screen = pygame.display.set_mode((288, 512))
background = pygame.image.load('background.png')


class Bird():
    def __init__(self):
        self.birdsprites = [pygame.image.load(
            'bird{}.png'.format(i))for i in range(3)]
        self.a = 0
        self.birdX = 50
        self.birdY = 100
        self.jumpspeed = 0
        self.g = 0.2
        self.rect = self.birdsprites[self.a].get_rect()
        self.rect.center = (self.birdX, self.birdY)

    def birdUpdate(self):
        self.jumpspeed -= self.g
        self.birdY -= self.jumpspeed
        self.rect.center = (self.birdX, self.birdY)
        if self.jumpspeed < 0:
            self.a = 1
        if self.jumpspeed > 0:
            self.a = 2

    def birdcrush(self):
        returnu = self.rect.colliderect(wall.walluprect)
        returnd = self.rect.colliderect(wall.walldownrect)
        if returnd or returnu:
            return True


bird = Bird()


class Wall():
    def __init__(self):
        self.walldown = pygame.image.load('bottom.png')
        self.wallup = pygame.image.load('top.png')
        self.wallx = 288
        self.gap = 100
        self.offset = random.randint(-50, 50)
        self.walluprect = self.wallup.get_rect()
        self.walldownrect = self.walldown.get_rect()
        self.walluprect.center = (self.wallx, 0-self.gap-self.offset)
        self.walldownrect.center = (self.wallx, 520-self.gap-self.offset)

    def wallupdate(self):
        self.wallx -= 2
        if self.wallx < -80:
            self.wallx = 288
            self.offset = random.randint(-50, 50)
        self.walluprect.center = (self.wallx, 0-self.gap-self.offset)
        self.walldownrect.center = (self.wallx, 620-self.gap-self.offset)


wall = Wall()
clock = pygame.time.Clock()
run = 'l'
while run:
    if bird.birdY>531 or bird.birdY<47:
        time.sleep(2)
        run=''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = ""
        if event.type == pygame.MOUSEBUTTONDOWN:
            bird.jumpspeed = 7
    bird.birdUpdate()
    screen.blit(background, (0, 0))
    screen.blit(bird.birdsprites[bird.a], bird.rect)
    screen.blit(wall.wallup, wall.walluprect)
    screen.blit(wall.walldown, wall.walldownrect)
    wall.wallupdate()
    if bird.birdcrush():
        run = ''
        time.sleep(2)
    pygame.display.update()
    clock.tick(60)
pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页