- Pygame 教程
- Pygame - 主页
- Pygame - 概述
- Pygame - 你好世界
- Pygame - 显示模式
- Pygame - 本地模块
- Pygame - 颜色对象
- Pygame - 事件对象
- Pygame - 键盘事件
- Pygame - 鼠标事件
- Pygame - 绘制形状
- Pygame - 加载图像
- Pygame - 在窗口中显示文本
- Pygame - 移动图像
- Pygame - 使用数字键盘移动
- Pygame - 用鼠标移动
- Pygame - 移动矩形对象
- Pygame - 使用文本作为按钮
- Pygame - 转换图像
- Pygame - 声音对象
- Pygame - 混合器通道
- Pygame - 播放音乐
- Pygame - 玩电影
- Pygame - 使用相机模块
- Pygame - 加载光标
- Pygame - 访问 CDROM
- Pygame - 精灵模块
- Pygame - PyOpenGL
- Pygame - 错误和异常
- Pygame 有用资源
- Pygame - 快速指南
- Pygame - 有用的资源
- Pygame - 讨论
Pygame - 用鼠标移动
根据鼠标指针的移动来移动对象很容易。pygame.mouse 模块定义 get_pos() 方法。它返回与鼠标当前位置的 x 和 y 坐标相对应的两项元组。
(mx,my) = pygame.mouse.get_pos()
捕获 mx 和我的位置后,在这些坐标处的 Surface 对象上的 bilt() 函数的帮助下渲染图像。
例子
以下程序在移动鼠标光标位置连续渲染给定图像。
filename = 'pygame.png'
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("Moving with mouse")
img = pygame.image.load(filename)
x = 0
y= 150
while True:
mx,my=pygame.mouse.get_pos()
screen.fill((255,255,255))
screen.blit(img, (mx, my))
for event in pygame.event.get():
if event.type == QUIT:
exit()
pygame.display.update()