python實現(xiàn)flappy bird游戲
flappy bird最近火遍大江南北,教你用python寫游戲的第一課就向它開刀了。
這個課程的基礎(chǔ)是假定你有比較不錯的編程功底,對python有一點點的基礎(chǔ)。
一、準(zhǔn)備工作
1、用python寫游戲需要什么呢?
1)當(dāng)然是python本身了,我用的是python2.7,不同版本大同小異。
2)pygame,這個非常重要,所有的核心都是基于這個lib的。
2、分析游戲
flappy bird這個游戲很簡單,大致可以分為4個部分:
1)背景。背景分為兩個,一個是bg,一個是land。bg就是那張有著天空白云的圖,land就是最下面有斜杠的圖。
2)bird。這個不用我說,主角是也。
3)pipe。就是那個水管。
4)其他。包括開始菜單和分?jǐn)?shù)板。
著重分析的就是bird和pipe。
bird會一直往右飛,不點屏幕就會往下掉。
pipe會不斷地出現(xiàn),每通過一個pipe就會加一分。
bird撞到pipe或者掉到地上游戲就會結(jié)束。
3、準(zhǔn)備資源
找一個flappy bird的apk,提取一下內(nèi)部文件,你就可以獲得:
1)一張叫做atlas.png的圖片。里面有所有我們要用得到的圖。
2)5個ogg文件,包含了所有音效。
3)一個叫做atlas.txt的文本文件,包含了圖片在大圖中的位置。
二、開始
上一中,我們已經(jīng)分析過了2個核心,bird和pipe。這一單元,我要講訴的就是bird。
首先呢,我們需要創(chuàng)建一個對象,這個對象取名為Bird。
Bird具有以下屬性:
1)圖片。具體來說就是他長什么樣。
2)大小。長多大。
3)是否撞到了。還記得游戲規(guī)則么,撞到就gameover了。
4)速度。每一幀移動多遠(yuǎn)。
這只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提供了一個函數(shù),可以讓我們方便的取出資源。
我們先載入圖片
#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)的圖片就落實了。
最后,因為是在電腦上運(yùn)行,點屏幕就需要改成相應(yīng)的按下空格鍵。
key_pressed = pygame.key.get_pressed()
if not bird.is_hit:
if key_pressed[K_SPACE]:
bird.click()
終于,任務(wù)完成了,雖然,雖然程序有點小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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Django自定義模板標(biāo)簽template_tags的用處
這篇文章主要介紹了淺談Django自定義模板標(biāo)簽template_tags的用處,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Python使用Matplotlib實現(xiàn)Logos設(shè)計代碼
這篇文章主要介紹了Python使用Matplotlib實現(xiàn)Logos設(shè)計代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

