首页 > 解决方案 > 如何在 python 街机上使用 set_exclusive_mouse?

问题描述

任何人都可以在街机库中提供这种方法的示例吗?我看了网站上的解释,但我不明白。我期待它具有可以使对象在没有光标位置的情况下随鼠标运动移动的功能。谢谢!

标签: pythongame-developmentarcade

解决方案


可能你不需要使用set_exclusive_mouse. 相反,隐藏鼠标光标set_mouse_visible并使用捕获光标位置on_mouse_motion

import arcade

class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 400)
        self.set_mouse_visible(False)
        self.mouse_x = None
        self.mouse_y = None

def on_draw(self):
    arcade.start_render()
    if self.mouse_x and self.mouse_y:
        arcade.draw_text(f'Cursor position: {self.mouse_x} {self.mouse_y}', 0, 370, arcade.color.RED, font_size=25)
        arcade.draw_circle_filled(self.mouse_x, self.mouse_y, 18, arcade.color.RED)

def on_mouse_motion(self, x, y, dx, dy):
    self.mouse_x = x
    self.mouse_y = y

MyGame()
arcade.run()

例子:

在此处输入图像描述


推荐阅读