有Gosu教程的问题,可能不仅仅是语法错误

时间:2014-01-30 04:53:40

标签: ruby syntax syntax-error libgosu

我一直在尝试Gosu教程,所以我可以学习制作游戏。我的代码出错了。有人能指出我做错了什么吗?这是教程链接:

https://github.com/jlnr/gosu/wiki/Ruby-Tutorial

这是我的代码:

require 'Gosu'

class GameWindow < Gosu::Window

        def initialize
        super 640, 480, false
        self.caption = "Gosu Tutorial Game"

        @background_image = Gosu::Image.new(self, "Usable  Images/final_fantasy_background.png", true)

        @player = Player.new(self)
        @player.warp(320, 240)

        @Star_anim = Gosu::Image::load_tiles(self, "Usable Images/donut.png", 25, 25, false)
        @Star = Array.new
    end

    def update
        if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then
            @player.turn_left
        end

        if button_down? Gosu::KbRight or button_down? Gosu::GpRight then
                @player.turn_left
        end

        if button_down? Gosu::KbUp or button_down? Gosu::GpButton0 then
            @player.accelerate
        end
        @player.move
        @player.collect_stars(@stars)

        if rand(100) < 4 and @Star.size < 25 then
            @Star.push(Star.new(@Star_anim))
        end
    end

    def draw
        @background_image.draw(0,0,ZOrder::Background)
        @player.draw
        stars = Star.new()
        @stars.each{ |star| star.draw}
    end

    def button_down(id)
        if id == Gosu::KbEscape
            close
        end
    end
end

 class Player
    def initialize(window)
        @image = Gosu::Image.new(window, "Usable Images/eric_cartman.png", false)
        @x = @y = @vel_x = @vel_y = @angle = 0.0
        @score = 0
    end

    def warp(x, y)
        @x, @y = x, y
    end

    def turn_left
        @angle -= 4.5
    end

    def turn_right
        @angle += 4.5
    end

    def accelerate
        @vel_x += Gosu::offset_x(@angle, 0.5)
        @vel_y += Gosu::offset_x(@angle, 0.5)
    end

    def move
        @x += @vel_x
        @y += @vel_y

        @x %= 640
        @y %= 480

        @vel_x *= 0.95
        @vel_y *= 0.95
    end

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

    def score
        @score
    end

    def collect_Star(star)
        if Star.reject! {|star| Gosu::distance(@x, @y, Star.x, star.y) < 35} then
            @score += 1
        end
    end
end

module ZOrder
    Background, Star, Player, UI = *0..3
end

class Star
    attr_reader :x, :y

    def initialize(animation)
        @animation = animation
        @color = Gosu::Color.new(0xff000000)
        @color.red = rand(256 - 40) + 40
        @color.green = rand(256 - 40) + 40
        @color.blue = rand(256 - 40) + 40

        @x = rand * 640
        @y = rand * 480
    end

    def draw
        img = @animation[Gosu::milliseconds / 100 % @animation.size];
        img.draw(@x - image.width / 2.0, @y - img.height / 2.0, ZOrder::Star, 1, 1, @color, :add)
    end
end

window = GameWindow.new
window.show

1 个答案:

答案 0 :(得分:0)

您有Star.new(),但您将其定义为initialize(animation)(带有一个必填参数)。