最近python語言大火,除了在科學計算領域python有用武之地之外,在游戲、后臺等方面,python也大放異彩,本篇博文將按照正規的項目開發流程,手把手教大家寫個python小游戲,來感受下其中的有趣之處。本次開發的游戲叫做alien invasion。
安裝pygame并創建能左右移動的飛船
本人電腦是windows 10、python3.6,pygame下載地址: 傳送門
請自行下載對應python版本的pygame 運行以下命令
$ pip install wheel$ pip install pygame?1.9.3?cp36?cp36m?win_amd64.whl
創建Pygame窗口及響應用戶輸入
新建一個文件夾alien_invasion,并在文件夾中新建alien_invasion.py文件,輸入如下代碼。
import sysimport pygamedef 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

為了在寫游戲的過程中能便捷地創建一些新功能,下面額外編寫一個settings模塊,其中包含一個Settings類,用于將所有設置存儲在一個地方。這樣在以后項目增大時修改游戲的外觀就更加容易。 我們首先將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中導入Settings類,并使用相關設置,修改如下:
import sysimport pygamefrom settings import Settingsdef 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默認加載位圖。雖然其他類型的圖像也能加載,但是需要安裝額外的庫。我們推薦去免費的圖片素材網站上去找圖像: 傳送門 。我們在主項目文件夾(alien_invasion)中新建一個文件夾叫images,將如下bmp圖片放入其中。

接下來,我們創建飛船類ship.py:
import pygameclass 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文件中調用blitme方法:
import sysimport pygamefrom settings import Settingsfrom ship import Settingsdef 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()重構:模塊game_functions
在大型項目中,經常需要在添加新代碼前重構既有代碼。重構的目的是為了簡化代碼的結構,使其更加容易擴展。我們將實現一個game_functions模塊,它將存儲大量讓游戲Alien invasion運行的函數。通過創建模塊game_functions,可避免alien_invasion.py太長,使其邏輯更容易理解。
首先我們將管理事件的代碼移到一個名為check_events()的函數中,目的是為了隔離事件循環
import sysimport pygamedef check_events(): #respond to keyboard and mouse item for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
然后我們修改alien_invasion.py代碼,導入game_functions模塊,并將事件循環替換成對函數check_events()的調用:
import sysimport pygamefrom settings import Settingsfrom ship import Shipimport game_functions as gfdef run_game(): #initialize game and create a dispaly object pygame.init() ai_settings = Settings()
總結
以上所述是小編給大家介紹的Python寫一個小游戲,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答