最近python語言大火,除了在科學(xué)計算領(lǐng)域python有用武之地之外,在游戲、后臺等方面,python也大放異彩,本篇博文將按照正規(guī)的項目開發(fā)流程,手把手教大家寫個python小游戲,項目來自《Python編程從入門到實踐》(本文將原項目中的部分錯誤進行修改完善,PS:強烈推薦這本書,真的很贊),來感受下其中的有趣之處。本次開發(fā)的游戲叫做alien invasion。
安裝pygame
本人電腦是windows 10、python3.6,pygame下載地址:傳送門
請自行下載對應(yīng)python版本的pygame
運行以下命令
$ pip install wheel
$ pip install pygame?1.9.3?cp36?cp36m?win_amd64.whl
創(chuàng)建Pygame窗口及響應(yīng)用戶輸入
新建一個文件夾alien_invasion,并在文件夾中新建alien_invasion.py文件,輸入如下代碼。
import sys
import pygame
def run_game():
#initialize game and create a dispaly object
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# fill color
screen.fill(bg_color)
# visualiaze the window
pygame.display.flip()
run_game()
運行上述代碼,我們可以得到一個灰色界面的窗口:
$ python alien_invasion.py
創(chuàng)建設(shè)置類
為了在寫游戲的過程中能便捷地創(chuàng)建一些新功能,下面額外編寫一個settings模塊,其中包含一個Settings類,用于將所有設(shè)置存儲在一個地方。這樣在以后項目增大時修改游戲的外觀就更加容易。
我們首先將alien_invasion.py中的顯示屏大小及顯示屏顏色進行修改。
首先在alien_invasion文件夾下新建python文件settings.py,并向其中添加如下代碼:
class Settings(object):
"""docstring for Settings"""
def __init__(self):
# initialize setting of game
# screen setting
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
然后再alien_invasion.py中導(dǎo)入Settings類,并使用相關(guān)設(shè)置,修改如下:
import sys
import pygame
from settings import Settings
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# fill color
screen.fill(ai_settings.bg_color)
# visualiaze the window
pygame.display.flip()
run_game()
添加飛船圖像
接下來,我們需要將飛船加入游戲中。為了在屏幕上繪制玩家的飛船,我們將加載一幅圖像,再使用Pygame()方法blit()繪制它。
在游戲中幾乎可以使用各種類型的圖像文件,但是使用位圖(.bmp)文件最為簡單,這是因為Pygame默認(rèn)加載位圖。雖然其他類型的圖像也能加載,但是需要安裝額外的庫。我們推薦去免費的圖片素材網(wǎng)站上去找圖像:傳送門。我們在主項目文件夾(alien_invasion)中新建一個文件夾叫images,將如下bmp圖片放入其中。
接下來,我們創(chuàng)建飛船類ship.py:
import pygame
class Ship():
def __init__(self,screen):
#initialize spaceship and its location
self.screen = screen
# load bmp image and get rectangle
self.image = pygame.image.load('image/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#put spaceship on the bottom of window
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
#buld the spaceship at the specific location
self.screen.blit(self.image,self.rect)
最后我們在屏幕上繪制飛船,即在alien_invasion.py文件中調(diào)用blitme方法:
import sys
import pygame
from settings import Settings
from ship import Settings
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(screen)
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# fill color
screen.fill(ai_settings.bg_color)
ship.blitme()
# visualiaze the window
pygame.display.flip()
run_game()
重構(gòu):模塊game_functions
在大型項目中,經(jīng)常需要在添加新代碼前重構(gòu)既有代碼。重構(gòu)的目的是為了簡化代碼的結(jié)構(gòu),使其更加容易擴展。我們將實現(xiàn)一個game_functions模塊,它將存儲大量讓游戲Alien invasion運行的函數(shù)。通過創(chuàng)建模塊game_functions,可避免alien_invasion.py太長,使其邏輯更容易理解。
函數(shù)check_events()
首先我們將管理事件的代碼移到一個名為check_events()的函數(shù)中,目的是為了隔離事件循環(huán)
import sys
import pygame
def check_events():
#respond to keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
然后我們修改alien_invasion.py代碼,導(dǎo)入game_functions模塊,并將事件循環(huán)替換成對函數(shù)check_events()的調(diào)用:
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(screen)
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
gf.check_events()
# fill color
screen.fill(ai_settings.bg_color)
ship.blitme()
# visualiaze the window
pygame.display.flip()
run_game()
函數(shù)update_screen()
將更新屏幕的代碼移到一個名為update_screen()函數(shù)中,并將這個函數(shù)放在模塊game_functions中:
def update_screen(ai_settings,screen,ship):
# fill color
screen.fill(ai_settings.bg_color)
ship.blitme()
# visualiaze the window
pygame.display.flip()
其中alien_invasion修改如下:
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(screen)
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
gf.check_events()
gf.update_screen(ai_settings,screen,ship)
run_game()
從上面一套流程走下來,我們發(fā)現(xiàn):在實際的開發(fā)過程中,我們一開始將代碼編寫得盡可能的簡單,并在項目越來越復(fù)雜時進行重構(gòu)。接下來我們開始處理游戲的動態(tài)方面。
駕駛飛船
這里我們要實現(xiàn)的就是使玩家通過左右箭頭鍵來控制飛船的左移與右移。
響應(yīng)按鍵
因為在pygame中,每次按鍵都被注冊為KEYDOWN事件,在check_events()中,我們通過event.type檢測到KEYDOWN事件后還需進一步判斷是哪個按鍵。代碼如下:
def check_events(ship):
#respond to keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
#move right
ship.rect.centerx +=1
允許不斷移動
玩家按住右箭頭不動時,我們希望飛船能不斷地移動,知道玩家松開為止。這里我們通過KETUO事件來判斷。因此我們設(shè)置一個標(biāo)志位moving_right來實現(xiàn)持續(xù)移動。原理如下:
飛船不動時,標(biāo)志moving_right將為false。玩家按下右箭頭時,我們將這個標(biāo)志設(shè)置為True;玩家松開時,我們將標(biāo)志重新設(shè)置成False。
這個移動屬性是飛船屬性的一種,我們用ship類來控制,因此我們給這個類增加一個屬性名稱叫,moving_right以及一個update()方法來檢測標(biāo)志moving_right的狀態(tài)。
ship
self.moving_right = False
def update(self):
if self.moving_right:
self.rect.centerx +=1
game_functions
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
#move right
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key = pygame.K_RIGHT:
ship.moving_right = False
最后在alien_invasion中調(diào)用update()方法
while True:
# supervise keyboard and mouse item
gf.check_events(ship)
ship.update()
左右移動
前面我們實現(xiàn)了向右移動,接下來實現(xiàn)向左移動,邏輯類似,代碼就不貼了。
調(diào)整飛船的速度
當(dāng)前,每次執(zhí)行while循環(huán)時,飛船最多移動一個像素,我們可以在Settings中添加ship_speed_factor,用于控制飛船的速度。我們將根據(jù)這個屬性決定飛船每次循環(huán)時最多移動多少距離。
Settings:
class Settings(object):
"""docstring for Settings"""
def __init__(self):
# initialize setting of game
# screen setting
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
self.ship_speed_factor = 1.5
Ship:
class Ship():
def __init__(self,ai_settings,screen):
#initialize spaceship and its location
self.screen = screen
self.ai_settings = ai_settings
限制飛船的活動范圍
如果玩家按住箭頭的時間過長,飛船就會消失,那么如何使飛船抵達屏幕邊緣時停止移動?這里我們只需要修改Ship類中的update方法,增加一個邏輯判斷。
重構(gòu)
這里我們主要講check_events()函數(shù)進行重構(gòu),將其中部分代碼分成兩部分,一部分處理KEYDOWN事件,一部分處理KEYUP事件。
game_functions:
def check_keydown_events(event,ship):
if event.key == pygame.K_RIGHT:
#move right
ship.moving_right = True
elif event.key == pygame.K_LEFT:
#move right
ship.moving_left = True
def check_keyup_events(event,ship):
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
#move right
ship.moving_left = False
def check_events(ship):
#respond to keyboard and mouse item
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event,ship)
elif event.type == pygame.KEYUP:
check_keyup_events(event,ship)
射擊
接下來添加射擊功能,使玩家按空格鍵時發(fā)射子彈,子彈將在屏幕中向上穿行,抵達屏幕后消失。
添加子彈設(shè)置
在Settings類中增加一些子彈的屬性,這里我們創(chuàng)建一個寬3像素,高15像素的深灰色子彈。子彈的速度比飛船稍低。
創(chuàng)建Bullet類
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired from the ship."""
def __init__(self, ai_settings, screen, ship):
"""Create a bullet object, at the ship's current position."""
super().__init__()
self.screen = screen
# Create bullet rect at (0, 0), then set correct position.
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
# Store a decimal value for the bullet's position.
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""Move the bullet up the screen."""
# Update the decimal position of the bullet.
self.y -= self.speed_factor
# Update the rect position.
self.rect.y = self.y
def draw_bullet(self):
"""Draw the bullet to the screen."""
pygame.draw.rect(self.screen, self.color, self.rect)
將子彈存儲到group中
前面定義了Bullet類和必要的設(shè)置后,就可以編寫代碼了,在玩家每次按空格鍵時都會發(fā)射一發(fā)子彈。首先,我們在alien_invasion中創(chuàng)建一個group,用于存儲所有的有效子彈。
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(ai_settings,screen)
bullets = Group()
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
gf.check_events(ai_settings, screen, ship,bullets)
ship.update()
bullets.update()
gf.update_screen(ai_settings, screen, ship,bullets)
開火
這里我們修改check_keydown_events()函數(shù),來監(jiān)聽玩家按下空格鍵的事件。這里還需要修改update_screen()函數(shù),確保屏幕每次更新時,都能重繪每一個子彈。
我們來看下效果:
刪除消失的子彈
在alien_invasion中刪除消失的子彈。
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
#initialize game and create a dispaly object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(ai_settings,screen)
bullets = Group()
pygame.display.set_caption("Alien Invasion")
# set backgroud color
bg_color = (230,230,230)
# game loop
while True:
# supervise keyboard and mouse item
gf.check_events(ai_settings, screen, ship,bullets)
ship.update()
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <=0:
bullets.remove(bullet)
gf.update_screen(ai_settings, screen,ship,bullets)
run_game()
限制子彈數(shù)量
為了鼓勵玩家有目標(biāo)的射擊,我們規(guī)定屏幕上只能同時存在3顆子彈,我們只需要在每次創(chuàng)建子彈前檢查未消失的子彈數(shù)目是否小于3即可。
創(chuàng)建update_bullets()函數(shù)
為了使alien_invasion中代碼更加簡單,我們將檢查子彈管理的代碼,移到game_functions模塊中:
def update_bullets(bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom<=0:
bullets.remove(bullet)
創(chuàng)建fire_bullet()函數(shù)
這里我們將發(fā)射子彈的代碼移到一個獨立的函數(shù)中:
def fire_bullet(ai_settings,screen,ship,bullets):
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
在我們完成新的任務(wù)之前,我們先給游戲添加一個結(jié)束游戲的快捷鍵Q:
創(chuàng)建第一個外星人
這里和創(chuàng)建飛船的方法一樣
class Alien(Sprite):
"""A class to represent a single alien in the fleet."""
def __init__(self, ai_settings, screen):
"""Initialize the alien, and set its starting position."""
super().__init__()
self.screen = screen
self.ai_settings = ai_settings
# Load the alien image, and set its rect attribute.
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# Start each new alien near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# Store the alien's exact position.
self.x = float(self.rect.x)
def blitme(self):
"""Draw the alien at its current location."""
self.screen.blit(self.image, self.rect)
創(chuàng)建一群外星人
這里我們首先確定一行能容納多少個外星人以及要繪制幾行。這里改動代碼較多,直接看效果:
移動外星人
前面我們創(chuàng)建的是靜態(tài)的外星人,現(xiàn)在我們需要讓外星人動起來。這里我們在Settings類中設(shè)置外星人移動的速度,然后通過Alien類中的update的方法來實現(xiàn)移動
射殺外星人
要想射殺外星人,就必須先檢測兩個編組成員之間是否發(fā)生碰撞,在游戲中,碰撞就是游戲元素重疊在一起。這里我們使用sprite.groupcollide()來檢測兩個編組的成員之間的碰撞。
子彈擊中外星人時,需要立馬知道,并同時使被碰撞的外星人立即消失,因此我們需要在更新子彈的位置后立即檢測碰撞。
結(jié)束游戲
這里我們還需要知道何時該結(jié)束游戲,有以下幾種情況:
實際效果:
最后我們將給游戲添加一個Play按鈕,用于根據(jù)需要啟動游戲以及在游戲結(jié)束后重啟游戲。我們還將實現(xiàn)一個計分系統(tǒng),能夠在玩家等級提高時加快節(jié)奏。
添加Play按鈕
這里可以先將游戲初始化為非活動狀態(tài),當(dāng)我們點擊了按鈕,就開始游戲。由于Pygame中沒有內(nèi)置的創(chuàng)建按鈕的方法。因此我們可以通過創(chuàng)建一個Button類來創(chuàng)建一個自帶標(biāo)簽的實心矩形。我們通過檢測鼠標(biāo)發(fā)生點擊后的坐標(biāo)是否與我們繪制的按鈕發(fā)生碰撞與否來判斷是否發(fā)生了點擊事件。
提高等級
為了使玩家將敵人消滅干凈后能夠提高游戲難度,增加趣味性,這里我們可以在Settings類中進行修改,增加靜態(tài)初始值,和動態(tài)初始值。
記分、等級、剩余飛船
上面游戲開發(fā)完了,那么你需要將其轉(zhuǎn)成文exe的可執(zhí)行文件。我們采用pyinstaller,安裝步驟參考:傳送門
github地址:傳送門