gpt4 book ai didi

ruby - 未定义方法(NoMethodError)ruby 和 Gosu

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:45 31 4
gpt4 key购买 nike

我在调用玩家的 x 位置和 y 位置时遇到问题。当我用 Projectile 类调用 origin_object.x 和 origin_object.y 时,它给我这个错误:

asteroids.rb:35:in `update': undefined method `x' for #<Player:0x007fa7c307c520> (NoMethodError)
from /usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb:135:in `tick'
from /usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb:135:in `tick'
from asteroids.rb:68:in `<main>'

这是我的代码:

class Player

attr_accessor :x, :y, :angle, :lives, :score
def initialize
@image = Gosu::Image.new("assets/nave1.png")
@x = @y = @vel_x = @vel_y = @angle = 0.0
@score = 0 #Puntaje
@lives = 5 #Vida
end
end

这是弹丸文件

class Projectile
attr_reader :x, :y, :angle
def initialize(origin_object)
@alive = true
@x, @y = origin_object.x, origin_object.y
@angle = origin_object.angle
@speed_modifier = 7
@image = Gosu::Image.new('assets/projectile.png')
@distance_traveled, @max_distance = 0, 50
end
end

这是主文件(我用它来运行项目)

require 'gosu'
require './lib/Player.rb'
require './lib/Projectile.rb'

class GameWindow < Gosu::Window

def initialize
super(1000, 700, false) #Creacion Pantalla
self.caption = "Asteroids Redes" #Titulo Pantalla
@font = Gosu::Font.new(self, "assets/victor-pixel.ttf", 34)
end

def setup_game
@player = Player.new
@player.warp(650, 350)
@game_in_progress = true
@projectiles = []
end

#--------------------------------------#
def update

if Gosu::button_down? Gosu::KbQ #Salir con tecla Q
close
end
if button_down? Gosu:: KbP
setup_game unless @game_in_progress
end

if @player #si existe jugador permite moverlo
if Gosu::button_down? Gosu::KbSpace
p @player.x
#@projectiles << Projectile.new(p @player)
end
if Gosu::button_down? Gosu::KbLeft or Gosu::button_down? Gosu::GpLeft then
@player.turn_left
end
if Gosu::button_down? Gosu::KbRight or Gosu::button_down? Gosu::GpRight then
@player.turn_right
end
if Gosu::button_down? Gosu::KbUp or Gosu::button_down? Gosu::GpButton0 then
@player.accelerate
end
@player.move
@projectiles.each {|projectile| projectile.move}
end

end
#--------------------------------------#
def draw
unless @game_in_progress #Si el no se esta ejecutando muestra el menu
@font.draw("ASTEROIDS", 260, 220, 50, 3, 3, Gosu::Color::rgb(255, 255, 255))
@font.draw("Presiona 'p' Para Jugar", 300, 320, 50, 1, 1, Gosu::Color::rgb(13, 123, 255))
@font.draw("Presiona 'q' Para Salir", 305, 345, 50, 1, 1, Gosu::Color::rgb(13, 123, 255))
end
if @player #Si existe jugador lo dibuja
@player.draw
@projectiles.each {|projectile| projectile.draw}
end
end
#--------------------------------------#
end
#........................................#
window = GameWindow.new
window.show

最佳答案

我已经从你的三个文件中复制了代码,为 movedrawaccelerateturn_left< 添加了空方法 stub /turn_right 这样代码就不会立即崩溃。

当我开始游戏并按 P,然后按空格键时,我在终端中看到“0.0”作为 p @player.x 的结果。

您是否正在运行与问题中包含的代码不同的代码版本?

关于ruby - 未定义方法(NoMethodError)ruby 和 Gosu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247595/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com