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

相比而言,鼠标非常的年轻,世界上最早的鼠标诞生于1964年,它是由美国人道格·恩格尔巴特(Doug Engelbart)发明的。IEEE协会把鼠标的发明列为计算机诞生50年来最重大的事件之一,可见其对IT历程的重大影响作用。1983年苹果公司给自家的电脑安上了鼠标,用户就开始离不开这个小东西了。而现代游戏,离开了鼠标,99%的都没法玩!我们自然得好好研究如何使用鼠标来操控我们的游戏。
使用鼠标控制精灵
我们已经看到如何画一个光标了,只是简单的在鼠标坐标上画一个图像而已,我们可以从MOUSEMOTION或者pygame.mouse.get_pos方法来获得坐标。但我们还可以使用这个坐标来控制方向,比如在3D游戏中,可以使用鼠标来控制视角。这种时候,我们不使用鼠标的位置,因为鼠标可能会跑到窗口外面,我们使用鼠标现在与上一帧的相对偏移量。在下一个例子中,我们演示使用鼠标的左右移动来转动我们熟悉的小鱼儿:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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()
|

![用Pygame和Python做游戏-从入门到精通11[python GUI教程]](https://www.zixueka.com/wp-content/uploads/2023/10/1697036289-3764d6f3a46d1b3.jpg)
