2048: fixed movement bug and show score at end (#6353)

pull/6356/head
Major Taylor 2020-09-12 02:25:44 -04:00 committed by GitHub
parent 60ecb7e4b6
commit 99a46c8657
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 8 deletions

View File

@ -170,6 +170,7 @@ enum LabelKind {
tile tile
victory victory
game_over game_over
score_end
} }
enum Direction { enum Direction {
@ -381,11 +382,17 @@ fn (mut app App) move(d Direction) {
.up { old.transpose().to_left().transpose() } .up { old.transpose().to_left().transpose() }
.down { old.transpose().hmirror().to_left().hmirror().transpose() } .down { old.transpose().hmirror().to_left().hmirror().transpose() }
} }
if old.shifts != new.shifts { // If the board hasn't changed, it's an illegal move, don't allow it.
app.moves++ for x in 0..4 {
app.board = new for y in 0..4 {
app.undo << old if old.field[x][y] != new.field[x][y] {
app.new_random_tile() app.moves++
app.board = new
app.undo << old
app.new_random_tile()
return
}
}
} }
} }
@ -424,6 +431,13 @@ fn (app &App) label_format(kind LabelKind) gx.TextCfg {
vertical_align: .middle vertical_align: .middle
size: app.ui.font_size * 2 size: app.ui.font_size * 2
} }
} .score_end {
return {
color: app.theme.padding_color
align: .center
vertical_align: .middle
size: app.ui.font_size
}
} }
} }
} }
@ -473,11 +487,13 @@ fn (app &App) draw() {
// TODO: Make transparency work in `gg` // TODO: Make transparency work in `gg`
if app.state == .over { if app.state == .over {
app.gg.draw_rect(0, 0, ww, wh, gx.rgba(15, 0, 0, 44)) app.gg.draw_rect(0, 0, ww, wh, gx.rgba(15, 0, 0, 44))
app.gg.draw_text(ww / 2, wh / 2, 'Game Over', app.label_format(.game_over)) app.gg.draw_text(ww / 2, wh / 3, 'Game Over', app.label_format(.game_over))
app.gg.draw_text(ww / 2, wh * 2 / 3, 'Score: $app.board.points', app.label_format(.score_end))
} }
if app.state == .victory { if app.state == .victory {
app.gg.draw_rect(0, 0, ww, wh, gx.rgba(0, 15, 0, 44)) app.gg.draw_rect(0, 0, ww, wh, gx.rgba(0, 15, 0, 44))
app.gg.draw_text(ww / 2, wh / 2, 'Victory!', app.label_format(.victory)) app.gg.draw_text(ww / 2, wh / 3, 'Victory!', app.label_format(.victory))
app.gg.draw_text(ww / 2, wh * 2 / 3, 'Score: $app.board.points', app.label_format(.score_end))
} }
} }