程序員小新人學(xué)習(xí) 2018-08-14 09:49:50 引言最近python語言大火,除了在科學(xué)計(jì)算領(lǐng)域python有用武之地之外,在游戲、后臺(tái)等方面,python也大放異彩,本篇博文將按照正規(guī)的項(xiàng)目開發(fā)流程,手把手教大家寫個(gè)python小游戲,項(xiàng)目來自《Python編程從入門到實(shí)踐》(本文將原項(xiàng)目中的部分錯(cuò)誤進(jìn)行修改完善,PS:強(qiáng)烈推薦這本書,真的很贊),來感受下其中的有趣之處。本次開發(fā)的游戲叫做alien invasion。 安裝pygame并創(chuàng)建能左右移動(dòng)的飛船安裝pygame 本人電腦是windows 10、python3.6,pygame下載地址:傳送門 請(qǐng)自行下載對(duì)應(yīng)python版本的pygame 運(yùn)行以下命令 $ pip install wheel $ pip install pygame?1.9.3?cp36?cp36m?win_amd64.whl
創(chuàng)建Pygame窗口及響應(yīng)用戶輸入 新建一個(gè)文件夾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()
運(yùn)行上述代碼,我們可以得到一個(gè)灰色界面的窗口: $ python alien_invasion.py
創(chuàng)建設(shè)置類 為了在寫游戲的過程中能便捷地創(chuàng)建一些新功能,下面額外編寫一個(gè)settings模塊,其中包含一個(gè)Settings類,用于將所有設(shè)置存儲(chǔ)在一個(gè)地方。這樣在以后項(xiàng)目增大時(shí)修改游戲的外觀就更加容易。 我們首先將alien_invasion.py中的顯示屏大小及顯示屏顏色進(jìn)行修改。 首先在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)文件最為簡(jiǎn)單,這是因?yàn)镻ygame默認(rèn)加載位圖。雖然其他類型的圖像也能加載,但是需要安裝額外的庫。我們推薦去免費(fèi)的圖片素材網(wǎng)站上去找圖像:傳送門。我們?cè)谥黜?xiàng)目文件夾(alien_invasion)中新建一個(gè)文件夾叫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)
最后我們?cè)谄聊簧侠L制飛船,即在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 在大型項(xiàng)目中,經(jīng)常需要在添加新代碼前重構(gòu)既有代碼。重構(gòu)的目的是為了簡(jiǎn)化代碼的結(jié)構(gòu),使其更加容易擴(kuò)展。我們將實(shí)現(xiàn)一個(gè)game_functions模塊,它將存儲(chǔ)大量讓游戲Alien invasion運(yùn)行的函數(shù)。通過創(chuàng)建模塊game_functions,可避免alien_invasion.py太長(zhǎng),使其邏輯更容易理解。 函數(shù)check_events() 首先我們將管理事件的代碼移到一個(gè)名為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)替換成對(duì)函數(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() 將更新屏幕的代碼移到一個(gè)名為update_screen()函數(shù)中,并將這個(gè)函數(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):在實(shí)際的開發(fā)過程中,我們一開始將代碼編寫得盡可能的簡(jiǎn)單,并在項(xiàng)目越來越復(fù)雜時(shí)進(jìn)行重構(gòu)。接下來我們開始處理游戲的動(dòng)態(tài)方面。 駕駛飛船 這里我們要實(shí)現(xiàn)的就是使玩家通過左右箭頭鍵來控制飛船的左移與右移。 響應(yīng)按鍵 因?yàn)樵趐ygame中,每次按鍵都被注冊(cè)為KEYDOWN事件,在check_events()中,我們通過event.type檢測(cè)到KEYDOWN事件后還需進(jìn)一步判斷是哪個(gè)按鍵。代碼如下: 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
允許不斷移動(dòng) 玩家按住右箭頭不動(dòng)時(shí),我們希望飛船能不斷地移動(dòng),知道玩家松開為止。這里我們通過KETUO事件來判斷。因此我們?cè)O(shè)置一個(gè)標(biāo)志位moving_right來實(shí)現(xiàn)持續(xù)移動(dòng)。原理如下:
這個(gè)移動(dòng)屬性是飛船屬性的一種,我們用ship類來控制,因此我們給這個(gè)類增加一個(gè)屬性名稱叫,moving_right以及一個(gè)update()方法來檢測(cè)標(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()
左右移動(dòng) 前面我們實(shí)現(xiàn)了向右移動(dòng),接下來實(shí)現(xiàn)向左移動(dòng),邏輯類似,代碼就不貼了。 調(diào)整飛船的速度 當(dāng)前,每次執(zhí)行while循環(huán)時(shí),飛船最多移動(dòng)一個(gè)像素,我們可以在Settings中添加ship_speed_factor,用于控制飛船的速度。我們將根據(jù)這個(gè)屬性決定飛船每次循環(huán)時(shí)最多移動(dòng)多少距離。 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
限制飛船的活動(dòng)范圍 如果玩家按住箭頭的時(shí)間過長(zhǎng),飛船就會(huì)消失,那么如何使飛船抵達(dá)屏幕邊緣時(shí)停止移動(dòng)?這里我們只需要修改Ship類中的update方法,增加一個(gè)邏輯判斷。 重構(gòu) 這里我們主要講check_events()函數(shù)進(jìn)行重構(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)
射擊 接下來添加射擊功能,使玩家按空格鍵時(shí)發(fā)射子彈,子彈將在屏幕中向上穿行,抵達(dá)屏幕后消失。 添加子彈設(shè)置 在Settings類中增加一些子彈的屬性,這里我們創(chuàng)建一個(gè)寬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)
將子彈存儲(chǔ)到group中 前面定義了Bullet類和必要的設(shè)置后,就可以編寫代碼了,在玩家每次按空格鍵時(shí)都會(huì)發(fā)射一發(fā)子彈。首先,我們?cè)赼lien_invasion中創(chuàng)建一個(gè)group,用于存儲(chǔ)所有的有效子彈。 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ù),確保屏幕每次更新時(shí),都能重繪每一個(gè)子彈。 我們來看下效果: 刪除消失的子彈 在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ù)量 為了鼓勵(lì)玩家有目標(biāo)的射擊,我們規(guī)定屏幕上只能同時(shí)存在3顆子彈,我們只需要在每次創(chuàng)建子彈前檢查未消失的子彈數(shù)目是否小于3即可。 創(chuàng)建update_bullets()函數(shù) 為了使alien_invasion中代碼更加簡(jiǎn)單,我們將檢查子彈管理的代碼,移到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ā)射子彈的代碼移到一個(gè)獨(dú)立的函數(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)
添加外星人,并檢測(cè)碰撞在我們完成新的任務(wù)之前,我們先給游戲添加一個(gè)結(jié)束游戲的快捷鍵Q: 創(chuàng)建第一個(gè)外星人 這里和創(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)建一群外星人 這里我們首先確定一行能容納多少個(gè)外星人以及要繪制幾行。這里改動(dòng)代碼較多,直接看效果: 移動(dòng)外星人 前面我們創(chuàng)建的是靜態(tài)的外星人,現(xiàn)在我們需要讓外星人動(dòng)起來。這里我們?cè)赟ettings類中設(shè)置外星人移動(dòng)的速度,然后通過Alien類中的update的方法來實(shí)現(xiàn)移動(dòng) 射殺外星人 要想射殺外星人,就必須先檢測(cè)兩個(gè)編組成員之間是否發(fā)生碰撞,在游戲中,碰撞就是游戲元素重疊在一起。這里我們使用sprite.groupcollide()來檢測(cè)兩個(gè)編組的成員之間的碰撞。 子彈擊中外星人時(shí),需要立馬知道,并同時(shí)使被碰撞的外星人立即消失,因此我們需要在更新子彈的位置后立即檢測(cè)碰撞。 結(jié)束游戲 這里我們還需要知道何時(shí)該結(jié)束游戲,有以下幾種情況:
實(shí)際效果: 計(jì)分最后我們將給游戲添加一個(gè)Play按鈕,用于根據(jù)需要啟動(dòng)游戲以及在游戲結(jié)束后重啟游戲。我們還將實(shí)現(xiàn)一個(gè)計(jì)分系統(tǒng),能夠在玩家等級(jí)提高時(shí)加快節(jié)奏。 添加Play按鈕 這里可以先將游戲初始化為非活動(dòng)狀態(tài),當(dāng)我們點(diǎn)擊了按鈕,就開始游戲。由于Pygame中沒有內(nèi)置的創(chuàng)建按鈕的方法。因此我們可以通過創(chuàng)建一個(gè)Button類來創(chuàng)建一個(gè)自帶標(biāo)簽的實(shí)心矩形。我們通過檢測(cè)鼠標(biāo)發(fā)生點(diǎn)擊后的坐標(biāo)是否與我們繪制的按鈕發(fā)生碰撞與否來判斷是否發(fā)生了點(diǎn)擊事件。 提高等級(jí) 為了使玩家將敵人消滅干凈后能夠提高游戲難度,增加趣味性,這里我們可以在Settings類中進(jìn)行修改,增加靜態(tài)初始值,和動(dòng)態(tài)初始值。 記分、等級(jí)、剩余飛船 打包成exe可執(zhí)行文件上面游戲開發(fā)完了,那么你需要將其轉(zhuǎn)成文exe的可執(zhí)行文件。我們采用pyinstaller,安裝步驟參考:傳送門 項(xiàng)目代碼github地址:傳送門 |
|