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

gosu_Gosu中的鼠标和键盘输入相关推荐

  1. python模拟键盘输入视频_python教程-模拟鼠标和键盘输入

    大家可能知道,有的情形下,如果我们需进行自动化操作的应用程序不提供相对应的的接口,就难以通过Python直接调用API来做到自动化.在此类情形下,Python也并非压根没有办法,我们可以通过模拟键盘和 ...

  2. java模拟器键盘输入_Java模拟鼠标和键盘输入

    用途 在电脑(Windows/Mac)上模拟鼠标和键盘输入 Mac运行需要打开相关权限,详见文末说明. 效果图 代码 import java.awt.*; import java.awt.event. ...

  3. 总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  4. 三、Unity中的鼠标、键盘的获取

    在Unity中,我们经常会处理点击鼠标的事件检测和键盘的事件检测.所以,我觉的应该将这个小知识点进行一个整理. 1.按下键盘的事件检测: 1.GetKey:   当通过名称指定的按键被用户按住时返回t ...

  5. bat脚本中如何多次键盘输入并判断_万花筒自动发布信息脚本【操作简单】

    亲,当您本店,请花掉您宝贵两分钟阅读下面两段话和看看下面的演示视频,这是我们多年B2B发帖行业心得和肺腑之言~~同时期待你成为我们的客户,我们好为你B2B推广之路添砖加瓦更上一层楼哦 万花筒自动发布信 ...

  6. 【Unity3D入门教程】鼠标和键盘输入与控制

    本文讲述了怎样进行鼠标和键盘的输入信息检测.外部设备输入检测需要每一帧运行,所以检测的函数需要写在Update函数中.本文讲的内容比较简单,直接上代码吧. using UnityEngine; usi ...

  7. VC 模拟鼠标和键盘输入

    模拟鼠标点击: 1.这里是鼠标左键按下和松开两个事件的组合即一次单击: mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, ...

  8. 解决VsCode中C程序无法键盘输入的问题

    文章目录 一.问题截图 二.解决办法 三.清空控制台 四.控制台乱码 一.问题截图 在OUTPUT窗口里,遇到scanf语句,无法进行键盘输入,问题出在Code Runner的设置 二.解决办法 需要 ...

  9. Java中的数组利用键盘输入求平均数

    package chap04; import java.util.Scanner; public class ExampleLength1_2 {public static void main(Str ...

最新文章

  1. STM32单片机真的落后?
  2. oracle常用关键字和函数
  3. 两步实现spark集群
  4. c语言项目为什么要build?(gcc、makefile、cmake(qmake)、CMakeLists.txt)(qmake、cmake、qbs区别解析)(qmake还是cmake,mingw作用)
  5. 第七届(16年)蓝桥杯java B组决赛真题及前四题解析
  6. python batch normalization_Batch Normalization 详解
  7. Python打印到文件
  8. 一种基于红黑树的定时器
  9. UVa 11044 - Searching for Nessy
  10. Linux由管道组成的值得学习的命令
  11. plsqldev解决中文乱码问题
  12. codeforces 贪心 Traps
  13. 移动安全-Android安全测试框架Drozer
  14. MATLAB图像识别技术在棉花叶面病虫害识别上的
  15. python 单例模式基本原则、使用场景、应用示例
  16. 【解码芯片MIPI输出 四合一】XS9922B 国产 4通道模拟复合视频解码芯片 对标TP2815
  17. 基于知识图谱的智能问答项目
  18. 注册gmail账号,手机无法接受验证码的问题
  19. getTasks: caller 10079 does not hold REAL_GET_TASKS; limiting output问题解决
  20. iPhone的指纹识别与面部识别(FaceID)

热门文章

  1. 微服务背景下的前后端分离
  2. 深度 | 深度学习并不是AI的未来
  3. mac安装windows虚拟机并且进行远程连接
  4. 数据库建表的 15 个最佳实践方式
  5. 数据库建表规则(三大范式)通俗易懂
  6. 【DRF+Django】微信小程序入门到实战_day03(下)
  7. 如何更改微信标签名字_如何添加和修改微信标签 【图文教程】
  8. 国内办理期货开户不需要费用的
  9. PC端、移动端响应式布局的常用解决方案对比(媒体查询、百分比、rem和vw/vh)
  10. 计算机软件产品备案,计算机信息系统安全专用产品销售许可备案办事指南