2018-11-13 16:10:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-16 21:09:29 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-11-18 18:07:29 +01:00
|
|
|
"bytes"
|
|
|
|
"github.com/pkg/term"
|
2018-11-20 23:15:57 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-11-13 16:10:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var gameBoard [10][30]string
|
|
|
|
var x, y int
|
|
|
|
var i, j int
|
|
|
|
var Player1 [10]string
|
|
|
|
var Player2 [10]string
|
2018-11-16 21:09:29 +01:00
|
|
|
var P1index int
|
2018-11-18 19:56:48 +01:00
|
|
|
var P2index int
|
2018-11-13 16:10:46 +01:00
|
|
|
var BallDirection [10][30]string
|
|
|
|
var BallSpeed int
|
2018-11-15 18:03:37 +01:00
|
|
|
var GameOver bool = false
|
2018-11-17 09:03:08 +01:00
|
|
|
var LastPressedKey rune
|
2018-11-20 23:15:57 +01:00
|
|
|
var BallX, BallY int
|
|
|
|
var BallDirX, BallDirY int
|
2018-11-17 09:25:33 +01:00
|
|
|
|
2018-11-13 16:10:46 +01:00
|
|
|
func main() {
|
2018-11-18 19:56:48 +01:00
|
|
|
SpawnEverything()
|
2018-11-20 23:15:57 +01:00
|
|
|
for !GameOver {
|
|
|
|
Ball()
|
2018-11-21 22:28:10 +01:00
|
|
|
|
2018-11-20 23:15:57 +01:00
|
|
|
}
|
2018-11-17 09:25:33 +01:00
|
|
|
}
|
|
|
|
func SpawnEverything() {
|
2018-11-13 16:10:46 +01:00
|
|
|
CreateBoard()
|
|
|
|
SpawnPlayers()
|
|
|
|
SpawnBall()
|
|
|
|
PrintBoard()
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:07:29 +01:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2018-11-13 16:10:46 +01:00
|
|
|
func CreateBoard() {
|
|
|
|
for i = 0; i < 10; i++ {
|
|
|
|
for j = 0; j < 30; j++ {
|
|
|
|
gameBoard[i][j] = " "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintBoard() {
|
2018-11-16 21:09:29 +01:00
|
|
|
c:=exec.Command("clear")
|
|
|
|
c.Stdout = os.Stdout
|
|
|
|
c.Run()
|
2018-11-13 16:10:46 +01:00
|
|
|
for i = 0; i < 10; i++ {
|
|
|
|
for j = 0; j < 30; j++ {
|
|
|
|
fmt.Printf("%s", gameBoard[i][j])
|
|
|
|
}
|
2018-11-16 21:09:29 +01:00
|
|
|
fmt.Print("\n")
|
2018-11-13 16:10:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func SpawnBall() {
|
|
|
|
x = 4
|
|
|
|
y = 14
|
2018-11-21 22:28:10 +01:00
|
|
|
gameBoard[x][y] = " "
|
2018-11-13 16:10:46 +01:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 17:18:33 +01:00
|
|
|
func Loop() {
|
2018-11-15 18:03:37 +01:00
|
|
|
i = 5
|
2018-11-18 19:56:48 +01:00
|
|
|
j = 5
|
2018-11-16 21:09:29 +01:00
|
|
|
P1index=3
|
2018-11-18 19:56:48 +01:00
|
|
|
P2index=3
|
|
|
|
|
2018-11-18 20:04:10 +01:00
|
|
|
for !GameOver {
|
2018-11-21 22:28:10 +01:00
|
|
|
|
2018-11-18 18:07:29 +01:00
|
|
|
c:=getch()
|
|
|
|
switch {
|
|
|
|
case bytes.Equal(c, []byte{3}):
|
|
|
|
return
|
2018-11-18 18:37:41 +01:00
|
|
|
|
|
|
|
case bytes.Equal(c, []byte{119}):
|
2018-11-18 19:56:48 +01:00
|
|
|
// "W" Key
|
|
|
|
if P1index != 0 {
|
|
|
|
P1index--
|
|
|
|
gameBoard[P1index][0] = "|"
|
|
|
|
gameBoard[P1index + 3][0] = " "
|
|
|
|
PrintBoard()
|
|
|
|
}
|
2018-11-18 18:37:41 +01:00
|
|
|
case bytes.Equal(c, []byte{115}):
|
2018-11-18 19:56:48 +01:00
|
|
|
// "S" Key
|
|
|
|
if P1index!=7 {
|
|
|
|
gameBoard[P1index][0] = " "
|
|
|
|
P1index++
|
|
|
|
gameBoard[P1index + 2][0] = "|"
|
|
|
|
PrintBoard()
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:37:41 +01:00
|
|
|
case bytes.Equal(c, []byte{105}):
|
2018-11-18 19:56:48 +01:00
|
|
|
// "I" Key
|
|
|
|
if P2index!=0 {
|
|
|
|
P2index--
|
|
|
|
gameBoard[P2index][29] = "|"
|
|
|
|
gameBoard[P2index + 3][29] = " "
|
|
|
|
PrintBoard()
|
|
|
|
}
|
2018-11-18 18:37:41 +01:00
|
|
|
case bytes.Equal(c, []byte{107}):
|
2018-11-18 19:56:48 +01:00
|
|
|
// "K" Key)
|
|
|
|
if P2index!=7 {
|
|
|
|
gameBoard[P2index][29] = " "
|
|
|
|
P2index++
|
|
|
|
gameBoard[P2index + 2][29] = "|"
|
|
|
|
PrintBoard()
|
|
|
|
}
|
2018-11-18 18:07:29 +01:00
|
|
|
default:
|
2018-11-18 18:37:41 +01:00
|
|
|
GameOver = true
|
2018-11-18 18:07:29 +01:00
|
|
|
}
|
2018-11-21 22:28:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:15:57 +01:00
|
|
|
|
|
|
|
func Ball() {
|
2018-11-21 22:28:10 +01:00
|
|
|
BallX = 7
|
2018-11-20 23:15:57 +01:00
|
|
|
BallY = 14
|
2018-11-21 22:28:10 +01:00
|
|
|
BallDirX = 8
|
|
|
|
BallDirY = 1
|
2018-11-20 23:15:57 +01:00
|
|
|
|
2018-11-21 22:28:10 +01:00
|
|
|
for BallX!=BallDirX || BallY!=BallDirY {
|
2018-11-20 23:15:57 +01:00
|
|
|
if BallX>BallDirX {
|
|
|
|
gameBoard[BallX][BallY] = " "
|
|
|
|
BallX--
|
|
|
|
gameBoard[BallX][BallY] = "o"
|
|
|
|
PrintBoard()
|
2018-11-21 22:28:10 +01:00
|
|
|
}
|
|
|
|
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)
|
2018-11-20 23:15:57 +01:00
|
|
|
}
|
|
|
|
if BallX < BallDirX {
|
|
|
|
gameBoard[BallX][BallY] = " "
|
|
|
|
BallX++
|
|
|
|
gameBoard[BallX][BallY] = "o"
|
|
|
|
PrintBoard()
|
|
|
|
time.Sleep(1 * time.Second)
|
2018-11-21 22:28:10 +01:00
|
|
|
}
|
2018-11-20 23:15:57 +01:00
|
|
|
if BallX==BallDirX {
|
|
|
|
if BallY < BallDirY {
|
|
|
|
gameBoard[BallX][BallY] = " "
|
|
|
|
BallY++
|
|
|
|
gameBoard[BallX][BallY] = "o"
|
|
|
|
PrintBoard()
|
|
|
|
time.Sleep(1 * time.Second)
|
2018-11-21 22:28:10 +01:00
|
|
|
}
|
2018-11-20 23:15:57 +01:00
|
|
|
if BallY > BallDirY {
|
|
|
|
gameBoard[BallX][BallY] = " "
|
|
|
|
BallY--
|
|
|
|
gameBoard[BallX][BallY] = "o"
|
|
|
|
PrintBoard()
|
|
|
|
time.Sleep(1 * time.Second)
|
2018-11-21 22:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if gameBoard[BallX][BallY-1] == " " {
|
|
|
|
GameOver = true
|
|
|
|
fmt.Print("Game Over!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-20 23:15:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:28:10 +01:00
|
|
|
func TickBall() {
|
|
|
|
BallX = 4
|
|
|
|
BallY = 14
|
|
|
|
for !GameOver {
|
|
|
|
if GoesUP {
|
|
|
|
if BallX < 9 {
|
|
|
|
BallX++
|
|
|
|
if GoesLEFT {
|
|
|
|
BallY--
|
|
|
|
}
|
|
|
|
if Bally == 9 {
|
|
|
|
GoesUP = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !GoesUP {
|
|
|
|
if BallX > 1 {
|
|
|
|
BallX--
|
|
|
|
if GoesLeft {
|
|
|
|
BallY++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-20 23:15:57 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|