-
Notifications
You must be signed in to change notification settings - Fork 2
/
launch_invaders.rb
executable file
·65 lines (53 loc) · 1.23 KB
/
launch_invaders.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
require 'gosu'
require_relative 'world'
class LaunchInvaders < Gosu::Window
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
def initialize
super(SCREEN_WIDTH, SCREEN_HEIGHT, false)
self.caption = 'Launch Invaders'
@world = World.new(SCREEN_WIDTH, SCREEN_HEIGHT, 10)
end
def update
@world.update
end
def draw
@world.invaders.each do |invader|
draw_rect(invader.bounds, Gosu::Color::WHITE)
end
@world.lasers.each do |laser|
draw_rect(laser.bounds, Gosu::Color::RED)
end
draw_rect(@world.player.bounds, Gosu::Color::BLUE)
end
def button_down(id)
case id
when Gosu::KbLeft
@world.player_accelerate(:left)
when Gosu::KbRight
@world.player_accelerate(:right)
when Gosu::KbSpace
@world.player_fire_laser
end
end
def button_up(id)
case id
when Gosu::KbLeft
@world.player_decelerate(:left)
when Gosu::KbRight
@world.player_decelerate(:right)
end
end
private
def draw_rect(rect, color)
draw_quad(
rect.left, rect.top, color,
rect.left, rect.bottom, color,
rect.right, rect.top, color,
rect.right, rect.bottom, color
)
end
end
window = LaunchInvaders.new
window.show