Old high school files. Lessson notes/codes/projects etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hsf/prog2/gametest/app.rb

73 lines
1.6 KiB

3 years ago
#!/usr/bin/ruby -w
require "gosu"
3 years ago
RED = Gosu::Color.argb(0xff_ff0000)
GREEN = Gosu::Color.argb(0xff_00ff00)
BLUE = Gosu::Color.argb(0xff_0000ff)
WHITE = Gosu::Color.argb(0xff_ffffff)
class PhysObj
3 years ago
attr_accessor :xspeed, :yspeed, :xaccel, :yaccel, :ela, :x, :y
attr_reader :width, :height
3 years ago
def initialize(xspeed=0, yspeed=0, xaccel=0, yaccel=0.1, ela=-0.5, xpos=0, ypos=0)
3 years ago
@x, @y = xpos, ypos
@xspeed, @yspeed = xspeed, yspeed
@xaccel, @yaccel = xaccel, yaccel
@ela = ela
@width, @height = 64, 64
end
3 years ago
def physics(width, height)
self.xspeed += self.xaccel
self.yspeed += self.yaccel
if( self.y >= height - self.height || self.y < 0 ) then
self.yspeed *= self.ela
end
if( self.x >= width - self.width || self.x < 0 ) then
self.xspeed *= self.ela
end
self.x += self.xspeed
self.y += self.yspeed
3 years ago
puts("#{self.x}, #{self.y} : #{self.xspeed}, #{self.yspeed}")
end
3 years ago
3 years ago
def render
Gosu.draw_quad(self.x, self.y, RED, self.x + self.width, self.y, GREEN, self.x, self.y + self.height, BLUE, self.x + self.width, self.y + self.height, WHITE)
3 years ago
end
3 years ago
end
class Game < Gosu::Window
attr_accessor :caption
attr_reader :width, :height
3 years ago
def initialize(width, height, physobjs = [])
3 years ago
super width, height
@width, @height = width, height
self.caption = "Test | #{width}x#{height}"
3 years ago
@physobjs = physobjs
3 years ago
end
def update
3 years ago
@physobjs.each do |obj|
obj.physics(self.width, self.height)
end
3 years ago
end
def draw
3 years ago
@physobjs.each do |obj|
3 years ago
obj.render
3 years ago
end
3 years ago
end
end
3 years ago
physobjs = []
for i in 0...1 do
physobjs << PhysObj.new(4, 0, 0, 0.1, -0.8, i, 0)
end
3 years ago
3 years ago
game = Game.new(640, 480, physobjs)
3 years ago
game.show