-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
135 lines (131 loc) · 4.61 KB
/
main.ts
File metadata and controls
135 lines (131 loc) · 4.61 KB
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
131
132
133
//% weight=100 color=#008080
//% groups=[ "Create", "Properties", "Actions", "List"]
namespace circle{
//% group="Create"
//% block="destroy %circle=variables_get(myCircle)"
export function destroy(circle:Circle) {
circle.destroy()
}
//% group="Create"
//% block="create circle of radius %radius color %color || fill-color %fillColor"
//% blockSetVariable=myCircle
//% radius.min=5 radius.max=60 radius.defl=30
//% color.min=0 color.max=15 color.defl=2
//% fillColor.min=0 fillColor.max=15 fillColor.defl=0
// color.shadow="colorindexpicker"
// fillColor.shadow="colorindexpicker"
export function createCircle(radius: number, color: number , fillColor:number = 0 ): Circle {
const output = new Circle( radius, color, fillColor)
// below is normally done in `sprites.create`
// below code is from statusBar https://github.com/jwunderl/pxt-status-bar
const cs = game.currentScene();
cs.physicsEngine.addSprite(output);
return output
}
//% group="List"
//% blockSetVariable=myCircle
//% block="find circle with %sprite=variables_get(mySprite) in circle list %circles=variables_get(myCirclesList)"
export function getCircleWithSprite(sprite:Sprite, circles:Circle[]):Circle {
for(let i = 0; i < circles.length; i++) {
if(circles[i].sprite == sprite) return circles[i]
}
return null
}
//% group="List"
//% blockSetVariable=myCircleList
//% block="get circles of kind %kind from circle list %circles=variables_get(myCirclesList)"
//% kind.shadow=spritekind
export function getCirclesOfKind( kind:number, circles: Circle[]):Circle[] {
let spriteList:Sprite[] = sprites.allOfKind(kind)
let j = spriteList.find(function(value: Sprite, index: number) {
return false
})
let circleList: Circle[] = []
for(let k = 0; k < spriteList.length; k++) {
circleList.push(getCircleWithSprite( spriteList[k], circles))
}
return circleList
}
//% group="List"
//% blockSetVariable=myCircleList
//% block="empty circle list"
export function emptyCircleList () {
let circleList = 0
return circleList
}
}
//% blockNamespace=circle
class Circle extends Sprite {
_radius: number = 0
_color: number = 0
_fillColor: number = 0
_dataText:string = ""
_dataNumber:number = 0
_centerX: number = 0
_centerY: number = 0
constructor(radius:number, color:number, fillColor:number ){
super(image.create(2 * radius + 2, 2 * radius + 2));
this._radius = radius
this._color = color
this._centerX = this._radius + 1
this._centerY = this._radius + 1
this._fillColor = fillColor;
if (this._fillColor == 0)
{
this.image.drawCircle(this._centerX, this._centerY, this._radius, this._color)
} else {
this.image.fillCircle(this._centerX, this._centerY, this._radius, this._fillColor)
}
this._dataText = ""
this._dataNumber = 0
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="sprite"
get sprite():Sprite {
return this // I wish I understood why this works
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="color"
get color(): number {
return this._color;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="color"
set color(value: number) {
this._color = value;
this.image.drawCircle(this._centerX, this._centerY, this._radius, this._color);
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="radius"
get radius(): number {
return this._radius;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="fill color"
get fillColor() {
return this._fillColor;
}
//% group="Actions"
//% block="erase fill from %Circle(myCircle)"
unfill() {
this._fillColor = 0;
this.image.fill(0); //clear anything in image
this.image.drawCircle(this._centerX, this._centerY, this._radius, this._color);
}
//% group="Actions"
//% block="fill %Circle(myCircle) || with color $color"
fill(color: number = -1 ){
if (color == -1)
{
this._fillColor = this._color
} else {
this._fillColor = color
}
this.image.fillCircle(this._centerX, this._centerY, this._radius, this._fillColor)
}
}