日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

pygame快乐游戏编程模块

發(fā)布時間:2024/4/24 综合教程 40 生活家
生活随笔 收集整理的這篇文章主要介紹了 pygame快乐游戏编程模块 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python菜鳥快樂游戲編程_pygame(博主錄制,2K分辨率,超高清)

https://study.163.com/course/courseMain.htm?courseId=1006188025&share=2&shareId=400000000398149

(原創(chuàng)聲明,轉(zhuǎn)載引用需要指明來源)

游戲編程模塊pygame介紹

pygame是一組旨在編寫視頻游戲的Python模塊。Pygame可以使用python語言創(chuàng)建功能齊全的游戲和多媒體程序。Pygame具有高度的可移植性,幾乎可以在所有平臺和操作系統(tǒng)上運行。Pygame至今已被下載了數(shù)百萬次。Pygame非常流行的一個原因是免費。 根據(jù)LGPL許可證發(fā)布的內(nèi)容,您可以使用它創(chuàng)建開源,免費軟件,共享軟件和商業(yè)游戲。Pygame歷史開始于2000年10月。六個月后發(fā)布了pygame 1.0版。pygame的目標是使可視化游戲編程變得簡單。 pygame是Python和SDL混合的產(chǎn)物。 SDL由Sam Lantinga創(chuàng)建,與DirectX相比,SDL是用于控制多媒體的跨平臺C庫。它已用于數(shù)百種商業(yè)和開源游戲。

迫不及待的想用pygame寫一個自己的游戲了嗎?用pip install pygame安裝此模塊吧。

pygame官網(wǎng)

學員查詢pygame模塊基礎(chǔ)語法最好方法是訪問官方文檔,網(wǎng)址為

https://www.pygame.org/docs/ref/surface.html

pygame模塊最常用的對象包括:頂層pygame包,顏色, 顯示,繪畫,事件,字體,圖片,鍵盤,鼠標,常量, 多媒體,矩形, 表面,時間,音樂

高級對象包括:游標,游戲桿, 圖像蒙版,精靈,轉(zhuǎn)換,計算機字體, 繪制形狀,重疊式展示,像素陣列,像素復制,數(shù)學

其他對象包括:相機,音頻CDROM控制,例子,事件和隊列交互, 快速事件,剪貼板支持,測試,觸摸,版本。

pygame 包是可供使用的最頂層的包。Pygame 被分成許多子模塊,但是并不會影響程序使用 Pygame。

pygame常見函數(shù)如下:

pygame.init() — 初始化所有導入的 pygame 模塊

pygame.quit() — 卸載所有導入的 pygame 模塊

pygame.error() — 標準 pygame 異常模塊

pygame.get_error() — 獲得當前錯誤信息

pygame.set_error() — 設(shè)置當前錯誤信息

pygame.get_sdl_version() — 獲得 SDL 的版本號

pygame.get_sdl_byteorder() — 獲得 SDL 的字節(jié)順序

pygame.register_quit() — 注冊一個函數(shù),這個函數(shù)將在 pygame 退出時被調(diào)用

pygame.encode_string() — 對 unicode 或字節(jié)對象編碼

pygame.encode_file_path() — 將 unicode 或字節(jié)對象編碼為文件系統(tǒng)路徑

pygame常用對象為

pygame.Surface表面

pygame.draw繪圖

pygame.font字體

pygame.image圖片

pygame.sprite精靈

pygame.transform轉(zhuǎn)換

pygame.event事件

pygame.time時間

pygame.mixer.Sound聲音

Pygame語法比較多,且pygame不支持互動shell,不能一行行執(zhí)行命令,因此最好學習方式是結(jié)合游戲?qū)崙?zhàn)編程。我們先用10行代碼就完成第一個pygame游戲窗口,順便了解pygame最主要的語法。

首先輸入import pygame,sys導入具有所有可用pygame模塊的包和系統(tǒng)模塊。

pygame.display.set_mode()游戲窗口設(shè)置

初始化游戲窗口
set_mode(size=(0, 0), flags=0, depth=0, display=0) -> Surface

size參數(shù)是一對數(shù)字,代表寬度和高度。 flags參數(shù)是其他選項的集合。

depth參數(shù)代表用于顏色的位數(shù)。顏色位數(shù)范圍range is {8...32},通常最好不要傳遞depth參數(shù)。對于系統(tǒng),它將默認為最佳和最快的顏色深度。如果您的游戲需要特定的顏色格式,則可以使用此參數(shù)控制深度。 Pygame將模擬不可用的顏色深度,該深度可能很慢。

如果我們想要讓游戲窗口在顯示器上全屏展示,我們用pygame.FULLSCREEN對象。
windowSurface=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),pygame.FULLSCREEN)

顏色color

pygame.locals

此模塊包含pygame使用的各種常量。 它的內(nèi)容會自動放置在pygame模塊的命名空間中。 但是,應用程序可以使用pygame.locals僅包含pygame.locals import *的pygame常量。

Event對象有一個名為type的成員變量(member variable,也叫作屬性,attributes或properties),它告訴我們對象表示何種事件。針對pygame.locals模塊中的每一種可能的類型,Pygame都有一個常量變量。第9行檢查Event對象的type是否等于常量QUIT。記住,由于我們使用了from pygame.locals import *形式的import語句,主要輸入QUIT就可以了,而不必輸入pygame.locals.QUIT。

如果Event對象是一個停止事件,就會調(diào)用pygame.quit()和sys.exit()函數(shù)。pygame. quit()是pygame.init()函數(shù)的一種相反的函數(shù),它運行的代碼會使得Pygame庫停止工作。在調(diào)用sys.exit()終止程序之前,總是應該先調(diào)用pygame.quit()。通常,由于程序退出之前,Python總是會關(guān)閉pygame,這不會真的有什么問題。但是,在IDLE中有一個bug,如果一個Pygame程序在調(diào)用pygame.quit()之前就終止了,將會導致IDLE掛起。

鍵盤按鍵

event.key==ord('a')
字母a-z: ord('a')----ord('z'), ord()內(nèi)字母必須小寫,否則出錯。傳遞的是鍵盤對應小寫字母的ASCII值

event.key==K_LEFT
箭頭鍵arrow keys: K_LEFT, K_RIGHT, K_UP, K_DOWN.鍵盤右邊的四個箭頭按鍵
退出鍵ESC: K_ESCAPE

因為導入了from pygame.locals import*,所以我們可以用K_LEFT代替pygame.locals.K_LEFT

Table 20-1: Constant Variables for Keyboard Keys
Pygame Constant Variable Keyboard Key Pygame Constant Variable Keyboard Key
K_LEFT Left arrow K_HOME Home
K_RIGHT Right arrow K_END End
K_UP Up arrow K_PAGEUP PgUp
K_DOWN Down arrow K_PAGEDOWN PgDn
K_ESCAPE Esc K_F1 F1
K_BACKSPACE Backspace K_F2 F2
K_TAB Tab K_F3 F3
K_RETURN Return or Enter K_F4 F4
K_SPACE Space bar K_F5 F5
K_DELETE Del K_F6 F6
K_LSHIFT Left Shift K_F7 F7
K_RSHIFT Right Shift K_F8 F8
K_LCTRL Left Ctrl K_F9 F9
K_RCTRL Right Ctrl K_F10 F10
K_LALT Left Alt K_F11 F11
K_RALT Right Alt K_F12 F12

pygame.event
用于處理事件與事件隊列的 Pygame 模塊。

函數(shù)
pygame.event.pump() — 讓 Pygame 內(nèi)部自動處理事件
pygame.event.get() — 從隊列中獲取事件
pygame.event.poll() — 從隊列中獲取一個事件
pygame.event.wait() — 等待并從隊列中獲取一個事件
pygame.event.peek() — 檢測某類型事件是否在隊列中
pygame.event.clear() — 從隊列中刪除所有的事件
pygame.event.event_name() — 通過 id 獲得該事件的字符串名字
pygame.event.set_blocked() — 控制哪些事件禁止進入隊列
pygame.event.set_allowed() — 控制哪些事件允許進入隊列
pygame.event.get_blocked() — 檢測某一類型的事件是否被禁止進入隊列
pygame.event.set_grab() — 控制輸入設(shè)備與其他應用程序的共享
pygame.event.get_grab() — 檢測程序是否共享輸入設(shè)備
pygame.event.post() — 放置一個新的事件到隊列中
pygame.event.Event() — 創(chuàng)建一個新的事件對象
pygame.event.EventType — 代表 SDL 事件的 Pygame 對象
Pygame 通過事件隊列控制所有的時間消息。該模塊中的程序?qū)湍愎芾硎录犃?。輸入隊列很大程度依賴?pygame 的 display 模塊。如果 display 沒有被初始化,顯示模式?jīng)]有被設(shè)置,那么事件隊列就還沒有開始真正工作。
常規(guī)的隊列是由 pygame.event.EventType 定義的事件對象的組成,有多種方法來訪問里邊的事件對象:從簡單的檢測事件是否存在,到直接從棧中獲取它們。
為了保持 Pygame 和系統(tǒng)同步,你需要調(diào)用 pygame.event.pump() 確保實時更新,你將在游戲的每次循環(huán)中調(diào)用這個函數(shù)。

在做測試時,你可以輸出事件對象以及相應的類型和成員。來自系統(tǒng)的事件都有一個事件類型和對應的成員屬性,下邊是每個事件類型以及對應的成員屬性列表:

pygame.event.get()
從隊列中獲取事件。
get() -> Eventlist
get(type) -> Eventlist
get(typelist) -> Eventlist
這將獲取并從隊列中刪除事件。如果指定一個或多個 type 參數(shù),那么只獲取并刪除指定類型的事件。
請注意,如果你只從隊列中獲取和刪除指定的事件,那么久而久之,隊列可能被你不關(guān)注的事件所填滿。

pygame.font字體

1.從系統(tǒng)字體庫創(chuàng)建一個 Font 對象
font = pygame.font.SysFont(None, 48) #不熟悉字體名的就用None
pygame.font.SysFont(“字體”,字體大小)
舉例
font = pygame.font.SysFont('Arial', 48)

2.使用對象里的 render方法渲染文字
render(text, antialias, color, background=None) -> Surface
舉例
textobj = font.render(“”hello“”, 1, TEXTCOLOR)

3.獲取字體矩形坐標
textrect = textobj.get_rect()

4.字體矩形坐標左上點賦值
textrect.topleft = (x, y)

5.字體更新到游戲窗口上
surface.blit(textobj, textrect)
drawText('Zombie VS Plants', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 4))

image圖像

>>> pygame.image.__doc__

'pygame module for image transfer'

>>> dir(pygame.image)

['__doc__', '__file__', '__name__', '__package__', 'frombuffer', 'fromstring', 'get_extended', 'load', 'load_basic', 'load_extended', 'save', 'save_extended', 'tostring']

2
圖形尺寸轉(zhuǎn)換的函數(shù):

>>> pygame.transform.scale.__doc__
'scale(Surface, (width, height), DestSurface = None) -> Surface
resize to new resolution'

zombieImage=pygame.image.load('zombie.png')
zombieImageStretchedImage=pygame.transform.scale(zombieImage,(40,40))

pygame.transform.scale()函數(shù)可以放大或縮小精靈。
第一個參數(shù)是pygame.Surface對象上畫的圖像。第二個參數(shù)是圖形新尺寸。
pygame.transform.scale()函數(shù)返回一個pygame.Surface對象,此對象上的圖像被賦予新尺寸。我們把原尺寸圖像保存在變量playerImage。但新的圖像保存在變量playerStretchedImage。
playerImageandfoodImage中保存的surface對象和用于window的surface對象一樣。游戲中,我們把這些圖像,字體的surfaces復制到window的surface,這樣用戶就可以在window窗口一目了然。Font對象render()方法產(chǎn)生的surface對象也一樣,為了顯示文字,我們不得不復制surface對象到window surface對象。最后通過update()方法把window surface對象顯示到屏幕上。

當矩形對象代表player的位置和尺寸時,player的圖像也被保存在 playerStretchedImage變量中。我們用 pygame.transform.scale()函數(shù)改變了圖像尺寸。確保傳遞原surface對象到變量 playerImage ,而不是到變量playerStretchedImage。圖像尺寸變化后,圖像會有點受損。如果反復變化尺寸,受損會越來越明顯。但我們把原圖像賦予新尺寸,受損只有小小一次。這就是為什么我們把playerImage變量作為第一個參數(shù)傳遞給pygame.transform.scale()

3
圖形載入的函數(shù)
>>> pygame.image.load.__doc__
'pygame module for image transfer'
pygame.image.load()函數(shù)被傳遞一個圖形文件的字符串參數(shù),然后加載。 value of pygame.image.load()返回一個surface對象,這個對象中,圖像文件中圖像畫在它的surface上。我們把這surface對象保存在playerimage內(nèi)。要確保圖像位置和python目錄一致。
sprite精靈代表一個二維圖像,并作為屏幕上圖形一部分。


這是精靈作為全圖的例子

精靈圖像被畫在背景頂部。注意我們可以水平翻轉(zhuǎn)精靈圖像,這樣精靈貌似朝著另一個方向。我們可以在同一個窗口畫多個相同精靈。我們也可以重新定義精靈的尺寸,變大或變小。背景圖像可以被看做一個大精靈。

精靈圖像被畫在背景頂部。注意我們可以水平翻轉(zhuǎn)精靈圖像,這樣精靈貌似朝著另一個方向。我們可以在同一個窗口畫多個相同精靈。我們也可以重新定義精靈的尺寸,變大或變小。背景圖像可以被看做一個大精靈。
精靈被保存在電腦圖像文件中pygame用的圖像格式包括: BMP, PNG, JPG (and JPEG), and GIF.你也可以從web瀏覽器下載圖像,你也可以用繪圖程序制作圖像。繪圖工具有photoshop,MS Paint or Tux Paint.

pygame.rect()

rect = pygame.Rect(300, 100, 40, 40)

Pygame 通過 Rect 對象存儲和操作矩形區(qū)域。一個 Rect 對象可以由 left,top,width,height 幾個值創(chuàng)建。

rect對象是用來存儲矩形對象的,rect對象有一些虛擬屬性,

比如top.left,bottom.right這些是用來固定矩形的位置的,

還有size,width,height,這些是描述矩形大小,寬高分別是多大,

center為矩形的中心點,其實就是關(guān)于橫縱坐標的二元組,因此又有centerx,centery兩個屬性。此外,還有x,y。

2.來自Pygame中文文檔的解釋:

class pygame.Rect

Rect是用于存儲矩形坐標的pygame對象。

“構(gòu)造”方法:

1).rect = pygame.Rect( left , top, width, height )

2). rect = pygame.Rect(( left , top),( width, height) )

3).rect = pygame.Rect(object)

Rect.move_ip
moves the rectangle, in place
Rect.move_ip(x, y): return None

Same as theRect.move- moves the rectanglemethod, but operates in place.

區(qū)別在于move_ip改變直接調(diào)整對象,move返回一個改變后的對象。

surface.blit VS pygame.display.update()

surface.blit是在游戲窗口surface上繪制對象

pygame.display.update()是把游戲窗口surface加載到電腦顯示屏幕上

Surface.blit()

windowSurface.blit(bouncerStretchedImage,rect)

做出blit這個動作的人是一個Surface類的實例,

這個人即將在自己身上畫圖,

他需要兩個參數(shù):要畫的圖片,和畫的位置,即source和rect.

source的類型是Surface, pygame.image.load(圖片路徑)返回的就是Surface

rect需要指定兩個值,left和top,Surface類有g(shù)et_rect()方法,返回Rect

Rect 是這個樣子:

Rect(left, top, width, height) -> Rect
(left,top)坐標確定圖片所在位置,width和height確定圖片寬和高

沖突檢測
圖形化游戲最常見行為就是沖突檢測。沖突檢測指:屏幕上兩個對象是否重疊。如果玩家碰到敵人,則可能損失生命值。在矩形彈動游戲中,沖突檢測指兩個矩形是否重疊。

>>> pygame.Rect.colliderect.__doc__
'colliderect(Rect) -> bool
test if two rectangles overlap'

#測試玩家是否觸碰到僵尸
def playerHasHitZombie(playerRect, zombies):
    for z in zombies:
        #test if two rectangles overlap測試是否觸碰到僵尸
        if playerRect.colliderect(z['rect']):
            return True
    return False

#測試子彈是否觸碰到僵尸。如果是,就把該僵尸移除
def bulletHasHitZombie(bullets, zombies):
    for b in bullets:
        if b['rect'].colliderect(z['rect']):
            bullets.remove(b)
            return True
    return False

#測試子彈是否觸碰到新僵尸。如果是,就把該僵尸移除
def bulletHasHitCrawler(bullets, newKindZombies):
    for b in bullets:
        if b['rect'].colliderect(c['rect']):
            bullets.remove(b)
            return True
    return False

創(chuàng)建一個簡單的游戲窗口

首先輸入import pygame,sys導入具有所有可用pygame模塊的包和系統(tǒng)模塊。

現(xiàn)在游戲窗口還沒有任何內(nèi)容,之后我們可以逐步添加,設(shè)計自己的游戲。我已經(jīng)用pygame完成了貪吃蛇,植物大戰(zhàn)僵尸,俄羅斯方塊,開心消消樂等知名游戲。

2.實例:python代碼寫個植物大戰(zhàn)僵尸游戲

植物大戰(zhàn)僵尸

《植物大戰(zhàn)僵尸》是由PopCap Games開發(fā)的一款益智策略類單機游戲,發(fā)售于2009年5月5日。玩家通過利用多種植,并切換不同的功能,快速有效地把僵尸阻擋在入侵的道路上。

游戲里有26種僵尸,包括鐵桶,報紙,鐵門,橄欖球頭盔,雪橇車,南瓜頭,礦工帽,鐵梯僵尸。這些僵尸會入侵我們后花園,我們要用各種植物消滅入侵僵尸。

49種植物每種都有不同的功能,例如櫻桃炸彈可以和周圍一定范圍內(nèi)的所有僵尸同歸于盡,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以針對不同僵尸的弱點來合理地種植植物,這也是勝利的訣竅。

下圖是我用pygame編寫的簡易版植物大戰(zhàn)僵尸游戲一個藍色植物正在吐出圓球攻擊僵尸,僵尸數(shù)量和移動速度可以自己控制。如果集中一個僵尸,score分數(shù)會增加一分,zombies gotten past記錄有多少僵尸已經(jīng)越過植物。當然我還可以設(shè)計一些作弊的按鍵,非常有趣!

不同的敵人,不同的玩法構(gòu)成五種不同的游戲模式,加之黑夜、濃霧以及泳池之類的障礙增加了游戲挑戰(zhàn)性。

這是房頂上植物們拼命抵抗僵尸入侵場景。

這是在后花園的游泳池里植物們拼命抵抗僵尸入侵場景。

僵尸挺聰明的,如果白天入侵不成功,就晚上搞偷襲。這是在深夜植物們拼命抵抗僵尸入侵場景。

這款python代碼當然不是復現(xiàn)原款游戲所有功能,而是簡單模擬一下其中樂趣。首先我們準備好以下素材。包括三張僵尸圖片:

BucketheadZombie.gif,ConeheadZombie.gif,zombie.png。

一張植物圖片plant.gif,一張背景圖片background.png,一張子彈圖片bullet.png

一首背景音樂background.mp3,一首游戲結(jié)束音樂gameover.mp3。由于游戲是之前基于python2.7版本寫的,因此建議素材名稱使用英文,python2版本對中文支持不太友好。

由于這款游戲代碼量太大,這里就不一一展開說明,我建議你們直接去下載源代碼和圖片,然后根據(jù)自己愛好,更改一下背景音樂,圖片,和僵尸數(shù)量,移動速度等參數(shù)設(shè)置。我對游戲一些重要語法做一些說明。

pygame.display.set_caption(words)設(shè)置窗口標題

pygame.display.set_caption(words)方法是設(shè)置窗口標題,words參數(shù)是窗口標題。我們沿用之前腳本運行下面腳本,我們就生成了一個有Zombie VS Plants標題的窗口。

pygame.event事件

常見事件有QUIT,KEYDOWN,KEYUP

while True:                  #main game loop游戲主循環(huán)
    for event in pygame.event.get(): #遍歷pygame事件列表
        if event.type==QUIT:        #如果點擊關(guān)閉按鈕(window右上)
            pygame.quit()           #關(guān)閉pygame庫
            sys.exit()              #系統(tǒng)退出

建立一個簡單游戲窗口

import pygame,sys            #導入pygame和sys模塊
from pygame.locals import*   #導入pygame 局部變量
pygame.init()                #pygame所有模塊初始化
screen=pygame.display.set_mode((400,300))#設(shè)置屏幕長和寬值
pygame.display.set_caption('Zombie VS Plants')# 設(shè)置窗口標題
while True:                  #main game loop游戲主循環(huán)
    for event in pygame.event.get(): #遍歷pygame事件列表
        if event.type==QUIT:        #如果點擊關(guān)閉按鈕(window右上)
            pygame.quit()           #關(guān)閉pygame庫
            sys.exit()              #系統(tǒng)退出

pygame.time.Clock

pygame.time.Clock創(chuàng)建一個新的Clock對象,該對象可用于跟蹤時間量。 時鐘還提供了多種功能來幫助控制游戲的幀頻。

pygame中的時間以毫秒(1/1000秒)表示。 大多數(shù)平臺的時間分辨率有限,大約為10毫秒。 該分辨率(以毫秒為單位)在TIMER_RESOLUTION常量中給出。

pygame.time.Clock.tick()

tick(framerate=0) -> milliseconds

每幀應調(diào)用一次此方法。 它將計算自上次調(diào)用以來經(jīng)過了多少毫秒。
如果您傳遞可選的幀速率參數(shù),該功能將延遲以使游戲的運行速度低于給定的每秒滴答聲。 這可以用來幫助限制游戲的運行速度。 通過每幀調(diào)用Clock.tick(40)一次,該程序?qū)⒂肋h不會以每秒40幀以上的速度運行。一般情況framerate設(shè)置為40.

游戲運行時每秒所運行的幀數(shù)(簡稱FPS,Frames Per Second) 和視頻一樣,F(xiàn)PS越大,在屏幕上的視頻就越來越平滑,直到一個臨界點(大約是100FPS),超過這個臨界點,再高的FPS都只是一個令人驚奇的數(shù)值,400FPS和100FPS在人的視覺中幾乎沒有差別。
FPS取決于顯卡,其次是內(nèi)存,CPU,然后是網(wǎng)絡(luò)(如果是網(wǎng)絡(luò)游戲的話)與硬盤。
一般游戲都是40左右fps就可以稱之為流暢了。比如策略類(三國志什么的)5fps也是可以接受的。但賽車類 5fps根本玩不下去。
每款游戲都會有一個官方提供的最低配置要求,尤其是網(wǎng)絡(luò)游戲,但這個最低配置僅僅適用于將游戲內(nèi)的所有視頻效果全部關(guān)閉的狀態(tài)下使用,而且網(wǎng)絡(luò)游戲中對于網(wǎng)速的問題是忽略不計的。
只有提高電腦的顯卡、內(nèi)存才能在網(wǎng)絡(luò)因素不確定的情況下達到最理想的游戲流暢效果。

音樂music

我們曾經(jīng)玩任何游戲都會伴隨動聽的背景音樂。pygame.mixer是一個用來處理聲音的模塊,其含義為“混音器”。因此我們準備用以下方法pygame.mixer.music.load('background.mp3')加載mp3格式背景音樂并準備播放。這樣游戲就會深動形象。

pygame.mixer.music.play(loop, start)方法用于播方音樂,loop表示循環(huán)次數(shù),如果loop=1表示音樂播方一次。如果loop=2, 表示音樂播方兩次。如果loop=-1,表示音樂不停循環(huán)。start 參數(shù)控制音樂從哪里開始播放。開始的位置取決于音樂的格式。MP3 和 OGG 使用時間表示播放位置(以秒為單位)。MOD使用模式順序編號表示播放位置。如果音樂文件無法設(shè)置開始位置,則傳遞了start參數(shù)后會產(chǎn)生一個NotImplementedError 錯誤。

pygame.mixer.music.stop()方法用于結(jié)束音樂播放。如果游戲結(jié)束,我們可以加上這句方法,停止播放音樂。

pygame.mixer.music.load('background.mp3') ##載入一個音樂文件用于播放
#,如果 loops = -1,則表示無限重復播放。start 參數(shù)控制音樂從哪里開始播放。
pygame.mixer.music.play(-1, 0.0) #開始播放音樂流
pygame.mixer.music.stop()#停止音樂播放

我們把加載和播方聲音的代碼加入主程序,這樣游戲窗口就有音樂了。

import pygame,sys            #導入pygame和sys模塊
from pygame.locals import*   #導入pygame 局部變量
pygame.init()                #pygame所有模塊初始化
screen=pygame.display.set_mode((400,300))#設(shè)置屏幕長和寬值
pygame.display.set_caption('Zombie VS Plants')

pygame.mixer.music.load('background.mp3') ##載入一個音樂文件用于播放
#,如果 loops = -1,則表示無限重復播放。start 參數(shù)控制音樂從哪里開始播放。
pygame.mixer.music.play(-1, 0.0) #開始播放音樂流
while True:                  #main game loop游戲主循環(huán)
    
    for event in pygame.event.get(): #遍歷pygame事件列表
        if event.type==QUIT:        #如果點擊關(guān)閉按鈕(window右上)
            pygame.quit()           #關(guān)閉pygame庫
            sys.exit()              #系統(tǒng)退出
pygame.mixer.music.stop()#停止音樂播放

游戲結(jié)束時也會有相應音樂,我們文件里游戲結(jié)束音樂時wav格式的,和之前mp3格式不一樣,我們用gameOverSound = pygame.mixer.Sound('gameover.wav')方法把游戲結(jié)束音樂放入gameOverSound變量。gameOverSound.play()方法播放游戲結(jié)束音樂。gameOverSound.stop()方法用于停止游戲結(jié)束聲音播放。

# set up sounds設(shè)置聲音
gameOverSound = pygame.mixer.Sound('gameover.wav')
gameOverSound.play()  #播放游戲結(jié)束時聲音
gameOverSound.stop() #游戲結(jié)束聲音停止

參數(shù)前置

我們用pygame.display.set_mode((400,300))方法設(shè)置屏幕大小時候400,300為屏幕的長和寬。在大型程序設(shè)計時,一般不把具體數(shù)字放入方法里,而是用變量代替。我們用WINDOWWIDTH表示游戲窗口寬度,用WINDOWHEIGHT 表示游戲窗口高度,WINDOWWIDTH = 1024,WINDOWHEIGHT = 600。我們用以下代碼實現(xiàn)游戲窗口大小的參數(shù)前置。

import pygame,sys            #導入pygame和sys模塊
from pygame.locals import*   #導入pygame 局部變量
#設(shè)置變量參數(shù),參數(shù)前置
WINDOWWIDTH = 1024           #游戲窗口寬度
WINDOWHEIGHT = 600           #游戲窗口高度
pygame.init()                #pygame所有模塊初始化
screen=pygame.display.set_mode((400,300))#設(shè)置屏幕長和寬值
pygame.display.set_caption('Zombie VS Plants')
#設(shè)置窗口大小
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.mixer.music.load('background.mp3') ##載入一個音樂文件用于播放
#,如果 loops = -1,則表示無限重復播放。start 參數(shù)控制音樂從哪里開始播放。
pygame.mixer.music.play(-1, 0.0) #開始播放音樂流
while True:                  #main game loop游戲主循環(huán)
    for event in pygame.event.get(): #遍歷pygame事件列表
        if event.type==QUIT:        #如果點擊關(guān)閉按鈕(window右上)
            pygame.mixer.music.stop()#停止播放音樂
            pygame.quit()           #關(guān)閉pygame庫
            sys.exit()              #系統(tǒng)退出

如果我們想要讓游戲窗口在顯示器上全屏展示,我們用pygame.FULLSCREEN對象。

import pygame,sys            #導入pygame和sys模塊
from pygame.locals import*   #導入pygame 局部變量
#設(shè)置變量參數(shù),參數(shù)前置
WINDOWWIDTH = 1024           #游戲窗口寬度
WINDOWHEIGHT = 600           #游戲窗口高度
pygame.init()                #pygame所有模塊初始化
screen=pygame.display.set_mode((400,300))#設(shè)置屏幕長和寬值
pygame.display.set_caption('Zombie VS Plants')
#設(shè)置窗口大小
windowSurface=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),pygame.FULLSCREEN)
pygame.mixer.music.load('background.mp3') ##載入一個音樂文件用于播放
#,如果 loops = -1,則表示無限重復播放。start 參數(shù)控制音樂從哪里開始播放。
pygame.mixer.music.play(-1, 0.0) #開始播放音樂流
while True:                  #main game loop游戲主循環(huán)
    for event in pygame.event.get(): #遍歷pygame事件列表
        if event.type==QUIT:        #如果點擊關(guān)閉按鈕(window右上)
            pygame.mixer.music.stop() #停止播放音樂
            pygame.quit()           #關(guān)閉pygame庫
            sys.exit()              #系統(tǒng)退出

鼠標

#鼠標設(shè)置不可見

pygame.mouse.set_visible(False)

鍵盤設(shè)置

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            terminate()
        if event.type==pygame.locals.KEYDOWN:
            #change the keyboard variables鍵盤鍵設(shè)置上下左右
            if event.key==pygame.locals.K_LEFT or event.key==ord('a'):
                moveRight=False
                moveLeft=True
            if event.key==pygame.locals.K_RIGHT or event.key==ord('d'):
                moveLeft=False
                moveRight=True
            if event.key==pygame.locals.K_UP or event.key==ord('w'):
                moveDown=False
                moveUp=True
            if event.key==pygame.locals.K_DOWN or event.key==ord('s'):
                moveDown=True
                moveUp=False
        #有KEYDOWN就一定有KEYUP,否則游戲?qū)ο髸掷m(xù)移動
        if event.type==pygame.locals.KEYUP:
            if event.key==pygame.locals.K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key==pygame.locals.K_LEFT or event.key==ord('a'):
                moveLeft=False
            if event.key==K_RIGHT or event.key==ord('d'):
                moveRight=False
            if event.key==K_UP or event.key==ord('w'):
                moveUp=False
            if event.key==K_DOWN or event.key==ord('s'):
                moveDown=False
          
    # move the bouncer data structure
    if moveDown and p_destination.bottom<WINDOWHEIGHT:
        p_destination.top+=PLAYERMOVERATE 
    
    if moveUp and p_destination.top>0:
        p_destination.top-=PLAYERMOVERATE 
        
    if moveLeft and p_destination.left>0:
        p_destination.left-=PLAYERMOVERATE 

    if moveRight and p_destination.right<WINDOWWIDTH:
        p_destination.right+=PLAYERMOVERATE 
        
    # Draw the game world on the window.
    windowSurface.blit(rescaledBackground, (0, 0))
    windowSurface.blit(zombieStretchedImage,z_destination)
    windowSurface.blit(playerImage,p_destination)

  

發(fā)射子彈

'''
playerRect = playerImage.get_rect()
playerRect.topleft = (50, WINDOWHEIGHT /2)

#子彈圖像加載
bulletImage = pygame.image.load('SnowPeashooterBullet.gif')
bulletRect = bulletImage.get_rect()

#連續(xù)發(fā)射兩個子彈的距離,值越小,子彈距離越近,反之亦然
BULLETSPEED = 30
#新增子彈速度,值越小,子彈發(fā)射越快
ADDNEWBULLETRATE = 5
b['rect'].move_ip(1 * BULLETSPEED, 0)
#增加子彈計數(shù)器
bulletAddCounter = 0
'''

import pygame, random, sys, time
from pygame.locals import *

#set up some variables
WINDOWWIDTH = 1024
WINDOWHEIGHT = 600
#值設(shè)置低一些,僵尸移動速度變慢
FPS = 20
PLAYERMOVERATE = 15
#連續(xù)發(fā)射兩個子彈的距離,值越小,子彈距離越近,反之亦然
BULLETSPEED = 30
#新增子彈速度,值越小,子彈發(fā)射越快
ADDNEWBULLETRATE = 5
shoot = False
#增加子彈計數(shù)器
bulletAddCounter = 0
RED = (255, 0, 0)

#結(jié)束游戲
def terminate():
    pygame.quit()
    sys.exit()

# set up pygame, the window, and the mouse cursor
pygame.init()
#reate an object to help track time
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN)
#窗口標題
pygame.display.set_caption('Zombie Defence')
#鼠標設(shè)置不可見
#pygame.mouse.set_visible(False)

# set up fonts設(shè)置字體
font = pygame.font.SysFont(None, 48)

#加載聲音
pygame.mixer.music.load('grasswalk.mp3')

# set up images
playerImage = pygame.image.load('SnowPea.gif')
#get the rectangular area of the Surface
#Returns a new rectangle covering the entire surface. This rectangle will 
#always start at (0, 0) with a width and height the same size as the image.
playerRect = playerImage.get_rect()
#子彈圖像加載
bulletImage = pygame.image.load('SnowPeashooterBullet.gif')
bulletRect = bulletImage.get_rect()

zombieImage=pygame.image.load("zombie.png")
zombieStretchedImage=pygame.transform.scale(zombieImage,(80,80))
z_destination=pygame.Rect(944, 300, 80, 80)
#游戲背景圖片加載
backgroundImage = pygame.image.load('background.png')
#對背景圖片尺寸重新調(diào)整大小,寬為WINDOWWIDTH,長為WINDOWHEIGHT
rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT))

zombies = []
bullets = []
playerRect.topleft = (50, WINDOWHEIGHT /2)
moveLeft = moveRight = False
moveUp=moveDown = False

pygame.mixer.music.play(-1, 0.0)

#游戲開始循環(huán)
while True: # the game loop runs while the game part is playing
    
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()

        if event.type == KEYDOWN:
            if event.key == K_UP or event.key == ord('w'):
                moveDown = False
                moveUp = True
            if event.key == K_DOWN or event.key == ord('s'):
                moveUp = False
                moveDown = True

            if event.key == K_SPACE:
                shoot = True

        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                terminate()

            if event.key == K_UP or event.key == ord('w'):
                moveUp = False
            if event.key == K_DOWN or event.key == ord('s'):
                moveDown = False
                
            if event.key == K_SPACE:
                shoot = False

    # add new bullet新增子彈
    #新增子彈計數(shù)器
    bulletAddCounter += 1
    print("bulletAddCounter:",bulletAddCounter)
    #當while循環(huán)次數(shù)大于ADDNEWBULLETRATE時,才能發(fā)射子彈
    if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True:
        print("shoot")
    #if  shoot == True:  #如果不限制,子彈會連發(fā),沒有距離感
        bulletAddCounter = 0
        #playerRect.centery-25是為了讓子彈上升一點,與植物炮口平行,centerx+10是為了子彈在前面點出現(xiàn)
        newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-30, bulletRect.width, bulletRect.height),
						 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)),
						}
        bullets.append(newBullet)
        

    # Move the player around.
    if moveUp and playerRect.top > 30:
        playerRect.move_ip(0,-1 * PLAYERMOVERATE)
    if moveDown and playerRect.bottom < WINDOWHEIGHT-10:
        playerRect.move_ip(0,PLAYERMOVERATE)
        
    # move the bullet移動子彈
    for b in bullets:
        b['rect'].move_ip(1 * BULLETSPEED, 0)
        #print(b['rect'])
        
    # Draw the game world on the window.
    windowSurface.blit(rescaledBackground, (0, 0))
    # Draw the player's rectangle, rails
    windowSurface.blit(playerImage, playerRect)
    windowSurface.blit(zombieStretchedImage,z_destination)
        
    # draw each bullet將所有子彈繪制到游戲界面上
    for b in bullets:
        windowSurface.blit(b['surface'], b['rect'])
       
    # update the display
    pygame.display.update()
    #tick(framerate=0) -> milliseconds
    #幀速率參數(shù),該功能將延遲以使游戲的運行速度低
    mainClock.tick(FPS)
    #print("bullets",bullets)

  

random.randint(a,b)隨機生成若干移動僵尸

在python中的random.randint(a,b)用于生成一bai個指定范圍內(nèi)的整數(shù)。du其中參數(shù)zhia是下限,參數(shù)b是上限,生成的隨機dao數(shù)n: a <= n <= b。

'''
僵尸移動
'''

import pygame, random, sys, time
from pygame.locals import *

#set up some variables
WINDOWWIDTH = 1024
WINDOWHEIGHT = 600
#值設(shè)置低一些,僵尸移動速度變慢
FPS = 20
PLAYERMOVERATE = 15
#連續(xù)發(fā)射兩個子彈的距離,值越小,子彈距離越近,反之亦然
BULLETSPEED = 30
#新增子彈速度,值越小,子彈發(fā)射越快
ADDNEWBULLETRATE = 5
shoot = False
#增加子彈計數(shù)器
bulletAddCounter = 0
#增加僵尸計數(shù)器
zombieAddCounter=0

ZOMBIESIZE = 100 #includes newKindZombies
#新增喪尸速度,值設(shè)置大些,喪失數(shù)量少些
ADDNEWZOMBIERATE = 50
ADDNEWKINDZOMBIE = ADDNEWZOMBIERATE

NORMALZOMBIESPEED = 2
NEWKINDZOMBIESPEED = NORMALZOMBIESPEED / 2

# set up the colors顏色值設(shè)置,可用于背景顏色
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
RED=(255, 0, 0)
#結(jié)束游戲
def terminate():
    pygame.quit()
    sys.exit()

# set up pygame, the window, and the mouse cursor
pygame.init()
#reate an object to help track time
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN)
#窗口標題
pygame.display.set_caption('Zombie Defence')
#鼠標設(shè)置不可見
#pygame.mouse.set_visible(False)

# set up fonts設(shè)置字體
font = pygame.font.SysFont(None, 48)

#加載聲音
pygame.mixer.music.load('grasswalk.mp3')

# set up images
playerImage = pygame.image.load('SnowPea.gif')
#get the rectangular area of the Surface
#Returns a new rectangle covering the entire surface. This rectangle will 
#always start at (0, 0) with a width and height the same size as the image.
playerRect = playerImage.get_rect()
#子彈圖像加載
bulletImage = pygame.image.load('SnowPeashooterBullet.gif')
bulletRect = bulletImage.get_rect()

zombieImage=pygame.image.load("zombie.png")
zombieStretchedImage=pygame.transform.scale(zombieImage,(80,80))
z_destination=pygame.Rect(944, 300, 80, 80)
#游戲背景圖片加載
backgroundImage = pygame.image.load('background.png')
#對背景圖片尺寸重新調(diào)整大小,寬為WINDOWWIDTH,長為WINDOWHEIGHT
rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT))

zombies = []
bullets = []
playerRect.topleft = (50, WINDOWHEIGHT /2)
moveLeft = moveRight = False
moveUp=moveDown = False

pygame.mixer.music.play(-1, 0.0)

#游戲開始循環(huán)
while True: # the game loop runs while the game part is playing
    
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()

        if event.type == KEYDOWN:
            if event.key == K_UP or event.key == ord('w'):
                moveDown = False
                moveUp = True
            if event.key == K_DOWN or event.key == ord('s'):
                moveUp = False
                moveDown = True

            if event.key == K_SPACE:
                shoot = True

        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                terminate()

            if event.key == K_UP or event.key == ord('w'):
                moveUp = False
            if event.key == K_DOWN or event.key == ord('s'):
                moveDown = False
                
            if event.key == K_SPACE:
                shoot = False
    # Add new zombies at the top of the screen, if needed.增加上市
    zombieAddCounter += 1
    if zombieAddCounter == ADDNEWKINDZOMBIE:
        zombieAddCounter = 0
        zombieSize = ZOMBIESIZE       
        newZombie = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-zombieSize-10), zombieSize, zombieSize),
                        'surface':pygame.transform.scale(zombieImage, (zombieSize, zombieSize)),
                        }
        zombies.append(newZombie)
            
    # add new bullet新增子彈
    #新增子彈計數(shù)器
    bulletAddCounter += 1
    print("bulletAddCounter:",bulletAddCounter)
    #當while循環(huán)次數(shù)大于ADDNEWBULLETRATE時,才能發(fā)射子彈
    if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True:
        print("shoot")
    #if  shoot == True:  #如果不限制,子彈會連發(fā),沒有距離感
        bulletAddCounter = 0
        #playerRect.centery-25是為了讓子彈上升一點,與植物炮口平行,centerx+10是為了子彈在前面點出現(xiàn)
        newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-30, bulletRect.width, bulletRect.height),
						 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)),
						}
        bullets.append(newBullet)
        

    # Move the player around.
    if moveUp and playerRect.top > 30:
        playerRect.move_ip(0,-1 * PLAYERMOVERATE)
    if moveDown and playerRect.bottom < WINDOWHEIGHT-10:
        playerRect.move_ip(0,PLAYERMOVERATE)
        
    # move the bullet移動子彈
    for b in bullets:
        b['rect'].move_ip(1 * BULLETSPEED, 0)
        #print(b['rect'])
        
    # Move the zombies.
    for z in zombies:
        z['rect'].move_ip(-1*NORMALZOMBIESPEED, 0)
        
    # Draw the game world on the window.
    windowSurface.blit(rescaledBackground, (0, 0))
    # Draw the player's rectangle, rails
    windowSurface.blit(playerImage, playerRect)
    windowSurface.blit(zombieStretchedImage,z_destination)
        
    # draw each bullet將所有子彈繪制到游戲界面上
    for b in bullets:
        windowSurface.blit(b['surface'], b['rect'])
    
    # Draw each baddie
    for z in zombies:
        windowSurface.blit(z['surface'], z['rect'])
       
    # update the display
    pygame.display.update()
    #tick(framerate=0) -> milliseconds
    #幀速率參數(shù),該功能將延遲以使游戲的運行速度低
    mainClock.tick(FPS)
    #print("bullets",bullets)

  

完整的植物大戰(zhàn)僵尸游戲代碼如下。你們可以根據(jù)自己偏好來改變?nèi)宋飯D片,背景,配音。

import pygame, random, sys, time
from pygame.locals import *

#set up some variables
WINDOWWIDTH = 1024
WINDOWHEIGHT = 600
#值設(shè)置低一些,僵尸移動速度變慢
FPS = 20
MAXGOTTENPASS = 10
ZOMBIESIZE = 100 #includes newKindZombies
#新增喪尸速度,值設(shè)置大些,喪失數(shù)量少些
ADDNEWZOMBIERATE = 50
ADDNEWKINDZOMBIE = ADDNEWZOMBIERATE
NORMALZOMBIESPEED = 2
NEWKINDZOMBIESPEED = NORMALZOMBIESPEED / 2
PLAYERMOVERATE = 15
#子彈速度
BULLETSPEED = 20
ADDNEWBULLETRATE = 15

TEXTCOLOR = (255, 255, 255)
RED = (255, 0, 0)

#結(jié)束游戲
def terminate():
    pygame.quit()
    sys.exit()
#等待玩家按鍵
def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                if event.key == K_RETURN:
                    return

#測試玩家是否觸碰到僵尸
def playerHasHitZombie(playerRect, zombies):
    for z in zombies:
        #test if two rectangles overlap測試是否觸碰到僵尸
        if playerRect.colliderect(z['rect']):
            return True
    return False

#測試子彈是否觸碰到僵尸。如果是,就把該僵尸移除
def bulletHasHitZombie(bullets, zombies):
    for b in bullets:
        if b['rect'].colliderect(z['rect']):
            bullets.remove(b)
            return True
    return False

#測試子彈是否觸碰到新僵尸。如果是,就把該僵尸移除
def bulletHasHitCrawler(bullets, newKindZombies):
    for b in bullets:
        if b['rect'].colliderect(c['rect']):
            bullets.remove(b)
            return True
    return False

#輸入文字顯示
def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    #draw one image onto another
    surface.blit(textobj, textrect)

# set up pygame, the window, and the mouse cursor
pygame.init()
#reate an object to help track time
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))#, pygame.FULLSCREEN)
#窗口標題
pygame.display.set_caption('Zombie VS Plants')
#鼠標設(shè)置不可見
pygame.mouse.set_visible(False)
# set up fonts設(shè)置字體
font = pygame.font.SysFont(None, 48)
# set up sounds設(shè)置聲音
gameOverSound = pygame.mixer.Sound('gameover.wav')
#加載聲音
pygame.mixer.music.load('background.mp3')
# set up images
playerImage = pygame.image.load('plant.gif')
#get the rectangular area of the Surface
#Returns a new rectangle covering the entire surface. This rectangle will 
#always start at (0, 0) with a width and height the same size as the image.
playerRect = playerImage.get_rect()
#子彈圖像加載
bulletImage = pygame.image.load('bullet.gif')
bulletRect = bulletImage.get_rect()
zombieImage = pygame.image.load('zombie.png')
newKindZombieImage = pygame.image.load('ConeheadZombie.gif')
#newKindZombieImage = pygame.image.load('trump31.png')
#游戲背景圖片加載
backgroundImage = pygame.image.load('background.png')
#對背景圖片尺寸重新調(diào)整大小,寬為WINDOWWIDTH,長為WINDOWHEIGHT
rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT))

# show the "Start" screen
windowSurface.blit(rescaledBackground, (0, 0))
windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70))
drawText('Zombie VS Plants', font, windowSurface, (WINDOWWIDTH / 4), (WINDOWHEIGHT / 4))
drawText('Press Enter to start', font, windowSurface, (WINDOWWIDTH / 3) - 10, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
#主循環(huán)
while True:
    # set up the start of the game
    zombies = []
    newKindZombies = []
    bullets = []
    zombiesGottenPast = 0
    score = 0
    playerRect.topleft = (50, WINDOWHEIGHT /2)
    moveLeft = moveRight = False
    moveUp=moveDown = False
    shoot = False
    zombieAddCounter = 0
    newKindZombieAddCounter = 0
    bulletAddCounter = 40
    pygame.mixer.music.play(-1, 0.0)
    #游戲開始循環(huán)
    while True: # the game loop runs while the game part is playing
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()

            if event.type == KEYDOWN:
                if event.key == K_UP or event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True

                if event.key == K_SPACE:
                    shoot = True

            if event.type == KEYUP:
                if event.key == K_ESCAPE:
                        terminate()

                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False
                
                if event.key == K_SPACE:
                    shoot = False

        # Add new zombies at the top of the screen, if needed.增加上市
        zombieAddCounter += 1
        if zombieAddCounter == ADDNEWKINDZOMBIE:
            zombieAddCounter = 0
            zombieSize = ZOMBIESIZE       
            newZombie = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-zombieSize-10), zombieSize, zombieSize),
                        'surface':pygame.transform.scale(zombieImage, (zombieSize, zombieSize)),
                        }

            zombies.append(newZombie)

        # Add new newKindZombies at the top of the screen, if needed.
        newKindZombieAddCounter += 1
        if newKindZombieAddCounter == ADDNEWZOMBIERATE:
            newKindZombieAddCounter = 0
            newKindZombiesize = ZOMBIESIZE
            newCrawler = {'rect': pygame.Rect(WINDOWWIDTH, random.randint(10,WINDOWHEIGHT-newKindZombiesize-10), newKindZombiesize, newKindZombiesize),
                        'surface':pygame.transform.scale(newKindZombieImage, (newKindZombiesize, newKindZombiesize)),
                        }
            newKindZombies.append(newCrawler)

        # add new bullet
        bulletAddCounter += 1
        if bulletAddCounter >= ADDNEWBULLETRATE and shoot == True:
            bulletAddCounter = 0
            newBullet = {'rect':pygame.Rect(playerRect.centerx+10, playerRect.centery-25, bulletRect.width, bulletRect.height),
						 'surface':pygame.transform.scale(bulletImage, (bulletRect.width, bulletRect.height)),
						}
            bullets.append(newBullet)

        # Move the player around.
        if moveUp and playerRect.top > 30:
            playerRect.move_ip(0,-1 * PLAYERMOVERATE)
        if moveDown and playerRect.bottom < WINDOWHEIGHT-10:
            playerRect.move_ip(0,PLAYERMOVERATE)

        # Move the zombies down.
        for z in zombies:
            z['rect'].move_ip(-1*NORMALZOMBIESPEED, 0)

        # Move the newKindZombies down.
        for c in newKindZombies:
            c['rect'].move_ip(-1*NEWKINDZOMBIESPEED,0)

        # move the bullet
        for b in bullets:
            b['rect'].move_ip(1 * BULLETSPEED, 0)

        # Delete zombies that have fallen past the bottom.
        for z in zombies[:]:
            if z['rect'].left < 0:
                zombies.remove(z)
                zombiesGottenPast += 1

        # Delete newKindZombies that have fallen past the bottom.
        for c in newKindZombies[:]:
            if c['rect'].left <0:
                newKindZombies.remove(c)
                zombiesGottenPast += 1
		
		for b in bullets[:]:
			if b['rect'].right>WINDOWWIDTH:
				bullets.remove(b)
				
        # check if the bullet has hit the zombie 檢查子彈是否觸碰到僵尸
        for z in zombies:
            if bulletHasHitZombie(bullets, zombies):
                score += 1
                zombies.remove(z)
        #檢查子彈是否觸碰到新僵尸
        for c in newKindZombies:
            if bulletHasHitCrawler(bullets, newKindZombies):
                score += 1
                newKindZombies.remove(c)      
        # Draw the game world on the window.
        windowSurface.blit(rescaledBackground, (0, 0))
        # Draw the player's rectangle, rails
        windowSurface.blit(playerImage, playerRect)
        # Draw each baddie
        for z in zombies:
            windowSurface.blit(z['surface'], z['rect'])
        for c in newKindZombies:
            windowSurface.blit(c['surface'], c['rect'])
        # draw each bullet
        for b in bullets:
            windowSurface.blit(b['surface'], b['rect'])
        # Draw the score and how many zombies got past
        drawText('zombies gotten past: %s' % (zombiesGottenPast), font, windowSurface, 10, 20)
        drawText('score: %s' % (score), font, windowSurface, 10, 50)
        # update the display
        pygame.display.update()
            
        # Check if any of the zombies has hit the player.
        if playerHasHitZombie(playerRect, zombies):
            break
        if playerHasHitZombie(playerRect, newKindZombies):
           break
        
        # check if score is over MAXGOTTENPASS which means game over
        if zombiesGottenPast >= MAXGOTTENPASS:
            break
        #tick(framerate=0) -> milliseconds
        #幀速率參數(shù),該功能將延遲以使游戲的運行速度低
        mainClock.tick(FPS)

    # Stop the game and show the "Game Over" screen.游戲結(jié)束執(zhí)行命令
    pygame.mixer.music.stop()
    gameOverSound.play()  #播放游戲結(jié)束時聲音
    time.sleep(1)
    if zombiesGottenPast >= MAXGOTTENPASS:
        windowSurface.blit(rescaledBackground, (0, 0))
        windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70))
        drawText('score: %s' % (score), font, windowSurface, 10, 30)
        drawText('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
        drawText('YOUR COUNTRY HAS BEEN DESTROIED', font, windowSurface, (WINDOWWIDTH / 4)- 80, (WINDOWHEIGHT / 3) + 100)
        drawText('Press enter to play again or escape to exit', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) + 150)
        pygame.display.update()
        waitForPlayerToPressKey()
    if playerHasHitZombie(playerRect, zombies):
        windowSurface.blit(rescaledBackground, (0, 0))
        windowSurface.blit(playerImage, (WINDOWWIDTH / 2, WINDOWHEIGHT - 70))
        drawText('score: %s' % (score), font, windowSurface, 10, 30)
        drawText('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
        drawText('YOU HAVE BEEN KISSED BY THE ZOMMBIE', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) +100)
        drawText('Press enter to play again or escape to exit', font, windowSurface, (WINDOWWIDTH / 4) - 80, (WINDOWHEIGHT / 3) + 150)
        pygame.display.update()
        waitForPlayerToPressKey()
    gameOverSound.stop() #游戲結(jié)束聲音停止

這一章我們學習了python的pygame模塊,了解了pygame模塊的基礎(chǔ)知識和如何編寫植物大戰(zhàn)僵尸的DIY游戲??傊畃ygame在編寫視頻游戲上方便,高效,且具有高度的可移植性,幾乎可以在所有平臺和操作系統(tǒng)上運行。不要等了,趕緊去安裝pygame模塊,開始第一個游戲編程之旅吧。

https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149(博主視頻教學主頁)

。

總結(jié)

以上是生活随笔為你收集整理的pygame快乐游戏编程模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。