自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

pgzero:用 Python 進行游戲開發(fā)

開發(fā) 后端 游戲開發(fā)
今天要和大家分享的pgzero(pygame zero)是在pygame基礎(chǔ)上做了進一步的封裝,使得設(shè)計一款游戲十分的方便,特別適合少兒編程領(lǐng)域的教學(xué), 與scratch相得益彰。

[[404459]]

1. pgzero

python在各個領(lǐng)域都有著豐富的第三方庫,pygame是python在游戲領(lǐng)域的應(yīng)用庫,可以用來開發(fā)各種不同的游戲。但是對于初學(xué)者來說,還是存在一定的門檻。

而今天要和大家分享的pgzero(pygame zero)是在pygame基礎(chǔ)上做了進一步的封裝,使得設(shè)計一款游戲十分的方便,特別適合少兒編程領(lǐng)域的教學(xué), 與scratch相得益彰。

  •  pgzero的安裝   
  1. pip install pygame  
  2. pip install pgzero 

2. 游戲設(shè)計的過程

我們可以簡單梳理下開發(fā)一款簡單游戲需要的過程:

  •  游戲的故事設(shè)計
  •  游戲的場景繪制(背景圖片和聲音)
  •  游戲的角色
  •  如何控制角色
  •  如何判斷成功與失敗
  •  游戲的關(guān)卡設(shè)計

3. pgzero基礎(chǔ)

pgzero游戲開發(fā)的過程如下:

  •  游戲屏幕區(qū)域screen pgzero中游戲界面窗口設(shè)置由全局變量和內(nèi)置對象screen來完成:
    •   窗口外觀:WIDTH , HEIGHT 和TITLE
    •   窗口清楚:screen.clear()
    •   窗口背景顏色:screen.fill((red, green, blue))
    •   在窗口繪制圖像:screen.blit(image, (left, top))
    •   在窗口繪制幾何圖案:screen.draw.line screen.draw.circle screen.draw.rect
  •  游戲角色Actor pgzero中所有以圖片顯示的元素都是Actor類來定義。   
  1. # 'alien' 表示alien圖片,默認是images/alien.png  
  2.     # (50, 50) 定義了Actor在窗口上顯示的位置  
  3.     alien = Actor('alien', (50, 50)) 
  • Actor的位置:

Actor重要屬性和方法:

  •  其他屬性同pygame.Rect
    •   外觀:image, 如alien.image = 'alien_hurt'
    •   位置: piex坐標值:x,y, 設(shè)置位置:pos,left/right/top/bottom
    •   角度:angle
    •   繪制f方法:draw()
    •   距離方法: Actor.distance_to(target)
    •   角度方法:Actor.angle_to(target)
  •  游戲渲染繪制draw
  •  游戲狀態(tài)的更新update
  •  游戲外部事件的觸發(fā)控制on_xxx_xxx pgzero提供了常用的鼠標和鍵盤事件

鍵盤的按鍵信息是通過keyboard內(nèi)置對象獲取的,鼠標是mouse來獲取的,如: 

  1. keyboard.a  # The 'A' key  
  2.   keyboard.left  # The left arrow key  
  3.   keyboard.rshift  # The right shift key  
  4.   keyboard.kp0  # The '0' key on the keypad  
  5.   keyboard.k_0  # The main '0' key   
  6.   mouse.LEFT  
  7.   mouse.RIGHT  
  8.   mouse.MIDDLE 

詳見

  •   https://pygame-zero.readthedocs.io/en/stable/hooks.html#mouse.WHEEL_DOWN
  •   鍵盤事件:on_key_down, on_key_up
  •   鼠標事件:on_mouse_down, on_mouse_up, on_mouse_move

其他重要元素

  •  聲音 sounds:支持wav和ogg, 資源對象目錄默認為./sounds
  1. # 播放聲音./sounds/drum.wav  
  2.  sounds.drum.play() 
  •  音樂 music: 支持mp3, 主要是時間較長的音頻文件。資源對象目錄默認為./music 
  1. # 播放聲音./music/drum.mp3  
  2.   music.play('drum') 
  •  動畫效果Animations,如移動角色到某個位置   
  1. # animate(object, tween='linear'duration=1on_finished=None, **targets)  
  2.    animate(alien, pos=(100, 100)) 

詳見:

https://pygame-zero.readthedocs.io/en/stable/builtins.html#Animations

4. pgzero游戲例子

了解了pgzero的基本使用情況,下面來舉一個例子,將游戲編寫制作的過程串起來。

我們來模擬手機上的一款游戲FlappyBird。游戲簡單操作說明 

  1. 在《FlappyBird》這款游戲中,玩家只需要用一根手指來操控,點擊觸摸屏幕,小鳥就會往上飛,不斷的點擊就會不斷的往高處飛。放松手指,則會快速下降。所以玩家要控制小鳥一直向前飛行,然后注意躲避途中高低不平的管子。 [3]  
  2. 1、在游戲開始后,點擊屏幕,要記住是有間歇的點擊屏幕,不要讓小鳥掉下來。  
  3. 2、盡量保持平和的心情,點的時候不要下手太重,盡量注視著小鳥。  
  4. 3、游戲的得分是,小鳥安全穿過一個柱子且不撞上就是1分。當然撞上就直接掛掉,只有一條命。 

pgzero游戲代碼結(jié)構(gòu): 

  1. import pgzrun  
  2. # 全局變量和初始化信息  
  3. TITLE = 'xxx'  
  4. WIDTH = 400  
  5. HEIGHT = 500  
  6. # 繪制游戲元素  
  7. def draw(): 
  8.     pass  
  9. # 更新游戲狀態(tài)  
  10. def update():  
  11.     pass  
  12. # 處理鍵盤事件  
  13. def on_key_down():  
  14.     pass  
  15. # 處理鍵盤事件  
  16. def on_mouse_down():  
  17.     pass  
  18. # 執(zhí)行  
  19. pgzrun.go()  
  1. import pgzrun  
  2. import random  
  3. TITLE = 'Flappy Bird'  
  4. WIDTH = 400  
  5. HEIGHT = 500  
  6. # These constants control the difficulty of the game  
  7. GAP = 130  
  8. GRAVITY = 0.3  
  9. FLAP_STRENGTH = 6.5  
  10. SPEED = 3  
  11. # bird   
  12. bird = Actor('bird1', (75, 200))  
  13. bird.dead = False  
  14. bird.score = 0  
  15. bird.vy = 0  
  16. storage = {}  
  17. storage['highscore'] = 0  
  18. def reset_pipes():  
  19.     # 設(shè)置隨機的高度  
  20.     pipe_gap_y = random.randint(200, HEIGHT - 200)  
  21.     pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2)  
  22.     pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2)  
  23. pipe_top = Actor('top', anchor=('left', 'bottom'))  
  24. pipe_bottom = Actor('bottom', anchor=('left', 'top'))  
  25. reset_pipes()  # Set initial pipe positions.  
  26. def update_pipes():  
  27.     # 不斷的移動柱子  
  28.     pipe_top.left -SPEED  
  29.     pipe_bottom.left -SPEED  
  30.     if pipe_top.right < 0:  
  31.         reset_pipes()  
  32.         if not bird.dead:  
  33.             bird.score += 1  
  34.             if bird.score > storage['highscore']:  
  35.                 storage['highscore'] = bird.score  
  36. def update_bird():  
  37.     # 小鳥下降  
  38.     uy = bird.vy  
  39.     bird.vy += GRAVITY  
  40.     bird.y += (uy + bird.vy) / 2  
  41.     bird.x = 75  
  42.     # 根據(jù)小鳥死亡切換小鳥的造型  
  43.     if not bird.dead:  
  44.         if bird.vy < -3:  
  45.             bird.image = 'bird2'  
  46.         else:  
  47.             bird.image = 'bird1'  
  48.     # 判斷小鳥死亡: 是否觸碰柱子  
  49.     if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):  
  50.         bird.dead = True  
  51.         bird.image = 'birddead'  
  52.     # 小鳥超過邊界 初始化  
  53.     if not 0 < bird.y < 720:  
  54.         bird.y = 200  
  55.         bird.dead = False  
  56.         bird.score = 0  
  57.         bird.vy = 0  
  58.         reset_pipes()   
  59. def update():  
  60.     update_pipes()  
  61.     update_bird()  
  62. # 按下任意鍵, 小鳥上升  
  63. def on_key_down():  
  64.     if not bird.dead:  
  65.         bird.vy = -FLAP_STRENGTH  
  66. #   
  67. def draw():  
  68.     # 背景圖片  
  69.     screen.blit('background', (0, 0))  
  70.      # 加載小鳥/柱子  
  71.     pipe_top.draw()  
  72.     pipe_bottom.draw()  
  73.     bird.draw()  
  74.     # 顯示分數(shù)和最佳  
  75.     screen.draw.text(  
  76.         str(bird.score),  
  77.         color='white'
  78.          midtop=(WIDTH // 2, 10),  
  79.         fontsize=70 
  80.         shadow=(1, 1)  
  81.     )  
  82.     screen.draw.text(  
  83.         "Best: {}".format(storage['highscore']),  
  84.         color=(200, 170, 0),  
  85.         midbottom=(WIDTH // 2, HEIGHT - 10),  
  86.         fontsize=30 
  87.         shadow=(1, 1)  
  88.     )  
  89. pgzrun.go() 

5. 總結(jié)

本文分享了基于pygame封裝版的pgzero開發(fā)python游戲的過程,希望對您有幫助。總結(jié)如下:

  •  pgzero開發(fā)三劍客:draw() / update() / on_xxx_xxx()
  •  pgzero內(nèi)置對象:screen負責窗口設(shè)置,Actor負責圖像顯示,sounds負責短音頻,music負責長音頻bgm,動畫效果有animate
  •  pgzero資源目錄:./images/xxx.png ./music/xxx.mp3  ./sounds/xxx/wav

6. 參考資料

https://pygame-zero.readthedocs.io/en/stable/ 

 

責任編輯:龐桂玉 來源: Python中文社區(qū) (ID:python-china)
相關(guān)推薦

2021-06-07 23:57:59

Python 游戲Pgzero

2024-07-08 08:38:37

Python游戲開發(fā)

2010-03-03 15:06:52

Android 游戲開

2010-03-12 13:32:02

python2.6

2019-09-23 09:11:02

Python文本編輯器操作系統(tǒng)

2010-02-01 14:48:43

2020-07-23 09:15:25

Python機器學(xué)習聚類分析

2022-06-27 08:59:40

Python游戲代碼

2010-02-03 14:15:18

Python 開發(fā)

2017-02-20 09:10:05

2011-11-14 09:13:06

2012-01-17 12:39:09

JavaSwing

2012-04-12 11:11:15

HTML5APIWEB

2021-04-09 20:49:44

PythonOCR圖像

2024-01-15 07:47:09

井字棋游戲編程練習Python

2013-05-21 11:20:37

Android游戲開發(fā)View手勢識別

2024-12-02 11:15:08

2021-12-30 10:55:54

Python游戲腳本

2022-01-27 14:12:49

Python游戲腳本

2019-11-20 12:30:21

Python編程語言語音識別
點贊
收藏

51CTO技術(shù)棧公眾號