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.

59
Pong.go
View File

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