2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
2019-08-09 08:28:37 +03:00
|
|
|
// that can be found in the LICENSE file.
|
2019-09-20 19:05:14 +03:00
|
|
|
module main
|
|
|
|
|
2020-06-03 23:08:55 +03:00
|
|
|
import os
|
2019-06-22 21:20:28 +03:00
|
|
|
import rand
|
|
|
|
import time
|
2019-08-09 08:28:37 +03:00
|
|
|
import gx
|
2020-06-04 17:05:12 +03:00
|
|
|
import gg
|
2021-02-17 08:44:01 +03:00
|
|
|
// import sokol.sapp
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
const (
|
2021-02-02 20:37:43 +03:00
|
|
|
block_size = 20 // virtual pixels
|
2020-10-18 09:48:13 +03:00
|
|
|
field_height = 20 // # of blocks
|
|
|
|
field_width = 10
|
|
|
|
tetro_size = 4
|
|
|
|
win_width = block_size * field_width
|
|
|
|
win_height = block_size * field_height
|
|
|
|
timer_period = 250 // ms
|
|
|
|
text_size = 24
|
2020-05-22 18:36:09 +03:00
|
|
|
limit_thickness = 3
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-08-17 02:56:09 +03:00
|
|
|
const (
|
2019-08-09 18:53:34 +03:00
|
|
|
text_cfg = gx.TextCfg{
|
2020-10-18 09:48:13 +03:00
|
|
|
align: .left
|
|
|
|
size: text_size
|
|
|
|
color: gx.rgb(0, 0, 0)
|
2019-08-09 18:53:34 +03:00
|
|
|
}
|
2019-10-27 10:24:28 +03:00
|
|
|
over_cfg = gx.TextCfg{
|
2020-10-18 09:48:13 +03:00
|
|
|
align: .left
|
|
|
|
size: text_size
|
|
|
|
color: gx.white
|
2019-10-27 10:24:28 +03:00
|
|
|
}
|
2019-08-17 02:56:09 +03:00
|
|
|
)
|
2019-08-09 18:53:34 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
const (
|
|
|
|
// Tetros' 4 possible states are encoded in binaries
|
2020-10-14 22:51:16 +03:00
|
|
|
// 0000 0 0000 0 0000 0 0000 0 0000 0 0000 0
|
|
|
|
// 0000 0 0000 0 0000 0 0000 0 0011 3 0011 3
|
|
|
|
// 0110 6 0010 2 0011 3 0110 6 0001 1 0010 2
|
|
|
|
// 0110 6 0111 7 0110 6 0011 3 0001 1 0010 2
|
|
|
|
// There is a special case 1111, since 15 can't be used.
|
2020-10-18 09:48:13 +03:00
|
|
|
b_tetros = [
|
2019-06-22 21:20:28 +03:00
|
|
|
[66, 66, 66, 66],
|
|
|
|
[27, 131, 72, 232],
|
|
|
|
[36, 231, 36, 231],
|
|
|
|
[63, 132, 63, 132],
|
|
|
|
[311, 17, 223, 74],
|
|
|
|
[322, 71, 113, 47],
|
|
|
|
[1111, 9, 1111, 9],
|
|
|
|
]
|
|
|
|
// Each tetro has its unique color
|
2020-10-18 09:48:13 +03:00
|
|
|
colors = [
|
2021-01-17 07:28:09 +03:00
|
|
|
gx.rgb(0, 0, 0), /* unused ? */
|
|
|
|
gx.rgb(255, 242, 0), /* yellow quad */
|
|
|
|
gx.rgb(174, 0, 255), /* purple triple */
|
|
|
|
gx.rgb(60, 255, 0), /* green short topright */
|
|
|
|
gx.rgb(255, 0, 0), /* red short topleft */
|
|
|
|
gx.rgb(255, 180, 31), /* orange long topleft */
|
|
|
|
gx.rgb(33, 66, 255), /* blue long topright */
|
|
|
|
gx.rgb(74, 198, 255), /* lightblue longest */
|
2021-02-04 18:34:49 +03:00
|
|
|
gx.rgb(0, 170, 170),
|
2019-06-22 21:20:28 +03:00
|
|
|
]
|
2020-05-22 20:00:46 +03:00
|
|
|
background_color = gx.white
|
2020-10-18 09:48:13 +03:00
|
|
|
ui_color = gx.rgba(255, 0, 0, 210)
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2020-04-02 00:49:11 +03:00
|
|
|
// TODO: type Tetro [tetro_size]struct{ x, y int }
|
2019-06-22 21:20:28 +03:00
|
|
|
struct Block {
|
2020-10-18 09:48:13 +03:00
|
|
|
mut:
|
2019-06-22 21:20:28 +03:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2019-08-17 02:56:09 +03:00
|
|
|
enum GameState {
|
2020-10-18 09:48:13 +03:00
|
|
|
paused
|
|
|
|
running
|
|
|
|
gameover
|
2019-08-17 02:56:09 +03:00
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct Game {
|
2020-10-18 09:48:13 +03:00
|
|
|
mut:
|
2019-08-09 08:28:37 +03:00
|
|
|
// Score of the current game
|
2021-01-17 07:28:09 +03:00
|
|
|
score int
|
2020-10-02 16:42:05 +03:00
|
|
|
// Lines of the current game
|
2021-01-17 07:28:09 +03:00
|
|
|
lines int
|
2019-08-17 02:56:09 +03:00
|
|
|
// State of the current game
|
2021-01-17 07:28:09 +03:00
|
|
|
state GameState
|
2021-02-02 20:37:43 +03:00
|
|
|
// Block size in screen dimensions
|
|
|
|
block_size int = block_size
|
|
|
|
// Field margin
|
|
|
|
margin int
|
2019-06-22 21:20:28 +03:00
|
|
|
// Position of the current tetro
|
2021-01-17 07:28:09 +03:00
|
|
|
pos_x int
|
|
|
|
pos_y int
|
2019-06-22 21:20:28 +03:00
|
|
|
// field[y][x] contains the color of the block with (x,y) coordinates
|
|
|
|
// "-1" border is to avoid bounds checking.
|
|
|
|
// -1 -1 -1 -1
|
|
|
|
// -1 0 0 -1
|
|
|
|
// -1 0 0 -1
|
|
|
|
// -1 -1 -1 -1
|
2021-01-17 07:28:09 +03:00
|
|
|
field [][]int
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO: tetro Tetro
|
2021-01-17 07:28:09 +03:00
|
|
|
tetro []Block
|
2019-06-26 17:39:40 +03:00
|
|
|
// TODO: tetros_cache []Tetro
|
2021-01-17 07:28:09 +03:00
|
|
|
tetros_cache []Block
|
2019-06-22 21:20:28 +03:00
|
|
|
// Index of the current tetro. Refers to its color.
|
2021-01-17 07:28:09 +03:00
|
|
|
tetro_idx int
|
2020-10-02 16:37:00 +03:00
|
|
|
// Idem for the next tetro
|
2020-10-18 09:48:13 +03:00
|
|
|
next_tetro_idx int
|
2019-06-22 21:20:28 +03:00
|
|
|
// Index of the rotation (0-3)
|
2021-01-17 07:28:09 +03:00
|
|
|
rotation_idx int
|
2019-06-22 21:20:28 +03:00
|
|
|
// gg context for drawing
|
2021-01-17 07:28:09 +03:00
|
|
|
gg &gg.Context = voidptr(0)
|
|
|
|
font_loaded bool
|
2021-06-24 11:59:18 +03:00
|
|
|
show_ghost bool = true
|
2020-06-03 19:52:44 +03:00
|
|
|
// frame/time counters:
|
2021-01-17 07:28:09 +03:00
|
|
|
frame int
|
|
|
|
frame_old int
|
2021-07-20 11:17:08 +03:00
|
|
|
frame_sw time.StopWatch = time.new_stopwatch()
|
|
|
|
second_sw time.StopWatch = time.new_stopwatch()
|
2020-06-03 16:45:26 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:37:43 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:46:43 +03:00
|
|
|
[if showfps ?]
|
2020-07-24 13:29:47 +03:00
|
|
|
fn (mut game Game) showfps() {
|
2020-06-03 16:45:26 +03:00
|
|
|
game.frame++
|
2020-10-18 09:48:13 +03:00
|
|
|
last_frame_ms := f64(game.frame_sw.elapsed().microseconds()) / 1000.0
|
|
|
|
ticks := f64(game.second_sw.elapsed().microseconds()) / 1000.0
|
2020-06-03 19:52:44 +03:00
|
|
|
if ticks > 999.0 {
|
2020-10-18 09:48:13 +03:00
|
|
|
fps := f64(game.frame - game.frame_old) * ticks / 1000.0
|
2020-09-21 03:42:28 +03:00
|
|
|
$if debug {
|
|
|
|
eprintln('fps: ${fps:5.1f} | last frame took: ${last_frame_ms:6.3f}ms | frame: ${game.frame:6} ')
|
|
|
|
}
|
2020-06-03 16:45:26 +03:00
|
|
|
game.second_sw.restart()
|
|
|
|
game.frame_old = game.frame
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:29:47 +03:00
|
|
|
fn frame(mut game Game) {
|
2021-12-21 17:54:26 +03:00
|
|
|
if game.gg.frame & 15 == 0 {
|
|
|
|
game.update_game_state()
|
|
|
|
}
|
2021-02-02 20:37:43 +03:00
|
|
|
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)
|
2020-06-03 16:45:26 +03:00
|
|
|
game.frame_sw.restart()
|
2020-06-01 13:57:04 +03:00
|
|
|
game.gg.begin()
|
|
|
|
game.draw_scene()
|
2020-06-03 16:45:26 +03:00
|
|
|
game.showfps()
|
2020-06-03 19:52:44 +03:00
|
|
|
game.gg.end()
|
2020-06-01 13:57:04 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn main() {
|
2020-06-02 16:35:37 +03:00
|
|
|
mut game := &Game{
|
2020-06-03 16:45:26 +03:00
|
|
|
gg: 0
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
2021-01-17 07:28:09 +03:00
|
|
|
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(
|
2020-06-01 13:57:04 +03:00
|
|
|
bg_color: gx.white
|
|
|
|
width: win_width
|
|
|
|
height: win_height
|
|
|
|
create_window: true
|
2022-06-25 05:47:44 +03:00
|
|
|
window_title: 'V Tetris'
|
2020-06-01 13:57:04 +03:00
|
|
|
user_data: game
|
2020-06-03 23:08:55 +03:00
|
|
|
frame_fn: frame
|
2020-06-04 09:48:49 +03:00
|
|
|
event_fn: on_event
|
2020-10-18 09:48:13 +03:00
|
|
|
font_path: fpath // wait_events: true
|
2021-01-17 07:28:09 +03:00
|
|
|
)
|
2019-06-22 21:20:28 +03:00
|
|
|
game.init_game()
|
2020-06-01 13:57:04 +03:00
|
|
|
game.gg.run() // Run the render loop in the main thread
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) init_game() {
|
2019-07-19 14:27:44 +03:00
|
|
|
g.parse_tetros()
|
2022-02-23 13:36:14 +03:00
|
|
|
g.next_tetro_idx = rand.intn(b_tetros.len) or { 0 } // generate initial "next"
|
2019-06-22 21:20:28 +03:00
|
|
|
g.generate_tetro()
|
2020-06-04 17:53:19 +03:00
|
|
|
g.field = []
|
2019-06-22 21:20:28 +03:00
|
|
|
// Generate the field, fill it with 0's, add -1's on each edge
|
2020-10-18 09:48:13 +03:00
|
|
|
for _ in 0 .. field_height + 2 {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut row := [0].repeat(field_width + 2)
|
2020-10-18 09:48:13 +03:00
|
|
|
row[0] = -1
|
|
|
|
row[field_width + 1] = -1
|
2020-11-27 22:41:17 +03:00
|
|
|
g.field << row.clone()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
for j in 0 .. field_width + 2 {
|
2020-11-24 20:14:44 +03:00
|
|
|
g.field[0][j] = -1
|
|
|
|
g.field[field_height + 1][j] = -1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-09 08:28:37 +03:00
|
|
|
g.score = 0
|
2020-10-02 16:42:05 +03:00
|
|
|
g.lines = 0
|
2019-08-17 02:56:09 +03:00
|
|
|
g.state = .running
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) parse_tetros() {
|
2020-04-02 00:49:11 +03:00
|
|
|
for b_tetros0 in b_tetros {
|
|
|
|
for b_tetro in b_tetros0 {
|
2019-06-26 17:39:40 +03:00
|
|
|
for t in parse_binary_tetro(b_tetro) {
|
2019-06-22 21:20:28 +03:00
|
|
|
g.tetros_cache << t
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2021-12-21 17:54:26 +03:00
|
|
|
fn (mut g Game) update_game_state() {
|
|
|
|
if g.state == .running {
|
|
|
|
g.move_tetro()
|
|
|
|
g.delete_completed_lines()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 10:30:15 +03:00
|
|
|
fn (g &Game) draw_ghost() {
|
|
|
|
if g.state != .gameover && g.show_ghost {
|
|
|
|
pos_y := g.move_ghost()
|
2020-10-18 09:48:13 +03:00
|
|
|
for i in 0 .. tetro_size {
|
2020-10-02 10:30:15 +03:00
|
|
|
tetro := g.tetro[i]
|
2021-06-24 11:59:18 +03:00
|
|
|
g.draw_block_color(pos_y + tetro.y, g.pos_x + tetro.x, gx.rgba(125, 125, 225,
|
|
|
|
40))
|
2020-10-02 10:30:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g Game) move_ghost() int {
|
|
|
|
mut pos_y := g.pos_y
|
|
|
|
mut end := false
|
|
|
|
for !end {
|
|
|
|
for block in g.tetro {
|
|
|
|
y := block.y + pos_y + 1
|
|
|
|
x := block.x + g.pos_x
|
|
|
|
if g.field[y][x] != 0 {
|
|
|
|
end = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos_y++
|
|
|
|
}
|
|
|
|
return pos_y - 1
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:45:26 +03:00
|
|
|
fn (mut g Game) move_tetro() bool {
|
2019-06-22 21:20:28 +03:00
|
|
|
// Check each block in current tetro
|
|
|
|
for block in g.tetro {
|
|
|
|
y := block.y + g.pos_y + 1
|
|
|
|
x := block.x + g.pos_x
|
|
|
|
// Reached the bottom of the screen or another block?
|
2020-06-04 17:53:19 +03:00
|
|
|
if g.field[y][x] != 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
// The new tetro has no space to drop => end of the game
|
|
|
|
if g.pos_y < 2 {
|
2019-08-17 02:56:09 +03:00
|
|
|
g.state = .gameover
|
2020-06-03 16:45:26 +03:00
|
|
|
return false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// Drop it and generate a new one
|
|
|
|
g.drop_tetro()
|
|
|
|
g.generate_tetro()
|
2020-06-03 16:45:26 +03:00
|
|
|
return false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g.pos_y++
|
2020-06-03 16:45:26 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) move_right(dx int) bool {
|
2019-06-26 17:39:40 +03:00
|
|
|
// Reached left/right edge or another tetro?
|
2020-10-18 09:48:13 +03:00
|
|
|
for i in 0 .. tetro_size {
|
2019-06-22 21:20:28 +03:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
y := tetro.y + g.pos_y
|
|
|
|
x := tetro.x + g.pos_x + dx
|
2020-11-27 22:41:17 +03:00
|
|
|
if g.field[y][x] != 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
// Do not move
|
2019-08-09 08:28:37 +03:00
|
|
|
return false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g.pos_x += dx
|
2019-08-09 08:28:37 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) delete_completed_lines() {
|
2020-05-22 18:36:09 +03:00
|
|
|
for y := field_height; y >= 1; y-- {
|
2019-06-22 21:20:28 +03:00
|
|
|
g.delete_completed_line(y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) delete_completed_line(y int) {
|
2020-05-22 18:36:09 +03:00
|
|
|
for x := 1; x <= field_width; x++ {
|
2020-11-24 20:14:44 +03:00
|
|
|
if g.field[y][x] == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 08:28:37 +03:00
|
|
|
g.score += 10
|
2020-10-02 16:42:05 +03:00
|
|
|
g.lines++
|
2019-06-22 21:20:28 +03:00
|
|
|
// Move everything down by 1 position
|
|
|
|
for yy := y - 1; yy >= 1; yy-- {
|
2020-05-22 18:36:09 +03:00
|
|
|
for x := 1; x <= field_width; x++ {
|
2021-01-17 07:28:09 +03:00
|
|
|
g.field[yy + 1][x] = g.field[yy][x]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place a new tetro on top
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) generate_tetro() {
|
2019-06-22 21:20:28 +03:00
|
|
|
g.pos_y = 0
|
2020-05-22 18:36:09 +03:00
|
|
|
g.pos_x = field_width / 2 - tetro_size / 2
|
2020-10-02 16:37:00 +03:00
|
|
|
g.tetro_idx = g.next_tetro_idx
|
2022-02-23 13:36:14 +03:00
|
|
|
g.next_tetro_idx = rand.intn(b_tetros.len) or { 0 }
|
2019-06-26 17:39:40 +03:00
|
|
|
g.rotation_idx = 0
|
|
|
|
g.get_tetro()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
// Get the right tetro from cache
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) get_tetro() {
|
2020-04-02 00:49:11 +03:00
|
|
|
idx := g.tetro_idx * tetro_size * tetro_size + g.rotation_idx * tetro_size
|
2021-03-17 15:45:32 +03:00
|
|
|
g.tetro = g.tetros_cache[idx..idx + tetro_size].clone()
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-09-17 13:37:25 +03:00
|
|
|
// TODO mut
|
2020-07-24 13:29:47 +03:00
|
|
|
fn (mut g Game) drop_tetro() {
|
2020-10-18 09:48:13 +03:00
|
|
|
for i in 0 .. tetro_size {
|
2019-06-22 21:20:28 +03:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
x := tetro.x + g.pos_x
|
|
|
|
y := tetro.y + g.pos_y
|
|
|
|
// Remember the color of each block
|
2020-06-04 17:53:19 +03:00
|
|
|
g.field[y][x] = g.tetro_idx + 1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g &Game) draw_tetro() {
|
2020-10-18 09:48:13 +03:00
|
|
|
for i in 0 .. tetro_size {
|
2019-06-22 21:20:28 +03:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 16:37:00 +03:00
|
|
|
fn (g &Game) draw_next_tetro() {
|
|
|
|
if g.state != .gameover {
|
|
|
|
idx := g.next_tetro_idx * tetro_size * tetro_size
|
2020-11-26 20:40:31 +03:00
|
|
|
next_tetro := g.tetros_cache[idx..idx + tetro_size].clone()
|
2020-10-02 16:37:00 +03:00
|
|
|
pos_y := 0
|
|
|
|
pos_x := field_width / 2 - tetro_size / 2
|
2020-10-18 09:48:13 +03:00
|
|
|
for i in 0 .. tetro_size {
|
2020-10-02 16:37:00 +03:00
|
|
|
block := next_tetro[i]
|
|
|
|
g.draw_block_color(pos_y + block.y, pos_x + block.x, gx.rgb(220, 220, 220))
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 10:30:15 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
fn (g &Game) draw_block_color(i int, j int, color gx.Color) {
|
2021-12-23 14:31:25 +03:00
|
|
|
g.gg.draw_rect_filled(f32((j - 1) * g.block_size) + g.margin, f32((i - 1) * g.block_size),
|
2021-02-02 20:37:43 +03:00
|
|
|
f32(g.block_size - 1), f32(g.block_size - 1), color)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
fn (g &Game) draw_block(i int, j int, color_idx int) {
|
2020-10-02 10:30:15 +03:00
|
|
|
color := if g.state == .gameover { gx.gray } else { colors[color_idx] }
|
|
|
|
g.draw_block_color(i, j, color)
|
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (g &Game) draw_field() {
|
2020-05-22 18:36:09 +03:00
|
|
|
for i := 1; i < field_height + 1; i++ {
|
|
|
|
for j := 1; j < field_width + 1; j++ {
|
2020-11-24 20:14:44 +03:00
|
|
|
if g.field[i][j] > 0 {
|
|
|
|
g.draw_block(i, j, g.field[i][j])
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) draw_ui() {
|
2021-02-02 20:37:43 +03:00
|
|
|
ws := gg.window_size()
|
|
|
|
textsize := int(remap(text_size, 0, win_width, 0, ws.width))
|
2020-07-06 20:45:00 +03:00
|
|
|
g.gg.draw_text(1, 3, g.score.str(), text_cfg)
|
2020-10-02 16:42:05 +03:00
|
|
|
lines := g.lines.str()
|
2021-02-02 20:37:43 +03:00
|
|
|
g.gg.draw_text(ws.width - lines.len * textsize, 3, lines, text_cfg)
|
2020-07-06 20:45:00 +03:00
|
|
|
if g.state == .gameover {
|
2021-12-23 14:31:25 +03:00
|
|
|
g.gg.draw_rect_filled(0, ws.height / 2 - textsize, ws.width, 5 * textsize, ui_color)
|
2021-02-02 20:37:43 +03:00
|
|
|
g.gg.draw_text(1, ws.height / 2 + 0 * textsize, 'Game Over', over_cfg)
|
|
|
|
g.gg.draw_text(1, ws.height / 2 + 2 * textsize, 'Space to restart', over_cfg)
|
2020-07-06 20:45:00 +03:00
|
|
|
} else if g.state == .paused {
|
2021-12-23 14:31:25 +03:00
|
|
|
g.gg.draw_rect_filled(0, ws.height / 2 - textsize, ws.width, 5 * textsize, ui_color)
|
2021-02-02 20:37:43 +03:00
|
|
|
g.gg.draw_text(1, ws.height / 2 + 0 * textsize, 'Game Paused', text_cfg)
|
|
|
|
g.gg.draw_text(1, ws.height / 2 + 2 * textsize, 'SPACE to resume', text_cfg)
|
2019-08-09 08:28:37 +03:00
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
// g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
|
2019-08-09 08:28:37 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut g Game) draw_scene() {
|
2020-10-02 10:30:15 +03:00
|
|
|
g.draw_ghost()
|
2020-10-02 16:37:00 +03:00
|
|
|
g.draw_next_tetro()
|
2019-06-22 21:20:28 +03:00
|
|
|
g.draw_tetro()
|
|
|
|
g.draw_field()
|
2019-10-23 16:19:05 +03:00
|
|
|
g.draw_ui()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:16:10 +03:00
|
|
|
fn parse_binary_tetro(t_ int) []Block {
|
2019-08-09 08:28:37 +03:00
|
|
|
mut t := t_
|
2020-06-04 17:53:19 +03:00
|
|
|
mut res := [Block{}].repeat(4)
|
2019-06-22 21:20:28 +03:00
|
|
|
mut cnt := 0
|
2020-10-18 09:48:13 +03:00
|
|
|
horizontal := t == 9 // special case for the horizontal line
|
|
|
|
ten_powers := [1000, 100, 10, 1]
|
2019-06-22 21:20:28 +03:00
|
|
|
for i := 0; i <= 3; i++ {
|
|
|
|
// Get ith digit of t
|
2020-06-03 16:45:26 +03:00
|
|
|
p := ten_powers[i]
|
2019-12-08 01:39:27 +03:00
|
|
|
mut digit := t / p
|
2019-06-22 21:20:28 +03:00
|
|
|
t %= p
|
|
|
|
// Convert the digit to binary
|
|
|
|
for j := 3; j >= 0; j-- {
|
|
|
|
bin := digit % 2
|
|
|
|
digit /= 2
|
2020-04-02 00:49:11 +03:00
|
|
|
if bin == 1 || (horizontal && i == tetro_size - 1) {
|
2020-06-04 17:53:19 +03:00
|
|
|
res[cnt].x = j
|
|
|
|
res[cnt].y = i
|
2019-06-22 21:20:28 +03:00
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
fn on_event(e &gg.Event, mut game Game) {
|
2020-10-18 09:48:13 +03:00
|
|
|
// println('code=$e.char_code')
|
2020-06-01 13:57:04 +03:00
|
|
|
if e.typ == .key_down {
|
|
|
|
game.key_down(e.key_code)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-02-02 20:37:43 +03:00
|
|
|
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
|
|
|
|
}
|
2020-06-01 13:57:04 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
fn (mut game Game) key_down(key gg.KeyCode) {
|
2019-08-17 02:56:09 +03:00
|
|
|
// global keys
|
2019-10-27 10:13:40 +03:00
|
|
|
match key {
|
2020-06-01 13:57:04 +03:00
|
|
|
.escape {
|
2021-11-11 18:45:22 +03:00
|
|
|
game.gg.quit()
|
2019-10-27 10:13:40 +03:00
|
|
|
}
|
2020-06-01 13:57:04 +03:00
|
|
|
.space {
|
2019-10-27 10:13:40 +03:00
|
|
|
if game.state == .running {
|
|
|
|
game.state = .paused
|
|
|
|
} else if game.state == .paused {
|
|
|
|
game.state = .running
|
|
|
|
} else if game.state == .gameover {
|
|
|
|
game.init_game()
|
|
|
|
game.state = .running
|
|
|
|
}
|
2019-08-17 02:56:09 +03:00
|
|
|
}
|
2019-12-07 17:13:25 +03:00
|
|
|
else {}
|
2019-08-17 02:56:09 +03:00
|
|
|
}
|
|
|
|
if game.state != .running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// keys while game is running
|
2019-10-27 10:13:40 +03:00
|
|
|
match key {
|
2020-06-01 13:57:04 +03:00
|
|
|
.up {
|
2020-04-16 13:30:33 +03:00
|
|
|
// Rotate the tetro
|
2021-02-02 20:37:43 +03:00
|
|
|
game.rotate_tetro()
|
2019-08-09 08:28:37 +03:00
|
|
|
}
|
2020-06-01 13:57:04 +03:00
|
|
|
.left {
|
2020-04-16 13:30:33 +03:00
|
|
|
game.move_right(-1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-06-01 13:57:04 +03:00
|
|
|
.right {
|
2020-04-16 13:30:33 +03:00
|
|
|
game.move_right(1)
|
|
|
|
}
|
2020-06-01 13:57:04 +03:00
|
|
|
.down {
|
2020-04-16 13:30:33 +03:00
|
|
|
game.move_tetro() // drop faster when the player presses <down>
|
|
|
|
}
|
2020-06-03 16:45:26 +03:00
|
|
|
.d {
|
2020-10-18 09:48:13 +03:00
|
|
|
for game.move_tetro() {
|
|
|
|
}
|
2020-06-03 16:45:26 +03:00
|
|
|
}
|
2020-10-02 10:30:15 +03:00
|
|
|
.g {
|
|
|
|
game.show_ghost = !game.show_ghost
|
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
else {}
|
2019-10-27 10:13:40 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-02-02 20:37:43 +03:00
|
|
|
|
2021-12-26 14:02:51 +03:00
|
|
|
fn (mut game Game) touch_event(touch_point gg.TouchPoint) {
|
2021-02-02 20:37:43 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|