package main import ( "fmt" "os" "os/exec" "bytes" "github.com/pkg/term" "sync" "time" ) 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 P2index int var BallDirection [10][30]string var BallSpeed int var GameOver bool = false var LastPressedKey rune var BallX, BallY int var BallDirX, BallDirY int var GoesUP bool = true var GoesLEFT bool = true func main() { SpawnEverything() for !GameOver { TickBall() } } func SpawnEverything() { CreateBoard() SpawnPlayers() SpawnBall() PrintBoard() } 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] = " " } 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 j = 5 P1index=3 P2index=3 for !GameOver { c:=getch() switch { case bytes.Equal(c, []byte{3}): return case bytes.Equal(c, []byte{119}): // "W" Key if P1index != 0 { P1index-- gameBoard[P1index][0] = "|" gameBoard[P1index + 3][0] = " " PrintBoard() } case bytes.Equal(c, []byte{115}): // "S" Key if P1index!=7 { gameBoard[P1index][0] = " " P1index++ gameBoard[P1index + 2][0] = "|" PrintBoard() } case bytes.Equal(c, []byte{105}): // "I" Key if P2index!=0 { P2index-- gameBoard[P2index][29] = "|" gameBoard[P2index + 3][29] = " " PrintBoard() } case bytes.Equal(c, []byte{107}): // "K" Key) if P2index!=7 { gameBoard[P2index][29] = " " P2index++ gameBoard[P2index + 2][29] = "|" PrintBoard() } default: GameOver = true } } } func Ball() { BallX = 7 BallY = 14 BallDirX = 8 BallDirY = 1 for BallX!=BallDirX || BallY!=BallDirY { if BallX>BallDirX { gameBoard[BallX][BallY] = " " BallX-- gameBoard[BallX][BallY] = "o" PrintBoard() } if BallY>BallDirY { gameBoard[BallX][BallY] = " " BallY-- gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1 * time.Second) } if BallY BallDirY { gameBoard[BallX][BallY] = " " BallY-- gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1 * time.Second) } } if gameBoard[BallX][BallY-1] == " " { GameOver = true fmt.Print("Game Over!\n") } } } func TickBall() { BallX = 4 BallY = 17 for !GameOver { if GoesUP { if BallX > 0 { gameBoard[BallX][BallY] = " " BallX-- gameBoard[BallX][BallY] = "o" if GoesLEFT { gameBoard[BallX][BallY] = " " BallY-- gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1 * time.Second) } if !GoesLEFT { gameBoard[BallX][BallY]= " " BallY++ gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1 * time.Second) } if BallX == 0 { PrintBoard() GoesUP = false } if BallY == 1 { if gameBoard[BallX+1][BallY-1] == "|" { GoesLEFT = false } else { fmt.Print("Game Over") GameOver = true } } if BallY == 29 { GoesLEFT = true } } } if !GoesUP { if BallX < 9 { gameBoard[BallX][BallY] = " " BallX++ gameBoard[BallX][BallY] = "o" PrintBoard() if GoesLEFT { gameBoard[BallX][BallY] = " " BallY-- gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1* time.Second) } if !GoesLEFT { gameBoard[BallX][BallY] = " " BallY++ gameBoard[BallX][BallY] = "o" PrintBoard() time.Sleep(1 * time.Second) } if BallY == 1 { if gameBoard[BallX][BallY -1] == "|" { GoesLEFT = false } else { fmt.Print("Game Over") GameOver = true } } if BallY == 29 { GoesLEFT = true } if BallX == 9 { GoesUP = true } if BallY == 0 { GoesLEFT = false } if BallY == 29 { GoesLEFT = true } } } } } func Parallelize(functions ...func()) { var waitGroup sync.WaitGroup waitGroup.Add(len(functions)) defer waitGroup.Wait() for _, function := range functions { go func(copy func()) { defer waitGroup.Done() copy() }(function) } }