mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples/tetris: an extra collision check
This commit is contained in:
@@ -193,7 +193,7 @@ fn (g mut Game) move_tetro() {
|
|||||||
g.pos_y++
|
g.pos_y++
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g mut Game) move_right(dx int) {
|
fn (g mut Game) move_right(dx int) bool {
|
||||||
// Reached left/right edge or another tetro?
|
// Reached left/right edge or another tetro?
|
||||||
for i := 0; i < TetroSize; i++ {
|
for i := 0; i < TetroSize; i++ {
|
||||||
tetro := g.tetro[i]
|
tetro := g.tetro[i]
|
||||||
@@ -202,10 +202,11 @@ fn (g mut Game) move_right(dx int) {
|
|||||||
row := g.field[y]
|
row := g.field[y]
|
||||||
if row[x] != 0 {
|
if row[x] != 0 {
|
||||||
// Do not move
|
// Do not move
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.pos_x += dx
|
g.pos_x += dx
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g mut Game) delete_completed_lines() {
|
fn (g mut Game) delete_completed_lines() {
|
||||||
@@ -324,11 +325,17 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
|
|||||||
glfw.set_should_close(wnd, true)
|
glfw.set_should_close(wnd, true)
|
||||||
case glfw.KeyUp:
|
case glfw.KeyUp:
|
||||||
// Rotate the tetro
|
// Rotate the tetro
|
||||||
|
old_rotation_idx := game.rotation_idx
|
||||||
game.rotation_idx++
|
game.rotation_idx++
|
||||||
if game.rotation_idx == TetroSize {
|
if game.rotation_idx == TetroSize {
|
||||||
game.rotation_idx = 0
|
game.rotation_idx = 0
|
||||||
}
|
}
|
||||||
game.get_tetro()
|
game.get_tetro()
|
||||||
|
if !game.move_right(0) {
|
||||||
|
game.rotation_idx = old_rotation_idx
|
||||||
|
game.get_tetro()
|
||||||
|
}
|
||||||
|
|
||||||
if game.pos_x < 0 {
|
if game.pos_x < 0 {
|
||||||
game.pos_x = 1
|
game.pos_x = 1
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user