-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello-world.rb
87 lines (70 loc) · 1.55 KB
/
hello-world.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
require 'gosu'
WIDTH = 750
HEIGHT = 600
SPEED = 5
class Cupcake
attr_reader :caught, :missed
def initialize(window)
@x = rand(WIDTH)
@y = 0
@image = Gosu::Image.new(window, "media/cake.png", false)
@caught = 0
@missed = 0
end
def draw
@image.draw(@x, @y, 1)
end
def fall(player)
@y += SPEED
if @y >= HEIGHT-35
if Gosu::distance(@x, 0, player.x, 0) <= 50
@caught += 1
true
elsif Gosu::distance(@x, 0, player.x, 0) > 50
@missed += 1
true
end
@y = 0
@x = rand(WIDTH)
end
end
end
class Player
attr_accessor :x, :y
def initialize(window)
@image = Gosu::Image.new(window, "media/player.png", false)
@x, @y = 0, 450
end
def draw
@image.draw(@x, @y, 2)
end
def move_left
@x -=10 if @x > 10
end
def move_right
@x += 10 if @x < 700
end
end
class GameWindow < Gosu::Window
def initialize
super(800, 600, false)
self.caption = 'Hello World'
@background_image = Gosu::Image.new(self, "media/background.png", false)
@player = Player.new(self)
@cupcake = Cupcake.new(self)
@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
end
def update
@cupcake.fall(@player)
@player.move_left if button_down? Gosu::KbLeft
@player.move_right if button_down? Gosu::KbRight
end
def draw
@background_image.draw(0, 0, 0)
@player.draw
@cupcake.draw
@font.draw("Caught: #{@cupcake.caught}", 10, 10, 0)
@font.draw("Missed: #{@cupcake.missed}", 130, 10, 0)
end
end
GameWindow.new.show