Think and write one simple Golang program that lets us play the classic Pacman game.…
- User
Think and write one simple Golang program that lets us play the classic Pacman game. With score, ghost, and all that. Controls is wasd ...
- Assistant
Think and write one simple Golang program that lets us play the classic Pacman game. With score, ghost, and all that. Controls is wasd ...
Sure! Below is a simple Go program that implements a basic version of Pac-Man. This program uses the terminal to display the game and accepts input using the WASD keys for movement. It includes a player (Pac-Man), ghosts, a score system, and pellets to collect.
Please note that this is a simplified version and runs in the terminal. It doesn't have all features of the classic Pac-Man game but provides a basic playable experience.
First, you'll need to install some dependencies:
Install the go-runewidth package for handling character widths in the terminal:
go get github.com/mattn/go-runewidth
Install the golang-collections/collections package:
go get github.com/golang-collections/collections
Here is the code:
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
"github.com/mattn/go-runewidth"
)
const (
width = 20
height = 10
pelletChar = '.'
wallChar = '#'
pacmanChar = 'C'
ghostChar = 'G'
emptyChar = ' '
)
type position struct {
x int
y int
}
var (
maze [][]rune
pacman position
ghost position
score int
pellets int
gameOver bool
inputChan chan rune
)
func initMaze() {
maze = make([][]rune, height)
for i := range maze {
maze[i] = make([]rune, width)
for j := range maze[i] {
if i == 0 || i == height-1 || j == 0 || j == width-1 {
maze[i][j] = wallChar
} else {
maze[i][j] = pelletChar
pellets++
}
}
}
// Set initial positions
pacman = position{1, 1}
maze[pacman.y][pacman.x] = pacmanChar
ghost = position{width - 2, height - 2}
maze[ghost.y][ghost.x] = ghostChar
}
func printMaze() {
for _, row := range maze {
for _, col := range row {
fmt.Print(string(col))
// Adjust for wide characters
if runewidth.RuneWidth(col) == 1 {
fmt.Print(" ")
}
}
fmt.Println()
}
fmt.Printf("Score: %d\n", score)
}
func movePacman(dx, dy int) {
newX := pacman.x + dx
newY := pacman.y + dy
if maze[newY][newX] != wallChar {
// Remove pellet and update score
if maze[newY][newX] == pelletChar {
score += 10
pellets--
}
maze[pacman.y][pacman.x] = emptyChar
pacman.x = newX
pacman.y = newY
// Check for collision with ghost
if pacman.x == ghost.x && pacman.y == ghost.y {
gameOver = true
fmt.Println("Game Over! You were caught by the ghost!")
return
}
maze[pacman.y][pacman.x] = pacmanChar
}
}
func moveGhost() {
dx := 0
dy := 0
rand.Seed(time.Now().UnixNano())
direction := rand.Intn(4)
switch direction {
case 0:
dx = -1 // Left
case 1:
dx = 1 // Right
case 2:
dy = -1 // Up
case 3:
dy = 1 // Down
}
newX := ghost.x + dx
newY := ghost.y + dy
if maze[newY][newX] != wallChar {
maze[ghost.y][ghost.x] = emptyChar
ghost.x = newX
ghost.y = newY
// Check for collision with Pac-Man
if pacman.x == ghost.x && pacman.y == ghost.y {
gameOver = true
fmt.Println("Game Over! You were caught by the ghost!")
return
}
maze[ghost.y][ghost.x] = ghostChar
}
}
func getInput() {
reader := bufio.NewReader(os.Stdin)
for {
char, _, err := reader.ReadRune()
if err == nil {
inputChan <- char
}
}
}
func main() {
initMaze()
inputChan = make(chan rune)
go getInput()
for !gameOver && pellets > 0 {
printMaze()
select {
case input := <-inputChan:
switch input {
case 'w':
movePacman(0, -1)
case 's':
movePacman(0, 1)
case 'a':
movePacman(-1, 0)
case 'd':
movePacman(1, 0)
}
default:
// No input, continue
}
moveGhost()
time.Sleep(200 * time.Millisecond)
fmt.Print("\033[H\033[2J") // Clear screen
}
if pellets == 0 {
fmt.Println("Congratulations! You won!")
}
}
Instructions to run the game:
Save the code to a file, for example, pacman.go.
Open your terminal and navigate to the directory containing pacman.go.
Run the game using:
go run pacman.go
Controls:
Game Details:
.) to win the game.G). If the ghost catches you, the game is over.#.C (Pac-Man).Note:
Enjoy the game! If you have any questions or need further enhancements, feel free to ask.
Not yet included in a published release.