引言
最近python語言大火,除了在科學(xué)計(jì)算領(lǐng)域python有用武之地之外,在游戲、后臺(tái)等方面,python也大放異彩,本篇博文將按照正規(guī)的項(xiàng)目開發(fā)流程,手把手教大家寫個(gè)python小游戲,來感受下其中的有趣之處。本次開發(fā)的游戲叫做alien invasion。
安裝pygame并創(chuàng)建能左右移動(dòng)的飛船
安裝pygame
本人電腦是windows 10、python3.6,pygame下載地址: 傳送門
請(qǐng)自行下載對(duì)應(yīng)python版本的pygame 運(yùn)行以下命令
1
2
|
$ 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文件,輸入如下代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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,并向其中添加如下代碼:
1
2
3
4
5
6
7
8
|
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è)置,修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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)文件最為簡單,這是因?yàn)镻ygame默認(rèn)加載位圖。雖然其他類型的圖像也能加載,但是需要安裝額外的庫。我們推薦去免費(fèi)的圖片素材網(wǎng)站上去找圖像: 傳送門 。我們?cè)谥黜?xiàng)目文件夾(alien_invasion)中新建一個(gè)文件夾叫images,將如下bmp圖片放入其中。
接下來,我們創(chuàng)建飛船類ship.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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é)構(gòu),使其更加容易擴(kuò)展。我們將實(shí)現(xiàn)一個(gè)game_functions模塊,它將存儲(chǔ)大量讓游戲Alien invasion運(yùn)行的函數(shù)。通過創(chuàng)建模塊game_functions,可避免alien_invasion.py太長,使其邏輯更容易理解。
函數(shù)check_events()
首先我們將管理事件的代碼移到一個(gè)名為check_events()的函數(shù)中,目的是為了隔離事件循環(huán)
1
2
3
4
5
6
7
|
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)用:
1
2
3
4
5
6
7
8
9
|
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寫一個(gè)小游戲,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://cloud.tencent.com/developer/article/1023469