2019-07-07 22:46:21 +03:00
|
|
|
// Build this example with
|
2019-07-15 18:32:00 +03:00
|
|
|
// v -live bounce.v
|
2019-07-07 22:46:21 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import gx
|
|
|
|
import gg
|
|
|
|
import time
|
|
|
|
|
|
|
|
struct Game {
|
2019-11-06 06:57:04 +03:00
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
gg &gg.Context = unsafe { nil }
|
2021-02-21 18:05:03 +03:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
dy int
|
|
|
|
dx int
|
|
|
|
height int
|
|
|
|
width int
|
|
|
|
draw_fn voidptr
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:26:36 +03:00
|
|
|
const (
|
2021-02-21 18:05:03 +03:00
|
|
|
window_width = 400
|
2020-05-04 13:26:36 +03:00
|
|
|
window_height = 300
|
2021-02-21 18:05:03 +03:00
|
|
|
width = 50
|
2020-05-04 13:26:36 +03:00
|
|
|
)
|
|
|
|
|
2019-07-07 22:46:21 +03:00
|
|
|
fn main() {
|
|
|
|
mut game := &Game{
|
2019-11-06 06:57:04 +03:00
|
|
|
gg: 0
|
|
|
|
dx: 2
|
|
|
|
dy: 2
|
2020-05-04 13:26:36 +03:00
|
|
|
height: window_height
|
|
|
|
width: window_width
|
2019-12-09 18:53:17 +03:00
|
|
|
draw_fn: 0
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
2021-02-21 18:05:03 +03:00
|
|
|
game.gg = gg.new_context(
|
2020-05-04 13:26:36 +03:00
|
|
|
width: window_width
|
|
|
|
height: window_height
|
2019-07-07 22:46:21 +03:00
|
|
|
font_size: 20
|
2020-06-04 17:53:19 +03:00
|
|
|
user_data: game
|
|
|
|
window_title: 'Hot code reloading demo'
|
|
|
|
create_window: true
|
|
|
|
frame_fn: frame
|
|
|
|
bg_color: gx.white
|
2021-02-21 18:05:03 +03:00
|
|
|
)
|
2020-06-04 17:53:19 +03:00
|
|
|
// window.onkeydown(key_down)
|
2019-07-17 03:55:18 +03:00
|
|
|
println('Starting the game loop...')
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn game.run()
|
2020-06-04 17:53:19 +03:00
|
|
|
game.gg.run()
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:53:17 +03:00
|
|
|
// Try uncommenting or changing the lines inside the live functions.
|
|
|
|
// Guess what will happen:
|
2019-07-15 12:38:12 +03:00
|
|
|
[live]
|
2021-02-21 18:05:03 +03:00
|
|
|
fn frame(mut game Game) {
|
2020-06-04 17:53:19 +03:00
|
|
|
game.gg.begin()
|
2020-08-27 07:46:18 +03:00
|
|
|
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
2021-12-23 14:31:25 +03:00
|
|
|
game.gg.draw_rect_filled(game.x, game.y, width, width, gx.blue)
|
|
|
|
game.gg.draw_rect_filled(window_width - width - game.x + 10, 200 - game.y + width,
|
|
|
|
width, width, gx.rgb(228, 10, 55))
|
|
|
|
game.gg.draw_rect_filled(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240,
|
|
|
|
55))
|
2020-06-04 17:53:19 +03:00
|
|
|
game.gg.end()
|
2019-12-09 18:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[live]
|
2020-05-02 02:36:29 +03:00
|
|
|
fn (mut game Game) update_model() {
|
2019-12-09 18:53:17 +03:00
|
|
|
speed := 2
|
|
|
|
game.x += speed * game.dx
|
2020-05-02 02:36:29 +03:00
|
|
|
game.y += speed * game.dy
|
2019-12-09 18:53:17 +03:00
|
|
|
if game.y >= game.height - width || game.y <= 0 {
|
2020-05-02 02:36:29 +03:00
|
|
|
game.dy = -game.dy
|
2019-12-09 18:53:17 +03:00
|
|
|
}
|
|
|
|
if game.x >= game.width - width || game.x <= 0 {
|
2020-05-02 02:36:29 +03:00
|
|
|
game.dx = -game.dx
|
2019-12-09 18:53:17 +03:00
|
|
|
}
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
|
|
|
|
2020-05-02 02:36:29 +03:00
|
|
|
fn (mut game Game) run() {
|
2019-07-07 22:46:21 +03:00
|
|
|
for {
|
2020-05-02 02:36:29 +03:00
|
|
|
game.update_model()
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(16 * time.millisecond) // 60fps
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
|
|
|
}
|