Game Development Reference
In-Depth Information
8
@draws
= 0
9
@buttons_down
= 0
10
end
11
12
def
update
13
@x
-= 1
if
button_down?(
Gosu
::
KbLeft
)
14
@x
+= 1
if
button_down?(
Gosu
::
KbRight
)
15
@y
-= 1
if
button_down?(
Gosu
::
KbUp
)
16
@y
+= 1
if
button_down?(
Gosu
::
KbDown
)
17
end
18
19
def
button_down
(
id
)
20
close
if
id
==
Gosu
::
KbEscape
21
@buttons_down
+= 1
22
end
23
24
def
button_up
(
id
)
25
@buttons_down
-= 1
26
end
27
28
def
needs_redraw?
29
@draws
== 0 ||
@buttons_down
> 0
30
end
31
32
def
draw
33
@draws
+= 1
34
@message
=
Gosu
::
Image
.
from_text(
35
self
, info,
Gosu
.
default_font_name,
30
)
36
@message
.
draw(
@x
,
@y
,
0
)
37
end
38
39
private
40
41
def
info
42
"[x:
#{
@x
}
;y:
#{
@y
}
;draws:
#{
@draws
}
]"
43
end
44
end
45
46
window
=
GameWindow
.
new
47
window
.
show
Run the program and try pressing arrow keys:
$
ruby 01-hello/hello_movement.rb