Skip to content

Commit

Permalink
score and level
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuechlin committed Mar 13, 2024
1 parent 8d774a8 commit a98f189
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 41 deletions.
55 changes: 32 additions & 23 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ package main

import (
"math/rand"
"strings"
)

const W = 10
const H = 20

var COLORS = map[int]int{
1: 1,
2: 2,
3: 4,
4: 5,
5: 6,
6: 7,
7: 8,
}

type Field struct {
Curr Current
Next int
Cells [W * H]int
}

Expand Down Expand Up @@ -129,12 +119,12 @@ func (f *Field) DropLines() int {
}

func (f *Field) Spawn() {
id := 1 + rand.Intn(7)
f.Curr = Current{
Id: id,
Id: f.Next,
X: W / 2,
Y: 0,
}
f.Next = 1 + rand.Intn(7)
}

func (f *Field) Collision(offsetX int, offsetY int, offsetR int) bool {
Expand All @@ -152,28 +142,47 @@ func (f *Field) Collision(offsetX int, offsetY int, offsetR int) bool {
return false
}

func (f *Field) String() []string {
func (f *Field) Display() []string {
lines := []string{}
lines = append(lines, "┌"+strings.Repeat("─", W*2)+"┐")
points := f.Curr.getPoints(0, 0, 0)
for y := 0; y < H; y++ {
value := "│"
odd := y%2 == 0
line := ""
for x := 0; x < W; x++ {
if contains(points, x, y) {
value += block(COLORS[f.Curr.Id])
line += block(f.Curr.Id)
} else {
i := idx(x, y)
val := f.Cells[i]
if val == 0 {
value += empty()
line += empty(odd)
} else {
value += block(COLORS[val])
line += block(val)
}
}
odd = !odd
}
lines = append(lines, line)
}
return container(lines, W*2)
}

func (f *Field) Preview() []string {
lines := []string{}
p := pieces[f.Next]
m := p.ToMatrix(0)
for y := 0; y < 2; y++ {
line := ""
for x := 0; x < 4; x++ {
if x >= p.Size {
line += empty(false)
} else if m[y][x] {
line += block(p.Id)
} else {
line += empty(false)
}
}
value += "│"
lines = append(lines, value)
lines = append(lines, line)
}
lines = append(lines, "└"+strings.Repeat("─", W*2)+"┘")
return lines
return container(lines, 8)
}
76 changes: 63 additions & 13 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ type Game struct {
ticker *time.Ticker
field Field
logs []string
level int
score int
}

func NewGame(done *chan bool) *Game {
game := Game{
done: done,
ticker: time.NewTicker(time.Second),
field: Field{
Curr: Current{
Id: 1,
X: W / 2,
Y: 0,
},
},
ticker: time.NewTicker(getSpeed(0)),
level: 0,
score: 0,
field: Field{},
}
game.field.Spawn()
game.field.Spawn()

go game.update()
go game.inputs()
Expand All @@ -33,11 +33,28 @@ func NewGame(done *chan bool) *Game {
}

func (g *Game) Draw() {
lines := g.field.String()
for i, line := range lines {
left := g.field.Display()
right := []string{
"# preview",
}
right = append(right, g.field.Preview()...)

right = append(right,
"",
"level: "+colored(5, fmt.Sprint(g.level)),
"score: "+colored(5, fmt.Sprint(g.score)),
)

// add logs
right = append(right, "", "# logs")
for _, l := range g.logs {
right = append(right, "- "+l)
}

for i, line := range left {
val := " " + line
if len(g.logs) > i {
val += " - " + g.logs[i]
if i < len(right) {
val += " " + right[i]
}
fmt.Println(val)
}
Expand Down Expand Up @@ -65,6 +82,39 @@ func (g *Game) log(msg string) {
}
}

func (g *Game) scoreLines(count int) {
switch count {
case 1:
g.score += 40 * (g.level + 1)
case 2:
g.score += 100 * (g.level + 1)
case 3:
g.score += 300 * (g.level + 1)
case 4:
g.score += 1200 * (g.level + 1)
}
g.log(fmt.Sprintf("%d lines dropped", count))
next := getLevel(g.score)
if next != g.level {
g.level = next
g.ticker.Reset(getSpeed(next))
g.log("level up")
}
}

func getLevel(score int) int {
return score / 200
}

func getSpeed(level int) time.Duration {
if level > 100 {
return 100 * time.Millisecond
}
p := float64(100-level) / 100
val := 900*p + 100
return time.Duration(val) * time.Millisecond
}

// ESC [
// 27 91 __
// - up 65
Expand Down Expand Up @@ -105,7 +155,7 @@ func (g *Game) moveDown() {
g.field.Place()
lines := g.field.DropLines()
if lines > 0 {
g.log(fmt.Sprintf("%d lines dropped", lines))
g.scoreLines(lines)
}
// Spawn next block
g.field.Spawn()
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os/exec"
"time"
)
Expand All @@ -16,7 +15,8 @@ func main() {

done := make(chan bool)
game := NewGame(&done)
fmt.Println("> gotris")

title()
game.Draw()

for {
Expand Down
2 changes: 1 addition & 1 deletion pieces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var pieceT = Piece{
},
}
var pieceL = Piece{
Id: PcJ,
Id: PcL,
Size: 3,
Cells: []bool{
false, false, true,
Expand Down
38 changes: 36 additions & 2 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import (
"strings"
)

var COLORS = map[int]int{
PcO: 3,
PcS: 2,
PcZ: 1,
PcT: 5,
PcL: 7,
PcJ: 4,
PcI: 6,
}

func title() {
var title = colored(COLORS[1], "G") +
colored(COLORS[2], "O") +
colored(COLORS[3], "T") +
colored(COLORS[4], "R") +
colored(COLORS[5], "I") +
colored(COLORS[6], "S")

fmt.Println("\n " + title)
}

const clear_line = "\033[1A\033[2K"

func clear_lines(lines int) {
Expand All @@ -16,8 +37,21 @@ func colored(color int, value string) string {
}

func block(color int) string {
return colored(color, "██")
return colored(COLORS[color], "██")
}
func empty() string {

func empty(odd bool) string {
if odd {
return "\033[48;5;0m \033[0m"
}
return " "
}

func container(lines []string, w int) []string {
for i := range lines {
lines[i] = "│" + lines[i] + "│"
}
lines = append([]string{"┌" + strings.Repeat("─", w) + "┐"}, lines...)
lines = append(lines, "└"+strings.Repeat("─", w)+"┘")
return lines
}

0 comments on commit a98f189

Please sign in to comment.