/
main.go
130 lines (104 loc) · 2.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"image"
"image/color"
"math/rand"
"time"
"github.com/GaryBrownEEngr/scratch"
"github.com/GaryBrownEEngr/scratch/models"
"github.com/GaryBrownEEngr/scratch/sprite"
"github.com/GaryBrownEEngr/scratch/tools"
)
var SkyBlue color.RGBA = color.RGBA{0x87, 0xCE, 0xEB, 0xFF}
func main() {
params := scratch.ScratchParams{Width: 1000, Height: 1000, ShowFPS: true}
scratch.Start(params, simStartFunc)
}
func simStartFunc(sim models.Scratch) {
go RunMouse(sim)
// Set the background by making a single pixel image and scaling it be be the entire screen.
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
img.Set(0, 0, SkyBlue)
sim.AddCostume(img, "skyBackground")
s := sim.AddSprite("background")
s.Costume("skyBackground")
s.Z(0)
s.Scale(1010)
s.Visible(true)
sim.AddCostume(sprite.DecodeCodedSprite(sprite.TurtleImage), "t")
s = sim.AddSprite("mainTurtle")
b := s.GetClickBody()
s.Costume("t")
s.Scale(1)
s.Angle(-90)
s.Pos(0, 0)
b.AddCircleBody(0, 0, 32)
s.Z(1)
// s.Visible(true)
// MainSpriteLoop:
for {
sClone := s.Clone(fmt.Sprintf("%d%d", rand.Uint64(), rand.Uint64()))
go RunClone(sim, sClone)
time.Sleep(time.Millisecond * time.Duration(rand.Float64()*3000))
if GameState.GameOver {
break
}
}
}
type GameStateStruct struct {
GameOver bool
Score int
}
var GameState GameStateStruct
func RunClone(sim models.Scratch, s models.Sprite) {
justPressedChan := sim.SubscribeToJustPressedUserInput()
cb := s.GetClickBody()
randomX := rand.Intn(1000) - 500
s.Pos(float64(randomX), 550)
s.Visible(true)
speed := rand.Float64() * 2
y := 550.0
// MainSpriteLoop:
for {
justPressed := scratch.GetNewestJustPressedFromChan(justPressedChan)
if justPressed != nil && justPressed.Mouse.Left {
if cb.IsMouseClickInBody(float64(justPressed.Mouse.MouseX), float64(justPressed.Mouse.MouseY)) {
s.DeleteSprite()
sim.UnSubscribeToJustPressedUserInput(justPressedChan)
GameState.Score++
fmt.Println(GameState.Score)
break
}
}
if y <= -500 {
GameState.GameOver = true
fmt.Println("Game Over")
ShowGameOver(sim)
}
if GameState.GameOver {
break
}
y -= speed
s.Pos(float64(randomX), y)
time.Sleep(time.Millisecond * 10)
}
}
// Test the WhoIsNearMe functionality.
func RunMouse(sim models.Scratch) {
for {
posInfo := sim.PressedUserInput()
nearMeList := sim.WhoIsNearMe(float64(posInfo.Mouse.MouseX), float64(posInfo.Mouse.MouseY), 50)
fmt.Printf("%v\n", nearMeList)
time.Sleep(time.Millisecond * 100)
}
}
func ShowGameOver(sim models.Scratch) {
imgGameOver := tools.CreateTextImg(fmt.Sprintf("GAME OVER\nSCORE: %d", GameState.Score), 1000, 130, 60, tools.Red)
sim.AddCostume(imgGameOver, "GameOver")
s := sim.AddSprite("GameOver")
s.Costume("GameOver")
s.Z(9)
s.Pos(0, 0)
s.Visible(true)
}