Pong/Pong.go

252 lines
4.7 KiB
Go
Raw Normal View History

2018-11-13 16:10:46 +01:00
package main
import (
"fmt"
2018-11-24 20:10:36 +01:00
// "os"
// "os/exec"
"bytes"
"github.com/pkg/term"
"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
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
var GameOver bool = false
var LastPressedKey rune
var BallX, BallY int
var BallDirX, BallDirY int
2018-11-22 22:55:41 +01:00
var GoesUP bool = true
var GoesLEFT bool = true
2018-11-13 16:10:46 +01:00
func main() {
2018-11-24 09:56:03 +01:00
i = 5
2018-11-24 20:10:36 +01:00
j = 5
P1index=3
P2index=3
2018-11-18 19:56:48 +01:00
SpawnEverything()
2018-11-24 20:10:36 +01:00
//Parallelize(TickBall, MovePlayers)
MovePlayers()
TickBall()
}
2018-11-24 20:10:36 +01:00
func SpawnEverything() {
2018-11-13 16:10:46 +01:00
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]
}
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-24 20:10:36 +01:00
//c:=exec.Command("clear")
//c.Stdout = os.Stdout
//c.Run()
fmt.Print("\033[H\033[2J")
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])
}
fmt.Print("\n")
2018-11-24 20:10:36 +01:00
time.Sleep(1*time.Nanosecond)
2018-11-13 16:10:46 +01:00
}
}
func SpawnBall() {
x = 4
y = 14
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-24 09:56:03 +01:00
func MovePlayers() {
2018-11-24 20:10:36 +01:00
if !GameOver {
c:=getch()
switch {
2018-11-24 20:10:36 +01:00
// case bytes.Equal(c, []byte{3}):
// return
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] = " "
2018-11-24 20:10:36 +01:00
// PrintBoard()
2018-11-18 19:56:48 +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] = "|"
2018-11-24 20:10:36 +01:00
// PrintBoard()
}
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] = " "
2018-11-24 20:10:36 +01:00
// PrintBoard()
2018-11-18 19:56:48 +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] = "|"
2018-11-24 20:10:36 +01:00
// PrintBoard()
2018-11-18 19:56:48 +01:00
}
default:
2018-11-24 09:56:03 +01:00
fmt.Print(" ")
2018-11-24 20:10:36 +01:00
GameOver=true
2018-11-24 09:56:03 +01:00
time.Sleep(40*time.Nanosecond)
}
2018-11-24 20:10:36 +01:00
PrintBoard()
}
2018-11-24 09:56:03 +01:00
}
func TickBall() {
BallX = 4
2018-11-22 22:55:41 +01:00
BallY = 17
for !GameOver {
if GoesUP {
2018-11-22 22:55:41 +01:00
if BallX > 0 {
gameBoard[BallX][BallY] = " "
BallX--
gameBoard[BallX][BallY] = "o"
if GoesLEFT {
2018-11-22 22:55:41 +01:00
gameBoard[BallX][BallY] = " "
BallY--
2018-11-22 22:55:41 +01:00
gameBoard[BallX][BallY] = "o"
2018-11-24 09:56:03 +01:00
time.Sleep(40*time.Millisecond)
2018-11-22 22:55:41 +01:00
}
if !GoesLEFT {
gameBoard[BallX][BallY]= " "
BallY++
gameBoard[BallX][BallY] = "o"
2018-11-24 09:56:03 +01:00
time.Sleep(40*time.Millisecond)
2018-11-22 22:55:41 +01:00
}
if BallX == 0 {
GoesUP = false
}
if BallY == 1 {
2018-11-24 09:56:03 +01:00
if gameBoard[BallX][0] == "|" {
2018-11-22 22:55:41 +01:00
GoesLEFT = false
} else {
fmt.Print("Game Over")
GameOver = true
}
}
2018-11-24 09:56:03 +01:00
if BallY == 28 {
if gameBoard[BallX][29] == "|" {
GoesLEFT = true
} else {
GameOver=true
fmt.Print("Game Over")
}
}
2018-11-24 20:10:36 +01:00
}
2018-11-24 20:10:36 +01:00
PrintBoard()
2018-11-22 22:55:41 +01:00
}
if !GoesUP {
2018-11-22 22:55:41 +01:00
if BallX < 9 {
gameBoard[BallX][BallY] = " "
BallX++
gameBoard[BallX][BallY] = "o"
2018-11-24 20:10:36 +01:00
//PrintBoard()
2018-11-22 22:55:41 +01:00
if GoesLEFT {
gameBoard[BallX][BallY] = " "
BallY--
gameBoard[BallX][BallY] = "o"
2018-11-24 20:10:36 +01:00
// PrintBoard()
2018-11-24 09:56:03 +01:00
time.Sleep(40*time.Millisecond)
2018-11-22 22:55:41 +01:00
}
if !GoesLEFT {
gameBoard[BallX][BallY] = " "
BallY++
2018-11-22 22:55:41 +01:00
gameBoard[BallX][BallY] = "o"
2018-11-24 20:10:36 +01:00
// PrintBoard()
2018-11-24 09:56:03 +01:00
time.Sleep(40*time.Millisecond)
2018-11-22 22:55:41 +01:00
}
if BallY == 1 {
2018-11-24 09:56:03 +01:00
if gameBoard[BallX][0] == "|" {
2018-11-22 22:55:41 +01:00
GoesLEFT = false
} else {
fmt.Print("Game Over")
GameOver = true
}
}
2018-11-24 09:56:03 +01:00
if BallY == 28 {
if gameBoard[BallX][29] == "|" {
GoesLEFT = true
} else {
GameOver=true
fmt.Print("GameOver")
}
2018-11-22 22:55:41 +01:00
}
if BallX == 9 {
GoesUP = true
}
2018-11-24 09:56:03 +01:00
if BallY == 1 {
2018-11-22 22:55:41 +01:00
GoesLEFT = false
}
if BallY == 29 {
GoesLEFT = true
}
}
2018-11-24 20:10:36 +01:00
PrintBoard()
2018-11-22 22:55:41 +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)
}
}