func NewPaddle(speed, max_speed float32, size sf.Vector2f, color sf.Color) *Paddle { shape := sf.NewRectangleShape() //Take 3 off each edge to account for outline thickness shape.SetSize(sf.Vector2f{size.X - 3, size.Y - 3}) shape.SetOutlineThickness(3) shape.SetOutlineColor(sf.ColorBlack()) shape.SetFillColor(color) shape.SetOrigin(sf.Vector2f{size.X / 2, size.Y / 2}) return &Paddle{speed, max_speed, size, shape} }
func NewBall(speed, max_speed, radius float32, sound_file string) *Ball { //Once again, accounting for outline thickness shape := sf.NewCircleShape(radius - 3) shape.SetOutlineThickness(3) shape.SetOutlineColor(sf.ColorBlack()) shape.SetFillColor(sf.ColorWhite()) shape.SetOrigin(sf.Vector2f{radius / 2, radius / 2}) buffer, _ := sf.NewSoundBufferFromFile(sound_file) sound := sf.NewSound(buffer) return &Ball{speed, max_speed, float32(0), radius, shape, sound} }
//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 (g *Game) run() { for g.window.IsOpen() { wait := true pollLoop: for event := g.window.PollEvent(); event != nil; event = g.window.PollEvent() { switch et := event.(type) { case sf.EventClosed: g.window.Close() case sf.EventTextEntered: wait = g.handleInput(et.Char) break pollLoop } } g.window.Clear(sf.ColorBlack()) // Draw status stuff. g.window.SetView(g.statusView) g.hpText.SetString("HP: " + strconv.Itoa(g.player.curhp) + "/" + strconv.Itoa(g.player.maxhp)) g.hpText.Draw(g.window, sf.DefaultRenderStates()) g.drawLog() if g.state != INVENTORY { g.window.SetView(g.gameView) g.Draw(g.area) //Draw items for _, i := range g.items { g.Draw(i) } //Process mobs Ai, check for deaths and draw them. for i, m := range g.mobs { if m.Mob == nil { mPos := m.Position() for i := 0; i < 3; i++ { // Spill blood. r := rand.Perm(3) g.area.tiles[(mPos.X+r[0]-1)+(mPos.Y+r[2]-1)*g.area.width].SetColor(sf.ColorRed()) } g.items = append(g.items, m) g.mobs = removeFromList(g.mobs, i) } if !wait && m != g.player && m.Mob != nil { g.processAI(m) } g.Draw(m) } //Check if player died. if g.player.Mob == nil { fmt.Print("Game Over, you died.\n") g.window.Close() return } if g.state == LOOK { g.Draw(g.cursor) g.lookText.Draw(g.window, sf.DefaultRenderStates()) } g.window.Display() } } }