Flybird创作

          今天我即兴发挥,创作了一款Flybird游戏,主要运用的是python运行器(版本要在3.20.21以上,否则运行不了),下面我来说下前期准备情况:

1、python运行器(版本要在3.20.21以上)

2、主程序(main.py)

3、图片(辅助运行)

           废话不多说,我们先来上源代码(main.py):

第一段(导入库):

#导入库
from itertools import cycle
import random
import sys

import pygame
from pygame.locals import *

第二段(主程序):

FPS = 30
SCREENWIDTH  = 288
SCREENHEIGHT = 512
# amount by which base can maximum shift to left
PIPEGAPSIZE  = 100 # gap between upper and lower part of pipe
BASEY        = SCREENHEIGHT * 0.79
# image, sound and hitmask  dicts
IMAGES, SOUNDS, HITMASKS = {}, {}, {}

# list of all possible players (tuple of 3 positions of flap)
PLAYERS_LIST = (
    # red bird
    (
        'redbird-upflap.png',
        'redbird-midflap.png',
        'redbird-downflap.png',
    ),
    # blue bird
    (
        # amount by which base can maximum shift to left
        'bluebird-upflap.png',
        'bluebird-midflap.png',
        'bluebird-downflap.png',
    ),
    # yellow bird
    (
        'yellowbird-upflap.png',
        'yellowbird-midflap.png',
        'yellowbird-downflap.png',
    ),
)

# list of backgrounds
BACKGROUNDS_LIST = (
    'background-day.png',
    'background-night.png',
)

# list of pipes
PIPES_LIST = (
    'pipe-green.png',
    'pipe-red.png',
)


try:
    xrange
except NameError:
    xrange = range


def main():
    global SCREEN, FPSCLOCK
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
    pygame.display.set_caption('Fly Bird')

    # numbers sprites for score display
    IMAGES['numbers'] = (
        pygame.image.load('0.png').convert_alpha(),
        pygame.image.load('1.png').convert_alpha(),
        pygame.image.load('2.png').convert_alpha(),
        pygame.image.load('3.png').convert_alpha(),
        pygame.image.load('4.png').convert_alpha(),
        pygame.image.load('5.png').convert_alpha(),
        pygame.image.load('6.png').convert_alpha(),
        pygame.image.load('7.png').convert_alpha(),
        pygame.image.load('8.png').convert_alpha(),
        pygame.image.load('9.png').convert_alpha()
    )

    # game over sprite
    IMAGES['gameover'] = pygame.image.load('gameover.png').convert_alpha()
    # message sprite for welcome screen
    IMAGES['message'] = pygame.image.load('message.png').convert_alpha()
    # base (ground) sprite
    IMAGES['base'] = pygame.image.load('base.png').convert_alpha()

    # sounds
    if 'win' in sys.platform:
        soundExt = '.wav'
    else:
        soundExt = '.ogg'

    SOUNDS['die']    = pygame.mixer.Sound('die' + soundExt)
    SOUNDS['hit']    = pygame.mixer.Sound('hit' + soundExt)
    SOUNDS['point']  = pygame.mixer.Sound('point' + soundExt)
    SOUNDS['swoosh'] = pygame.mixer.Sound('swoosh' + soundExt)
    SOUNDS['wing']   = pygame.mixer.Sound('wing' + soundExt)

    while True:
        # select random background sprites
        randBg = random.randint(0, len(BACKGROUNDS_LIST) - 1)
        IMAGES['background'] = pygame.image.load(BACKGROUNDS_LIST[randBg]).convert()

        # select random player sprites
        randPlayer = random.randint(0, len(PLAYERS_LIST) - 1)
        IMAGES['player'] = (
            pygame.image.load(PLAYERS_LIST[randPlayer][0]).convert_alpha(),
            pygame.image.load(PLAYERS_LIST[randPlayer][1]).convert_alpha(),
            pygame.image.load(PLAYERS_LIST[randPlayer][2]).convert_alpha(),
        )

        # select random pipe sprites
        pipeindex = random.randint(0, len(PIPES_LIST) - 1)
        IMAGES['pipe'] = (
            pygame.transform.rotate(
                pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(), 180),
            pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(),
        )

        # hismask for pipes
        HITMASKS['pipe'] = (
            getHitmask(IMAGES['pipe'][0]),
            getHitmask(IMAGES['pipe'][1]),
        )

        # hitmask for player
        HITMASKS['player'] = (
            getHitmask(IMAGES['player'][0]),
            getHitmask(IMAGES['player'][1]),
            getHitmask(IMAGES['player'][2]),
        )

        movementInfo = showWelcomeAnimation()
        crashInfo = mainGame(movementInfo)
        showGameOverScreen(crashInfo)


def showWelcomeAnimation():
    """Shows welcome screen animation of flappy bird"""
    # index of player to blit on screen
    playerIndex = 0
    playerIndexGen = cycle([0, 1, 2, 1])
    # iterator used to change playerIndex after every 5th iteration
    loopIter = 0

    playerx = int(SCREENWIDTH * 0.2)
    playery = int((SCREENHEIGHT - IMAGES['player'][0].get_height()) / 2)

    messagex = int((SCREENWIDTH - IMAGES['message'].get_width()) / 2)
    messagey = int(SCREENHEIGHT * 0.12)

    basex = 0
    # amount by which base can maximum shift to left
    baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()

    # player shm for up-down motion on welcome screen
    playerShmVals = {'val': 0, 'dir': 1}

    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP) or event.type==pygame.MOUSEBUTTONDOWN:
                # make first flap sound and return values for mainGame
                SOUNDS['wing'].play()
                return {
                    'playery': playery + playerShmVals['val'],
                    'basex': basex,
                    'playerIndexGen': playerIndexGen,
                }

        # adjust playery, playerIndex, basex
        if (loopIter + 1) % 5 == 0:
            playerIndex = next(playerIndexGen)
        loopIter = (loopIter + 1) % 30
        basex = -((-basex + 4) % baseShift)
        playerShm(playerShmVals)

        # draw sprites
        SCREEN.blit(IMAGES['background'], (0,0))
        SCREEN.blit(IMAGES['player'][playerIndex],
                    (playerx, playery + playerShmVals['val']))
        SCREEN.blit(IMAGES['message'], (messagex, messagey))
        SCREEN.blit(IMAGES['base'], (basex, BASEY))

        pygame.display.update()
        FPSCLOCK.tick(FPS)


def mainGame(movementInfo):
    score = playerIndex = loopIter = 0
    playerIndexGen = movementInfo['playerIndexGen']
    playerx, playery = int(SCREENWIDTH * 0.2), movementInfo['playery']

    basex =
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
App Inventor是一种用于创建手机应用程序的可视化编程工具,而“Fly Bird”是一种很受欢迎的游戏。使用App Inventor可以轻松创建一个“Fly Bird”游戏应用程序。 在开始之前,我们需要理解“Fly Bird”游戏的规则和要求。游戏的目标是通过控制小鸟的飞行,躲避障碍物并尽可能地飞得远。我们需要在App Inventor中实现以下功能: 1. 创建一个小鸟和背景:我们可以使用App Inventor的图像组件来设计一个小鸟的图像,并设置一个合适的背景。 2. 控制小鸟的飞行:我们可以使用App Inventor的触摸事件来根据玩家的输入改变小鸟的飞行高度。通过监测触摸事件,我们可以使小鸟向上飞行。 3. 添加障碍物:我们可以使用App Inventor的图像组件来设计障碍物,并通过移动它们来制造游戏的难度。障碍物可以是管道、树木或其他物体。 4. 碰撞检测:我们需要使用App Inventor的碰撞检测功能来判断小鸟是否与障碍物相撞。如果小鸟与任何障碍物碰撞,则游戏结束。 5. 计分系统:我们还可以在屏幕上添加一个计分板,通过小鸟飞过的障碍物数量来计算玩家的得分。 通过上述步骤,我们可以在App Inventor中创建一个可以玩“Fly Bird”游戏的应用程序。在设计和开发过程中,我们可以使用App Inventor的其他功能,如声音特效、计时器和动画效果,来增加游戏的娱乐性和创意性。在完成开发后,可以将应用程序导出为APK文件,以便在Android设备上安装和运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值