1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: tetris: better mobile device support (#8519)

This commit is contained in:
Larpon 2021-02-02 18:37:43 +01:00 committed by GitHub
parent 02bef1ae2b
commit c818ad97eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,7 @@ import gg
import sokol.sapp import sokol.sapp
const ( const (
block_size = 20 // pixels block_size = 20 // virtual pixels
field_height = 20 // # of blocks field_height = 20 // # of blocks
field_width = 10 field_width = 10
tetro_size = 4 tetro_size = 4
@ -88,6 +88,10 @@ mut:
lines int lines int
// State of the current game // State of the current game
state GameState state GameState
// Block size in screen dimensions
block_size int = block_size
// Field margin
margin int
// Position of the current tetro // Position of the current tetro
pos_x int pos_x int
pos_y int pos_y int
@ -119,6 +123,10 @@ mut:
second_sw time.StopWatch = time.new_stopwatch({}) second_sw time.StopWatch = time.new_stopwatch({})
} }
fn remap(v f32, min f32, max f32, new_min f32, new_max f32) f32 {
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
}
[if showfps] [if showfps]
fn (mut game Game) showfps() { fn (mut game Game) showfps() {
game.frame++ game.frame++
@ -135,6 +143,11 @@ fn (mut game Game) showfps() {
} }
fn frame(mut game Game) { fn frame(mut game Game) {
ws := gg.window_size()
bs := remap(block_size, 0, win_height, 0, ws.height)
m := (f32(ws.width) - bs * field_width) * 0.5
game.block_size = int(bs)
game.margin = int(m)
game.frame_sw.restart() game.frame_sw.restart()
game.gg.begin() game.gg.begin()
game.draw_scene() game.draw_scene()
@ -343,8 +356,8 @@ fn (g &Game) draw_next_tetro() {
} }
fn (g &Game) draw_block_color(i int, j int, color gx.Color) { fn (g &Game) draw_block_color(i int, j int, color gx.Color) {
g.gg.draw_rect(f32((j - 1) * block_size), f32((i - 1) * block_size), f32(block_size - 1), g.gg.draw_rect(f32((j - 1) * g.block_size) + g.margin, f32((i - 1) * g.block_size),
f32(block_size - 1), color) f32(g.block_size - 1), f32(g.block_size - 1), color)
} }
fn (g &Game) draw_block(i int, j int, color_idx int) { fn (g &Game) draw_block(i int, j int, color_idx int) {
@ -363,17 +376,19 @@ fn (g &Game) draw_field() {
} }
fn (mut g Game) draw_ui() { fn (mut g Game) draw_ui() {
ws := gg.window_size()
textsize := int(remap(text_size, 0, win_width, 0, ws.width))
g.gg.draw_text(1, 3, g.score.str(), text_cfg) g.gg.draw_text(1, 3, g.score.str(), text_cfg)
lines := g.lines.str() lines := g.lines.str()
g.gg.draw_text(win_width - lines.len * text_size, 3, lines, text_cfg) g.gg.draw_text(ws.width - lines.len * textsize, 3, lines, text_cfg)
if g.state == .gameover { if g.state == .gameover {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color) g.gg.draw_rect(0, ws.height / 2 - textsize, ws.width, 5 * textsize, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg) g.gg.draw_text(1, ws.height / 2 + 0 * textsize, 'Game Over', over_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg) g.gg.draw_text(1, ws.height / 2 + 2 * textsize, 'Space to restart', over_cfg)
} else if g.state == .paused { } else if g.state == .paused {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color) g.gg.draw_rect(0, ws.height / 2 - textsize, ws.width, 5 * textsize, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg) g.gg.draw_text(1, ws.height / 2 + 0 * textsize, 'Game Paused', text_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg) g.gg.draw_text(1, ws.height / 2 + 2 * textsize, 'SPACE to resume', text_cfg)
} }
// g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color) // g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
} }
@ -416,6 +431,28 @@ fn on_event(e &sapp.Event, mut game Game) {
if e.typ == .key_down { if e.typ == .key_down {
game.key_down(e.key_code) game.key_down(e.key_code)
} }
if e.typ == .touches_began || e.typ == .touches_moved {
if e.num_touches > 0 {
touch_point := e.touches[0]
game.touch_event(touch_point)
}
}
}
fn (mut game Game) rotate_tetro() {
old_rotation_idx := game.rotation_idx
game.rotation_idx++
if game.rotation_idx == tetro_size {
game.rotation_idx = 0
}
game.get_tetro()
if !game.move_right(0) {
game.rotation_idx = old_rotation_idx
game.get_tetro()
}
if game.pos_x < 0 {
// game.pos_x = 1
}
} }
fn (mut game Game) key_down(key sapp.KeyCode) { fn (mut game Game) key_down(key sapp.KeyCode) {
@ -443,19 +480,7 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
match key { match key {
.up { .up {
// Rotate the tetro // Rotate the tetro
old_rotation_idx := game.rotation_idx game.rotate_tetro()
game.rotation_idx++
if game.rotation_idx == tetro_size {
game.rotation_idx = 0
}
game.get_tetro()
if !game.move_right(0) {
game.rotation_idx = old_rotation_idx
game.get_tetro()
}
if game.pos_x < 0 {
// game.pos_x = 1
}
} }
.left { .left {
game.move_right(-1) game.move_right(-1)
@ -476,3 +501,18 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
else {} else {}
} }
} }
fn (mut game Game) touch_event(touch_point C.sapp_touchpoint) {
ws := gg.window_size()
tx := touch_point.pos_x
ty := touch_point.pos_y
if ty < f32(ws.height) * 0.5 {
game.rotate_tetro()
} else {
if tx <= f32(ws.width) * 0.5 {
game.move_right(-1)
} else {
game.move_right(1)
}
}
}