找回密码
 立即注册
查看: 134|回复: 0

pgzero:用 Python 进行游戏开发

[复制链接]
发表于 2024-9-13 09:46 | 显示全部楼层 |阅读模式
以下文章来源于Python中文社区,作者wedo尝试君
链接:pgzero:用 Python 进行游戏开发
<hr/>



1. pgzero

python在各个范围都有着丰硕的第三方库,pygame是python在游戏范围的应用库,可以用来开发各种分歧的游戏。但是对于初学者来说,还是存在必然的门槛。而今天要和大师分享的pgzero(pygame zero)是在pygame基础上做了进一步的封装,使得设计一款游戏十分的便利,出格适合少儿编程范围的教学, 与scratch相得益彰。

  • pgzero的安装pip install pygame
    pip install pgzero
2. 游戏设计的过程

我们可以简单梳理下开发一款简单游戏需要的过程:

  • 游戏的故事设计
  • 游戏的场景绘制(布景图片和声音)
  • 游戏的角色
  • 如何控制角色
  • 如何判断成功与掉败
  • 游戏的关卡设计
3. pgzero基础

pgzero游戏开发的过程如下:




  • 游戏屏幕区域screenpgzero中游戏界面窗口设置由全局变量和内置对象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

  • 游戏角色Actorpgzero中所有以图片显示的元素都是Actor类来定义。# &#39;alien&#39; 暗示alien图片,默认是images/alien.png
    # (50, 50) 定义了Actor在窗口上显示的位置
    alien = Actor(&#39;alien&#39;, (50, 50))

    Actor的位置:




Actor重要属性和方式:

  • 其他属性同pygame.Rect

    • 外不雅观:image, 如alien.image = &#39;alien_hurt&#39;
    • 位置: piex坐标值:x,y, 设置位置:pos,left/right/top/bottom
    • 角度:angle
    • 绘制f方式:draw()
    • 距离方式: Actor.distance_to(target)
    • 角度方式:Actor.angle_to(target)

  • 游戏衬着绘制draw
  • 游戏状态的更新update
  • 游戏外部事件的触发控制on_xxx_xxxpgzero提供了常用的鼠标和键盘事件键盘的按键信息是通过keyboard内置对象获取的,鼠标是mouse来获取的,如: keyboard.a  # The &#39;A&#39; key
    keyboard.left  # The left arrow key
    keyboard.rshift  # The right shift key
    keyboard.kp0  # The &#39;0&#39; key on the keypad
    keyboard.k_0  # The main &#39;0&#39; key

    mouse.LEFT
    mouse.RIGHT
    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# 播放声音./sounds/drum.wav
    sounds.drum.play()
  • 音乐 music: 撑持mp3, 主要是时间较长的音频文件。资源对象目录默认为./music
    # 播放声音./music/drum.mp3
    music.play(&#39;drum&#39;)
  • 动画效果Animations,如移动角色到某个位置
    # animate(object, tween=&#39;linear&#39;, duration=1, on_finished=None, **targets)
    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游戏代码布局:
  1. import pgzrun
  2. # 全局变量和初始化信息
  3. TITLE = &#39;xxx&#39;
  4. WIDTH = 400
  5. HEIGHT = 500
  6. # 绘制游戏元素
  7. def draw():
  8.     pass
  9. # 更新游戏状态
  10. def update():
  11.     pass
  12. # 措置键盘事件
  13. def on_key_down():
  14.     pass
  15. # 措置键盘事件
  16. def on_mouse_down():
  17.     pass
  18. # 执行
  19. pgzrun.go()
  20. import pgzrun
  21. import random
  22. TITLE = &#39;Flappy Bird&#39;
  23. WIDTH = 400
  24. HEIGHT = 500
  25. # These constants control the difficulty of the game
  26. GAP = 130
  27. GRAVITY = 0.3
  28. FLAP_STRENGTH = 6.5
  29. SPEED = 3
  30. # bird
  31. bird = Actor(&#39;bird1&#39;, (75, 200))
  32. bird.dead = False
  33. bird.score = 0
  34. bird.vy = 0
  35. storage = {}
  36. storage[&#39;highscore&#39;] = 0
  37. def reset_pipes():
  38.     # 设置随机的高度
  39.     pipe_gap_y = random.randint(200, HEIGHT - 200)
  40.     pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2)
  41.     pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2)
  42. pipe_top = Actor(&#39;top&#39;, anchor=(&#39;left&#39;, &#39;bottom&#39;))
  43. pipe_bottom = Actor(&#39;bottom&#39;, anchor=(&#39;left&#39;, &#39;top&#39;))
  44. reset_pipes()  # Set initial pipe positions.
  45. def update_pipes():
  46.     # 不竭的移动柱子
  47.     pipe_top.left -= SPEED
  48.     pipe_bottom.left -= SPEED
  49.     if pipe_top.right < 0:
  50.         reset_pipes()
  51.         if not bird.dead:
  52.             bird.score += 1
  53.             if bird.score > storage[&#39;highscore&#39;]:
  54.                 storage[&#39;highscore&#39;] = bird.score
  55. def update_bird():
  56.     # 小鸟下降
  57.     uy = bird.vy
  58.     bird.vy += GRAVITY
  59.     bird.y += (uy + bird.vy) / 2
  60.     bird.x = 75
  61.     # 按照小鸟死亡切换小鸟的造型
  62.     if not bird.dead:
  63.         if bird.vy < -3:
  64.             bird.image = &#39;bird2&#39;
  65.         else:
  66.             bird.image = &#39;bird1&#39;
  67.     # 判断小鸟死亡: 是否触碰柱子
  68.     if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):
  69.         bird.dead = True
  70.         bird.image = &#39;birddead&#39;
  71.     # 小鸟超过边界 初始化
  72.     if not 0 < bird.y < 720:
  73.         bird.y = 200
  74.         bird.dead = False
  75.         bird.score = 0
  76.         bird.vy = 0
  77.         reset_pipes()
  78. def update():
  79.     update_pipes()
  80.     update_bird()
  81. # 按下任意键, 小鸟上升
  82. def on_key_down():
  83.     if not bird.dead:
  84.         bird.vy = -FLAP_STRENGTH
  85. #
  86. def draw():
  87.     # 布景图片
  88.     screen.blit(&#39;background&#39;, (0, 0))
  89.    
  90.     # 加载小鸟/柱子
  91.     pipe_top.draw()
  92.     pipe_bottom.draw()
  93.     bird.draw()
  94.     # 显示分数和最佳
  95.     screen.draw.text(
  96.         str(bird.score),
  97.         color=&#39;white&#39;,
  98.         midtop=(WIDTH // 2, 10),
  99.         fontsize=70,
  100.         shadow=(1, 1)
  101.     )
  102.     screen.draw.text(
  103.         ”Best: {}”.format(storage[&#39;highscore&#39;]),
  104.         color=(200, 170, 0),
  105.         midbottom=(WIDTH // 2, HEIGHT - 10),
  106.         fontsize=30,
  107.         shadow=(1, 1)
  108.     )
  109. pgzrun.go()
复制代码



5. 总结

本文分享了基于pygame封装版的pgzero开发python游戏的过程,但愿对您有辅佐。总结如下:

  • pgzero开发三剑客:draw() / update() / on_xxx_xxx()
  • pgzero内置对象:screen负责窗口设置,Actor负责图像显示,sounds负责短音频,music负责长音频bgm,动画效果有animate
  • pgzero资源目录:./images/xxx.png ./music/xxx.mp3  ./sounds/xxx/wav
6. 参考资料


  • https://pygame-zero.readthedocs.io/en/stable/
<hr/>

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-19 09:25 , Processed in 0.097942 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表