|
|
@ -8,52 +8,114 @@ import ( |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func DisplayMap(screen tcell.Screen, level []string) { |
|
|
|
|
|
|
|
screen.Clear() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for y, line := range level { |
|
|
|
type Game struct { |
|
|
|
for x, cell := range line { |
|
|
|
screen tcell.Screen |
|
|
|
|
|
|
|
level []string |
|
|
|
|
|
|
|
player_x int |
|
|
|
|
|
|
|
player_y int |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Render() { |
|
|
|
var comb []rune |
|
|
|
var comb []rune |
|
|
|
screen.SetContent(x, y, cell, comb, tcell.StyleDefault) |
|
|
|
game.screen.Clear() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for y, line := range game.level { |
|
|
|
|
|
|
|
for x, cell := range line { |
|
|
|
|
|
|
|
game.screen.SetContent(x, y, cell, comb, tcell.StyleDefault) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
screen.Show() |
|
|
|
game.screen.SetContent(game.player_x, game.player_y, '@', comb, tcell.StyleDefault) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.screen.Show() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
func (game *Game) Exit() { |
|
|
|
func main() { |
|
|
|
game.screen.Fini() |
|
|
|
level := []string{ |
|
|
|
os.Exit(0) |
|
|
|
"####################", |
|
|
|
} |
|
|
|
"###....#############", |
|
|
|
|
|
|
|
"###....#############", |
|
|
|
func (game *Game) Occupied(x int, y int) bool { |
|
|
|
"###...........######", |
|
|
|
return game.level[y][x] != '.' |
|
|
|
"#############.######", |
|
|
|
} |
|
|
|
"#############....###", |
|
|
|
|
|
|
|
"#############....###", |
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
"####################", |
|
|
|
if !game.Occupied(game.player_x + x_delta, game.player_y + y_delta) { |
|
|
|
|
|
|
|
game.player_x += x_delta |
|
|
|
|
|
|
|
game.player_y += y_delta |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HandleKeys(ev *tcell.EventKey) { |
|
|
|
|
|
|
|
switch ev.Key() { |
|
|
|
|
|
|
|
case tcell.KeyEscape: |
|
|
|
|
|
|
|
game.Exit() |
|
|
|
|
|
|
|
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': |
|
|
|
|
|
|
|
game.Exit() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HandleEvents() { |
|
|
|
|
|
|
|
switch ev := game.screen.PollEvent().(type) { |
|
|
|
|
|
|
|
case *tcell.EventResize: |
|
|
|
|
|
|
|
game.screen.Sync() |
|
|
|
|
|
|
|
case *tcell.EventKey: |
|
|
|
|
|
|
|
game.HandleKeys(ev) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func MakeGame() (*Game) { |
|
|
|
|
|
|
|
var game Game |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
encoding.Register() |
|
|
|
encoding.Register() |
|
|
|
|
|
|
|
|
|
|
|
screen, err := tcell.NewScreen() |
|
|
|
game.screen, err = tcell.NewScreen() |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
err = screen.Init(); |
|
|
|
err = game.screen.Init() |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
DisplayMap(screen, level) |
|
|
|
game.player_x = 1 |
|
|
|
|
|
|
|
game.player_y = 1 |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
return &game |
|
|
|
switch ev := screen.PollEvent().(type) { |
|
|
|
} |
|
|
|
case *tcell.EventResize: |
|
|
|
|
|
|
|
screen.Sync() |
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
DisplayMap(screen, level) |
|
|
|
func main() { |
|
|
|
case *tcell.EventKey: |
|
|
|
game := MakeGame() |
|
|
|
if ev.Key() == tcell.KeyEscape { |
|
|
|
|
|
|
|
screen.Fini() |
|
|
|
game.level = []string{ |
|
|
|
os.Exit(0) |
|
|
|
"###########################", |
|
|
|
} |
|
|
|
"#...#.....................#", |
|
|
|
|
|
|
|
"#.###.#########.#########.#", |
|
|
|
|
|
|
|
"#.....#.......#...#.....#.#", |
|
|
|
|
|
|
|
"#######.#####.#####.###.#.#", |
|
|
|
|
|
|
|
"#.....#.#.......#...#...#.#", |
|
|
|
|
|
|
|
"#.###.#.#.....#.#.#.#.###.#", |
|
|
|
|
|
|
|
"#...#.#.#.....#.#.#.#.....#", |
|
|
|
|
|
|
|
"###.#.#.#.....#.#.#.#######", |
|
|
|
|
|
|
|
"#...#...#.....#...#.......#", |
|
|
|
|
|
|
|
"###########################", |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.Render() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
game.HandleEvents() |
|
|
|
|
|
|
|
game.Render() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|