使用Python寫一個小游戲
引言
最近python語言大火,除了在科學(xué)計算領(lǐng)域python有用武之地之外,在游戲、后臺等方面,python也大放異彩,本篇博文將按照正規(guī)的項目開發(fā)流程,手把手教大家寫個python小游戲,來感受下其中的有趣之處。本次開發(fā)的游戲叫做alien invasion。
安裝pygame并創(chuàng)建能左右移動的飛船
安裝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()
總結(jié)
以上所述是小編給大家介紹的Python寫一個小游戲,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
matplotlib jupyter notebook 圖像可視化 plt show操作
這篇文章主要介紹了matplotlib jupyter notebook 圖像可視化 plt show操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Numpy array數(shù)據(jù)的增、刪、改、查實例
今天小編就為大家分享一篇Numpy array數(shù)據(jù)的增、刪、改、查實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Win11平臺安裝和配置NeoVim0.8.2編輯器搭建Python3開發(fā)環(huán)境詳細過程(2023最新攻略)
這篇文章主要介紹了Win11平臺安裝和配置NeoVim0.8.2編輯器搭建Python3開發(fā)環(huán)境(2023最新攻略),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01