-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_game.rb
123 lines (101 loc) · 2.25 KB
/
my_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'rubygems'
require 'gosu'
require './player'
require './obstacle'
require './thief'
require './bullet'
require './badge'
class MyGame < Gosu::Window
attr_reader :pieces
def initialize
super(500, 500, false)
@font = Gosu::Font.new(self, "Arial", 18)
@background =Gosu::Image.new(self, 'images/background.jpg', true)
start_game
end
def update
# escape always quites
if button_down? Gosu::Button::KbEscape
exit
end
# game stopped state
unless @running
if button_down? Gosu::Button::KbSpace
start_game
end
return
end
# game running state
if button_down? Gosu::Button::KbLeft
@player.move_left
end
if button_down? Gosu::Button::KbRight
@player.move_right
end
if button_down? Gosu::Button::KbUp
@player.move_up
end
if button_down? Gosu::Button::KbDown
@player.move_down
end
if button_down? Gosu::Button::KbSpace
@player.shoot
end
add_points @pieces.count
# Keep any pieces that are able to update
@pieces.each { |piece|
piece.update
@pieces.each { |other_piece| other_piece.hit_by? piece }
}
# remove any pieces that have been destroyed
@pieces.reject! { |piece| piece.destroyed}
if (Time.now - @stopwatch) > 5
@stopwatch = Time.now
add_obstacle
if (rand(1..2) ==2)
add_thief
end
if rand(1..10) == 7
add_badge
end
end
end
def draw
@font.draw("Score <c=ffff00>#{@score}</c>", 10, 10, 1.0, 1.0, 1.0)
#fx = self.width/@background.width
#fy = self.height/@background.height
#@background.draw(0,0,0, fx, fy)
@background.draw(0,0,0)
@pieces.each do |obstacle|
obstacle.draw
end
end
def stop_game!
@running=false
end
def start_game
@score = 0
@stopwatch = Time.now
@player = Player.new(self)
@pieces = [@player]
add_obstacle
@running=true
end
def add_points(score)
@score += score
end
def add_obstacle
@pieces << Obstacle.new(self)
end
def add_thief
@pieces << Thief.new(self)
end
def add_badge
@pieces << Badge.new(self)
end
def add_bullet(player)
@pieces << Bullet.new(self, player)
end
end
window = MyGame.new
window.show