Nie dziala tak jak bym chcial ale ok.

This commit is contained in:
knmarcin 2018-11-24 20:10:36 +01:00
parent 9784f10b59
commit a156aae6fa
2 changed files with 32 additions and 39 deletions

BIN
Pong

Binary file not shown.

71
Pong.go
View File

@ -1,9 +1,8 @@
package main package main
import ( import (
"fmt" "fmt"
"os" // "os"
"os/exec" // "os/exec"
"bytes" "bytes"
"github.com/pkg/term" "github.com/pkg/term"
"sync" "sync"
@ -28,15 +27,16 @@ var GoesLEFT bool = true
func main() { func main() {
i = 5 i = 5
j = 5 j = 5
P1index=3 P1index=3
P2index=3 P2index=3
SpawnEverything() SpawnEverything()
for !GameOver { //Parallelize(TickBall, MovePlayers)
MovePlayers() MovePlayers()
TickBall() TickBall()
}
} }
func SpawnEverything() { func SpawnEverything() {
CreateBoard() CreateBoard()
SpawnPlayers() SpawnPlayers()
@ -66,14 +66,16 @@ func CreateBoard() {
} }
func PrintBoard() { func PrintBoard() {
c:=exec.Command("clear") //c:=exec.Command("clear")
c.Stdout = os.Stdout //c.Stdout = os.Stdout
c.Run() //c.Run()
fmt.Print("\033[H\033[2J")
for i = 0; i < 10; i++ { for i = 0; i < 10; i++ {
for j = 0; j < 30; j++ { for j = 0; j < 30; j++ {
fmt.Printf("%s", gameBoard[i][j]) fmt.Printf("%s", gameBoard[i][j])
} }
fmt.Print("\n") fmt.Print("\n")
time.Sleep(1*time.Nanosecond)
} }
} }
func SpawnBall() { func SpawnBall() {
@ -95,18 +97,18 @@ func SpawnPlayers() {
} }
} }
func MovePlayers() { func MovePlayers() {
if !GameOver {
c:=getch() c:=getch()
switch { switch {
case bytes.Equal(c, []byte{3}): // case bytes.Equal(c, []byte{3}):
return // return
case bytes.Equal(c, []byte{119}): case bytes.Equal(c, []byte{119}):
// "W" Key // "W" Key
if P1index != 0 { if P1index != 0 {
P1index-- P1index--
gameBoard[P1index][0] = "|" gameBoard[P1index][0] = "|"
gameBoard[P1index + 3][0] = " " gameBoard[P1index + 3][0] = " "
PrintBoard() // PrintBoard()
} }
case bytes.Equal(c, []byte{115}): case bytes.Equal(c, []byte{115}):
// "S" Key // "S" Key
@ -114,16 +116,15 @@ func MovePlayers() {
gameBoard[P1index][0] = " " gameBoard[P1index][0] = " "
P1index++ P1index++
gameBoard[P1index + 2][0] = "|" gameBoard[P1index + 2][0] = "|"
PrintBoard() // PrintBoard()
} }
case bytes.Equal(c, []byte{105}): case bytes.Equal(c, []byte{105}):
// "I" Key // "I" Key
if P2index!=0 { if P2index!=0 {
P2index-- P2index--
gameBoard[P2index][29] = "|" gameBoard[P2index][29] = "|"
gameBoard[P2index + 3][29] = " " gameBoard[P2index + 3][29] = " "
PrintBoard() // PrintBoard()
} }
case bytes.Equal(c, []byte{107}): case bytes.Equal(c, []byte{107}):
// "K" Key) // "K" Key)
@ -131,16 +132,16 @@ func MovePlayers() {
gameBoard[P2index][29] = " " gameBoard[P2index][29] = " "
P2index++ P2index++
gameBoard[P2index + 2][29] = "|" gameBoard[P2index + 2][29] = "|"
PrintBoard() // PrintBoard()
} }
default: default:
fmt.Print(" ") fmt.Print(" ")
GameOver=true
time.Sleep(40*time.Nanosecond) time.Sleep(40*time.Nanosecond)
} }
PrintBoard()
}
} }
func TickBall() { func TickBall() {
BallX = 4 BallX = 4
BallY = 17 BallY = 17
@ -154,19 +155,15 @@ func TickBall() {
gameBoard[BallX][BallY] = " " gameBoard[BallX][BallY] = " "
BallY-- BallY--
gameBoard[BallX][BallY] = "o" gameBoard[BallX][BallY] = "o"
PrintBoard()
time.Sleep(40*time.Millisecond) time.Sleep(40*time.Millisecond)
} }
if !GoesLEFT { if !GoesLEFT {
gameBoard[BallX][BallY]= " " gameBoard[BallX][BallY]= " "
BallY++ BallY++
gameBoard[BallX][BallY] = "o" gameBoard[BallX][BallY] = "o"
PrintBoard()
time.Sleep(40*time.Millisecond) time.Sleep(40*time.Millisecond)
} }
if BallX == 0 { if BallX == 0 {
PrintBoard()
GoesUP = false GoesUP = false
} }
if BallY == 1 { if BallY == 1 {
@ -185,29 +182,30 @@ func TickBall() {
fmt.Print("Game Over") fmt.Print("Game Over")
} }
} }
} }
PrintBoard()
} }
if !GoesUP { if !GoesUP {
if BallX < 9 { if BallX < 9 {
gameBoard[BallX][BallY] = " " gameBoard[BallX][BallY] = " "
BallX++ BallX++
gameBoard[BallX][BallY] = "o" gameBoard[BallX][BallY] = "o"
PrintBoard() //PrintBoard()
if GoesLEFT { if GoesLEFT {
gameBoard[BallX][BallY] = " " gameBoard[BallX][BallY] = " "
BallY-- BallY--
gameBoard[BallX][BallY] = "o" gameBoard[BallX][BallY] = "o"
PrintBoard() // PrintBoard()
time.Sleep(40*time.Millisecond) time.Sleep(40*time.Millisecond)
} }
if !GoesLEFT { if !GoesLEFT {
gameBoard[BallX][BallY] = " " gameBoard[BallX][BallY] = " "
BallY++ BallY++
gameBoard[BallX][BallY] = "o" gameBoard[BallX][BallY] = "o"
PrintBoard() // PrintBoard()
time.Sleep(40*time.Millisecond) time.Sleep(40*time.Millisecond)
} }
if BallY == 1 { if BallY == 1 {
if gameBoard[BallX][0] == "|" { if gameBoard[BallX][0] == "|" {
GoesLEFT = false GoesLEFT = false
@ -216,7 +214,6 @@ func TickBall() {
GameOver = true GameOver = true
} }
} }
if BallY == 28 { if BallY == 28 {
if gameBoard[BallX][29] == "|" { if gameBoard[BallX][29] == "|" {
GoesLEFT = true GoesLEFT = true
@ -225,7 +222,6 @@ func TickBall() {
fmt.Print("GameOver") fmt.Print("GameOver")
} }
} }
if BallX == 9 { if BallX == 9 {
GoesUP = true GoesUP = true
} }
@ -236,12 +232,10 @@ func TickBall() {
GoesLEFT = true GoesLEFT = true
} }
} }
PrintBoard()
} }
} }
} }
func Parallelize(functions ...func()) { func Parallelize(functions ...func()) {
var waitGroup sync.WaitGroup var waitGroup sync.WaitGroup
waitGroup.Add(len(functions)) waitGroup.Add(len(functions))
@ -255,4 +249,3 @@ func Parallelize(functions ...func()) {
}(function) }(function)
} }
} }