1
0

main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021 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. "image/color"
  17. "log"
  18. "math/rand/v2"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/vector"
  21. )
  22. const (
  23. screenWidth = 640
  24. screenHeight = 480
  25. scale = 64
  26. starsCount = 1024
  27. )
  28. type Star struct {
  29. fromx, fromy, tox, toy, brightness float32
  30. }
  31. func (s *Star) Init() {
  32. s.tox = rand.Float32() * screenWidth * scale
  33. s.fromx = s.tox
  34. s.toy = rand.Float32() * screenHeight * scale
  35. s.fromy = s.toy
  36. s.brightness = rand.Float32() * 0xff
  37. }
  38. func (s *Star) Update(x, y float32) {
  39. s.fromx = s.tox
  40. s.fromy = s.toy
  41. s.tox += (s.tox - x) / 32
  42. s.toy += (s.toy - y) / 32
  43. s.brightness += 1
  44. if 0xff < s.brightness {
  45. s.brightness = 0xff
  46. }
  47. if s.fromx < 0 || screenWidth*scale < s.fromx || s.fromy < 0 || screenHeight*scale < s.fromy {
  48. s.Init()
  49. }
  50. }
  51. func (s *Star) Draw(screen *ebiten.Image) {
  52. c := color.RGBA{
  53. R: uint8(0xbb * s.brightness / 0xff),
  54. G: uint8(0xdd * s.brightness / 0xff),
  55. B: uint8(0xff * s.brightness / 0xff),
  56. A: 0xff}
  57. vector.StrokeLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, 1, c, true)
  58. }
  59. type Game struct {
  60. stars [starsCount]Star
  61. }
  62. func NewGame() *Game {
  63. g := &Game{}
  64. for i := 0; i < starsCount; i++ {
  65. g.stars[i].Init()
  66. }
  67. return g
  68. }
  69. func (g *Game) Update() error {
  70. x, y := ebiten.CursorPosition()
  71. for i := 0; i < starsCount; i++ {
  72. g.stars[i].Update(float32(x*scale), float32(y*scale))
  73. }
  74. return nil
  75. }
  76. func (g *Game) Draw(screen *ebiten.Image) {
  77. for i := 0; i < starsCount; i++ {
  78. g.stars[i].Draw(screen)
  79. }
  80. }
  81. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  82. return screenWidth, screenHeight
  83. }
  84. func main() {
  85. ebiten.SetWindowSize(screenWidth, screenHeight)
  86. ebiten.SetWindowTitle("Stars (Ebitengine Demo)")
  87. if err := ebiten.RunGame(NewGame()); err != nil {
  88. log.Fatal(err)
  89. }
  90. }