A Go version of the https://lcthw.dev/learn-code-the-hard-way/curseyou-python-rogue that makes a tiny Rogue in Go.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
3.7 KiB
206 lines
3.7 KiB
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"log"
|
|
"fmt"
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/gdamore/tcell/v2/encoding"
|
|
)
|
|
|
|
var dbg *log.Logger
|
|
|
|
const (
|
|
WALL = '#'
|
|
SPACE = '.'
|
|
RENDER = true
|
|
)
|
|
|
|
type Map [][]rune
|
|
type Paths [][]int
|
|
|
|
type Position struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
type Enemy struct {
|
|
hp int
|
|
pos Position
|
|
damage int
|
|
}
|
|
|
|
type Game struct {
|
|
screen tcell.Screen
|
|
level Map
|
|
player Enemy
|
|
status string
|
|
width int
|
|
height int
|
|
}
|
|
|
|
func (game *Game) DrawText(x int, y int, text string) {
|
|
for i, cell := range text {
|
|
game.screen.SetContent(x+i, y, cell, nil, tcell.StyleDefault)
|
|
}
|
|
}
|
|
|
|
func (game *Game) DrawStatus() {
|
|
game.DrawText(0, game.height, game.status)
|
|
|
|
hp := fmt.Sprintf("HP: %d", game.player.hp)
|
|
|
|
game.DrawText(game.width - len(hp), game.height, hp)
|
|
}
|
|
|
|
func (game *Game) Status(msg string) {
|
|
game.status = msg
|
|
}
|
|
|
|
func (game *Game) DrawEntity(symbol rune, pos Position, color tcell.Color) {
|
|
style := tcell.StyleDefault.Bold(true).Foreground(color)
|
|
game.screen.SetContent(pos.x, pos.y, symbol, nil, style)
|
|
}
|
|
|
|
func (game *Game) DrawMap() {
|
|
gray := tcell.StyleDefault.Foreground(tcell.ColorGray)
|
|
|
|
for y, line := range game.level {
|
|
for x, cell := range line {
|
|
if cell == SPACE {
|
|
game.screen.SetContent(x, y, cell, nil, gray)
|
|
} else {
|
|
game.screen.SetContent(x, y, cell, nil, tcell.StyleDefault)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (game *Game) Render() {
|
|
if !RENDER { return }
|
|
|
|
game.screen.Clear()
|
|
|
|
game.DrawMap()
|
|
|
|
game.DrawEntity('@', game.player.pos, tcell.ColorYellow)
|
|
|
|
game.DrawStatus()
|
|
|
|
game.screen.Show()
|
|
}
|
|
|
|
func (game *Game) Exit() {
|
|
if RENDER {
|
|
game.screen.Fini()
|
|
}
|
|
|
|
os.Exit(0)
|
|
}
|
|
|
|
func (game *Game) Occupied(x int, y int) bool {
|
|
return game.level[y][x] != SPACE
|
|
}
|
|
|
|
func (game *Game) NewMap() {
|
|
game.level = Map{
|
|
[]rune("#################"),
|
|
[]rune("#.#...#.........#"),
|
|
[]rune("#.#.###.#.###.#.#"),
|
|
[]rune("#.#.....#...#.#.#"),
|
|
[]rune("#.#.#######.#.###"),
|
|
[]rune("#.#...#...#.#...#"),
|
|
[]rune("#.###.###...###.#"),
|
|
[]rune("#...#.......#...#"),
|
|
[]rune("#.#.#########...#"),
|
|
[]rune("#.#.............#"),
|
|
[]rune("#################"),
|
|
}
|
|
}
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) {
|
|
target := Position{
|
|
game.player.pos.x + x_delta,
|
|
game.player.pos.y + y_delta,
|
|
}
|
|
|
|
if !game.Occupied(target.x, target.y) {
|
|
game.player.pos = target
|
|
}
|
|
}
|
|
|
|
func (game *Game) HandleKeys(ev *tcell.EventKey) bool {
|
|
switch ev.Key() {
|
|
case tcell.KeyEscape:
|
|
return false
|
|
case tcell.KeyUp:
|
|
game.MovePlayer(0, -1)
|
|
case tcell.KeyDown:
|
|
game.MovePlayer(0, 1)
|
|
case tcell.KeyRight:
|
|
game.MovePlayer(1, 0)
|
|
case tcell.KeyLeft:
|
|
game.MovePlayer(-1, 0)
|
|
}
|
|
|
|
switch ev.Rune() {
|
|
case 'q':
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (game *Game) HandleEvents() bool {
|
|
if !RENDER { return false }
|
|
|
|
switch ev := game.screen.PollEvent().(type) {
|
|
case *tcell.EventResize:
|
|
game.screen.Sync()
|
|
case *tcell.EventKey:
|
|
return game.HandleKeys(ev)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (game *Game) InitScreen() {
|
|
encoding.Register()
|
|
|
|
var err error
|
|
game.screen, err = tcell.NewScreen()
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
err = game.screen.Init()
|
|
if err != nil { log.Fatal(err) }
|
|
}
|
|
|
|
func NewGame(width int, height int) (*Game) {
|
|
var game Game
|
|
|
|
game.width = width
|
|
game.height = height
|
|
game.level = make(Map, height, height)
|
|
game.player = Enemy{20, Position{1,1}, 4}
|
|
|
|
return &game
|
|
}
|
|
|
|
func main() {
|
|
out, err := os.Create("debug.log")
|
|
if err != nil { log.Fatal(err) }
|
|
dbg = log.New(out, "", log.LstdFlags)
|
|
|
|
game := NewGame(17, 11)
|
|
err := game.InitScreen()
|
|
|
|
game.NewMap()
|
|
game.Render()
|
|
|
|
for game.HandleEvents() {
|
|
game.Render()
|
|
}
|
|
|
|
game.Exit()
|
|
}
|
|
|