-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkouler.rb
111 lines (93 loc) · 2.72 KB
/
kouler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'rubygems'
require 'gosu'
load 'ball.rb'
load 'enemy.rb'
load 'player.rb'
module ZOrder
Background, Player = *0..1
end
class GameWindow < Gosu::Window
attr_reader :height, :width
def initialize
@height = 600
@width = 800
super(@width, @height, false)
self.caption = "Kouler"
@background_image = Gosu::Image.new(self, "media/space.png", true)
@buttons = []
@buttons[0] = [Gosu::KbUp, Gosu::KbRight, Gosu::KbDown, Gosu::KbLeft]
@buttons[1] = [Gosu::KbW, Gosu::KbD, Gosu::KbS, Gosu::KbA]
start
end
def update
if (player_button_dir = button_dir(0))
@player.thrust(player_button_dir)
end
# Check for dead balls
@enemies.length.times do |n|
@enemies[n] = nil if @enemies[n].dead?
end
@enemies = @enemies.compact
# Check for collisions
collisions_checked = []
@enemies.each do |enemy|
enemy.chase(@player)
# We don't want to ever check for a collision between something and itself, so pretend
# we already did it.
collisions_checked << [enemy, enemy]
# Check each other enemy
@enemies.each do |other_enemy|
# unless we've already checked these two
unless collisions_checked.include?([other_enemy, enemy])
enemy.check_collision(other_enemy)
collisions_checked << [enemy, other_enemy]
end
end
# Check the player
enemy.check_collision(@player)
end
# Update position, velocity, dead-ness state, etc
@player.update
@enemies.map(&:update)
end
def draw
@background_image.draw(0, 0, ZOrder::Background)
@player.draw
@enemies.map(&:draw)
end
def button_dir(player_num)
dir = if button_down?(@buttons[player_num][0]) && button_down?(@buttons[player_num][1]); 45
elsif button_down?(@buttons[player_num][1]) && button_down?(@buttons[player_num][2]); 135
elsif button_down?(@buttons[player_num][2]) && button_down?(@buttons[player_num][3]); 225
elsif button_down?(@buttons[player_num][3]) && button_down?(@buttons[player_num][0]); 315
elsif button_down?(@buttons[player_num][0]); 0
elsif button_down?(@buttons[player_num][1]); 90
elsif button_down?(@buttons[player_num][2]); 180
elsif button_down?(@buttons[player_num][3]); 270
end
end
def button_down(id)
if id == Gosu::KbEscape then
close
end
if id == Gosu::KbSpace then
restart
end
end
def start
@player = Player.new(self)
@player.warp(@width / 2, @height / 2)
@enemies = []
3.times do
@enemies << e = Enemy.new(self)
e.warp_random
end
end
def restart
@player = nil if @player
@enemies = nil if @enemies
start
end
end
window = GameWindow.new
window.show