国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Python寫一個小游戲

2020-01-04 15:30:41
字體:
來源:轉載
供稿:網友

引言

最近python語言大火,除了在科學計算領域python有用武之地之外,在游戲、后臺等方面,python也大放異彩,本篇博文將按照正規的項目開發流程,手把手教大家寫個python小游戲,來感受下其中的有趣之處。本次開發的游戲叫做alien invasion。

安裝pygame并創建能左右移動的飛船

安裝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

python,小游戲

創建設置類

為了在寫游戲的過程中能便捷地創建一些新功能,下面額外編寫一個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圖片放入其中。

python,小游戲

接下來,我們創建飛船類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()

首先我們將管理事件的代碼移到一個名為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武林網網站的支持!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴海县| 姚安县| 乌恰县| 中山市| 甘孜县| 临邑县| 泗水县| 嘉兴市| 宾川县| 安丘市| 松阳县| 香河县| 武清区| 儋州市| 商洛市| 长兴县| 晋江市| 天全县| 比如县| 泽库县| 祁连县| 旬阳县| 石屏县| 博湖县| 颍上县| 朝阳县| 宽城| 昭苏县| 南雄市| 九台市| 延长县| 霍山县| 仁化县| 稻城县| 敦煌市| 社会| 霍州市| 花莲市| 华阴市| 昌宁县| 公安县|