samples/pong/pong.rb in ray-0.1.0.pre1 vs samples/pong/pong.rb in ray-0.1.0

- old
+ new

@@ -5,61 +5,69 @@ File.expand_path(File.dirname(__FILE__) + "/../../test/res/#{res}") end require 'ray' +# NB: this tests Ray::ImageTarget, there's no other reason to use it. + module Pong class Scene < Ray::Scene scene_name :pong_scene def setup - @font = Ray::Font.default + player_img = Ray::Image.new [20, 50] + image_target(player_img) do |target| + target.draw Ray::Polygon.rectangle([0, 0, 20, 50], Ray::Color.blue) + target.update + end - player_img = Ray::Image.new(:w => 20, :h => 50) - player_img.draw_filled_rect(Ray::Rect.new(0, 0, 20, 50), Ray::Color.blue) - player_img.update + adv_img = Ray::Image.new [20, 50] + image_target(adv_img) do |target| + target.clear Ray::Color.red + target.update + end - adv_img = Ray::Image.new(:w => 20, :h => 50) - adv_img.draw_filled_rect(Ray::Rect.new(0, 0, 20, 50), Ray::Color.red) - adv_img.update + ball_img = Ray::Image.new [10, 10] + image_target(ball_img) do |target| + target.draw Ray::Polygon.circle([5, 5], 5, Ray::Color.white) + target.update + end - ball_img = Ray::Image.new(:w => 10, :h => 10) - ball_img.draw_filled_circle([5, 5], 5, Ray::Color.white) - ball_img.update + @player = sprite player_img + @adv = sprite adv_img + @ball = sprite ball_img - @player = sprite(player_img) - @adv = sprite(adv_img) - @ball = sprite(ball_img) + width, height = window.size.to_a - screen = Ray.screen - width, height = screen.w, screen.h + @screen_rect = Ray::Rect[0, 0, width, height] - @screen_rect = Ray::Rect.new(0, 0, width, height) - @player.y = (height / 2) - 25 @adv.y = (height / 2) - 25 @player.x = 30 @adv.x = width - 30 reset_ball @scores = {:player => 0, :adv => 0} + + @score = text "0 - 0", :size => 12 end def reset_ball @ball.y = (@screen_rect.h / 2) - 5 @ball.x = (@screen_rect.w / 2) - 5 - @ball_movement = [6, 6] + @ball_movement = Ray::Vector2[6, 6] end def register self.loops_per_second = 60 on :point_gained do |by| @scores[by] += 1 + @score.string = "#{@scores[:player]} - #{@scores[:adv]}" reset_ball end always do if holding? key(:down) @@ -76,43 +84,37 @@ raise_event :point_gained, :adv end if @ball.collide?(@player) || @ball.collide?(@adv) || @ball.y >= @screen_rect.h || @ball.y <= 0 - @ball_movement[-1] *= -1 - @ball.y += @ball_movement.last + @ball_movement.y *= -1 end if @ball.collide?(@player) || @ball.collide?(@adv) - @ball_movement[0] *= -1 - @ball.x += @ball_movement.first + @ball_movement.x *= -1 end - adv_center = @adv.y + (@adv.image.h / 2) - if @ball.y > adv_center - @adv.y += 4 - elsif @ball.y < adv_center - @adv.y -= 4 + if @ball_movement.x > 0 + adv_center = @adv.y + (@adv.image.h / 2) + if @ball.y > adv_center + @adv.y += 4 + elsif @ball.y < adv_center + @adv.y -= 4 + end end - @ball.x += @ball_movement.first - @ball.y += @ball_movement.last - - need_render! + @ball.pos += @ball_movement end end def render(win) - win.fill(Ray::Color.black) + win.clear Ray::Color.black - @player.draw_on win - @adv.draw_on win - @ball.draw_on win + win.draw @player + win.draw @adv + win.draw @ball - @font.draw("#{@scores[:player]} - #{@scores[:adv]}", - :at => [0, 0], :on => win, - :color => Ray::Color.white, - :size => 12) + win.draw @score end end class Game < Ray::Game def initialize