Created
July 17, 2014 00:53
-
-
Save odanado/ce32c0befd88d3fb57dc to your computer and use it in GitHub Desktop.
game1
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 "dxruby" | |
load "object.rb" | |
Window.width=800 | |
Window.height=600 | |
font = Font.new(32) | |
win = false | |
lose = false | |
def distance(obj1,obj2) | |
Math.sqrt((obj1.x-obj2.x)**2 + (obj1.y-obj2.y)**2 ) | |
end | |
r = 16 | |
ball1 = Image.new(r*2,r*2).circleFill(r,r,r,[255,0,0]) | |
ball2 = Image.new(r*2,r*2).circleFill(r,r,r,[0,255,0]) | |
enemy1 = Object.new(ball1,100,100,0,8,0.1) | |
enemy2 = Object.new(ball1,400,100,0,8,0.1) | |
player = Object.new(ball2,300,500,0,8,0.1) | |
Window.loop do | |
if distance(enemy1,enemy2) < r * 2 | |
enemy1.v_x = enemy2.v_x - enemy1.v_x | |
enemy1.v_y = enemy2.v_y - enemy1.v_y | |
enemy2.v_x = enemy1.v_x - enemy2.v_x | |
enemy2.v_y = enemy1.v_y - enemy2.v_y | |
end | |
player.thate = Math.atan2(Input.y,Input.x) | |
thate1 = Math.atan2(player.y - enemy1.y, player.x - enemy1.x) | |
thate2 = Math.atan2(player.y - enemy2.y, player.x - enemy2.x) | |
player.update(Input.x, Input.y) | |
enemy1.update(Math.cos(thate1), Math.sin(thate1)) | |
enemy2.update(Math.cos(thate2), Math.sin(thate2)) | |
Window.draw(enemy1.x, enemy1.y, enemy1.image) if enemy1.exists | |
Window.draw(enemy2.x, enemy2.y, enemy2.image) if enemy2.exists | |
Window.draw(player.x, player.y, player.image) if player.exists | |
unless 0<= enemy1.x && enemy1.x < Window.width && | |
0<= enemy1.y && enemy1.y < Window.height | |
enemy1.delete | |
end | |
unless 0<= enemy2.x && enemy2.x < Window.width && | |
0<= enemy2.y && enemy2.y < Window.height | |
enemy2.delete | |
end | |
if distance(enemy1,player) < r * 2 || distance(enemy2,player) < r * 2 | |
player.exists = false | |
end | |
unless 0<= player.x && player.x < Window.width && | |
0<= player.y && player.y < Window.height | |
player.exists = false | |
end | |
if !enemy1.exists && !enemy2.exists && !lose | |
Window.draw_font(100, 100, "Win", font) | |
win = true | |
player.delete | |
end | |
if !player.exists && !win | |
lose = true | |
Window.draw_font(100, 100, "Lose", font) | |
enemy1.delete | |
enemy2.delete | |
end | |
end |
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
class Object | |
def initialize(image,x,y,thate,f,dt) | |
@image = image.clone | |
@x, @y = x, y | |
@v_x,@v_y = 0, 0 | |
@a_x, @a_y = 0, 0 | |
@thate = thate | |
@f_x, @f_y = f, f | |
@dt = dt | |
@time = 1 | |
@exists = true | |
end | |
attr_accessor :image, :x, :y, :thate, :v_x, :v_y, :exists | |
def update(a_x,a_y) | |
if a_x * @v_x < 0 | |
@f_x += 2 | |
else | |
@f_x = 8 | |
end | |
if a_y * @v_y < 0 | |
@f_y += 2 | |
else | |
@f_y = 8 | |
end | |
dv_x = @f_x * a_x * @dt | |
dv_y = @f_y * a_y * @dt | |
@v_x += dv_x | |
@v_y += dv_y | |
dx = @v_x * @dt + @f_x * a_x * (2 * @time * @dt * @dt**2) / 2 | |
dy = @v_y * @dt + @f_y * a_y * (2 * @time * @dt * @dt**2) / 2 | |
@x += dx | |
@y += dy | |
#@time = @time + 1 if @time < 10 | |
end | |
def delete | |
@x = -100 | |
@y = -100 | |
@dt = 0 | |
@exists = false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment