index
- 视频
- 完善上次课代码
- 处理鼠标事件
- `pygame.event.MOUSEMOTION`鼠标移动事件
- `pygame.event.MOUSEBUTTONUP`鼠标键释放事件
- `pygame.event.MOUSEBUTTONDOWN` 鼠标键按下事件
- 用鼠标拖动小球移动
- 如何让小球移动
- 边界处理
- 本次课代码
完善上次课代码
import pygame, sys
pygame.init()
screen_width , screen_height = 600,480
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("小小工坊")
ball = pygame.image.load('pygame/images/ball.gif')
ball_rect = ball.get_rect()
fclock = pygame.time.Clock()
speed = [1,1]
orientation = [1,1]
still = False
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_UP:
speed[1] = speed[1] - 1 if speed[1] > 0 else 0
elif e.key == pygame.K_DOWN:
speed[1] = speed[1] + 1
elif e.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1
elif e.key == pygame.K_LEFT:
speed[0] = speed[0] - 1 if speed[0] >0 else 0
if ball_rect.bottom >= screen_height or ball_rect.top < 0:
orientation[1] = -orientation[1]
if ball_rect.right >= screen_width or ball_rect.left < 0:
orientation[0] = -orientation[0]
ball_rect = ball_rect.move(speed[0]*orientation[0],speed[1]*orientation[1])
# if ball_rect.bottom >= screen_height:
# speed[1] = -1
# # ball_rect.bottom = screen_height
# if ball_rect.top <= 0:
# speed[1] = 1
# # ball_rect.top = 0
# if ball_rect.right >= screen_width:
# speed[0] = -1
# # ball_rect.right = screen_width
# if ball_rect.left <= 0:
# speed[0] = 1
# # ball_rect.left = 0
screen.fill((0,0,255))
screen.blit(ball,ball_rect)
pygame.display.update()
fclock.tick(200)
处理鼠标事件
鼠标是计算机最重要外接设备之一,同时它也是游戏玩家必不可少的工具之一。
Pygame 提供了三个鼠标事件,分别是鼠标移动
(MOUSEMOTION
)、鼠标按下(MOUSEBUTTONDOWN
)、鼠标释放(MOUSEBUTTONUP
),不同事件类型对应着不同的成员属性。如下所示:
pygame.event.MOUSEMOTION
鼠标移动事件
-
event.pos
相对于窗口左上角,鼠标的当前坐标值(x,y) -
event.rel
鼠标相对运动距离(x,y),相对于上次事件 -
event.buttons
鼠标按钮初始状态(0,0,0)
,分别对应(左键,滑轮,右键),移动过程中点击那个键,相应位置变会为1
pygame.event.MOUSEBUTTONUP
鼠标键释放事件
-
event.pos
相对于窗口左上角,鼠标的当前坐标值(x,y) -
event.button
鼠标释放键编号(整数)左键为1,按下滚动轮2、右键为3
pygame.event.MOUSEBUTTONDOWN
鼠标键按下事件
-
event.pos
相对于窗口左上角,鼠标的当前坐标值(x,y) -
event.button
鼠标按下键编号(整数),左键为1,按下滚动轮2、右键为3,向前滚动滑轮4、向后滚动滑轮5
通过一组简单的示例对鼠标事件进行演示,示例代码如下:
import pygame
from random import randint
# 初始化程序
pygame.init()
screen = pygame.display.set_mode((450,400))
pygame.display.set_caption("鼠标拖拽演示")
# 更新显示
pygame.display.flip()
while True:
#等待事件发生
event = pygame.event.wait()
if event.type == pygame.QUIT:
exit("成功退出")
if event.type == pygame.MOUSEBUTTONDOWN:
# pos 获取鼠标当前位置
print('鼠标按下',event.pos)
mx,my = event.pos
# 调用 pygame.draw 模块画圆
pygame.draw.circle(screen,(255,255,0),(mx,my),50)
if event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹起')
pass
if event.type == pygame.MOUSEMOTION:
print('鼠标移动')
mx, my = event.pos
# 随机生成 RGB 颜色值
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
pygame.draw.circle(screen, (r,g,b,),(mx, my), 50)
# 处理完,更新显示
pygame.display.update()
用鼠标拖动小球移动
如何让小球移动
MOUSEBUTTONDOWN
+int event.button
+tuple event.pos
+ballStopMove()
MOUSEMOTION
+tuple event.pos
+ballMove(pos) : rect
MOUSEBUTTONUP
+tuple event.pos
+ballMove(pos) : rect
+ballStartMove()
边界处理
关键是:
- 水平方向,只要小球超过右边的边界,就需要保证让小球向左运动,即运动方向向左。
- 纵向,只要小球超出下边界,就需要保证小球向上运动,即运动方向向上。
本次课代码
import pygame, sys
pygame.init()
screen_width , screen_height = 600,480
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("小小工坊")
ball = pygame.image.load('pygame/images/ball.gif')
ball_rect = ball.get_rect()
fclock = pygame.time.Clock()
speed = [1,1]
orientation = [1,1]
still = False
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_UP:
speed[1] = speed[1] - 1 if speed[1] > 0 else 0
elif e.key == pygame.K_DOWN:
speed[1] = speed[1] + 1
elif e.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1
elif e.key == pygame.K_LEFT:
speed[0] = speed[0] - 1 if speed[0] >0 else 0
elif e.type == pygame.MOUSEBUTTONDOWN:
if e.button == 1:
still = True
elif e.type == pygame.MOUSEBUTTONUP:
still = False
ball_rect.left,ball_rect.top = e.pos
elif e.type == pygame.MOUSEMOTION:
if e.buttons[0] == 1:
ball_rect.left, ball_rect.top = e.pos
if ball_rect.bottom >= screen_height or ball_rect.top < 0:
orientation[1] = -orientation[1]
if ball_rect.right >= screen_width or ball_rect.left < 0:
orientation[0] = -orientation[0]
if not still:
ball_rect = ball_rect.move(speed[0]*orientation[0],speed[1]*orientation[1])
# if ball_rect.bottom >= screen_height:
# speed[1] = -1
# # ball_rect.bottom = screen_height
# if ball_rect.top <= 0:
# speed[1] = 1
# # ball_rect.top = 0
# if ball_rect.right >= screen_width:
# speed[0] = -1
# # ball_rect.right = screen_width
# if ball_rect.left <= 0:
# speed[0] = 1
# # ball_rect.left = 0
screen.fill((0,0,255))
screen.blit(ball,ball_rect)
pygame.display.update()
fclock.tick(200)