示例#1
0
func (g *Game) getSpeedAndMovementFromControl() (sf.Vector2f, sf.Vector2f) {
	movement := sf.Vector2f{0, 0}
	speed := sf.Vector2f{2, 2}

	if sf.KeyboardIsKeyPressed(sf.KeySpace) {
		speed.X *= 3
		speed.Y *= 3
	}

	if sf.KeyboardIsKeyPressed(sf.KeyRight) {
		movement.X += 1.0
	}

	if sf.KeyboardIsKeyPressed(sf.KeyLeft) {
		movement.X += -1.0
	}

	if sf.KeyboardIsKeyPressed(sf.KeyUp) {
		movement.Y += -1.0
	}

	if sf.KeyboardIsKeyPressed(sf.KeyDown) {
		movement.Y += 1.0
	}

	return speed, movement
}
示例#2
0
文件: gopong.go 项目: bwilkins/gopong
func main() {
	//Define some variables for the game
	//This block mostly will not change
	paddleMaxSpeed := float32(400.0)
	paddleDefaultSize := sf.Vector2f{25, 100}
	isPlaying := false

	gameWidth := uint(800)
	gameHeight := uint(600)
	bitDepth := uint(32)

	ballMaxSpeed := float32(400.0)
	ballRadius := float32(10.0)

	//These are a little more special... guess what they do!
	ticker := time.NewTicker(time.Second / 60)
	aiTicker := time.NewTicker(time.Second / 10)
	rand.Seed(time.Now().UnixNano())

	//Instantiate the render window for SFML
	renderWindow := sf.NewRenderWindow(sf.VideoMode{gameWidth, gameHeight, bitDepth}, "Pong (Brett's Go test)", sf.StyleDefault, nil)

	//Create the left paddle
	leftPaddle := paddle.NewPaddle(paddleMaxSpeed, paddleMaxSpeed, paddleDefaultSize, sf.Color{100, 100, 200, 255})

	//Create the right paddle
	rightPaddle := paddle.NewPaddle(0, paddleMaxSpeed, paddleDefaultSize, sf.Color{200, 100, 100, 255})

	//Create the ball
	ball := ball.NewBall(ballMaxSpeed, ballMaxSpeed, ballRadius, "resources/ball.wav")

	//Load font
	font, _ := sf.NewFontFromFile("resources/sansation.ttf")

	//Init the pause message
	pauseMessage := sf.NewText(font)
	pauseMessage.SetCharacterSize(40)
	pauseMessage.SetPosition(sf.Vector2f{170, 150})
	pauseMessage.SetColor(sf.ColorWhite())
	pauseMessage.SetString("Welcome to Brett's SFML Pong!\nPress space to start the game.")

	for renderWindow.IsOpen() {
		select {
		case <-ticker.C:
			//poll for events
			for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
				switch ev := event.(type) {
				case sf.EventKeyReleased:
					switch ev.Code {
					case sf.KeyEscape:
						renderWindow.Close()
					case sf.KeySpace:
						if !isPlaying {
							//restart the game
							isPlaying = true
							leftPaddle.Shape.SetPosition(sf.Vector2f{10 + leftPaddle.Size.X/2, float32(gameHeight) / 2})
							rightPaddle.Shape.SetPosition(sf.Vector2f{float32(gameWidth) - 10 - rightPaddle.Size.X/2, float32(gameHeight) / 2})
							ball.Shape.SetPosition(sf.Vector2f{float32(gameWidth) / 2, float32(gameHeight) / 2})

							//ensure the ball angle isn't too vertical
							for {
								ball.Angle = rand.Float32() * math.Pi * 2

								if math.Abs(math.Cos(float64(ball.Angle))) > 0.7 {
									break
								}
							}
						}
					}
				case sf.EventClosed:
					renderWindow.Close()
				}
			}

			if isPlaying {
				deltaTime := time.Second / 60

				//Move the player's paddle
				if sf.KeyboardIsKeyPressed(sf.KeyUp) && leftPaddle.TopLeft().Y > 5 {
					leftPaddle.Shape.Move(sf.Vector2f{0, -leftPaddle.Speed * float32(deltaTime.Seconds())})
				}
				if sf.KeyboardIsKeyPressed(sf.KeyDown) && leftPaddle.BottomRight().Y < float32(gameHeight)-5 {
					leftPaddle.Shape.Move(sf.Vector2f{0, leftPaddle.Speed * float32(deltaTime.Seconds())})
				}

				//Move the ai's paddle
				if (rightPaddle.Speed < 0 && rightPaddle.TopLeft().Y > 5) || (rightPaddle.Speed > 0 && rightPaddle.BottomRight().Y < float32(gameHeight)-5) {
					rightPaddle.Shape.Move(sf.Vector2f{0, rightPaddle.Speed * float32(deltaTime.Seconds())})
				}

				//Move ze ball
				factor := ball.Speed * float32(deltaTime.Seconds())
				ball.Shape.Move(sf.Vector2f{float32(math.Cos(float64(ball.Angle))) * factor, float32(math.Sin(float64(ball.Angle))) * factor})

				//Check collisions between ball and screen edge
				if ball.TopLeft().X < 0 {
					isPlaying = false
					pauseMessage.SetString("You lost!\nPress space to restart or\nescape to quit")
				}

				if ball.BottomRight().X > float32(gameWidth) {
					isPlaying = false
					pauseMessage.SetString("You won!\nPress space to play again or\nescape to quit")
				}

				if ball.TopLeft().Y < 0 {
					ball.Angle = -ball.Angle
					ball.Shape.SetPosition(sf.Vector2f{ball.Center().X, ball.Radius + 0.1})
					ball.Sound.Play()
				}

				if ball.BottomRight().Y > float32(gameHeight) {
					ball.Angle = -ball.Angle
					ball.Shape.SetPosition(sf.Vector2f{ball.Center().X, float32(gameHeight) - ball.Radius - 0.1})
					ball.Sound.Play()
				}

				//Check collisions between the ball and the left paddle
				if leftPaddle.CollideRight(ball) {

					if ball.Center().Y > leftPaddle.Center().Y {
						ball.Angle = math.Pi - ball.Angle + rand.Float32()*math.Pi*0.2
					} else {
						ball.Angle = math.Pi - ball.Angle - rand.Float32()*math.Pi*0.2
					}

					ball.Shape.SetPosition(sf.Vector2f{leftPaddle.Center().X + ball.Radius + leftPaddle.Size.X/2 + 0.1, ball.Center().Y})
					ball.Sound.Play()
				}

				//Check collisions between the ball and the right paddle
				if rightPaddle.CollideLeft(ball) {

					if ball.Center().Y > rightPaddle.Center().Y {
						ball.Angle = math.Pi - ball.Angle + rand.Float32()*math.Pi*0.2
					} else {
						ball.Angle = math.Pi - ball.Angle - rand.Float32()*math.Pi*0.2
					}

					ball.Shape.SetPosition(sf.Vector2f{rightPaddle.Center().X - ball.Radius - rightPaddle.Size.X/2 - 0.1, ball.Center().Y})
					ball.Sound.Play()
				}
			}

			//Clear the window
			renderWindow.Clear(sf.Color{50, 200, 50, 0})

			//Draw some shit
			if isPlaying {
				renderWindow.Draw(leftPaddle.Shape, nil)
				renderWindow.Draw(rightPaddle.Shape, nil)
				renderWindow.Draw(ball.Shape, nil)
			} else {
				renderWindow.Draw(pauseMessage, nil)
			}

			//Draw everything to the screen
			renderWindow.Display()

		case <-aiTicker.C:
			if ball.BottomRight().Y > rightPaddle.BottomRight().Y {
				rightPaddle.Speed = rightPaddle.MaxSpeed
			} else if ball.TopLeft().Y < rightPaddle.TopLeft().Y {
				rightPaddle.Speed = -rightPaddle.MaxSpeed
			} else {
				rightPaddle.Speed = 0
			}
		}
	}

}
示例#3
0
func (this *Game) Update() {
	if this.State == 0 {
		// Opening state
		for event := this.RenderWindow.PollEvent(); event != nil; event = this.RenderWindow.PollEvent() {
			switch ev := event.(type) {
			case sf.EventKeyReleased:
				if ev.Code == sf.KeyEscape {
					this.RenderWindow.Close()
				}
				if ev.Code == sf.KeyReturn {
					this.State = 1
					this.GameStateInit()
				}
			case sf.EventClosed:
				this.RenderWindow.Close()
			}
		}
	} else if this.State == 1 {
		// Game state
		for event := this.RenderWindow.PollEvent(); event != nil; event = this.RenderWindow.PollEvent() {
			switch ev := event.(type) {
			case sf.EventKeyReleased:
				if ev.Code == sf.KeyEscape {
					this.RenderWindow.Close()
				}
			case sf.EventClosed:
				this.RenderWindow.Close()
			}
		}
		t := time.Now().UnixNano()
		this.TimeText.SetString(fmt.Sprintf("Collection time: %v", time.Now().Unix()-this.StartTime))
		if t > this.LastEnemyGenerated+this.EnemyGenerationSpeed {
			this.LastEnemyGenerated = t
			this.EnemyList = append(this.EnemyList, NewEnemy(int(this.Width), this.Textures))
		}
		if sf.KeyboardIsKeyPressed(sf.KeyLeft) == true || sf.KeyboardIsKeyPressed(sf.KeyA) == true {
			this.Player.MoveLeft()
		}
		if sf.KeyboardIsKeyPressed(sf.KeyUp) == true || sf.KeyboardIsKeyPressed(sf.KeyW) == true {
			this.Player.MoveUp()
		}
		if sf.KeyboardIsKeyPressed(sf.KeyRight) == true || sf.KeyboardIsKeyPressed(sf.KeyD) == true {
			this.Player.MoveRight()
		}
		if sf.KeyboardIsKeyPressed(sf.KeyDown) == true || sf.KeyboardIsKeyPressed(sf.KeyS) == true {
			this.Player.MoveDown()
		}
		for i := 0; i < len(this.EnemyList); i++ {
			if this.EnemyList[i].Collision(this.Player) == true {
				if this.EnemyList[i].GetHasFather() == true && this.EnemyList[i].GetHasMother() == false {
					if this.EnemyList[i].GetFatherHasWeapon() == false {
						this.ChildStolen.Play()
						this.ChildsCollected += 1
						this.ChildsCollectedText.SetString(fmt.Sprintf("Childs Collected: %v", this.ChildsCollected))
						this.EnemyList = append(this.EnemyList[:i], this.EnemyList[i+1:]...)
						i--
					} else if this.EnemyList[i].GetFatherHasWeapon() == true {
						// End the game, because the Child Service worker was killed.
						this.KilledStateInit()
						this.State = 2
					}
				} else {
					// End the game and call the Child Service worker a Monster.
					this.MonsterStateInit()
					this.State = 3
				}
			} else {
				this.EnemyList[i].MoveDown()
				if this.EnemyList[i].Destroy(int(this.Height)) == true {
					this.EnemyList = append(this.EnemyList[:i], this.EnemyList[i+1:]...)
					i--
				}
			}
		}
		if this.ChildsCollected > 20 && this.ChildsCollected < 40 {
			this.EnemyGenerationSpeed = 500000000
		} else if this.ChildsCollected > 40 && this.ChildsCollected < 60 {
			this.EnemyGenerationSpeed = 400000000
		} else if this.ChildsCollected > 60 && this.ChildsCollected < 80 {
			this.EnemyGenerationSpeed = 300000000
		} else if this.ChildsCollected > 80 && this.ChildsCollected < 100 {
			this.EnemyGenerationSpeed = 200000000
		} else if this.ChildsCollected > 100 && this.ChildsCollected < 150 {
			this.EnemyGenerationSpeed = 100000000
		} else if this.ChildsCollected > 150 && this.ChildsCollected < 300 {
			this.EnemyGenerationSpeed = 50000000
		} else if this.ChildsCollected > 300 {
			this.EnemyGenerationSpeed = 25000000
		}
	} else if this.State == 2 {
		// ending when killed state
		this.Music.Stop()
		for event := this.RenderWindow.PollEvent(); event != nil; event = this.RenderWindow.PollEvent() {
			switch ev := event.(type) {
			case sf.EventKeyReleased:
				if ev.Code == sf.KeyEscape {
					this.RenderWindow.Close()
				}
				if ev.Code == sf.KeyReturn {
					this.GameStateInit()
					this.State = 0
				}
			case sf.EventClosed:
				this.RenderWindow.Close()
			}
		}
	} else if this.State == 3 {
		// ending when called a monster state
		this.Music.Stop()
		for event := this.RenderWindow.PollEvent(); event != nil; event = this.RenderWindow.PollEvent() {
			switch ev := event.(type) {
			case sf.EventKeyReleased:
				if ev.Code == sf.KeyEscape {
					this.RenderWindow.Close()
				}
				if ev.Code == sf.KeyReturn {
					this.GameStateInit()
					this.State = 0
				}
			case sf.EventClosed:
				this.RenderWindow.Close()
			}
		}
	}
}