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

examples/tetris: minor fixes

This commit is contained in:
Alexander Medvednikov 2019-04-16 04:28:52 +02:00
parent ae920dda25
commit 4225f72204

View File

@ -74,8 +74,8 @@ const (
struct Game { struct Game {
// Position of the dropping tetromino // Position of the dropping tetromino
posX int pos_x int
posY 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
@ -87,9 +87,9 @@ struct Game {
// TODO: tetro Tetro // TODO: tetro Tetro
tetro []Block tetro []Block
// Index of the dropping tetromino. Refers to its color. // Index of the dropping tetromino. Refers to its color.
tetroIdx int tetro_idx int
// Index of the rotation (0-3) // Index of the rotation (0-3)
rotationIdx int rotation_idx int
// gg context for drawing // gg context for drawing
gg *gg.GG gg *gg.GG
} }
@ -156,14 +156,14 @@ fn (g mut Game) move_tetro() {
// Check each block in the dropping tetro // Check each block in the dropping tetro
for i := 0; i < TETRO_SIZE; i++ { for i := 0; i < TETRO_SIZE; i++ {
tetro := g.tetro[i] tetro := g.tetro[i]
y := tetro.y + g.posY + 1 y := tetro.y + g.pos_y + 1
x := tetro.x + g.posX x := tetro.x + g.pos_x
// Reached the bottom of the screen or another block? // Reached the bottom of the screen or another block?
// TODO: if g.field[y][x] != 0 // TODO: if g.field[y][x] != 0
row := g.field[y] row := g.field[y]
if row[x] != 0 { if row[x] != 0 {
// The new tetro has no space to drop => end of the game // The new tetro has no space to drop => end of the game
if g.posY < 2 { if g.pos_y < 2 {
g.init_game() g.init_game()
return return
} }
@ -173,22 +173,22 @@ fn (g mut Game) move_tetro() {
return return
} }
} }
g.posY++ g.pos_y++
} }
fn (g mut Game) move_right(dx int) { fn (g mut Game) move_right(dx int) {
for i := 0; i < TETRO_SIZE; i++ { for i := 0; i < TETRO_SIZE; i++ {
// Reached left/right edges? // Reached left/right edges?
tetro := g.tetro[i] tetro := g.tetro[i]
y := tetro.y + g.posY y := tetro.y + g.pos_y
x := tetro.x + g.posX + dx x := tetro.x + g.pos_x + dx
row := g.field[y] row := g.field[y]
if row[x] != 0 { if row[x] != 0 {
// Do not move // Do not move
return return
} }
} }
g.posX += dx g.pos_x += dx
} }
fn (g mut Game) delete_completed_lines() { fn (g mut Game) delete_completed_lines() {
@ -216,30 +216,30 @@ fn (g mut Game) delete_completed_line(y int) {
// Place a new tetro on top // Place a new tetro on top
fn (g mut Game) generate_tetro() { fn (g mut Game) generate_tetro() {
g.posY = 0 g.pos_y = 0
g.posX = FIELD_WIDTH / 2 - TETRO_SIZE / 2 g.pos_x = FIELD_WIDTH / 2 - TETRO_SIZE / 2
g.tetroIdx = rand.next(B_TETROS.len) g.tetro_idx = rand.next(B_TETROS.len)
g.rotationIdx = 0 g.rotation_idx = 0
b := B_TETROS[g.tetroIdx] b := B_TETROS[g.tetro_idx]
g.tetro = parse_binary_tetro(b[0]) g.tetro = parse_binary_tetro(b[0])
} }
fn (g mut Game) drop_tetro() { fn (g mut Game) drop_tetro() {
for i := 0; i < TETRO_SIZE; i++ { for i := 0; i < TETRO_SIZE; i++ {
tetro := g.tetro[i] tetro := g.tetro[i]
x := tetro.x + g.posX x := tetro.x + g.pos_x
y := tetro.y + g.posY y := tetro.y + g.pos_y
// Remember the color of each block // Remember the color of each block
// TODO: g.field[y][x] = g.tetroIdx + 1 // TODO: g.field[y][x] = g.tetro_idx + 1
mut row := g.field[y] mut row := g.field[y]
row[x] = g.tetroIdx + 1 row[x] = g.tetro_idx + 1
} }
} }
fn (g &Game) draw_tetro() { fn (g &Game) draw_tetro() {
for i := 0; i < TETRO_SIZE; i++ { for i := 0; i < TETRO_SIZE; i++ {
tetro := g.tetro[i] tetro := g.tetro[i]
g.draw_block(g.posY + tetro.y, g.posX + tetro.x, g.tetroIdx + 1) g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
} }
} }
@ -291,7 +291,7 @@ fn parse_binary_tetro(t int) []Block {
} }
// TODO: this exposes the unsafe C interface, clean up // TODO: this exposes the unsafe C interface, clean up
fn key_down(wnd *glfw.Window, key int, code int, action, mods int) { fn key_down(wnd voidptr, key int, code int, action, mods int) {
if action != 2 && action != 1 { if action != 2 && action != 1 {
return return
} }
@ -300,15 +300,15 @@ fn key_down(wnd *glfw.Window, key int, code int, action, mods int) {
switch key { switch key {
case GLFW_KEY_UP: case GLFW_KEY_UP:
// Rotate the tetro // Rotate the tetro
game.rotationIdx++ game.rotation_idx++
if game.rotationIdx == TETRO_SIZE { if game.rotation_idx == TETRO_SIZE {
game.rotationIdx = 0 game.rotation_idx = 0
} }
t := B_TETROS[game.tetroIdx] t := B_TETROS[game.tetro_idx]
// game.tetro = parse_binary_tetro(B_TETROS[game.tetroIdx][game.rotationIdx]) // game.tetro = parse_binary_tetro(B_TETROS[game.tetro_idx][game.rotation_idx])
game.tetro = parse_binary_tetro(t[game.rotationIdx]) game.tetro = parse_binary_tetro(t[game.rotation_idx])
if game.posX < 0 { if game.pos_x < 0 {
game.posX = 1 game.pos_x = 1
} }
case GLFW_KEY_LEFT: case GLFW_KEY_LEFT:
game.move_right(-1) game.move_right(-1)