Pygame Zero库教程-鼠标点击事件

Actor的位置:

image

Actor重要属性和方法:

  • 其他属性同pygame.Rect

    • 外观:image, 如alien.image = 'alien_hurt'
    • 位置: piex坐标值:x,y, 设置位置:pos,left/right/top/bottom
    • 角度:angle
    • 绘制f方法:draw()
    • 距离方法: Actor.distance_to(target)
    • 角度方法:Actor.angle_to(target)
  • 游戏渲染绘制draw

  • 游戏状态的更新update

  • 游戏外部事件的触发控制on_xxx_xxx pgzero提供了常用的鼠标和键盘事件

键盘的按键信息是通过keyboard内置对象获取的,鼠标是mouse来获取的,如:


 keyboard.a  # The 'A' key
 keyboard.left  # The left arrow key
 keyboard.rshift  # The right shift key
 keyboard.kp0  # The '0' key on the keypad
 keyboard.k_0  # The main '0' 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

pgzero游戏代码结构:

import pgzrun

# 全局变量和初始化信息
TITLE = 'xxx'
WIDTH = 400
HEIGHT = 500

# 绘制游戏元素
def draw():
    pass

# 更新游戏状态
def update():
    pass

# 处理键盘事件
def on_key_down():
    pass

# 处理键盘事件
def on_mouse_down():
    pass

# 执行
pgzrun.Go()

作业

使用pgzero实现小球弹跳效果

进阶练习

阅读pgzero开发游戏教程

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容