1

我正在使用 ruby​​ 和 gosu 创建游戏,但出现错误:

C:/Ruby26-x64/destroy!/bullet.rb:14:in draw_rot': There is no rendering queue for this operation (RuntimeError) from C:/Ruby26-x64/destroy!/bullet.rb:14:indraw' from C:/Ruby26-x64/destroy!/player.rb:45:in fire' from C:/Ruby26-x64/destroy!/destroy.rb:36:inupdate' from C:/Ruby26-x64/lib/ruby /gems/2.6.0/gems/gosu-0.14.5-x64-mingw32/lib/gosu/patches.rb:72:intick' from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/gosu-0.14.5-x64-mingw32/lib/gosu/patches.rb:72:in来自 C:/Ruby26-x64/destroy!/destroy.rb:40:in `' [0.517s 完成]

我试图将子弹类中的draw更改为draw_rot,但无济于事。

我的课程

破坏

require 'gosu'
require 'cmath'
require_relative 'player.rb'
require_relative 'enemy.rb'
require_relative 'bullet.rb'
class Destroy < Gosu::Window
  def initialize
    super(800, 600)
    self.caption = 'Destroy!'
    @player = Player.new(self)
    @enemies = []
    @enemies.push(Enemy.new(self))
  end

  def draw
    @player.draw
    @enemies.each do |enemy|
      enemy.draw
    end
  end

  def update
    @player.righturn if button_down?(Gosu::KbRight)
    @player.lefturn if button_down?(Gosu::KbLeft)
    @player.startmove
    @player.move if button_down?(Gosu::KbUp)
    @freq = 0.0025
    if rand < @freq
      @enemies.push(Enemy.new(self))
    end
    @enemies.each do |enemy|
      enemy.move
      enemy.update(@player.x, @player.y)
    end
    if @freq < 0.5
      @freq += 0.0002
    end
    @player.fire(self)
  end
end
window = Destroy.new
window.show

子弹

class Bullet
  attr_accessor :x
  attr_accessor :y
  attr_accessor :fired

  def initialize(window)
    @image = Gosu::Image.new('C:\Ruby26-x64\destroy!\images\bullet.png')
    @fired = 0
    @x = 0
    @y = 0
  end

  def draw(x, y)
    @x = x
    @y = y
    @image.draw_rot(x, y, 1, 0)
    @fired = 1
  end

  def update(angle)
    @xspeed = Gosu.offset_x(angle, 2)
    @yspeed = Gosu.offset_y(angle, 2)
    if (@x > 800 || @x < 0 || @y > 600 || @y < 0)
      @fired = 0
    end
  end
end

播放器

require_relative 'bullet.rb'
require 'gosu'
class Player
  attr_accessor :x
  attr_accessor :y

  def initialize(window)
    @x = 200
    @y = 200
    @xspeed = 0
    @yspeed = 0
    @angle = 0
    @image = Gosu::Image.new('C:/Ruby26-x64/destroy!/images/man.png')
  end

  def draw
    @image.draw_rot(@x, @y, 1, @angle)
  end

  def righturn
    @angle += 3
  end

  def lefturn
    @angle -= 3
  end

  def startmove
    @xspeed = Gosu.offset_x(@angle, 2)
    @yspeed = Gosu.offset_y(@angle, 2)
  end

  def move
    if @x > 767
      @xpeed = 0
      @x = 767
    end
    if @x < 33
      @xpeed = 0
      @x = 33
    end
    if @y > 567
      @yspeed = 0
      @y = 567
    end
    @x += @xspeed
    @y += @yspeed
  end

  def fire(window)
    @bullet = Bullet.new(window)
    @bullet.draw(@x, @y)
    while @bullet.fired = 1
      @bullet.update(@angle)
    end
  end
end

敌人

require 'cmath'
class Enemy
  def initialize(window)
    @flipped = 0
    @x = rand(800 - 2 * 30) + 30
    @y = rand(600 - 2 * 30) + 30
    @firefreq = 1 / 60
    @health = 5
    @numkilled = 0
    @dead = 0
    @xspeed = 0
    @yspeed = 0
    @angle = 0
    @image = Gosu::Image.new('C:/Ruby26-x64/destroy!/images/enemy.png')
    @imageflipped = Gosu::Image.new('C:/Ruby26-x64/destroy!/images/enemyflip.png')
  end

  def gethit
    @health -= 1
  end

  def die
    @numkilled += 1
    @dead = 1
  end

  def move
    if rand < 0.1
      @xspeed = rand(-3..3)
      @yspeed = rand(-3..3)
    end
    if @x + @xspeed > 800
      @x = 800
    end
    if @x + @xspeed < 0
      @x = 0
    end
    if @y + @yspeed > 600
      @y = 600
    end
    if @y + @yspeed < 0
      @y = 0
    end
    if !(@y + @yspeed < 0 && @y + @yspeed > 600 && @x + @xspeed < 0 && @x + @xspeed > 800)
      @y += @yspeed
      @x += @xspeed
    end
  end

  def draw
    if @dead == 0
      if @flipped == 1
        @imageflipped.draw_rot(@x, @y, 1, @angle)
      end
      if @flipped == 0
        @image.draw_rot(@x, @y, 1, @angle)
      end
    end
  end

  def update(xdist, ydist)
    if @x < xdist
      (@angle = CMath.atan((ydist - @y) / (xdist - @x)) * 180 / 3.14159265358979323846264338327950289)
      @flipped = 0
    end
    if @x > xdist
      (@angle = CMath.atan(-(ydist - @y) / (@x - xdist)) * 180 / 3.14159265358979323846264338327950289)
      @flipped = 1
    end
  end
end

我一点也不明白。

4

1 回答 1

4

您的Destroy#update方法调用Player#firewhich calls Bullet#drawwhich calls Gosu::Image#draw_rot。您不能从 main 方法中调用 draw 方法update

您必须将@bullet.draw方法调用从Player#fire(在 main 期间调用update)移到Player#draw(在 main 期间调用draw

于 2019-10-15T02:18:07.603 回答