func (g *Game) Draw(screen *ebiten.Image) error { if g.boardImage == nil { var err error w, h := g.board.Size() g.boardImage, err = ebiten.NewImage(w, h, ebiten.FilterNearest) if err != nil { return err } } if err := screen.Fill(backgroundColor); err != nil { return err } if err := g.board.Draw(g.boardImage); err != nil { return err } op := &ebiten.DrawImageOptions{} sw, sh := screen.Size() bw, bh := g.boardImage.Size() x := (sw - bw) / 2 y := (sh - bh) / 2 op.GeoM.Translate(float64(x), float64(y)) if err := screen.DrawImage(g.boardImage, op); err != nil { return err } return nil }
func update(screen *ebiten.Image) error { for key, _ := range keyStates { if !ebiten.IsKeyPressed(key) { keyStates[key] = 0 continue } keyStates[key]++ } d := 0 switch screenScale { case 1: d = 32 case 2: d = 16 } if keyStates[ebiten.KeyUp] == 1 { screenHeight += d } if keyStates[ebiten.KeyDown] == 1 { if 16 < screenHeight { screenHeight -= d } } if keyStates[ebiten.KeyLeft] == 1 { if 16 < screenWidth { screenWidth -= d } } if keyStates[ebiten.KeyRight] == 1 { screenWidth += d } if keyStates[ebiten.KeyS] == 1 { screenScale = 3 - screenScale // Swap 1 and 2 } ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenScale(screenScale) screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}) w, h := gophersImage.Size() w2, h2 := screen.Size() op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(-w+w2)/2, float64(-h+h2)/2) if err := screen.DrawImage(gophersImage, op); err != nil { return err } x, y := ebiten.CursorPosition() msg := fmt.Sprintf(`Press arrow keys to change the window size Press S key to change the window scale Cursor: (%d, %d) FPS: %0.2f`, x, y, ebiten.CurrentFPS()) ebitenutil.DebugPrint(screen, msg) return nil }
func updateGroundImage(ground *ebiten.Image) error { if err := ground.Clear(); err != nil { return err } x16, y16 := thePlayer.Position() a := thePlayer.Angle() gw, gh := ground.Size() w, h := gophersImage.Size() op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(-x16)/16, float64(-y16)/16) op.GeoM.Translate(float64(-w*2), float64(-h*2)) op.GeoM.Rotate(float64(-a)*2*math.Pi/maxAngle + math.Pi*3.0/2.0) op.GeoM.Translate(float64(gw)/2, float64(gh)-32) if err := ground.DrawImage(repeatedGophersImage, op); err != nil { return err } return nil }
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) error { w, _ := ground.Size() op := &ebiten.DrawImageOptions{} op.GeoM.Translate(-float64(w)/2, 0) op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8) op.GeoM.Translate(float64(w)/2, 0) op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3) op.ImageParts = &groundParts{ground} if err := screen.DrawImage(ground, op); err != nil { return err } op = &ebiten.DrawImageOptions{} op.GeoM.Translate(-float64(w)/2, 0) op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8) op.GeoM.Translate(float64(w)/2, 0) op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3) if err := screen.DrawImage(fogImage, op); err != nil { return err } return nil }