Ruby unexpected $end help

Discussion in 'Mac Programming' started by Tomple, Feb 26, 2011.

  1. Tomple macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #1
    I continue to get the "unexpected end, expecting kEND" error

    Could someone please take a look at this :D

    main.rb

    Code:
    require 'rubygems'
    require 'gosu'
    require 'player'
    
    class MyGame < Gosu::Window
      def initialize
        super(300, 400, false)
        @player = Player.new(self)
      end
      
      def update
        if button_down? Gosu::Button::KbLeft
          @player.move_left
        if button_down? Gosu::Button::KbRight
          @player.move_right
      end
      
      def draw
        @player.draw
      end
      
    end
    
    window = MyGame.new
    window.show
    player.rb

    Code:
    class Player
      
      def initialize(game_window)
        @game_window = game_window
        @icon = Gosu::Image.new(@game_window, "images/orgbad.png", true)
        @x = 50
        @y = 50
      end
      
      def draw
        @icon.draw(@x, @y, 1)
      end
      
      def move_left
        @x = @x - 2
      end
      
      def move_right
        @x = @x + 2
      end
        
    end
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    In update, you haven't terminated the two ifs.

    Code:
      def update
        if button_down? Gosu::Button::KbLeft
          @player.move_left
        [color="red"]end[/color]
        if button_down? Gosu::Button::KbRight
          @player.move_right
        [color="red"]end[/color]
      end
    
     
  3. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #3
    D'oh!

    Thanks :D
     
  4. Tomple, Feb 26, 2011
    Last edited: Feb 26, 2011

    Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #4
    Well, might as well ask another question while im here...

    Is there an easy way to add/center text other then @message = " " and font.draw
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Perhaps you could use font's text_width method like this:

    Code:
    class Gosu::Font
      def draw_centered(area_width,
                        text, x, y, z,
                        factor_x = 1, factor_y = 1, 
                        color = 0xffffffff, mode = :default)
        w = text_width(text, factor_x)
        dx = (area_width - w) / 2
        draw(text, x + dx, y, z, factor_x, factory_y, color, mode)
      end
    end
    
     
  6. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #6
    Hmm thanks for this, should help :D
     
  7. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #7
    Ok, time for another dumb question.

    When using

    Code:
    class Gosu::Font
      def draw_centered(area_width,
                        text, x, y, z,
                        factor_x = 1, factor_y = 1, 
                        color = 0xffffffff, mode = :default)
        w = text_width(text, factor_x)
        dx = (area_width - w) / 2
        draw(text, x + dx, y, z, factor_x, factory_y, color, mode)
      end
    end
    How do I then have it say something, where does the text go?
     

Share This Page