python实现flappy bird游戏

yipeiwu_com7年前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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python查询文件夹下excel的sheet名代码实例

本文实例为大家分享了python查询文件夹下excel的sheet的具体代码,供大家参考,具体内容如下 import os,sys,stat,xlrd path=r"F:\360D...

使用Python中的tkinter模块作图的方法

使用Python中的tkinter模块作图的方法

python简述: Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。P...

python实现学生信息管理系统

继上篇博客Python实现简易通讯录后,我就想写一个复杂点的学生信息管理系统,这次实现的功能有 1.学生信息的录入管理;   2.学生选课操作;   3.学生选课情况查询; 这次仍然...

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

MapReduce与HDFS简介 什么是Hadoop? Google为自己的业务需要提出了编程模型MapReduce和分布式文件系统Google File System,并发布了相关论文...

Python中实例化class的执行顺序示例详解

前言 本文主要介绍了关于Python实例化class的执行顺序的相关内容,下面话不多说了,来一起看看详细的介绍吧 Python里对类的实例化时有怎样的顺序 一般来说一个类里面有类变量和...