使用Python開發簡單的小遊戲

php中世界最好的语言
發布: 2018-04-09 11:33:58
原創
7801 人瀏覽過

這次帶給大家使用Python開發簡單的小遊戲,使用Python開發小遊戲的注意事項有哪些,以下就是實戰案例,一起來看一下。

引言

最近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 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

建立設定類別

為了在寫遊戲的過程中能便捷地創建一些新功能,下面額外編寫一個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 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預設載入位圖。雖然其他類型的圖像也能加載,但是需要安裝額外的庫。我們推薦去免費的圖片素材網站去找圖片: 傳送門 。我們在主專案資料夾(alien_invasion)中新建一個資料夾叫images,將如下bmp圖片放入其中。

接下來,我們建立飛船類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檔案中呼叫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()
登入後複製

重構:模組game_functions

在大型專案中,經常需要在新增程式碼前重構既有程式碼。重構的目的是為了簡化程式碼的結構,使其更容易擴展。我們將實作一個game_functions模組,它將儲存大量讓遊戲Alien invasion運作的函數。透過建立模組game_functions,可避免alien_invasion.py太長,使其邏輯更容易理解。

函數check_events()

#首先我們將管理事件的程式碼移到一個名為check_events()的函數中,目的是為了隔離事件循環

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程式碼,導入game_functions模組,並將事件循環替換成對函數check_events()的呼叫:

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()
登入後複製

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

python怎麼逐行讀寫txt檔案

Python呼叫mysql更新資料的方法

以上是使用Python開發簡單的小遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!