-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
108 lines (87 loc) · 1.69 KB
/
game.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
require 'chingu'
class Game < Chingu::Window
#constructor
def initialize
super
self.input = {esc: :exit}
Background.create
@player = Player.create
5.times { Asteroid.create}
end
def update
super
Laser.each_bounding_circle_collision(Asteroid) do |laser, target|
laser.destroy
target.destroy
end
end
end
class Player < Chingu::GameObject
has_traits :velocity
#meta-constructor
def setup
@x, @y = 750, 400
@image = Gosu::Image["ship.png"]
@velocity_x = 0
@velocity_y = 0
self.input = {
holding_left: :turn_left,
holding_right: :turn_right,
holding_up: :accelerate,
holding_space: :fire
}
end
def turn_left
@angle -= 4.5
end
def turn_right
@angle += 4.5
end
def accelerate
@velocity_x += Gosu::offset_x(@angle, 0.5)
@velocity_y += Gosu::offset_y(@angle, 0.5)
end
def update
@velocity_x *= 0.95
@velocity_y *= 0.95
end
def fire
Laser.create(x: self.x, y: self.y)
end
end
class Laser < Chingu::GameObject
has_traits :velocity, :collision_detection, :bounding_circle, :timer
def setup
@image = Gosu::Image["laser.png"]
self.velocity_y = -10
Gosu::Sound["pew-pew.wav"].play
after(300) do
self.destroy
end
end
end
class Background < Chingu::GameObject
def setup
@x, @y = 400,300
@image = Gosu::Image["paper.jpg"]
end
end
class Asteroid < Chingu::GameObject
has_traits :collision_detection, :bounding_circle, :velocity
def setup
@x = rand(800)
@y = 100
@rotation = rand()
@velocity_y = rand()
@velocity_x = rand()
@image = Gosu::Image["asteroid#{rand(4)}.png"]
end
def update
super
if @x - self.width > $window.width
@x = -self.width
end
@angle += @rotation
end
end
Game.new.show