|
|
@@ -4,6 +4,7 @@ import (
|
|
|
_ "embed"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
+ "math/rand"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
@@ -11,6 +12,10 @@ import (
|
|
|
"github.com/gdamore/tcell/v3/color"
|
|
|
)
|
|
|
|
|
|
+type coords struct {
|
|
|
+ x, y int
|
|
|
+}
|
|
|
+
|
|
|
//go:embed assets/title/title_idle.txt
|
|
|
var title_idle string
|
|
|
|
|
|
@@ -170,6 +175,21 @@ func DrawCat(s tcell.Screen, x1,y1,x2,y2 int, style tcell.Style, direction rune)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// x1,y1 ------- x2,y1
|
|
|
+// | |
|
|
|
+// | |
|
|
|
+// x1,y2 ------- x2,y2
|
|
|
+func GetFoodPositions(x1,y1,x2,y2,level int) []*coords{
|
|
|
+ var res []*coords
|
|
|
+ nb_food := 5*level
|
|
|
+ for range nb_food {
|
|
|
+ x := rand.Intn((x2-1)-(x1+1)) + (x1+1)
|
|
|
+ y := y1
|
|
|
+ food_pos := coords{x: x, y: y}
|
|
|
+ res = append(res, &food_pos)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
@@ -204,6 +224,8 @@ func main() {
|
|
|
level := 1
|
|
|
direction := 'n'
|
|
|
x_offset := 0
|
|
|
+ new_level := true
|
|
|
+ var food_positions []*coords
|
|
|
game_loop:
|
|
|
go_to_menu := false
|
|
|
for {
|
|
|
@@ -216,39 +238,54 @@ game_loop:
|
|
|
|
|
|
length_factor := 5
|
|
|
width_factor := 4
|
|
|
+ // x1,y1 ------- x2,y1
|
|
|
+ // | |
|
|
|
+ // | |
|
|
|
+ // x1,y2 ------- x2,y2
|
|
|
|
|
|
- up_right_border := 3*xmax/width_factor
|
|
|
- width_map := up_right_border - (xmax/width_factor) + 1
|
|
|
+ x1 := xmax/width_factor
|
|
|
+ x2 := 3*xmax/width_factor
|
|
|
+ width_map := x2 - x1 + 1
|
|
|
|
|
|
- down_right_border := 4*ymax/length_factor
|
|
|
- length_map := down_right_border - (ymax/length_factor) + 1
|
|
|
+ y1 := ymax/length_factor
|
|
|
+ y2 := 4*ymax/length_factor
|
|
|
+ length_map := y2 - y1 + 1
|
|
|
|
|
|
// Draw UI
|
|
|
- drawText(s, xmax/width_factor, ymax/length_factor - 5, xmax, ymax, defStyle, "Use arrows or AD to move. Press q to return to Main Menu.")
|
|
|
- drawText(s, xmax/width_factor, ymax/length_factor - 3, xmax, ymax, defStyle, fmt.Sprintf("Level: %d", level))
|
|
|
- drawText(s, xmax/width_factor, ymax/length_factor - 2, xmax, ymax, defStyle, fmt.Sprintf("Score: %d", score))
|
|
|
+ drawText(s, x1, y1 - 5, xmax, ymax, defStyle, "Use arrows or AD to move. Press q to return to Main Menu.")
|
|
|
+ drawText(s, x1, y1 - 3, xmax, ymax, defStyle, fmt.Sprintf("Level: %d", level))
|
|
|
+ drawText(s, x1, y1 - 2, xmax, ymax, defStyle, fmt.Sprintf("Score: %d", score))
|
|
|
|
|
|
// Draw Game Map Borders
|
|
|
// upper border
|
|
|
for i := range(width_map){
|
|
|
- drawText(s,xmax/width_factor+i, ymax/length_factor-1, xmax, ymax, defStyle, "=")
|
|
|
+ drawText(s,x1+i, y1-1, xmax, ymax, defStyle, "=")
|
|
|
}
|
|
|
// bottom border
|
|
|
for i := range(width_map){
|
|
|
- drawText(s,xmax/width_factor+i, down_right_border, xmax, ymax, defStyle, "=")
|
|
|
+ drawText(s,x1+i, y2, xmax, ymax, defStyle, "=")
|
|
|
}
|
|
|
// left border
|
|
|
for i := range(length_map){
|
|
|
- drawText(s,xmax/width_factor, ymax/length_factor+i, xmax, ymax, defStyle, "|")
|
|
|
+ drawText(s,x1, y1+i, xmax, ymax, defStyle, "|")
|
|
|
}
|
|
|
// right border
|
|
|
for i := range(length_map){
|
|
|
- drawText(s,up_right_border, ymax/length_factor+i, xmax, ymax, defStyle, "|")
|
|
|
+ drawText(s,x2, y1+i, xmax, ymax, defStyle, "|")
|
|
|
}
|
|
|
|
|
|
- starting_point_x := (up_right_border + xmax/width_factor)/2 + x_offset
|
|
|
- DrawCat(s, starting_point_x, down_right_border - 3, xmax,ymax,defStyle, direction)
|
|
|
-
|
|
|
+ position_cat_x := (x2 + x1)/2 + x_offset
|
|
|
+ DrawCat(s, position_cat_x, y2 - 3, xmax,ymax,defStyle, direction)
|
|
|
+ if (new_level){
|
|
|
+ food_positions = GetFoodPositions(x1,y1,x2,y2,level)
|
|
|
+ new_level = false
|
|
|
+ }
|
|
|
+ for i, food_pos := range food_positions {
|
|
|
+ drawText(s,food_pos.x,food_pos.y,xmax,ymax,defStyle,"@")
|
|
|
+ if i == rand.Intn(len(food_positions)) && food_pos.y < (y2-1) {
|
|
|
+ food_pos.y += 1
|
|
|
+ }
|
|
|
+ }
|
|
|
s.Show()
|
|
|
|
|
|
ev := <- s.EventQ()
|
|
|
@@ -261,12 +298,12 @@ game_loop:
|
|
|
return
|
|
|
} else if (ev.Key() == tcell.KeyRight || ev.Str() == "d" || ev.Str() == "D") {
|
|
|
direction = 'r'
|
|
|
- if (starting_point_x + 1 < (up_right_border-6)){
|
|
|
+ if (position_cat_x + 1 < (x2-6)){
|
|
|
x_offset += 1
|
|
|
}
|
|
|
} else if (ev.Key() == tcell.KeyLeft || ev.Str() == "a" || ev.Str() == "A") {
|
|
|
direction = 'l'
|
|
|
- if (starting_point_x + 1 > (xmax/width_factor + 2)){
|
|
|
+ if (position_cat_x + 1 > (x1 + 2)){
|
|
|
x_offset -= 1
|
|
|
}
|
|
|
} else if (ev.Key() == tcell.KeyUp || ev.Str() == "w" || ev.Str() == "W") {
|