2019-07-07 21:46:21 +02:00
|
|
|
// Build this example with
|
2019-07-15 17:32:00 +02:00
|
|
|
// v -live bounce.v
|
2019-07-07 21:46:21 +02:00
|
|
|
module main
|
|
|
|
|
|
|
|
import gx
|
|
|
|
import gg
|
|
|
|
import time
|
|
|
|
|
|
|
|
struct Game {
|
2019-11-06 04:57:04 +01:00
|
|
|
mut:
|
2019-09-04 14:10:18 +02:00
|
|
|
gg &gg.GG
|
2019-07-07 21:46:21 +02:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
dy int
|
|
|
|
dx int
|
|
|
|
height int
|
|
|
|
width int
|
|
|
|
draw_fn voidptr
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:26:36 +02:00
|
|
|
const (
|
|
|
|
window_width = 400
|
|
|
|
window_height = 300
|
|
|
|
width = 50
|
|
|
|
)
|
|
|
|
|
2019-07-07 21:46:21 +02:00
|
|
|
fn main() {
|
|
|
|
mut game := &Game{
|
2019-11-06 04:57:04 +01:00
|
|
|
gg: 0
|
|
|
|
dx: 2
|
|
|
|
dy: 2
|
2020-05-04 12:26:36 +02:00
|
|
|
height: window_height
|
|
|
|
width: window_width
|
2019-12-09 16:53:17 +01:00
|
|
|
draw_fn: 0
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
2020-06-04 16:53:19 +02:00
|
|
|
game.gg = gg.new_context({
|
2020-05-04 12:26:36 +02:00
|
|
|
width: window_width
|
|
|
|
height: window_height
|
2019-07-07 21:46:21 +02:00
|
|
|
font_size: 20
|
|
|
|
use_ortho: true
|
2020-06-04 16:53:19 +02:00
|
|
|
user_data: game
|
|
|
|
window_title: 'Hot code reloading demo'
|
|
|
|
create_window: true
|
|
|
|
frame_fn: frame
|
|
|
|
bg_color: gx.white
|
2019-07-07 21:46:21 +02:00
|
|
|
})
|
2020-06-04 16:53:19 +02:00
|
|
|
// window.onkeydown(key_down)
|
2019-07-17 02:55:18 +02:00
|
|
|
println('Starting the game loop...')
|
2019-07-07 21:46:21 +02:00
|
|
|
go game.run()
|
2020-06-04 16:53:19 +02:00
|
|
|
game.gg.run()
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 16:53:17 +01:00
|
|
|
// Try uncommenting or changing the lines inside the live functions.
|
|
|
|
// Guess what will happen:
|
2019-07-15 11:38:12 +02:00
|
|
|
[live]
|
2020-06-04 16:53:19 +02:00
|
|
|
fn frame (mut game Game) {
|
|
|
|
game.gg.begin()
|
|
|
|
game.gg.draw_rect(game.x, game.y, width, width, gx.blue)
|
2020-05-04 12:26:36 +02:00
|
|
|
game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, width, gx.rgb(228, 10, 55))
|
2020-05-03 17:59:11 +02:00
|
|
|
game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240, 55))
|
2020-06-04 16:53:19 +02:00
|
|
|
game.gg.end()
|
2019-12-09 16:53:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[live]
|
2020-05-02 01:36:29 +02:00
|
|
|
fn (mut game Game) update_model() {
|
2019-12-09 16:53:17 +01:00
|
|
|
speed := 2
|
|
|
|
game.x += speed * game.dx
|
2020-05-02 01:36:29 +02:00
|
|
|
game.y += speed * game.dy
|
2019-12-09 16:53:17 +01:00
|
|
|
if game.y >= game.height - width || game.y <= 0 {
|
2020-05-02 01:36:29 +02:00
|
|
|
game.dy = -game.dy
|
2019-12-09 16:53:17 +01:00
|
|
|
}
|
|
|
|
if game.x >= game.width - width || game.x <= 0 {
|
2020-05-02 01:36:29 +02:00
|
|
|
game.dx = -game.dx
|
2019-12-09 16:53:17 +01:00
|
|
|
}
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 01:36:29 +02:00
|
|
|
fn (mut game Game) run() {
|
2019-07-07 21:46:21 +02:00
|
|
|
for {
|
2020-05-02 01:36:29 +02:00
|
|
|
game.update_model()
|
2020-06-04 16:53:19 +02:00
|
|
|
//glfw.post_empty_event() // Refresh
|
2020-05-04 12:26:36 +02:00
|
|
|
time.sleep_ms(17) // 60fps
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 12:26:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|