-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
157 lines (154 loc) · 5.03 KB
/
main.ts
File metadata and controls
157 lines (154 loc) · 5.03 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Circle extension creates a sprite with data for color, radius and fill/unfilled state
* Circle extension also provides the colorIndexPicker for use in any block where a color is required
* and provides a random color generator with the ability to exclude up to 3 colors.
*/
//% weight=100 color=#008080
namespace circle{
/**
* Returns selected color
*/
//% blockId=colorIndexPicker
//% block="color %color"
//% color.shadow="colorindexpicker"
//% help=circle/color-index-picker
export function colorIndexPicker(color:number){
return color
}
/**
* returns a random color 0-15 excluding up to 3 colors
*/
//% blockId=randomColor
//% block=" random color || excluding %n colors: %c1 %c2 %c3"
//% inlineInputMode=inline
//% n.min=0 n.max=3 n.defl=2
//% c1.min=0 c1.max=15 c1.defl=0
//% c2.min=0 c2.max=15 c2.defl=15
//% c3.min=0 c3.max=15 c3.defl=0
//% help=github:circle-ext-best/circle/random-color
export function randomColor(n:number = 2, c1:number = 0, c2: number = 15, c3:number = 0): number{
switch(n) {
case 0: {
c1 = -1
c2 = -1
c3 = -1
break;
}
case 1: {
c2 = -1
c3 = -1
break;
}
case 2: {
c3 = -1
break;
}
default: {
break;
}
}
let clr = randint(0, 15)
// c1 or c2 or c3 = -1 will not be color
while(clr == c1 || clr == c2 || clr == c3) {
clr = randint(0, 15)
}
return clr
}
/**
* Erases fill from circle
*/
//% blockId=unfill
//% block="erase fill from %c=variables_get(myCircleSprite)"
//% help=circle/unfill
export function unfill(c: Sprite) {
sprites.setDataBoolean(c, "filled", false)
makeCircle(c)
}
/**
* Fills a circle with the previously set color
*/
//% blockId=fill
//% block="fill %c=variables_get(myCircleSprite)"
//% help=circle/fill
export function fill(c: Sprite){
sprites.setDataBoolean(c, "filled", true)
makeCircle(c)
}
/**
* Returns true if circle filled, false otherwise
*/
//% blockId=getFilled
//% block="%c=variables_get(myCircleSprite) filled"
//% help=circle/get-filled
export function getFilled(c:Sprite): boolean {
return sprites.readDataBoolean(c,"filled" )
}
/**
* Returns the color of a circle
*/
//% blockId=getColor
//% block="%c=variables_get(myCircleSprite) color"
//% help=circle/get-color
export function getColor(c:Sprite): number {
return sprites.readDataNumber(c, "color")
}
/**
* Set the color of a circle
*/
//% blockId=setColor
//% block="set %c=variables_get(myCircleSprite) color to %color"
//% color.min=0 color.max=15 color.defl=2
//% help=circle/set-color
export function setColor(c: Sprite, color: number) {
sprites.setDataNumber(c, "color", color % 16)
makeCircle(c)
}
/**
* Return the radius of a circle
*/
//% blockId=getRadius
//% block="%c=variables_get(myCircleSprite) radius"
//% help=circle/get-radius
export function getRadius(c: Sprite): number {
return sprites.readDataNumber(c, "radius")
}
/**
* Create and returns a circle object
*/
//% blockId=creaeCircle
//% blockSetVariable=myCircleSprite
//% block="create circle of radius %radius color %color || filled %fillColor"
//% radius.min=5 radius.max=60 radius.defl=30
//% color.min=0 color.max=15 color.defl=2
//% fill.defl=false
//% help=github:circle-ext-best/circle/create-circle
export function createCircle(radius: number, color: number , filled:boolean = false ): Sprite {
let circleImage = image.create(2 * radius + 2, 2 * radius + 2);
let centerX = radius + 1
let centerY = radius + 1
let c = sprites.create(circleImage)
sprites.setDataNumber(c, "radius", radius)
sprites.setDataNumber(c, "centerX", centerX)
sprites.setDataNumber(c, "centerY", centerY)
sprites.setDataNumber(c, "color", color % 16)
sprites.setDataBoolean(c, "filled", filled)
makeCircle(c)
return c
}
/**
* Draws a circle in a sprite using info from sprite data
*/
function makeCircle(c:Sprite){
let radius: number = sprites.readDataNumber(c,"radius")
let color: number = sprites.readDataNumber(c,"color")
let centerX: number = sprites.readDataNumber(c,"centerX")
let centerY: number = sprites.readDataNumber(c,"centerY")
let filled: boolean = sprites.readDataBoolean(c,"filled")
if (filled){
c.image.fillCircle(centerX, centerY, radius, color)
} else{
c.image.fillCircle(centerX, centerY, radius, 0)
}
c.image.drawCircle(centerX, centerY, radius, color)
}
}