Forums Archived

The forums are being archived. See this topic for more information.

pong

Get help and support with Rubygame

pong

Postby sitting » Wed Aug 18, 2010 12:35 am

Oh hello this pong game dont have anything with the rubygame book pong game, so i was thinking best to make a new thread.
Code: Select all
require 'rubygems'
require 'rubygame'
require 'app/GameObject.rb'
require 'app/Background.rb'

class Game
   def initialize
      @screen = Rubygame::Screen.new [640, 480], 0, [Rubygame::HWSURFACE, Rubygame::DOUBLEBUF]
      @screen.title = "Pong"
   
      @queue = Rubygame::EventQueue.new
      @clock = Rubygame::Clock.new
      @clock.target_framerate = 60
      
      # ...
      @background = Background.new @screen.width, @screen.height
      
   end
   
   def run!
      loop do
         update
         draw
         @clock.tick
      end
   end
   
   def update
      @queue.each do |ev|
         case ev
            when Rubygame::QuitEvent
               Rubygame.quit
               exit
         end
      end
   end
   
   def draw
      @screen.fill [0,0,0]
      @background.draw @screen
      @screen.flip
   end
end


g = Game.new
g.run!

Code: Select all
class GameObject
   attr_accessor :x, :y, :width, :height, :surface
   
   
   def intialize x, y, surface
      @x = x
      @x = y
      @surface = surface
      @width = surface.width
      @height = surface.height
   end
   
   def update
   end
   
   def draw screen
      @surface.blit screen, [@x, @y]
   end
   
   def handle_event event
   end
   
   
end
   

Code: Select all
class Background < GameObject
   def initialize width, height
      surface = Rubygame::Surface.new [width, height]
      
      # Draw background
      white = [255, 255, 255]
      
      # Top
      surface.draw_box_s [0, 0], [surface.width, 10], white
      # Left
      surface.draw_box_s [0, 0], [10, surface.height], white
      # Bottom
      surface.draw_box_s [0, surface.height-10], [surface.width, surface.height], white
      # Right
      surface.draw_box_s [surface.width-10, 0], [surface.width, surface.width, surface.height], white
      # Divide
      surface.draw_box_s [surface.width/2-5, 0], [surface.width/2+5, surface.height], white
   
         super 0, 0, surface
   end
end


The error i get:

C:\Users\jonathan\Desktop\Projekt>main.rb
C:/Users/jonathan/Desktop/Projekt/app/Background.rb:19:in `initialize': wrong nu
mber of arguments(3 for 0) (ArgumentError)
from C:/Users/jonathan/Desktop/Projekt/app/Background.rb:19:in `initiali
ze'
from C:/Users/jonathan/Desktop/Projekt/main.rb:16:in `new'
from C:/Users/jonathan/Desktop/Projekt/main.rb:16:in `initialize'
from C:/Users/jonathan/Desktop/Projekt/main.rb:49:in `new'
from C:/Users/jonathan/Desktop/Projekt/main.rb:49:in `<main>'


i dont really know, but something about the nummmbers in super 0, 0, surface =)
any one got any ideas ? :)

// Jonathan
sitting
 
Posts: 8
Joined: Thu Jul 29, 2010 3:58 pm

Re: pong

Postby blue-wiz » Wed Aug 18, 2010 12:01 pm

The problem was that you've spelt initialize wrong in GameObject and you said
Code: Select all
@x = x
@x = y

:P . Don't worry, we all do it :lol:

so GameObject (working on my laptop) is:
Code: Select all
class GameObject
   attr_accessor :x, :y, :width, :height, :surface

   def initialize x, y, surface
      @x = x
      @y = y

      @surface = surface
      @width = surface.width
      @height = surface.height
   end

   def update
   end

   def draw screen
      @surface.blit screen, [@x, @y]
   end

   def handle_event event
   end
end


Hope it helped :)

Gwilym
blue-wiz
 
Posts: 12
Joined: Mon Jun 28, 2010 4:27 pm

Re: pong

Postby sitting » Wed Aug 18, 2010 7:16 pm

blue-wiz wrote:The problem was that you've spelt initialize wrong in GameObject and you said
Code: Select all
@x = x
@x = y

:P . Don't worry, we all do it :lol:

so GameObject (working on my laptop) is:
Code: Select all
class GameObject
   attr_accessor :x, :y, :width, :height, :surface

   def initialize x, y, surface
      @x = x
      @y = y

      @surface = surface
      @width = surface.width
      @height = surface.height
   end

   def update
   end

   def draw screen
      @surface.blit screen, [@x, @y]
   end

   def handle_event event
   end
end


Hope it helped :)

Gwilym


Thanks it worked! :)
when I read this I facepalmed myself, i was 100% sure i check everything, but its hard when I also have some kind of small type of dyslexia x) But thanks for the help! :)

Its awesome when you see the process of something that you work on, and it works :D i smiled for myself when i saw that it worked x)
sitting
 
Posts: 8
Joined: Thu Jul 29, 2010 3:58 pm


Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron