mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00

Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
80 lines
1.6 KiB
V
80 lines
1.6 KiB
V
// Build this example with
|
|
// v -live bounce.v
|
|
module main
|
|
|
|
import gg
|
|
import time
|
|
|
|
struct Game {
|
|
mut:
|
|
gg &gg.Context = unsafe { nil }
|
|
x int
|
|
y int
|
|
dy int
|
|
dx int
|
|
height int
|
|
width int
|
|
draw_fn voidptr
|
|
}
|
|
|
|
const window_width = 400
|
|
const window_height = 300
|
|
const width = 50
|
|
|
|
fn main() {
|
|
mut game := &Game{
|
|
dx: 2
|
|
dy: 2
|
|
height: window_height
|
|
width: window_width
|
|
draw_fn: 0
|
|
}
|
|
game.gg = gg.new_context(
|
|
width: window_width
|
|
height: window_height
|
|
font_size: 20
|
|
user_data: game
|
|
window_title: 'Hot code reloading demo'
|
|
create_window: true
|
|
frame_fn: frame
|
|
bg_color: gg.white
|
|
)
|
|
// window.onkeydown(key_down)
|
|
println('Starting the game loop...')
|
|
spawn game.run()
|
|
game.gg.run()
|
|
}
|
|
|
|
// Try uncommenting or changing the lines inside the live functions.
|
|
// Guess what will happen:
|
|
@[live]
|
|
fn frame(mut game Game) {
|
|
game.gg.begin()
|
|
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
|
game.gg.draw_rect_filled(game.x, game.y, width, width, gg.blue)
|
|
game.gg.draw_rect_filled(window_width - width - game.x + 10, 200 - game.y + width,
|
|
width, width, gg.rgb(228, 10, 55))
|
|
game.gg.draw_rect_filled(game.x - 25, 250 - game.y, width, width, gg.rgb(28, 240,
|
|
55))
|
|
game.gg.end()
|
|
}
|
|
|
|
@[live]
|
|
fn (mut game Game) update_model() {
|
|
speed := 2
|
|
game.x += speed * game.dx
|
|
game.y += speed * game.dy
|
|
if game.y >= game.height - width || game.y <= 0 {
|
|
game.dy = -game.dy
|
|
}
|
|
if game.x >= game.width - width || game.x <= 0 {
|
|
game.dx = -game.dx
|
|
}
|
|
}
|
|
|
|
fn (mut game Game) run() {
|
|
for {
|
|
game.update_model()
|
|
time.sleep(16 * time.millisecond) // 60fps
|
|
}
|
|
}
|