gosu_Gosu中的鼠标和键盘输入

gosu

Games are, by definition, interactive. Gosu makes this interaction straightforward with a simple interface for detecting and reacting to key and mouse button presses.

根据定义,游戏是交互式的。 Gosu通过用于检测按键和鼠标按键按下并做出React的简单界面,使交互变得简单明了。

There are two primary ways to handle input in your program. The first is an event-oriented approach. When buttons are pressed, your programs receives an event and you can react accordingly. The second is to check if, at the time of an update, a certain button is pressed. Both techniques are perfectly valid, use whichever one suits you best.

有两种主要方法可以处理程序中的输入。 第一种是面向事件的方法。 当按下按钮时,您的程序会收到一个事件,您可以做出相应的React。 第二个是检查更新时是否按下了某个按钮。 两种技术都非常有效,请使用最适合您的一种。

键和按钮常数 ( Key and Button Constants )

Behind the scenes, buttons are represented by integers. These integer codes are platform-dependent and probably shouldn't find their way into your game code. To abstract this away, Gosu provides a number of constants to use.

在幕后,按钮由整数表示。 这些整数代码取决于平台,并且可能不应该在您的游戏代码中找到它们。 为了对此进行抽象,Gosu提供了许多要使用的常量。

For every keyboard key, there is a Gosu::Kb* constant. For most of the keys, the names of these constants are easily guessed. For example, the arrow keys are Gosu::KbLeft, Gosu::KbRight, Gosu::KbUp and Gosu::KbDown. For a complete list, see the documentation for the Gosu module.

对于每个键盘按键,都有一个Gosu :: Kb *常量。 对于大多数键,这些常数的名称很容易猜到。 例如,箭头键是Gosu :: KbLeftGosu :: KbRightGosu :: KbUpGosu :: KbDown 。 有关完整列表,请参阅Gosu模块文档

There are also similar constants for mouse buttons. You'll mainly be using the Gosu::MsLeft and Gosu::MsRight for left and right click. There is also support for gamepads via the Gosu::Gp* constants.

鼠标按钮也有类似的常数。 您将主要使用Gosu :: MsLeftGosu :: MsRight进行左键和右键单击。 还可以通过Gosu :: Gp *常量支持游戏手柄。

This article is part of a series. Read more articles about Rapid Game Prototyping in Ruby

本文是系列文章的一部分。 阅读有关Ruby中快速游戏原型的更多文章

面向事件的输入 ( Event-Oriented Input )

Input events are delivered to the Gosu::Window instance. In the main loop, before update is called, Gosu will deliver events for all buttons that have either been pressed or released. It does this by calling the button_down and button_up methods, passing the id of the key or button pressed.

输入事件将传递到Gosu :: Window实例。 在主循环中,在调用更新之前,Gosu将为所有按下或释放的按钮传递事件。 它通过调用button_downbutton_up方法,传递按下的键或按钮的ID来完成此操作。

In the button_down and button_up methods, you often find a case statement. This, beside being very function, provides a very elegant and expressive way to decide what to do depending on which button was pressed or released. The following is a short example of what a button_down method can look like. It should be placed in your Gosu::Window subclass, and will close the window (ending the program) when the escape key is pressed.

button_downbutton_up方法中,通常会找到一个case语句。 除了非常有用的功能外,它还提供了一种非常优雅且富有表现力的方式,可以根据按下或释放的按钮来决定要做什么。 以下是button_down方法的外观的简短示例。 应该放置在你的古薮::窗口的子类,并且会关闭窗口(结束程序)按下ESC键时。

def button_down(id)
case id
when Gosu::KbEscape
close
end
end

Easy, right? Let's expand this. Here is a Player class. It can move left and right if the left and right keys are pressed. Note that this class also has button_down and button_up methods. They work just like the methods from a Gosu::Window subclass. Gosu doesn't know anything about Player though, we'll be calling the Player's methods manually from the Gosu::Window's methods. A full, runnable example can be found here.

容易吧? 让我们扩展一下。 这是一个播放器类。 如果按下左右键,它可以左右移动。 请注意,此类也具有button_downbutton_up方法。 它们的工作方式就像Gosu :: Window子类中的方法一样。 Gosu对Player一无所知,我们将从Gosu :: Window的方法中手动调用Player的方法。 可以在此处找到完整的可运行示例。

class Player
# In pixels/second
SPEED = 200
def self.load(window)
with_data('player.png') do|f|
@@image = Gosu::Image.new(window, f, false)
end
end
def initialize(window)
@window = window
@x = (@window.width / 2) - (@@image.width / 2)
@y = @window.height - @@image.height
@direction = 0
end
def update(delta)
@x += @direction * SPEED * delta
@x = 0 if @x @window.width - @@image.width
@x = @window.width - @@image.width
end
end
def draw
@@image.draw(@x, @y, Z::Player)
end
def button_down(id)
case id
when Gosu::KbLeft
@direction -= 1
when Gosu::KbRight
@direction += 1
end
end
def button_up(id)
case id
when Gosu::KbLeft
@direction += 1
when Gosu::KbRight
@direction -= 1
end
end
end

This article is part of a series. Read more articles about Rapid Game Prototyping in Ruby

本文是系列文章的一部分。 阅读有关Ruby中快速游戏原型的更多文章

查询输入 ( Querying Input )

If event-based input is not your style, you can query any Gosu::Window to see if any button or key is pressed, at any time. You can ignore the button_down and button_up callbacks entirely.

如果基于事件的输入不是您的样式,则可以随时查询任何Gosu :: Window以查看是否按下了任何按钮或键。 您可以完全忽略button_downbutton_up回调。

To query the Gosu::Window to see if a key is pressed, call the button_down? method with the id of the button you'd like to check. Don't forget the question mark in this call! If you call button_down(Gosu::KbLeft), you'll be reporting a button press to the Gosu::Window subclass. Even if you don't have any callback methods defined, the parent class, Gosu::Window will. There will be no error, it just won't work as you expect. Just don't forget that question mark!

要查询Gosu :: Window以查看是否按下了某个键,请调用button_down? 方法,其中包含您要检查的按钮的ID。 不要忘了这个电话中的问号! 如果调用button_down(Gosu :: KbLeft)则将Gosu :: Window子类报告按钮按下。 即使您没有定义任何回调方法,父类Gosu :: Window也可以。 不会有错误,只是无法按预期工作。 只是不要忘记那个问号!

Here is the Player class re-written to use button_down? instead of events. A full, runnable example is available here. This time, input is checked for at the beginning of the update method. You'll also notice that this example is shorter but, in my opinion, less elegant.

这是Player类重写为使用button_down吗? 而不是事件。 此处提供完整的可运行示例。 这次,在更新方法的开头检查输入。 您还将注意到该示例较短,但我认为它不太优雅。

class Player
attr_reader :x, :y
# In pixels/second
SPEED = 200
def self.load(window)
with_data('player.png') do|f|
@@image = Gosu::Image.new(window, f, false)
end
end
def initialize(window)
@window = window
@x = (@window.width / 2) - (@@image.width / 2)
@y = @window.height - @@image.height
@direction = 0
end
def update(delta)
@direction = 0
if @window.button_down?(Gosu::KbLeft)
@direction -= 1
end
if @window.button_down?(Gosu::KbRight)
@direction += 1
end
@x += @direction * SPEED * delta
@x = 0 if @x @window.width - @@image.width
@x = @window.width - @@image.width
end
end
def draw
@@image.draw(@x, @y, Z::Player)
end
end

This article is part of a series. Read more articles about Rapid Game Prototyping in Ruby

本文是系列文章的一部分。 阅读有关Ruby中快速游戏原型的更多文章

鼠标输入 ( Mouse Input )

The mouse buttons are handled in the same way as keyboard and gamepad buttons. You can both query them with button_down? and events with button_down and button_up. However, mouse movement may only be queried, there are no events for mouse movement. Gosu::Window's mouse_x and mouse_y methods provide the X and Y coordinates of the mouse pointer.

鼠标按钮的处理方式与键盘和游戏板按钮相同。 您都可以通过button_down查询它们吗? 以及具有button_downbutton_up的事件。 但是,只能查询鼠标的移动,没有鼠标移动的事件。 Gosu :: Windowmouse_xmouse_y方法提供鼠标指针的X和Y坐标。

Note that the X and Y coordinates are relative to the game window. So, for example, if the mouse is at the top left corner, it will be near the coordinate (0,0). Also, if the mouse pointer is outside of the game window entirely, it will still report where the pointer is relative to the window. So both mouse_x and mouse_y can be less than zero and more than the width or height of the window.

请注意,X和Y坐标是相对于游戏窗口的。 因此,例如,如果鼠标在左上角,它将靠近坐标(0,0) 。 同样,如果鼠标指针完全位于游戏窗口之外 ,它仍将报告指针相对于窗口的位置。 因此, mouse_xmouse_y都可以小于零且大于窗口的宽度或高度。

The following program will display a new sprite wherever you click the mouse. Note that it uses both event-driven input (for the clicks), and query-driven input (to get the position of the mouse). A full, runnable file is available here.

无论您在哪里单击鼠标,以下程序都将显示一个新的精灵。 请注意,它同时使用了事件驱动的输入(用于单击)和查询驱动的输入(用于获取鼠标的位置)。 完整的可运行文件在此处提供

class MyWindow

翻译自: https://www.thoughtco.com/mouse-and-keyboard-input-in-gosu-2908025

gosu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值