main.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "image/color"
  18. "log"
  19. "math/rand/v2"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. )
  23. const (
  24. screenWidth = 320
  25. screenHeight = 240
  26. )
  27. type Game struct {
  28. offscreen *ebiten.Image
  29. }
  30. func NewGame() *Game {
  31. return &Game{
  32. offscreen: ebiten.NewImage(screenWidth, screenHeight),
  33. }
  34. }
  35. func (g *Game) Update() error {
  36. s := g.offscreen.Bounds().Size()
  37. x := rand.IntN(s.X)
  38. y := rand.IntN(s.Y)
  39. c := color.RGBA{
  40. byte(rand.IntN(256)),
  41. byte(rand.IntN(256)),
  42. byte(rand.IntN(256)),
  43. byte(0xff),
  44. }
  45. g.offscreen.Set(x, y, c)
  46. return nil
  47. }
  48. func (g *Game) Draw(screen *ebiten.Image) {
  49. screen.DrawImage(g.offscreen, nil)
  50. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
  51. }
  52. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  53. return screenWidth, screenHeight
  54. }
  55. func main() {
  56. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  57. ebiten.SetWindowTitle("Set (Ebitengine Demo)")
  58. if err := ebiten.RunGame(NewGame()); err != nil {
  59. log.Fatal(err)
  60. }
  61. }