Created
January 14, 2018 17:49
-
-
Save obelisk68/04e8c321a7a5ac6d78e14bc626976344 to your computer and use it in GitHub Desktop.
Gosu + Chipmunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gosu' | |
require 'rmagick' | |
require 'chipmunk' | |
Width, Height = 600, 600 | |
Wall_h = 20 | |
class MyWindow < Gosu::Window | |
def initialize | |
super Width, Height, false | |
self.caption = "Gosu + Chipmunk" | |
@space = CP::Space.new | |
@space.gravity = CP::Vec2.new(0, 250) | |
circle_image = Magick::Image.new(21, 21) {self.background_color = 'transparent'} | |
gc = Magick::Draw.new | |
gc.fill('yellow') | |
gc.circle(10, 10, 0, 10) | |
gc.draw(circle_image) | |
@circle = Gosu::Image.new(circle_image) | |
background = Magick::Image.new(Width, Height) {self.background_color = "black"} | |
create_wall(0, Height - Wall_h, Width, Height - Wall_h, Wall_h, background) | |
create_wall(Wall_h, 0, Wall_h, Height, Wall_h, background) | |
create_wall(Width, 0, Width, Height, Wall_h, background) | |
create_wall(120, 150, 400, 200, Wall_h, background) | |
create_wall(200, 450, 480, 400, Wall_h, background) | |
@background_image = Gosu::Image.new(background) | |
@circles = [] | |
end | |
def create_circle | |
body = CP::Body.new(1, CP::INFINITY) | |
body.p = CP::Vec2.new(rand(Width / 2 + Width / 4), -20) | |
body.v = CP::Vec2.new(rand * 500 - 250, 0) | |
shape = CP::Shape::Circle.new(body, 10, CP::Vec2.new(0, 0)) | |
shape.e = 0.8 | |
shape.u = 0 | |
@space.add_body(body) | |
@space.add_shape(shape) | |
body | |
end | |
def create_wall(x1, y1, x2, y2, height, bg) | |
sb = CP::Body.new_static | |
p1 = CP::Vec2.new(x1, y1) | |
p4 = CP::Vec2.new(x2, y2) | |
p0 = p4 - p1 | |
p2 = p1 + p0.normalize.rotate(CP::Vec2.new(0, 1)) * height | |
p3 = p2 + p0 | |
verts = [p1, p2, p3, p4] | |
wshape = CP::Shape::Poly.new(sb, verts, CP::Vec2.new(0, 0)) | |
wshape.e = 1 | |
wshape.u = 0 | |
@space.add_shape(wshape) | |
gc = Magick::Draw.new | |
gc.fill('firebrick') | |
verts = verts.map {|vt| [vt.x, vt.y]}.flatten | |
gc.polygon(*verts) | |
gc.draw(bg) | |
end | |
def update | |
@space.step(1.0 / 60) | |
@circles << create_circle if rand < 0.1 and @circles.size < 300 | |
end | |
def draw | |
@background_image.draw(0, 0, 0) | |
@circles.each {|c| @circle.draw_rot(c.p.x, c.p.y, 1, 0)} | |
end | |
end | |
MyWindow.new.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment