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

python实现flappy bird游戏

程序员文章站 2022-11-01 21:50:38
flappy bird最近火遍大江南北,教你用python写游戏的第一课就向它开刀了。 这个课程的基础是假定你有比较不错的编程功底,对python有一点点的基础。 一、...

flappy bird最近火遍大江南北,教你用python写游戏的第一课就向它开刀了。

这个课程的基础是假定你有比较不错的编程功底,对python有一点点的基础。

一、准备工作

1、用python写游戏需要什么呢?

 1)当然是python本身了,我用的是python2.7,不同版本大同小异。

 2)pygame,这个非常重要,所有的核心都是基于这个lib的。

2、分析游戏

  flappy bird这个游戏很简单,大致可以分为4个部分:

  1)背景。背景分为两个,一个是bg,一个是land。bg就是那张有着天空白云的图,land就是最下面有斜杠的图。

   2)bird。这个不用我说,主角是也。

  3)pipe。就是那个水管。

  4)其他。包括开始菜单和分数板。

着重分析的就是bird和pipe。

 bird会一直往右飞,不点屏幕就会往下掉。

 pipe会不断地出现,每通过一个pipe就会加一分。

 bird撞到pipe或者掉到地上游戏就会结束。

3、准备资源

 找一个flappy bird的apk,提取一下内部文件,你就可以获得:

 1)一张叫做atlas.png的图片。里面有所有我们要用得到的图。

 2)5个ogg文件,包含了所有音效。

 3)一个叫做atlas.txt的文本文件,包含了图片在大图中的位置。

二、开始

上一中,我们已经分析过了2个核心,bird和pipe。这一单元,我要讲诉的就是bird。

首先呢,我们需要创建一个对象,这个对象取名为bird。

bird具有以下属性:

  1)图片。具体来说就是他长什么样。

  2)大小。长多大。

  3)是否撞到了。还记得游戏规则么,撞到就gameover了。

  4)速度。每一帧移动多远。

这只bird没事还会往下掉,点一下就会往上飞,这就是两个动作。

于是,编写了如下代码:

class bird(pygame.sprite.sprite):
  def __init__(self,bird_img,pos):
    pygame.sprite.sprite.__init__(self)
    self.image = bird_img
    self.rect = self.image.get_rect()
    self.rect.midbottom = pos
    self.speed = 1
    self.is_hit = false
  def move(self):
    self.rect.left += self.speed
    self.rect.top += self.speed
  def click(self):
    self.rect.top -= 1.5*self.speed

还记得最开始我说过,flappy bird所有的图片资源都在一张图片altas.png上。

pygame提供了一个函数,可以让我们方便的取出资源。

我们先载入图片

#load img
game_img = pygame.image.load('res/img/atlas.png')
bg_rect = pygame.rect(0,0,288,512)
bg_img = game_img.subsurface(bg_rect).convert()
 然后分别获取需要的图片。
#config bird
bird_rect = pygame.rect(0,970,48,48)
bird_pos = [100,230]
bird_img = game_img.subsurface(bird_rect).convert_alpha()
bird = bird(bird_img,bird_pos)

这样 bird和bg(background)的图片就落实了。

最后,因为是在电脑上运行,点屏幕就需要改成相应的按下空格键。

key_pressed = pygame.key.get_pressed()
  if not bird.is_hit:
    if key_pressed[k_space]:
      bird.click()

 终于,任务完成了,虽然,虽然程序有点小bug,但这是下面要说的问题了。

完整代码如下:

# -*- coding: utf-8 -*-
"""
@author: kevio
"""
import pygame
from pygame.locals import *
from sys import exit
import random
 
# configure
screen_w = 288
screen_h = 512
 
# class
class bird(pygame.sprite.sprite):
  def __init__(self,bird_img,pos):
    pygame.sprite.sprite.__init__(self)
    self.image = bird_img
    self.rect = self.image.get_rect()
    self.rect.midbottom = pos
    self.speed = 1
    self.is_hit = false
  def move(self):
    self.rect.left += self.speed
    self.rect.top += self.speed
  def click(self):
    self.rect.top -= 1.5*self.speed
    
# init the game
pygame.init()
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption('flappy bird @kevio')
 
#load img
game_img = pygame.image.load('res/img/atlas.png')
bg_rect = pygame.rect(0,0,288,512)
bg_img = game_img.subsurface(bg_rect).convert()
#config bird
bird_rect = pygame.rect(0,970,48,48)
bird_pos = [100,230]
bird_img = game_img.subsurface(bird_rect).convert_alpha()
bird = bird(bird_img,bird_pos)
#config the game
score = 0
clock = pygame.time.clock()
running = true
 
while running:
  clock.tick(60)
 
  screen.fill(0)
  screen.blit(bg_img,(0,0))
 
  if not bird.is_hit:
    screen.blit(bird.image,bird.rect)
    bird.move()
  else:
    running = false
    
  pygame.display.update()
 
  for event in pygame.event.get():
    if event.type == pygame.quit:
      pygame.quit()
      exit()
 
  key_pressed = pygame.key.get_pressed()
  if not bird.is_hit:
    if key_pressed[k_space]:
      bird.click()

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

上一篇: 放牛

下一篇: 什么是团结