用Python和Pygame写游戏-从入门到精通(11)

By | 2011/07/04

上次我们说明了使用键盘操作游戏,键盘是非常古老的输入设备,甚至笔计算机本身都要古老的多,因为它发源于打字机,貌似1868年就有成熟的打字机问世了。不得不说的是,现在最常用的键位排部,并不是最科学的,相比上一次说过的DUORAK键盘,打字者的手指平均每日运动1英里,而QWERTY则是12到20英里。当然这对游戏毫无意义……

相比而言,鼠标非常的年轻,世界上最早的鼠标诞生于1964年,它是由美国人道格·恩格尔巴特(Doug Engelbart)发明的。IEEE协会把鼠标的发明列为计算机诞生50年来最重大的事件之一,可见其对IT历程的重大影响作用。1983年苹果公司给自家的电脑安上了鼠标,用户就开始离不开这个小东西了。而现代游戏,离开了鼠标,99%的都没法玩!我们自然得好好研究如何使用鼠标来操控我们的游戏。

使用鼠标控制精灵

我们已经看到如何画一个光标了,只是简单的在鼠标坐标上画一个图像而已,我们可以从MOUSEMOTION或者pygame.mouse.get_pos方法来获得坐标。但我们还可以使用这个坐标来控制方向,比如在3D游戏中,可以使用鼠标来控制视角。这种时候,我们不使用鼠标的位置,因为鼠标可能会跑到窗口外面,我们使用鼠标现在与上一帧的相对偏移量。在下一个例子中,我们演示使用鼠标的左右移动来转动我们熟悉的小鱼儿:

background_image_filename = 'sushiplate.jpg'
sprite_image_filename = 'fugu.png'

import pygame
from pygame.locals import *
from sys import exit
from gameobjects.vector2 import Vector2
from math import *

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)

background = pygame.image.load(background_image_filename).convert()
sprite = pygame.image.load(sprite_image_filename).convert_alpha()

clock = pygame.time.Clock()

# 让pygame完全控制鼠标
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)

sprite_pos = Vector2(200, 150)
sprite_speed = 300.
sprite_rotation = 0.
sprite_rotation_speed = 360.

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        # 按Esc则退出游戏
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                exit()

    pressed_keys = pygame.key.get_pressed()
    # 这里获取鼠标的按键情况
    pressed_mouse = pygame.mouse.get_pressed()

    rotation_direction = 0.
    movement_direction = 0.

    # 通过移动偏移量计算转动
    rotation_direction = pygame.mouse.get_rel()[0]/5.0

    if pressed_keys[K_LEFT]:
        rotation_direction = +1.
    if pressed_keys[K_RIGHT]:
        rotation_direction = -1.
    # 多了一个鼠标左键按下的判断
    if pressed_keys[K_UP] or pressed_mouse[0]:
        movement_direction = +1.
    # 多了一个鼠标右键按下的判断
    if pressed_keys[K_DOWN] or pressed_mouse[2]:
        movement_direction = -1.

    screen.blit(background, (0,0))

    rotated_sprite = pygame.transform.rotate(sprite, sprite_rotation)
    w, h = rotated_sprite.get_size()
    sprite_draw_pos = Vector2(sprite_pos.x-w/2, sprite_pos.y-h/2)
    screen.blit(rotated_sprite, sprite_draw_pos)

    time_passed = clock.tick()
    time_passed_seconds = time_passed / 1000.0

    sprite_rotation += rotation_direction * sprite_rotation_speed * time_passed_seconds

    heading_x = sin(sprite_rotation*pi/180.)
    heading_y = cos(sprite_rotation*pi/180.)
    heading = Vector2(heading_x, heading_y)
    heading *= movement_direction

    sprite_pos+= heading * sprite_speed * time_passed_seconds

    pygame.display.update()

一旦打开这个例子,鼠标就看不到了,我们得使用Esc键来退出程序,除了上一次的方向键,当鼠标左右移动的时候,小鱼转动,按下鼠标左右键的时候,小鱼前进/后退。看代码,基本也是一样的,就多了几句带注释的。

这里使用了

pygame.mouse.set_visible(False)
pygame.event.set_grab(True)

来完全控制鼠标,这样鼠标的光标看不见,也不会跑到pygame窗口外面去,一个副作用就是无法使用鼠标关闭窗口了,所以你得准备一句代码来退出程序。

然后我们使用

rotation_direction = pygame.mouse.get_rel()[0] / 5.

来获得x方向上的偏移量,除以5是把动作放慢一点……

还有

lmb, mmb, rmb = pygame.mouse.get_pressed()

获得了鼠标按键的情况,如果有一个按键按下,那么对应的值就会为True。

总结一下pygame.mouse的函数:

  • pygame.mouse.get_pressed —— 返回按键按下情况,返回的是一元组,分别为(左键, 中键, 右键),如按下则为True
  • pygame.mouse.get_rel —— 返回相对偏移量,(x方向, y方向)的一元组
  • pygame.mouse.get_pos —— 返回当前鼠标位置(x, y)
  • pygame.mouse.set_pos —— 显而易见,设置鼠标位置
  • pygame.mouse.set_visible —— 设置鼠标光标是否可见
  • pygame.mouse.get_focused —— 如果鼠标在pygame窗口内有效,返回True
  • pygame.mouse.set_cursor —— 设置鼠标的默认光标式样,是不是感觉我们以前做的事情白费了?哦不会,我们使用的方法有着更好的效果。
  • pyGame.mouse.get_cursor —— 不再解释。

关于使用鼠标

在游戏中活用鼠标是一门学问,像在FPS中,鼠标用来瞄准,ARPG或RTS中,鼠标用来指定位置和目标。而在很多策略型的小游戏中,鼠标的威力更是被发挥的 淋漓尽致,也许是可以放置一些道具,也许是用来操控蓄力。我们现在使用的屏幕是二维的,而鼠标也能在2维方向到达任何的位置,所以鼠标相对键盘,更适合现代的复杂操作,只有想不到没有做不到啊。

绝大多数时候,鼠标和键盘是合作使用的,比如使用键盘转换视角,使用键盘移动,或者键盘对应很多快捷键,而键盘则用来指定位置。开动大脑,创造未来!

下一章节讲述使用游戏控制器,也就是手柄啦~ 手里拿一个手柄玩游戏,感觉就是不一样,不过被爸妈看到立刻就知道不在干正事了……

>> 用Python和Pygame写游戏-从入门到精通(12)

23 thoughts on “用Python和Pygame写游戏-从入门到精通(11)

  1. icy

    rotation_direction = pygame.mouse.get_rel()[0]/5.0
    这个是鼠标的x / 5.0吧, 但是屏幕宽度是640, 640 / 5.0 = 128, 这个最多也就旋转128度
    应该改为640/360=1.778, 所以这样改一下
    rotation_direction = pygame.mouse.get_rel()[0]/1.5
    请博主指正!

    Reply
    1. xishui Post author

      嗯,很仔细!不过如果实际运行一下就会发现,鼠标横向移动的范围并不仅仅是一个窗口宽……

      Reply
    2. 山中的白胡子哥

      你运行一下就知道是怎么回事了。

      Reply
  2. pltc325

    @icy:
    sprite_rotation += rotation_direction * sprite_rotation_speed * time_passed_seconds

    由这个表达式可以看出rotation_direction想表达的是在一个速度单位下转动角度的概念,所以增加转动速度sprite_rotation_speed 的值也可以起到在一定x的量偏移下增大rotation幅度的大小的作用。

    Reply
  3. copyxgod

    heading_x = sin(sprite_rotation*pi/180.)
    heading_y = cos(sprite_rotation*pi/180.)
    heading = Vector2(heading_x, heading_y)
    请问博主,heading算法的原理是什么?

    Reply
    1. PulqueDelaRiddlehous

      如果没记错,应该是-sin与-cos
      sprite_rotation角度是x正半轴沿顺时针方向的角度,乘以pi/180.0转化为弧度进行运算
      而图形中的x正半轴向右,y正半轴向下.
      作个图试试.

      Reply
  4. 杏帘fly

    为什么鼠标左右移动的时候小鱼会晃动?

    Reply
    1. wayhome

      # 通过移动偏移量计算转动
      rotation_direction = pygame.mouse.get_rel()[0]/5.0

      Reply
  5. yang123

    博主,怎么求一个向量的垂直向量

    Reply
  6. yang123

    感觉这个程序控制鱼的效果有点别扭,我自己写了一个,请斧正
    background_filename = ‘sushiplate.jpg’
    sprite_filename = ‘fugu.png’

    import pygame,sys
    from pygame.locals import *
    from gameobjects.vector2 import Vector2
    from math import *

    pygame.init()
    screen=pygame.display.set_mode((640,480),0,32)

    background=pygame.image.load(background_filename)
    sprite=pygame.image.load(sprite_filename)

    sprite_pos_center=Vector2(200,200)

    clock=pygame.time.Clock()
    speed=300
    xylist=[[]]
    while True:
    for event in pygame.event.get():
    if event.type==QUIT:
    pygame.display.quit()
    sys.exit()
    screen.blit(background,(0,0))
    movex=0
    movey=0
    x,y=pygame.mouse.get_pos()
    K_pressed=pygame.key.get_pressed()
    if K_pressed[K_w]:
    movey-=1
    if K_pressed[K_s]:
    movey+=1
    if K_pressed[K_a] and x>=sprite_pos_center[0]:
    movex-=1
    if K_pressed[K_a] and x=sprite_pos_center[0]:
    movex+=1
    if K_pressed[K_d] and xsprite_pos_center[0]:
    angel=asin(mouse_to_sprite1[1]/hypot)
    if x==sprite_pos_center[0] and y>sprite_pos_center[1]:
    angel=pi/2
    if x==sprite_pos_center[0] and y<sprite_pos_center[1]:
    angel=-pi/2
    if x==sprite_pos_center[0] and y==sprite_pos_center[1]:
    angel=0
    if x=sprite_pos_center[1]:
    angel=pi-asin(mouse_to_sprite1[1]/hypot)
    if x<sprite_pos_center[0] and y<sprite_pos_center[1]:
    angel=-asin(mouse_to_sprite1[1]/hypot)-pi

    angel=-angel*180/pi

    sprite_rotate=pygame.transform.rotate(sprite,angel)
    ver=Vector2(sin(angel*pi/180.),cos(angel*pi/180.))

    sprite_pos_center+=movex*speed*time_passed/1000.*mouse_to_sprite+movey*speed*time_passed/1000.*ver
    w,h=sprite_rotate.get_size()
    sprite_pos=Vector2(sprite_pos_center[0]-w/2,sprite_pos_center[1]-h/2)

    screen.blit(sprite_rotate,sprite_pos)
    pygame.display.update()

    Reply
  7. yang123

    刚刚传错了
    background_filename = ‘sushiplate.jpg’
    sprite_filename = ‘fugu.png’

    import pygame,sys
    from pygame.locals import *
    from gameobjects.vector2 import Vector2
    from math import *

    pygame.init()
    screen=pygame.display.set_mode((640,480),0,32)

    background=pygame.image.load(background_filename)
    sprite=pygame.image.load(sprite_filename)

    sprite_pos_center=Vector2(200,200)

    clock=pygame.time.Clock()
    speed=300
    xylist=[[]]
    while True:
    for event in pygame.event.get():
    if event.type==QUIT:
    pygame.display.quit()
    sys.exit()
    screen.blit(background,(0,0))
    movex=0
    movey=0
    x,y=pygame.mouse.get_pos()
    K_pressed=pygame.key.get_pressed()
    if K_pressed[K_w]:
    movey-=1
    if K_pressed[K_s]:
    movey+=1
    if K_pressed[K_a] and x>=sprite_pos_center[0]:
    movex-=1
    if K_pressed[K_a] and x=sprite_pos_center[0]:
    movex+=1
    if K_pressed[K_d] and xsprite_pos_center[0]:
    angel=asin(mouse_to_sprite1[1]/hypot)
    if x==sprite_pos_center[0] and y>sprite_pos_center[1]:
    angel=pi/2
    if x==sprite_pos_center[0] and y<sprite_pos_center[1]:
    angel=-pi/2
    if x==sprite_pos_center[0] and y==sprite_pos_center[1]:
    angel=0
    if x=sprite_pos_center[1]:
    angel=pi-asin(mouse_to_sprite1[1]/hypot)
    if x<sprite_pos_center[0] and y<sprite_pos_center[1]:
    angel=-asin(mouse_to_sprite1[1]/hypot)-pi

    angel=-angel*180/pi

    sprite_rotate=pygame.transform.rotate(sprite,angel)
    ver=Vector2(sin(angel*pi/180.),cos(angel*pi/180.))

    sprite_pos_center+=movex*speed*time_passed/1000.*mouse_to_sprite+movey*speed*time_passed/1000.*ver
    w,h=sprite_rotate.get_size()
    sprite_pos=Vector2(sprite_pos_center[0]-w/2,sprite_pos_center[1]-h/2)

    screen.blit(sprite_rotate,sprite_pos)
    pygame.display.update()

    Reply
  8. yang123

    擦,为什么程序穿出来是这个样子,面目全非啊

    Reply
    1. xishui Post author

      评论是会删除空格的,你可以使用<pre></pre>包裹,比如

      if x>1
          x = 0
      
      Reply
  9. qiuyuejs

    为何鼠标左键有效,能够前进,但鼠标右键却无效?PS:键盘能正常操控
    代码如下:

    background_image_filename = 'sushiplate.jpg'
    sprite_image_filename = 'fugu.png'
    
    from math import *
    import pygame
    from pygame.locals import *
    from sys import exit
    from gameobjects.vector2 import Vector2
    
    pygame.init()
    
    screen = pygame.display.set_mode((640,480), 0, 32)
    
    background = pygame.image.load(background_image_filename).convert()
    sprite = pygame.image.load(sprite_image_filename).convert_alpha()
    
    clock = pygame.time.Clock()
    
    pygame.mouse.set_visible(False)
    pygame.event.set_grab(True)
    
    sprite_pos = Vector2(200, 150)
    sprite_speed = 300
    sprite_rotation = 0
    sprite_rotation_speed = 360
    
    while True:
    	for event in pygame.event.get():
    		if event.type == QUIT:
    			exit()
    			
    		if event.type == KEYDOWN:
    			if event.key == K_ESCAPE:
    				exit()
    	
    	pressed_keys = pygame.key.get_pressed()
    	
    	pressed_mouse = pygame.mouse.get_pressed()
    	
    	rotation_direction = 0
    	movement_direction = 0
    	
    	rotation_direction = pygame.mouse.get_rel()[0] / 5
    	
    	if pressed_keys[K_LEFT]:
    		rotation_direction = 1
    	if pressed_keys[K_RIGHT]:
    		rotation_direction = -1	
    	
    	if pressed_keys[K_UP] or pressed_mouse[0]:
    		movement_direction = 1
    	if pressed_keys[K_DOWN] or pressed_mouse[2]:
    		movement_direction = -1
    		
    	screen.blit(background, (0, 0))
    	
    	rotated_sprite = pygame.transform.rotate(sprite, sprite_rotation)
    	w, h = rotated_sprite.get_size()
    	sprite_draw_pos = Vector2(sprite_pos.x-w/2, sprite_pos.y-h/2)
    	screen.blit(rotated_sprite, sprite_draw_pos)
    	
    	time_passed = clock.tick(120)
    	time_passed_seconds = time_passed / 1000
    	
    	sprite_rotation += rotation_direction * sprite_rotation_speed * time_passed_seconds
    	
    	heading_x = cos(sprite_rotation * pi /180)
    	heading_y = -sin(sprite_rotation * pi /180)
    	heading = Vector2(heading_x, heading_y)
    	heading *= movement_direction
    	
    	sprite_pos += heading * sprite_speed * time_passed_seconds
    	
    	pygame.display.update()
    
    Reply
  10. Frank Chen

    from gameobjects.vector2 import Vector2
    博主,我在写这一句时gameobjects和Vector2报错了,请问是什么原因?我的Python版本是35-32的。

    Reply
    1. zzhdx

      我查了一下网上是这么说的
      from gameobjects.vector2 import Vector2 替换为

      from pygame.math import *
      就行
      不过感觉你已经早就知道了

      Reply
  11. EdwardYu

    博主,我感觉在屏幕里小鱼左右晃的幅度有点奇怪,想做到小鱼的头跟着鼠标动,就像射击游戏的准星一样,这要怎样才能做到呢

    Reply
  12. EdwardYu

    啊没问题了,只要做一个小鱼位置到鼠标位置的向量就够了- –

    Reply
  13. Pingback: 用Python和Pygame写游戏-从入门到精通(1) – 易可信blog系统

  14. zzhdx

    居然是11年写的,那个时候我才初中啊。时间过得好快。

    Reply
  15. Pingback: 如何用Pygame写游戏(十一)-识荒者

  16. Pingback: 用 Python 和 Pygame 写游戏 – 从入门到精通(目录) – ITPCB

杏帘fly进行回复 取消回复

您的电子邮箱地址不会被公开。