Ruby 向量';玩家';而不是使用Chingu和Gosu的图像
Chingu示例如下所示:Ruby 向量';玩家';而不是使用Chingu和Gosu的图像,ruby,libgosu,Ruby,Libgosu,Chingu示例如下所示: require 'rubygems' require 'chingu' class Game < Chingu::Window def initialize super @player = Player.new end end class Player < Chingu::GameObject def initialize(options = {}) super(options.merge(:image =>
require 'rubygems'
require 'chingu'
class Game < Chingu::Window
def initialize
super
@player = Player.new
end
end
class Player < Chingu::GameObject
def initialize(options = {})
super(options.merge(:image => Gosu::Image["player.png"])
end
end
Game.new.show
需要“rubygems”
需要“金谷”
类游戏<金谷::窗口
def初始化
超级的
@player=player.new
结束
结束
类玩家Gosu::image[“player.png”])
结束
结束
游戏新节目
如果我想用线而不是图像来绘制播放器对象,我该怎么做呢
下面的代码看起来很直观,但我无法让它工作
class Player < Chingu::BasicGameObject
def initialize(options = {})
super
@radius = options[:radius]
@c = Gosu::Color.new(0xffff0000)
end
def draw
$window.draw_rect([@x-1,@y+1,@x+1,@y+1,@x+1,@y-1,@x+1,@y+1],@c,1)
end
end
class-Player
我做错什么了吗?让我们想想办法
我想这些是你实际代码的不完整片段,
由于所示的代码调用draw_rect时@x和@y设置为nil,
正在为nil:nilClass引发“未定义的方法”-“异常,因为
你不能从零中减去任何东西。)
我猜你看到的是一扇没有画任何东西的空白窗户,
因为如前所述,你的Player.draw永远不会被调用
为什么??因为Chingu为所有用户提供自动绘图和更新
它的游戏对象,但仅当您使用GameObject.create而不是
GameObject.new
()
游戏对象
在游戏中使用此选项进行所有操作
物体。玩家,敌人,敌人
子弹,能量,战利品
到处躺着。它是非常可重复使用的
不包含任何游戏逻辑
(这取决于你!)。唯一要放的东西
它以某种方式出现在屏幕上。如果你这样做
GameObject.create()而不是new()
Chingu将继续保存该对象
“游戏对象”-自动游戏列表
更新/绘制
Chingu::BasicGameObject
的new()与create()行为
GameObject来自BasicGameObject
所以我们需要解决这个问题。然而
现在,Player.draw在每一帧都被正确调用
通过金谷,我们发现了一个新问题:问题
调用draw\u rect不起作用!这是Ruby告诉我的:
在[99,101,101,101,101,99,101,101]:数组(NoMethodError)的未定义方法中
嗯。。。我可以看到什么被传递到draw_rect方法中,
我想知道它期望得到什么?让我们看看代码
()
啊,现在有道理了。draw_rect需要传递一个矩形对象,而不是
一堆坐标。这是:
()
所以我们只需要先创建一个Rect对象,然后调用
使用该矩形作为第一个参数绘制矩形
好的,让我们开始吧。这是工作代码--
需要“rubygems”
需要“金谷”
类游戏<金谷::窗口
def初始化
超级的
放置“正在初始化播放器…”
@player=player.create
结束
结束
类播放器
现在运行它会在100100处显示一个红色的小矩形
希望有帮助
c~
# Draws an unfilled rect in given color
#
def draw_rect(rect, color, zorder)
$window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
$window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
$window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
$window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
end
Chingu::Rect
Constructor Details
- (Rect) initialize(*argv)
Create a new Rect, attempting to extract its own information from the
given arguments.
The arguments must fall into one of these cases:
- 4 integers +(x, y, w, h)+.
- 1 Rect or Array containing 4 integers +([x, y, w, h])+.
- 2 Arrays containing 2 integers each +([x,y], [w,h])+.
- 1 object with a +rect+ attribute which is a valid Rect object.
All rect core attributes (x,y,w,h) must be integers.
require 'rubygems'
require 'chingu'
class Game < Chingu::Window
def initialize
super
puts "initializing player..."
@player = Player.create
end
end
class Player < Chingu::BasicGameObject
def initialize(options = {})
super
@x = 100
@y = 100
@rect = Chingu::Rect.new(@x, @y, 10, 10)
@c = Gosu::Color.new(0xffff0000)
end
def draw
puts "inside draw"
puts @x, @y
$window.draw_rect(@rect, @c, 1)
end
end
Game.new.show