tetris: enable to run on android, run v fmt (#8136)

pull/8159/head
Larpon 2021-01-17 05:28:09 +01:00 committed by GitHub
parent 371730f8a8
commit 4044abef0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 33 deletions

View File

@ -53,15 +53,15 @@ const (
] ]
// Each tetro has its unique color // Each tetro has its unique color
colors = [ colors = [
gx.rgb(0, 0, 0), // unused ? gx.rgb(0, 0, 0), /* unused ? */
gx.rgb(255, 242, 0), // yellow quad gx.rgb(255, 242, 0), /* yellow quad */
gx.rgb(174, 0, 255), // purple triple gx.rgb(174, 0, 255), /* purple triple */
gx.rgb(60, 255, 0), // green short topright gx.rgb(60, 255, 0), /* green short topright */
gx.rgb(255, 0, 0), // red short topleft gx.rgb(255, 0, 0), /* red short topleft */
gx.rgb(255, 180, 31), // orange long topleft gx.rgb(255, 180, 31), /* orange long topleft */
gx.rgb(33, 66, 255), // blue long topright gx.rgb(33, 66, 255), /* blue long topright */
gx.rgb(74, 198, 255), // lightblue longest gx.rgb(74, 198, 255), /* lightblue longest */
gx.rgb(0, 170, 170), // unused ? gx.rgb(0, 170, 170), /* unused ? */
] ]
background_color = gx.white background_color = gx.white
ui_color = gx.rgba(255, 0, 0, 210) ui_color = gx.rgba(255, 0, 0, 210)
@ -83,46 +83,42 @@ enum GameState {
struct Game { struct Game {
mut: mut:
// Score of the current game // Score of the current game
score int score int
// Lines of the current game // Lines of the current game
lines int lines int
// State of the current game // State of the current game
state GameState state GameState
// Position of the current tetro // Position of the current tetro
pos_x int pos_x int
pos_y int pos_y int
// field[y][x] contains the color of the block with (x,y) coordinates // field[y][x] contains the color of the block with (x,y) coordinates
// "-1" border is to avoid bounds checking. // "-1" border is to avoid bounds checking.
// -1 -1 -1 -1 // -1 -1 -1 -1
// -1 0 0 -1 // -1 0 0 -1
// -1 0 0 -1 // -1 0 0 -1
// -1 -1 -1 -1 // -1 -1 -1 -1
field [][]int field [][]int
// TODO: tetro Tetro // TODO: tetro Tetro
tetro []Block tetro []Block
// TODO: tetros_cache []Tetro // TODO: tetros_cache []Tetro
tetros_cache []Block tetros_cache []Block
// Index of the current tetro. Refers to its color. // Index of the current tetro. Refers to its color.
tetro_idx int tetro_idx int
// Idem for the next tetro // Idem for the next tetro
next_tetro_idx int next_tetro_idx int
// Index of the rotation (0-3) // Index of the rotation (0-3)
rotation_idx int rotation_idx int
// gg context for drawing // gg context for drawing
gg &gg.Context = voidptr(0) gg &gg.Context = voidptr(0)
font_loaded bool font_loaded bool
show_ghost bool show_ghost bool
// frame/time counters: // frame/time counters:
frame int frame int
frame_old int frame_old int
frame_sw time.StopWatch = time.new_stopwatch({}) frame_sw time.StopWatch = time.new_stopwatch({})
second_sw time.StopWatch = time.new_stopwatch({}) second_sw time.StopWatch = time.new_stopwatch({})
} }
const (
fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
)
[if showfps] [if showfps]
fn (mut game Game) showfps() { fn (mut game Game) showfps() {
game.frame++ game.frame++
@ -150,7 +146,11 @@ fn main() {
mut game := &Game{ mut game := &Game{
gg: 0 gg: 0
} }
game.gg = gg.new_context({ mut fpath := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf'))
$if android {
fpath = 'fonts/RobotoMono-Regular.ttf'
}
game.gg = gg.new_context(
bg_color: gx.white bg_color: gx.white
width: win_width width: win_width
height: win_height height: win_height
@ -161,7 +161,7 @@ fn main() {
frame_fn: frame frame_fn: frame
event_fn: on_event event_fn: on_event
font_path: fpath // wait_events: true font_path: fpath // wait_events: true
}) )
game.init_game() game.init_game()
go game.run() // Run the game loop in a new thread go game.run() // Run the game loop in a new thread
game.gg.run() // Run the render loop in the main thread game.gg.run() // Run the render loop in the main thread
@ -290,7 +290,7 @@ fn (mut g Game) delete_completed_line(y int) {
// Move everything down by 1 position // Move everything down by 1 position
for yy := y - 1; yy >= 1; yy-- { for yy := y - 1; yy >= 1; yy-- {
for x := 1; x <= field_width; x++ { for x := 1; x <= field_width; x++ {
g.field[yy + 1][x] = g.field[yy][x] g.field[yy + 1][x] = g.field[yy][x]
} }
} }
} }