-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.rb
More file actions
133 lines (109 loc) · 2.73 KB
/
Copy pathshared.rb
File metadata and controls
133 lines (109 loc) · 2.73 KB
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
124
125
126
127
128
129
130
131
132
class State
def update
end
def draw
end
def state_change way
end
end
class GameObject
attr_accessor :x, :y, :width, :height, :surface
def initialize x, y, surface
@x = x
@y = y
@surface = surface
@width = surface.width
@height = surface.height
end
def update
end
def draw screen
@surface.blit screen, [@x, @y]
end
def handle_event event
end
def center_x w
@x = w/2-@width/2
end
def center_y h
@y = h/2-@height/2
end
end
class Text < GameObject
def initialize x=0, y=0, text="Hello, World!", size=48
@font = Rubygame::TTF.new "Freshman.ttf", size
@text = text
super x, y, rerender_text()
end
def rerender_text
@width,@height = @font.size_text(@text)
@surface = @font.render(@text, true, [255, 255, 255])
end
def text
@text
end
def text= string
@text = string
rerender_text
end
end
class Selector < GameObject
attr_reader :choice
def initialize choice, *texts
@texts = texts
@choice = choice
@x = @x2 = 0
@y = @texts[choice].y+3
@speed = 10
@left = Rubygame::Surface.new [5, 40]
@right = Rubygame::Surface.new [5, 40]
@left.fill [255, 255, 255]
@right.fill [255, 255, 255]
super x, y, @left
end
def draw screen
@left.blit screen, [@x, @y]
@right.blit screen, [@x2, @y]
end
def update
x_target = @texts[@choice].x-10
x2_target = @texts[@choice].x+@texts[@choice].width+4
y_target = @texts[@choice].y+3
if @x != x_target and (@x-x_target).abs <= @speed
@x = x_target
elsif @x < x_target
@x += @speed
elsif @x > x_target
@x -= @speed
end
if @x2 != x2_target and (@x2-x2_target).abs <= @speed
@x2 = x2_target
elsif @x2 < x2_target
@x2 += @speed
elsif @x2 > x2_target
@x2 -= @speed
end
if @y != y_target and (@y-y_target).abs <= @speed
@y = y_target
elsif @y < y_target
@y += @speed
elsif @y > y_target
@y -= @speed
end
end
def handle_event ev
case ev
when Rubygame::KeyDownEvent
if ev.key == Rubygame::K_UP
unless @choice == 0
@choice -= 1
end
end
if ev.key == Rubygame::K_DOWN
unless @choice == @texts.length-1
@choice += 1
end
end
end
end
end