snipplets.dev/code/V/example_window/game.v

113 lines
2.0 KiB
Coq
Raw Normal View History

2023-07-02 19:40:27 +03:00
import os
import gg
import gx
import sokol.sapp
const (
win_width = 512
win_height = 512
)
struct Game {
mut:
2023-07-02 21:16:14 +03:00
gg &gg.Context = unsafe { nil }
player Player
bg_texture BgTexture
}
struct BgTexture {
mut:
image gg.Image
pos_x f32
pos_y f32
2023-07-02 19:40:27 +03:00
}
struct Player {
mut:
pos_x int
pos_y int
lives int
image gg.Image
}
fn frame(mut game Game) {
2023-07-02 21:16:14 +03:00
game.update()
game.draw()
2023-07-02 19:40:27 +03:00
}
fn main() {
print('Loading... ')
mut game := &Game{}
mut font_path := os.resource_abs_path(os.join_path('..', '..', '.assets', 'monogram-extended.ttf'))
game.gg = gg.new_context(
bg_color: gx.hex(0x2A2A3AFF)
width: win_width
height: win_height
create_window: true
enable_dragndrop: false
window_title: 'Runner'
font_path: font_path
user_data: game
frame_fn: frame
event_fn: on_event
)
game.player = &Player{
pos_x: 10
pos_y: 10
lives: 3
}
2023-07-02 21:16:14 +03:00
game.bg_texture = &BgTexture{
pos_x: 0
pos_y: 0
}
game.bg_texture.image = game.gg.create_image(os.resource_abs_path(os.join_path('..',
'..', '.assets', 'fondo para itchio.png'))) or { panic(err) }
2023-07-02 19:44:00 +03:00
game.player.image = game.gg.create_image(os.resource_abs_path(os.join_path('..', '..',
'.assets', 'v-logo.png'))) or { panic(err) }
2023-07-02 19:40:27 +03:00
println('OK!\n')
println('High DPI: ${gg.high_dpi()}')
println('Window size: ${gg.window_size().width}x${gg.window_size().height}')
println('Screen size: ${gg.screen_size().width}x${gg.screen_size().height}')
println('Fullscreen: ${gg.is_fullscreen()}\n')
game.gg.run()
println('\nBye!')
}
fn (mut game Game) key_down(key gg.KeyCode) {
match key {
2023-07-02 19:44:00 +03:00
// vfmt off
.escape { game.gg.quit() }
// vfmt on
2023-07-02 19:40:27 +03:00
.f11 {
sapp.toggle_fullscreen()
println('Set fullscreen: ${gg.is_fullscreen()}')
}
2023-07-02 19:44:00 +03:00
// vfmt off
.left { game.player.pos_x -= 2 }
.right { game.player.pos_x += 2 }
.up { game.player.pos_y -= 2 }
.down { game.player.pos_y += 2 }
// vfmt on
2023-07-02 19:40:27 +03:00
else {}
}
}
fn on_event(e &gg.Event, mut game Game) {
// println('code=$e.char_code')
// println('code=$e.key_code')
match e.typ {
.key_down {
game.key_down(e.key_code)
}
else {}
}
}