1
+ float posx;
2
+ float posy;
3
+ float scaleValue = 1 ;
4
+ color backColor;
5
+ int blink = 2 ;
6
+
7
+ long lastTime = 0 ;
8
+ void setup () {
9
+ size (800 , 500 );
10
+ frameRate (24 );
11
+ posx = random (0 , width );
12
+ posy = random (0 , height );
13
+ backColor = color (255 );
14
+ }
15
+
16
+ void draw () {
17
+
18
+ background (backColor);
19
+
20
+
21
+ drawRobot(scaleValue, blink, posx, posy); // calling the function with fixed parameters
22
+
23
+ if (millis () - lastTime > 200 ){
24
+ blink = (int )random (0 ,3 );
25
+ lastTime = millis ();
26
+ }
27
+ // posx = posx + 10;
28
+ // posy = posy + 10;
29
+ float r = map (posx, 0 , width , 0 , 255 );
30
+ float b = map (posy, 0 , width , 0 , 255 );
31
+
32
+ backColor = color (r, b, 255 );
33
+
34
+ if (posx > width ) {
35
+ posx = random (0 , width );
36
+ scaleValue = random (0.5 , 5 );
37
+ }
38
+ if (posy > height ) {
39
+ posy = random (0 , height );
40
+ scaleValue = random (0.5 , 5 );
41
+ }
42
+
43
+ pushStyle ();
44
+ fill (0 );
45
+ noStroke ();
46
+ textSize (20 );
47
+ text (" Press the arrows to move, q and w to random" , 10 ,20 );
48
+ popStyle ();
49
+ }
50
+
51
+ void keyPressed () {
52
+ if (key == ' q' ) {
53
+ posx = random (0 , width );
54
+ posy = random (0 , height );
55
+ } else if (key == ' w' ) {
56
+ scaleValue = random (0.5 , 5 );
57
+ } else if (key == CODED ) {
58
+ // arrow keys tests
59
+ if (keyCode == UP ) {
60
+ posy = posy - 10 ;
61
+ } else if (keyCode == DOWN ) {
62
+ posy = posy + 10 ;
63
+ } else if (keyCode == LEFT ) {
64
+ posx = posx - 10 ;
65
+ } else if (keyCode == RIGHT ) {
66
+ posx = posx + 10 ;
67
+ }
68
+ }
69
+ }
70
+
71
+
72
+ // function to draw my shape
73
+ void drawRobot (float scaleValue , int blink , float posx , float posy ) {
74
+ pushMatrix ();
75
+ // translate to given parameters
76
+
77
+ translate (posx, posy);
78
+ scale (scaleValue);
79
+ if (blink == 0 ) {
80
+
81
+ ellipse (0 , 0 , 50 , 50 );
82
+ ellipse (- 10 , 0 , 10 , 10 );
83
+ ellipse (- 10 , 0 , 3 , 3 );
84
+ ellipse (10 , 0 , 10 , 10 );
85
+ ellipse (10 , 0 , 3 , 3 );
86
+
87
+ } else if (blink == 1 ) {
88
+
89
+ ellipse (0 , 0 , 50 , 50 );
90
+
91
+ ellipse (- 10 , 0 , 10 , 1 );
92
+ ellipse (- 10 , 0 , 3 , 3 );
93
+
94
+ ellipse (10 , 0 , 10 , 1 );
95
+ ellipse (10 , 0 , 3 , 3 );
96
+
97
+ } else if (blink == 2 ) {
98
+
99
+ ellipse (0 , 0 , 50 , 50 );
100
+
101
+ ellipse (- 10 , 0 , 10 , 15 );
102
+ ellipse (- 10 , 0 , 3 , 1 );
103
+
104
+ ellipse (10 , 0 , 10 , 15 );
105
+ ellipse (10 , 0 , 3 , 1 );
106
+
107
+ }
108
+
109
+ arc (0 , 10 , 20 , 20 , radians (30 ), radians (150 )); // randians convert degrees
110
+
111
+ popMatrix ();
112
+ }
0 commit comments