2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
2019-08-09 07:28:37 +02:00
|
|
|
// that can be found in the LICENSE file.
|
2019-06-23 04:21:30 +02:00
|
|
|
|
2019-09-20 18:05:14 +02:00
|
|
|
module main
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
import rand
|
|
|
|
import time
|
2019-08-09 07:28:37 +02:00
|
|
|
import gx
|
2019-06-22 20:20:28 +02:00
|
|
|
import gg
|
|
|
|
import glfw
|
|
|
|
import math
|
2019-08-09 07:28:37 +02:00
|
|
|
import freetype
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-04-16 12:30:33 +02:00
|
|
|
const (
|
|
|
|
k_up = glfw.key_up
|
|
|
|
k_left = glfw.key_left
|
|
|
|
k_right = glfw.key_right
|
|
|
|
k_down = glfw.key_down
|
|
|
|
k_escape = glfw.key_escape
|
|
|
|
k_space = glfw.key_space
|
|
|
|
)
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
const (
|
|
|
|
BlockSize = 20 // pixels
|
|
|
|
FieldHeight = 20 // # of blocks
|
|
|
|
FieldWidth = 10
|
2020-04-01 23:49:11 +02:00
|
|
|
tetro_size = 4
|
2019-06-22 20:20:28 +02:00
|
|
|
WinWidth = BlockSize * FieldWidth
|
|
|
|
WinHeight = BlockSize * FieldHeight
|
2019-06-27 19:02:47 +02:00
|
|
|
TimerPeriod = 250 // ms
|
2019-08-17 01:56:09 +02:00
|
|
|
TextSize = 12
|
2019-10-23 15:19:05 +02:00
|
|
|
LimitThickness = 3
|
2019-06-22 20:20:28 +02:00
|
|
|
)
|
|
|
|
|
2019-08-17 01:56:09 +02:00
|
|
|
const (
|
2019-08-09 17:53:34 +02:00
|
|
|
text_cfg = gx.TextCfg{
|
2020-04-02 01:45:22 +02:00
|
|
|
align:gx.align_left
|
2019-08-17 01:56:09 +02:00
|
|
|
size:TextSize
|
|
|
|
color:gx.rgb(0, 0, 0)
|
2019-08-09 17:53:34 +02:00
|
|
|
}
|
2019-10-27 08:24:28 +01:00
|
|
|
over_cfg = gx.TextCfg{
|
2020-04-02 01:45:22 +02:00
|
|
|
align:gx.align_left
|
2019-10-27 08:24:28 +01:00
|
|
|
size:TextSize
|
|
|
|
color:gx.White
|
|
|
|
}
|
2019-08-17 01:56:09 +02:00
|
|
|
)
|
2019-08-09 17:53:34 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
const (
|
|
|
|
// Tetros' 4 possible states are encoded in binaries
|
2020-04-01 23:49:11 +02:00
|
|
|
b_tetros = [
|
2019-06-22 20:20:28 +02:00
|
|
|
// 0000 0
|
|
|
|
// 0000 0
|
|
|
|
// 0110 6
|
|
|
|
// 0110 6
|
|
|
|
[66, 66, 66, 66],
|
|
|
|
// 0000 0
|
|
|
|
// 0000 0
|
|
|
|
// 0010 2
|
|
|
|
// 0111 7
|
|
|
|
[27, 131, 72, 232],
|
|
|
|
// 0000 0
|
|
|
|
// 0000 0
|
|
|
|
// 0011 3
|
|
|
|
// 0110 6
|
|
|
|
[36, 231, 36, 231],
|
|
|
|
// 0000 0
|
|
|
|
// 0000 0
|
|
|
|
// 0110 6
|
|
|
|
// 0011 3
|
|
|
|
[63, 132, 63, 132],
|
|
|
|
// 0000 0
|
|
|
|
// 0011 3
|
|
|
|
// 0001 1
|
|
|
|
// 0001 1
|
|
|
|
[311, 17, 223, 74],
|
|
|
|
// 0000 0
|
|
|
|
// 0011 3
|
|
|
|
// 0010 2
|
|
|
|
// 0010 2
|
|
|
|
[322, 71, 113, 47],
|
|
|
|
// Special case since 15 can't be used
|
|
|
|
// 1111
|
|
|
|
[1111, 9, 1111, 9],
|
|
|
|
]
|
|
|
|
// Each tetro has its unique color
|
|
|
|
Colors = [
|
2019-08-17 01:56:09 +02:00
|
|
|
gx.rgb(0, 0, 0), // unused ?
|
2019-12-12 12:36:01 +01:00
|
|
|
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
|
2019-08-17 01:56:09 +02:00
|
|
|
gx.rgb(0, 170, 170), // unused ?
|
2019-06-22 20:20:28 +02:00
|
|
|
]
|
2019-10-23 15:19:05 +02:00
|
|
|
|
|
|
|
BackgroundColor = gx.White
|
|
|
|
UIColor = gx.Red
|
2019-06-22 20:20:28 +02:00
|
|
|
)
|
|
|
|
|
2020-04-01 23:49:11 +02:00
|
|
|
// TODO: type Tetro [tetro_size]struct{ x, y int }
|
2019-06-22 20:20:28 +02:00
|
|
|
struct Block {
|
|
|
|
mut:
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:56:09 +02:00
|
|
|
enum GameState {
|
|
|
|
paused running gameover
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
struct Game {
|
|
|
|
mut:
|
2019-08-09 07:28:37 +02:00
|
|
|
// Score of the current game
|
|
|
|
score int
|
2019-08-17 01:56:09 +02:00
|
|
|
// State of the current game
|
|
|
|
state GameState
|
2019-06-22 20:20:28 +02:00
|
|
|
// Position of the current tetro
|
|
|
|
pos_x int
|
|
|
|
pos_y int
|
|
|
|
// 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
|
2019-08-09 07:28:37 +02:00
|
|
|
field [][]int
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO: tetro Tetro
|
|
|
|
tetro []Block
|
2019-06-26 16:39:40 +02:00
|
|
|
// TODO: tetros_cache []Tetro
|
|
|
|
tetros_cache []Block
|
2019-06-22 20:20:28 +02:00
|
|
|
// Index of the current tetro. Refers to its color.
|
|
|
|
tetro_idx int
|
|
|
|
// Index of the rotation (0-3)
|
|
|
|
rotation_idx int
|
|
|
|
// gg context for drawing
|
2019-09-03 08:04:11 +02:00
|
|
|
gg &gg.GG
|
2019-08-09 07:28:37 +02:00
|
|
|
// ft context for font drawing
|
2019-11-20 05:10:19 +01:00
|
|
|
ft &freetype.FreeType
|
2019-08-17 01:56:09 +02:00
|
|
|
font_loaded bool
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-10-12 05:03:15 +02:00
|
|
|
glfw.init_glfw()
|
2020-04-16 12:30:33 +02:00
|
|
|
|
|
|
|
gconfig := gg.Cfg {
|
2019-07-19 13:27:44 +02:00
|
|
|
width: WinWidth
|
|
|
|
height: WinHeight
|
|
|
|
use_ortho: true // This is needed for 2D drawing
|
|
|
|
create_window: true
|
|
|
|
window_title: 'V Tetris'
|
2020-04-04 12:56:31 +02:00
|
|
|
//window_user_ptr: game
|
2020-04-16 12:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fconfig := gg.Cfg{
|
2020-01-09 01:39:47 +01:00
|
|
|
width: WinWidth
|
|
|
|
height: WinHeight
|
|
|
|
use_ortho: true
|
2020-03-07 14:14:37 +01:00
|
|
|
font_path: '../assets/fonts/RobotoMono-Regular.ttf'
|
2020-01-09 01:39:47 +01:00
|
|
|
font_size: 18
|
|
|
|
scale: 2
|
|
|
|
window_user_ptr: 0
|
2020-04-16 12:30:33 +02:00
|
|
|
}
|
|
|
|
mut game := &Game{
|
|
|
|
gg: gg.new_context(gconfig)
|
|
|
|
ft: freetype.new_context(fconfig)
|
2019-08-09 07:28:37 +02:00
|
|
|
}
|
2019-08-22 23:00:31 +02:00
|
|
|
game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works
|
2019-06-22 20:20:28 +02:00
|
|
|
game.init_game()
|
2019-07-19 13:27:44 +02:00
|
|
|
game.gg.window.onkeydown(key_down)
|
2019-06-22 20:20:28 +02:00
|
|
|
go game.run() // Run the game loop in a new thread
|
2019-10-23 15:19:05 +02:00
|
|
|
gg.clear(BackgroundColor)
|
2020-01-09 01:39:47 +01:00
|
|
|
game.font_loaded = game.ft != 0
|
2019-06-22 20:20:28 +02:00
|
|
|
for {
|
2019-10-23 15:19:05 +02:00
|
|
|
gg.clear(BackgroundColor)
|
2019-06-22 20:20:28 +02:00
|
|
|
game.draw_scene()
|
2019-08-09 07:28:37 +02:00
|
|
|
game.gg.render()
|
2019-07-19 13:27:44 +02:00
|
|
|
if game.gg.window.should_close() {
|
|
|
|
game.gg.window.destroy()
|
2019-08-09 07:28:37 +02:00
|
|
|
return
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Game) init_game() {
|
2019-07-19 13:27:44 +02:00
|
|
|
g.parse_tetros()
|
2019-12-31 17:11:47 +01:00
|
|
|
rand.seed(time.now().unix)
|
2019-06-22 20:20:28 +02:00
|
|
|
g.generate_tetro()
|
2019-11-14 08:00:22 +01:00
|
|
|
g.field = [] // TODO: g.field = [][]int
|
2019-06-22 20:20:28 +02:00
|
|
|
// Generate the field, fill it with 0's, add -1's on each edge
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in 0..FieldHeight + 2 {
|
2019-09-16 00:55:54 +02:00
|
|
|
mut row := [0].repeat(FieldWidth + 2)
|
2019-06-22 20:20:28 +02:00
|
|
|
row[0] = - 1
|
|
|
|
row[FieldWidth + 1] = - 1
|
|
|
|
g.field << row
|
|
|
|
}
|
|
|
|
mut first_row := g.field[0]
|
|
|
|
mut last_row := g.field[FieldHeight + 1]
|
2020-02-24 17:55:16 +01:00
|
|
|
for j in 0..FieldWidth + 2 {
|
2019-06-22 20:20:28 +02:00
|
|
|
first_row[j] = - 1
|
|
|
|
last_row[j] = - 1
|
|
|
|
}
|
2019-08-09 07:28:37 +02:00
|
|
|
g.score = 0
|
2019-08-17 01:56:09 +02:00
|
|
|
g.state = .running
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Game) parse_tetros() {
|
2020-04-01 23:49:11 +02:00
|
|
|
for b_tetros0 in b_tetros {
|
|
|
|
for b_tetro in b_tetros0 {
|
2019-06-26 16:39:40 +02:00
|
|
|
for t in parse_binary_tetro(b_tetro) {
|
2019-06-22 20:20:28 +02:00
|
|
|
g.tetros_cache << t
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
|
|
|
|
fn (g mut Game) run() {
|
|
|
|
for {
|
2019-08-17 01:56:09 +02:00
|
|
|
if g.state == .running {
|
|
|
|
g.move_tetro()
|
|
|
|
g.delete_completed_lines()
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
glfw.post_empty_event() // force window redraw
|
2019-06-27 19:02:47 +02:00
|
|
|
time.sleep_ms(TimerPeriod)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Game) move_tetro() {
|
|
|
|
// 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?
|
|
|
|
// TODO: if g.field[y][x] != 0
|
2019-08-09 07:28:37 +02:00
|
|
|
//if g.field[y][x] != 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
row := g.field[y]
|
|
|
|
if row[x] != 0 {
|
|
|
|
// The new tetro has no space to drop => end of the game
|
|
|
|
if g.pos_y < 2 {
|
2019-08-17 01:56:09 +02:00
|
|
|
g.state = .gameover
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Drop it and generate a new one
|
|
|
|
g.drop_tetro()
|
|
|
|
g.generate_tetro()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.pos_y++
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:28:37 +02:00
|
|
|
fn (g mut Game) move_right(dx int) bool {
|
2019-06-26 16:39:40 +02:00
|
|
|
// Reached left/right edge or another tetro?
|
2020-04-01 23:49:11 +02:00
|
|
|
for i in 0..tetro_size {
|
2019-06-22 20:20:28 +02:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
y := tetro.y + g.pos_y
|
|
|
|
x := tetro.x + g.pos_x + dx
|
|
|
|
row := g.field[y]
|
|
|
|
if row[x] != 0 {
|
|
|
|
// Do not move
|
2019-08-09 07:28:37 +02:00
|
|
|
return false
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g.pos_x += dx
|
2019-08-09 07:28:37 +02:00
|
|
|
return true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Game) delete_completed_lines() {
|
|
|
|
for y := FieldHeight; y >= 1; y-- {
|
|
|
|
g.delete_completed_line(y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Game) delete_completed_line(y int) {
|
|
|
|
for x := 1; x <= FieldWidth; x++ {
|
|
|
|
f := g.field[y]
|
|
|
|
if f[x] == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:28:37 +02:00
|
|
|
g.score += 10
|
2019-06-22 20:20:28 +02:00
|
|
|
// Move everything down by 1 position
|
|
|
|
for yy := y - 1; yy >= 1; yy-- {
|
|
|
|
for x := 1; x <= FieldWidth; x++ {
|
|
|
|
mut a := g.field[yy + 1]
|
2019-07-25 14:13:35 +02:00
|
|
|
b := g.field[yy]
|
2019-06-22 20:20:28 +02:00
|
|
|
a[x] = b[x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place a new tetro on top
|
|
|
|
fn (g mut Game) generate_tetro() {
|
|
|
|
g.pos_y = 0
|
2020-04-01 23:49:11 +02:00
|
|
|
g.pos_x = FieldWidth / 2 - tetro_size / 2
|
|
|
|
g.tetro_idx = rand.next(b_tetros.len)
|
2019-06-26 16:39:40 +02:00
|
|
|
g.rotation_idx = 0
|
|
|
|
g.get_tetro()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:39:40 +02:00
|
|
|
// Get the right tetro from cache
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (g mut Game) get_tetro() {
|
2020-04-01 23:49:11 +02:00
|
|
|
idx := g.tetro_idx * tetro_size * tetro_size + g.rotation_idx * tetro_size
|
|
|
|
g.tetro = g.tetros_cache[idx..idx+tetro_size]
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2019-09-17 12:37:25 +02:00
|
|
|
// TODO mut
|
|
|
|
fn (g &Game) drop_tetro() {
|
2020-04-01 23:49:11 +02:00
|
|
|
for i in 0..tetro_size{
|
2019-06-22 20:20:28 +02:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
x := tetro.x + g.pos_x
|
|
|
|
y := tetro.y + g.pos_y
|
|
|
|
// Remember the color of each block
|
|
|
|
// TODO: g.field[y][x] = g.tetro_idx + 1
|
|
|
|
mut row := g.field[y]
|
|
|
|
row[x] = g.tetro_idx + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g &Game) draw_tetro() {
|
2020-04-01 23:49:11 +02:00
|
|
|
for i in 0..tetro_size {
|
2019-06-22 20:20:28 +02:00
|
|
|
tetro := g.tetro[i]
|
|
|
|
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:39:40 +02:00
|
|
|
fn (g &Game) draw_block(i, j, color_idx int) {
|
2019-10-27 08:24:28 +01:00
|
|
|
color := if g.state == .gameover { gx.Gray } else { Colors[color_idx] }
|
2019-06-26 16:39:40 +02:00
|
|
|
g.gg.draw_rect((j - 1) * BlockSize, (i - 1) * BlockSize,
|
2019-10-27 08:24:28 +01:00
|
|
|
BlockSize - 1, BlockSize - 1, color)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (g &Game) draw_field() {
|
|
|
|
for i := 1; i < FieldHeight + 1; i++ {
|
|
|
|
for j := 1; j < FieldWidth + 1; j++ {
|
|
|
|
f := g.field[i]
|
|
|
|
if f[j] > 0 {
|
|
|
|
g.draw_block(i, j, f[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 15:19:05 +02:00
|
|
|
fn (g mut Game) draw_ui() {
|
2019-10-23 16:02:39 +02:00
|
|
|
if g.font_loaded {
|
2019-10-27 08:24:28 +01:00
|
|
|
g.ft.draw_text(1, 3, g.score.str(), text_cfg)
|
2019-08-17 01:56:09 +02:00
|
|
|
if g.state == .gameover {
|
2019-10-23 15:19:05 +02:00
|
|
|
g.gg.draw_rect(0, WinHeight / 2 - TextSize, WinWidth,
|
2019-10-23 16:02:39 +02:00
|
|
|
5 * TextSize, UIColor)
|
2019-10-27 08:24:28 +01:00
|
|
|
g.ft.draw_text(1, WinHeight / 2 + 0 * TextSize, 'Game Over', over_cfg)
|
|
|
|
g.ft.draw_text(1, WinHeight / 2 + 2 * TextSize, 'Space to restart', over_cfg)
|
2019-08-17 01:56:09 +02:00
|
|
|
} else if g.state == .paused {
|
2019-10-23 15:19:05 +02:00
|
|
|
g.gg.draw_rect(0, WinHeight / 2 - TextSize, WinWidth,
|
2019-10-23 16:02:39 +02:00
|
|
|
5 * TextSize, UIColor)
|
2019-08-17 01:56:09 +02:00
|
|
|
g.ft.draw_text(1, WinHeight / 2 + 0 * TextSize, 'Game Paused', text_cfg)
|
|
|
|
g.ft.draw_text(1, WinHeight / 2 + 2 * TextSize, 'SPACE to resume', text_cfg)
|
|
|
|
}
|
2019-08-09 07:28:37 +02:00
|
|
|
}
|
2019-10-27 08:24:28 +01:00
|
|
|
//g.gg.draw_rect(0, BlockSize, WinWidth, LimitThickness, UIColor)
|
2019-08-09 07:28:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 21:28:27 +02:00
|
|
|
fn (g mut Game) draw_scene() {
|
2019-06-22 20:20:28 +02:00
|
|
|
g.draw_tetro()
|
|
|
|
g.draw_field()
|
2019-10-23 15:19:05 +02:00
|
|
|
g.draw_ui()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:16:10 +02:00
|
|
|
fn parse_binary_tetro(t_ int) []Block {
|
2019-08-09 07:28:37 +02:00
|
|
|
mut t := t_
|
2019-09-15 11:26:05 +02:00
|
|
|
res := [Block{}].repeat(4)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut cnt := 0
|
|
|
|
horizontal := t == 9// special case for the horizontal line
|
|
|
|
for i := 0; i <= 3; i++ {
|
|
|
|
// Get ith digit of t
|
|
|
|
p := int(math.pow(10, 3 - i))
|
2019-12-07 23:39:27 +01:00
|
|
|
mut digit := t / p
|
2019-06-22 20:20:28 +02:00
|
|
|
t %= p
|
|
|
|
// Convert the digit to binary
|
|
|
|
for j := 3; j >= 0; j-- {
|
|
|
|
bin := digit % 2
|
|
|
|
digit /= 2
|
2020-04-01 23:49:11 +02:00
|
|
|
if bin == 1 || (horizontal && i == tetro_size - 1) {
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO: res[cnt].x = j
|
|
|
|
// res[cnt].y = i
|
|
|
|
mut point := &res[cnt]
|
|
|
|
point.x = j
|
|
|
|
point.y = i
|
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this exposes the unsafe C interface, clean up
|
2019-06-26 16:39:40 +02:00
|
|
|
fn key_down(wnd voidptr, key, code, action, mods int) {
|
2019-06-22 20:20:28 +02:00
|
|
|
if action != 2 && action != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Fetch the game object stored in the user pointer
|
|
|
|
mut game := &Game(glfw.get_window_user_pointer(wnd))
|
2019-08-17 01:56:09 +02:00
|
|
|
// global keys
|
2019-10-27 08:13:40 +01:00
|
|
|
match key {
|
2020-04-16 12:30:33 +02:00
|
|
|
k_escape {
|
2019-10-27 08:13:40 +01:00
|
|
|
glfw.set_should_close(wnd, true)
|
|
|
|
}
|
2020-04-16 12:30:33 +02:00
|
|
|
k_space {
|
2019-10-27 08:13:40 +01: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 01:56:09 +02:00
|
|
|
}
|
2019-12-07 15:13:25 +01:00
|
|
|
else {}
|
2019-08-17 01:56:09 +02:00
|
|
|
}
|
2019-11-30 10:37:34 +01:00
|
|
|
|
2019-08-17 01:56:09 +02:00
|
|
|
if game.state != .running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// keys while game is running
|
2019-10-27 08:13:40 +01:00
|
|
|
match key {
|
2020-04-16 12:30:33 +02:00
|
|
|
k_up {
|
|
|
|
// Rotate the tetro
|
|
|
|
old_rotation_idx := game.rotation_idx
|
|
|
|
game.rotation_idx++
|
|
|
|
if game.rotation_idx == tetro_size {
|
|
|
|
game.rotation_idx = 0
|
|
|
|
}
|
2019-07-23 03:29:11 +02:00
|
|
|
game.get_tetro()
|
2020-04-16 12:30:33 +02:00
|
|
|
if !game.move_right(0) {
|
|
|
|
game.rotation_idx = old_rotation_idx
|
|
|
|
game.get_tetro()
|
|
|
|
}
|
|
|
|
if game.pos_x < 0 {
|
|
|
|
//game.pos_x = 1
|
|
|
|
}
|
2019-08-09 07:28:37 +02:00
|
|
|
}
|
2020-04-16 12:30:33 +02:00
|
|
|
k_left {
|
|
|
|
game.move_right(-1)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-04-16 12:30:33 +02:00
|
|
|
k_right {
|
|
|
|
game.move_right(1)
|
|
|
|
}
|
|
|
|
k_down {
|
|
|
|
game.move_tetro() // drop faster when the player presses <down>
|
|
|
|
}
|
|
|
|
else { }
|
2019-10-27 08:13:40 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|