func (this *Game) KilledStateInit() { this.EndTime = time.Now().Unix() this.EndingText, _ = sf.NewText(this.Font) this.EndingText.SetString(fmt.Sprintf("You were killed by a Father who had a weapon.")) this.EndingText.SetCharacterSize(36) this.EndingText.SetColor(sf.Color{0, 0, 0, 255}) EndingTextRect := this.EndingText.GetLocalBounds() x := (float32(this.Width) - EndingTextRect.Width) / 2 this.EndingText.SetPosition(sf.Vector2f{x, 250}) this.EndingPointsText, _ = sf.NewText(this.Font) this.EndingPointsText.SetString(fmt.Sprintf("You collected %v children in %v seconds.", this.ChildsCollected, this.EndTime-this.StartTime)) this.EndingPointsText.SetCharacterSize(36) this.EndingPointsText.SetColor(sf.Color{0, 0, 0, 255}) EndingPointsTextRect := this.EndingPointsText.GetLocalBounds() x = (float32(this.Width) - EndingPointsTextRect.Width) / 2 this.EndingPointsText.SetPosition(sf.Vector2f{x, 350}) this.RestartText, _ = sf.NewText(this.Font) this.RestartText.SetString(fmt.Sprintf("Press Enter to restart the game!")) this.RestartText.SetCharacterSize(42) this.RestartText.SetColor(sf.Color{0, 0, 0, 255}) RestartTextRect := this.RestartText.GetLocalBounds() x = (float32(this.Width) - RestartTextRect.Width) / 2 this.RestartText.SetPosition(sf.Vector2f{x, 450}) this.CreditsText, _ = sf.NewText(this.Font) this.CreditsText.SetString(fmt.Sprintf("Created for Ludum Dare 33 Compo by Toni Korpela!")) this.CreditsText.SetCharacterSize(42) this.CreditsText.SetColor(sf.Color{0, 0, 0, 255}) CreditsTextRect := this.CreditsText.GetLocalBounds() x = (float32(this.Width) - CreditsTextRect.Width) / 2 this.CreditsText.SetPosition(sf.Vector2f{x, 550}) }
func (this *Game) GameStateInit() { this.Player = NewPlayer(int(this.Width), int(this.Height), this.Textures) this.EnemyList = make([]*Enemy, 0) this.EnemyList = append(this.EnemyList, NewEnemy(int(this.Width), this.Textures)) this.LastEnemyGenerated = time.Now().UnixNano() this.EnemyGenerationSpeed = 500000000 this.ChildsCollected = 0 this.ChildsCollectedText, _ = sf.NewText(this.Font) this.ChildsCollectedText.SetString(fmt.Sprintf("Childs Collected: %v", this.ChildsCollected)) this.ChildsCollectedText.SetCharacterSize(24) this.ChildsCollectedText.SetColor(sf.Color{0, 0, 0, 255}) this.ChildsCollectedText.SetPosition(sf.Vector2f{20, 20}) this.StartTime = time.Now().Unix() this.TimeText, _ = sf.NewText(this.Font) this.TimeText.SetString(fmt.Sprintf("Collection time: %v", time.Now().Unix()-this.StartTime)) TimeTextRect := this.TimeText.GetLocalBounds() this.TimeText.SetOrigin(sf.Vector2f{float32(TimeTextRect.Width), 0.0}) this.TimeText.SetCharacterSize(24) this.TimeText.SetColor(sf.Color{0, 0, 0, 255}) this.TimeText.SetPosition(sf.Vector2f{float32(int(this.Width) - 20), 20}) this.Music, _ = sf.NewMusicFromFile("res/audio/Child_Service_-_Going_to_steal_your_Kids.ogg") this.Music.SetLoop(true) this.Music.Play() this.ChildStolen, _ = sf.NewMusicFromFile("res/audio/childstolen.wav") }
func (this *Game) StartStateInit() { this.WelcomeText, _ = sf.NewText(this.Font) this.WelcomeText.SetString(fmt.Sprintf("You work now in Child Service and you must collect every child who has no mother!")) this.WelcomeText.SetCharacterSize(36) this.WelcomeText.SetColor(sf.Color{0, 0, 0, 255}) WelcomeTextRect := this.WelcomeText.GetLocalBounds() x := (float32(this.Width) - WelcomeTextRect.Width) / 2 this.WelcomeText.SetPosition(sf.Vector2f{x, 250}) this.PlayText, _ = sf.NewText(this.Font) this.PlayText.SetString(fmt.Sprintf("Press Enter to play!")) this.PlayText.SetCharacterSize(42) this.PlayText.SetColor(sf.Color{0, 0, 0, 255}) PlayTextRect := this.PlayText.GetLocalBounds() x = (float32(this.Width) - PlayTextRect.Width) / 2 this.PlayText.SetPosition(sf.Vector2f{x, 350}) }
//NewGame initializes a Game struct. func NewGame() *Game { g := new(Game) g.Settings = readSettings() g.window = sf.NewRenderWindow(sf.VideoMode{uint(g.resW), uint(g.resH), 32}, "GoSFMLike", sf.StyleDefault, sf.DefaultContextSettings()) g.state = PLAY g.area = NewArea() g.player = NewEntity("player", 0, 0, 3, 4, g.area) g.cursor = NewEntity("cursor", 0, 0, 2, 2, g.area) for i := 0; i < 3; i++ { g.mobs = append(g.mobs, NewEntityFromFile("orc", 3+i, 1, g.area)) g.items = append(g.items, NewEntityFromFile("potion", 4, 4, g.area)) } g.mobs = append(g.mobs, g.player) g.gameView = sf.NewView() g.gameView.SetCenter(g.player.PosVector()) g.gameView.SetSize(sf.Vector2f{g.resW * 0.75, g.resH * 0.75}) g.gameView.SetViewport(sf.FloatRect{0, 0, .75, .75}) g.statusView = sf.NewView() g.statusView.SetSize(sf.Vector2f{g.resW * 0.25, g.resH}) g.statusView.SetCenter(sf.Vector2f{(g.resW * 0.25) / 2, g.resH / 2}) g.statusView.SetViewport(sf.FloatRect{.77, 0, .25, 1}) g.hpText, _ = sf.NewText(Font) g.hpText.SetCharacterSize(12) g.logView = sf.NewView() var err error g.lookText, err = sf.NewText(Font) if err != nil { panic(err) } g.lookText.SetCharacterSize(12) g.logText, _ = sf.NewText(Font) g.logText.SetCharacterSize(12) return g }
func MockNewGame() *Game { g := new(Game) g.area = NewArea() g.player = NewEntity("player", 0, 0, 12, 12, g.area) g.cursor = NewEntity("cursor", 0, 0, 12, 12, g.area) g.gameView = sf.NewView() var err error g.lookText, err = sf.NewText(Font) if err != nil { panic(err) } return g }
//listUsables lists all the items that have an effect, and prompts the user to use one. func (g *Game) listUsables() { letter := 'a' listText, _ := sf.NewText(Font) listText.SetCharacterSize(12) listText.SetPosition(sf.Vector2f{12, 12}) usables := make(map[rune]*Item) names := make(map[*Item]string) for k, i := range g.player.inventory { if i.effect != nil { appendString(listText, strconv.QuoteRune(letter)+" - "+k+" x"+strconv.Itoa(i.stack)) usables[letter] = i names[i] = k letter++ } } listLoop: for g.window.IsOpen() { for event := g.window.PollEvent(); event != nil; event = g.window.PollEvent() { switch et := event.(type) { case sf.EventTextEntered: done, used := g.inventoryInput(et.Char, usables, names) if used != "" { usedI := g.player.inventory[used] if usedI.stack > 1 { usedI.stack-- break listLoop } delete(g.player.inventory, used) break listLoop } if done { break listLoop } } } g.window.Clear(sf.ColorBlack()) g.window.SetView(g.logView) g.drawLog() g.window.SetView(g.gameView) listText.Draw(g.window, sf.DefaultRenderStates()) g.window.Display() } g.state = PLAY }
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 } } } }