欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现简单flappy bird

程序员文章站 2023-02-20 21:40:27
本文实例为大家分享了python实现flappy bird的简单代码,供大家参考,具体内容如下 import pygame from pygame.locals...

本文实例为大家分享了python实现flappy bird的简单代码,供大家参考,具体内容如下

import pygame
from pygame.locals import *
from sys import exit
import random
 
# 屏幕宽度
screenwidth = 288
# 屏幕高度
screenheight = 512
images = {}
# 背景图片地址
background_path = 'back_ground.png'
pipe_path = 'pipe.png'
base_path = 'base.png'
player_path = (
  'bird2_0.png',
  'bird2_1.png',
  'bird2_2.png',
)
# 初始化
pygame.init()
# 创建窗口
screen = pygame.display.set_mode((screenheight, screenheight))
# 设置窗口标题
pygame.display.set_caption("flappy bird")
 
# 加载图片,透明用convert_alpha,不透明用convert
images['background'] = pygame.image.load(background_path).convert()
images['base'] = pygame.image.load(base_path).convert_alpha()
images['bird'] = (
  pygame.image.load(player_path[0]).convert_alpha(),
  pygame.image.load(player_path[1]).convert_alpha(),
  pygame.image.load(player_path[2]).convert_alpha(),
)
images['pipe'] = (
  pygame.transform.rotate(pygame.image.load(pipe_path).convert_alpha(), 180),
  pygame.image.load(pipe_path).convert_alpha()
 
)
basey = screenheight * 0.82
 
# 设置帧率
fps = 30
fpsclock = pygame.time.clock()
 
pipe_width = images['pipe'][0].get_width()
pipe_height = images['pipe'][0].get_height()
player_width = images['bird'][0].get_width()
player_height = images['bird'][0].get_height()
 
 
pipegapsize = 100 # 两个水管之间的距离
x = screenwidth//2
y = screenheight//2
move_x = 0
move_y = 0
 
flap = 0 # 小鸟初始状态
pipevelx = -4 # 管道x方向的速度
playervely = 0 # 小鸟y方向的初速度
playermaxvely = 10 # 小鸟y方向的最大速度
playerminvely = -8 # 小鸟y方向的最小速度
playeraccy = 2 # 小鸟y方向的下降加速度
playerflapacc = -3 # 小鸟y方向的上升加速度
playerflapped = false # 当小鸟飞的时候为真
playery = int((screenheight - player_height)/2)
 
 
# 随机移动柱子
def getrandompipe():
  # 两个水管之间的距离有如下变量
  gapys = [20, 30, 40, 50, 60, 70, 80, 90]
  index = random.randint(0, len(gapys) - 1)
  gapy = gapys[index]
 
  gapy += int(basey * 0.2)
  # 水管x坐标
  pipex = screenwidth + 10
 
  return [
    {'x': pipex, 'y': gapy - pipe_height},  # 上面水管的左上角位置
    {'x': pipex, 'y': gapy + pipegapsize},  # 下面水管的左上角位置
  ]
 
 
 
 
newpipel = getrandompipe()
 
upperpipes = [
  {'x': screenwidth, 'y':newpipel[0]['y']}
]
lowerpipes = [
  {'x': screenwidth, 'y':newpipel[1]['y']}
]
 
while true:
 
  for event in pygame.event.get():
    if event.type == quit:
      exit()
    elif event.type == keydown:
      if event.key == k_left:
        move_x = -3
      elif event.key == k_right:
        move_x = 3
      elif event.key == k_up:
        move_y = -3
      elif event.key == k_down:
        move_y = 3
    elif event.type == keyup:
      move_x = 0
      move_y = 0
 
  x = x + move_x
  y = y + move_y
 
  # 防止冲出边界
  if x > screenwidth:
    x = 0
  elif x < 0:
    x = screenwidth
 
  if y > screenheight:
    y = 0
  elif y < 0:
    y = screenheight
 
  # 贴图在左上角
  screen.blit(images['background'], (0, 0)) # 背景
  # 显示水管
  for upipe, lpipe in zip(upperpipes, lowerpipes):
    screen.blit(images['pipe'][0], (upipe['x'], upipe['y']))
    screen.blit(images['pipe'][1], (lpipe['x'], lpipe['y']))
 
 
 
  # 放小鸟
  screen.blit(images['bird'][flap], (x, y))
  flap = flap + 1
 
  if flap % 3 == 0:
    flap = 0
 
 
  for upipe, lpipe in zip(upperpipes, lowerpipes):
    upipe['x'] += pipevelx
    lpipe['x'] += pipevelx
 
 
  # 当水管移动到某一位置的时候,生成新的水管
 
  if 0 < upperpipes[0]['x'] < 5:
    newpipe = getrandompipe()
    upperpipes.append(newpipe[0])
    lowerpipes.append(newpipe[1])
  
 
  # 如果水管从右往左移动到边缘,则摧毁水管
  if upperpipes[0]['x'] < -pipe_width:
    # 队列头出队
    upperpipes.pop(0)
    lowerpipes.pop(0)
 
 
  # 刷新画面
  pygame.display.update()
  fpsclock.tick(fps)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。