PyGame鼠标

版权声明:本文为博主原创文章,转载请注明出处: https://blog.csdn.net/Hubz131/article/details/86771151

一、前言

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

二、使用鼠标控制精灵

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

# -*- coding: utf-8 -*- 
# Time : 2019/2/7 12:40 
# Author : hubozhi
import pygame
from pygame.locals import *
from sys import exit
from gameobjects.vector2 import Vector2
from math import *

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

if __name__ == "__main__":
    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()
    pass

     一旦打开这个例子,鼠标就看不到了,我们得使用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()

总结一下pygame.mouse的函数:获得了鼠标按键的情况,如果有一个按键按下,那么对应的值就会为True。

pygame.mouse.get_pressed

返回按键按下情况,返回的是一元组,分别为(左键, 中键, 右键),如按下则为True
pygame.mouse.set_visible 设置鼠标光标是否可见

pygame.mouse.get_rel

返回相对偏移量,(x方向, y方向)的一元组

pygame.mouse.get_pos

扫描二维码关注公众号,回复: 5720256 查看本文章
返回当前鼠标位置(x, y)
pygame.mouse.set_pos 设置鼠标位置

pygame.mouse.get_focused

如果鼠标在pygame窗口内有效,返回True
pygame.mouse.set_cursor 设置鼠标的默认光标式样,是不是感觉我们以前做的事情白费了?哦不会,我们使用的方法有着更好的效果。

pygame.mouse.get_cursor

不再解释

三、关于使用鼠标

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

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

猜你喜欢

转载自blog.csdn.net/Hubz131/article/details/86771151