Pong/Pong.go

129 lines
2.0 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"bytes"
"github.com/pkg/term"
)
var gameBoard [10][30]string
var x, y int
var i, j int
var Player1 [10]string
var Player2 [10]string
var P1index int
var Ball [10][30]string
var BallDirection [10][30]string
var BallSpeed int
var GameOver bool = false
var LastPressedKey rune
func main() {
// SpawnEverything()
loop2()
}
func SpawnEverything() {
CreateBoard()
SpawnPlayers()
SpawnBall()
PrintBoard()
Loop()
}
func getch() []byte {
t, _ := term.Open("/dev/tty")
term.RawMode(t)
bytes := make([]byte, 3)
numRead, err := t.Read(bytes)
t.Restore()
t.Close()
if err != nil {
return nil
}
return bytes[0:numRead]
}
func CreateBoard() {
for i = 0; i < 10; i++ {
for j = 0; j < 30; j++ {
gameBoard[i][j] = " "
}
}
}
func PrintBoard() {
c:=exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
for i = 0; i < 10; i++ {
for j = 0; j < 30; j++ {
fmt.Printf("%s", gameBoard[i][j])
}
fmt.Print("\n")
}
}
func SpawnBall() {
x = 4
y = 14
gameBoard[x][y] = "o"
}
func SpawnPlayers() {
for i = 0; i < 10; i++ {
Player1[i] = " "
Player2[i] = " "
}
for i = 3; i < 6; i++ {
Player1[i] = "|"
Player2[i] = "|"
j = 0
gameBoard[i][0] = Player1[i]
gameBoard[i][29] = Player2[i]
}
}
func Loop() {
i = 5
P1index=3
for GameOver == false {
fmt.Scanf("%c", &LastPressedKey)
if LastPressedKey == 'w' {
if P1index != 0 {
P1index--
gameBoard[P1index][0] = "|"
gameBoard[P1index + 3][0] = " "
PrintBoard()
} else {
PrintBoard()
}
}
if LastPressedKey=='s' {
if P1index!=7 {
gameBoard[P1index][0] = " "
P1index++
gameBoard[P1index + 2][0] = "|"
PrintBoard()
} else {
PrintBoard()
}
}
if LastPressedKey=='r' {
}
}
}
func loop2() {
for !GameOver {
c:=getch()
switch {
case bytes.Equal(c, []byte{3}):
return
case bytes.Equal(c, []byte{27, 91, 68}):
fmt.Println("Left pressed")
default:
PrintBoard()
}
}
}