国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現flappy bird游戲

2020-01-04 13:43:34
字體:
來源:轉載
供稿:網友

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 imggame_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 birdbird_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 pygamefrom pygame.locals import *from sys import exitimport random # configurescreen_w = 288screen_h = 512 # classclass 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 gamepygame.init()screen = pygame.display.set_mode((screen_w,screen_h))pygame.display.set_caption('flappy bird @Kevio') #load imggame_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 birdbird_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 gamescore = 0clock = 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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 多伦县| 淮北市| 荃湾区| 汉中市| 济宁市| 昔阳县| 桑日县| 竹山县| 长寿区| 汪清县| 三穗县| 赞皇县| 广德县| 永嘉县| 中阳县| 哈尔滨市| 大田县| 平阳县| 玛曲县| 南江县| 原阳县| 东乡县| 辰溪县| 邵武市| 罗城| 泰和县| 龙门县| 元朗区| 军事| 监利县| 临海市| 衡南县| 永胜县| 肇庆市| 鄂州市| 社旗县| 崇文区| 微山县| 晋宁县| 呼图壁县| 达日县|