1
+ final int STATE_START = 0 ;
2
+ final int STATE_PLAYING = 1 ;
3
+ final int STATE_UPDATE_LEVEL = 2 ;
4
+ final int STATE_OVER = 3 ;
5
+
6
+ final int MAX_SCORE = 10 ;
7
+
8
+
9
+ int currentState;
10
+ int currentScore = 5 ;
11
+
12
+
13
+ float posx;
14
+ float posy;
15
+ float incx = 5 ;
16
+ float incy = 5 ;
17
+
18
+
19
+ // cointains the pad position and width and height
20
+ float posXPad, posYPad;
21
+ float padW, padH;
22
+
23
+
24
+ float scaleValue = 1 ;
25
+ color backColor;
26
+ int blink = 2 ;
27
+
28
+ long lastTime = 0 ;
29
+
30
+ void setup () {
31
+ size (800 , 500 );
32
+ frameRate (60 );
33
+ posx = random (0 , width );
34
+ posy = random (0 , height );
35
+ backColor = color (255 );
36
+
37
+ posXPad = 100 ;
38
+ posYPad = height / 2 ;
39
+ padW = 50 ; // width
40
+ padH = 90 ; // height
41
+
42
+ currentState = STATE_START ;
43
+ }
44
+
45
+ void draw () {
46
+
47
+ background (255 );
48
+
49
+ if (currentState == STATE_START ) {
50
+ textSize (45 );
51
+ fill (255 , 0 , 0 );
52
+ textAlign (CENTER );
53
+ textSize (10 + frameCount % 100 );
54
+ text (" Click to Start.." , width / 2 , height - 100 );
55
+
56
+
57
+ if (mousePressed == true ) {
58
+ currentState = STATE_PLAYING ;
59
+ }
60
+ } else if (currentState == STATE_PLAYING ) {
61
+ background (backColor);
62
+
63
+ mainGame();
64
+ textSize (50 );
65
+ fill (255 );
66
+ text (currentScore, width / 2 , height / 2 );
67
+
68
+ if (currentScore <= 0 ) {
69
+ currentState = STATE_OVER ;
70
+ lastTime = millis ();
71
+
72
+ } else if (currentScore >= MAX_SCORE ) {
73
+ currentState = STATE_UPDATE_LEVEL ;
74
+ lastTime = millis ();
75
+ }
76
+ } else if (currentState == STATE_UPDATE_LEVEL ) {
77
+
78
+ long cTimer = millis () - lastTime;
79
+
80
+ background (255 );
81
+ textSize (45 );
82
+ fill (255 , 0 , 0 );
83
+ text (" NEXT LEVEL" , width / 2 , height / 2 );
84
+ text ((2 - int (cTimer/ 1000 )), width / 2 , height / 2 + 100 );
85
+ if (cTimer> 2000 ) {
86
+ currentScore = 5 ;
87
+ currentState = STATE_PLAYING ;
88
+ padH -= 10 ;
89
+ lastTime = millis ();
90
+ }
91
+
92
+ } else if (currentState == STATE_OVER ) {
93
+ long cTimer = millis () - lastTime;
94
+
95
+ background (255 );
96
+ textSize (45 );
97
+ fill (255 , 0 , 0 );
98
+ text (" GAME OVER" , width / 2 , height / 2 );
99
+ text ((4 - int (cTimer/ 1000 )), width / 2 , height / 2 + 100 );
100
+ if (cTimer> 4000 ) {
101
+
102
+ currentState = STATE_START ;
103
+ currentScore = 5 ;
104
+ lastTime = millis ();
105
+ }
106
+ }
107
+ }
108
+
109
+ void mainGame () {
110
+
111
+ drawRobot(scaleValue, blink, posx, posy); // calling the function with fixed parameters
112
+ drawPad(posXPad, posYPad);
113
+
114
+
115
+ if (millis () - lastTime > 200 ) {
116
+ blink = (int )random (0 , 3 );
117
+ lastTime = millis ();
118
+ }
119
+
120
+ float r = map (posx, 0 , width , 0 , 255 );
121
+ float b = map (posy, 0 , width , 0 , 255 );
122
+
123
+ backColor = color (r, b, 255 );
124
+
125
+ posx = posx + incx;
126
+ posy = posy + incy;
127
+
128
+ posYPad = mouseY ;
129
+
130
+
131
+ // --- try to make the dection here
132
+ //
133
+ if (posx < (posXPad + padW)) {
134
+ if (posx > posXPad) { // testing for X
135
+
136
+ if (posy > posYPad) {// testing for Y
137
+ if (posy < (posYPad + padH)) {
138
+
139
+ incx = - incx; // change both directions
140
+ incy = - incy;// change both directions
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+
147
+
148
+ // -----
149
+
150
+
151
+ // hit detection with the boudaries
152
+ // 0 < posx < width
153
+ if (posx > width ) {
154
+ incx = - incx;
155
+ currentScore = currentScore + 1 ;
156
+ }
157
+ if (posx < 0 ) {
158
+ // incx = -incx;
159
+ currentScore = currentScore - 1 ;
160
+ posx = random (0 , width );
161
+ posy = random (0 , height );
162
+ }
163
+ if (posy > height ) {
164
+ incy = - incy;
165
+ }
166
+ if (posy < 0 ) {
167
+
168
+ incy = - incy;
169
+ }
170
+ }
171
+
172
+ void drawPad (float padX , float padY ) {
173
+
174
+ pushMatrix ();
175
+ translate (padX, padY);
176
+ pushStyle ();
177
+ // rectMode(CENTER);
178
+ fill (255 , 0 , 0 );
179
+ noStroke ();
180
+ rect (0 , 0 , padW, padH);
181
+ popStyle ();
182
+ popMatrix ();
183
+ }
184
+
185
+ void keyPressed () {
186
+ if (key == ' q' ) {
187
+ } else if (key == ' w' ) {
188
+ scaleValue = random (0.5 , 5 );
189
+ } else if (key == CODED ) {
190
+ // arrow keys tests
191
+ if (keyCode == UP ) {
192
+ } else if (keyCode == DOWN ) {
193
+ } else if (keyCode == LEFT ) {
194
+ } else if (keyCode == RIGHT ) {
195
+ }
196
+ }
197
+ }
198
+
199
+
200
+ // function to draw my shape
201
+ void drawRobot (float scaleValue , int blink , float posx , float posy ) {
202
+ pushMatrix ();
203
+ // translate to given parameters
204
+
205
+ translate (posx, posy);
206
+ scale (scaleValue);
207
+ if (blink == 0 ) {
208
+
209
+ ellipse (0 , 0 , 50 , 50 );
210
+ ellipse (- 10 , 0 , 10 , 10 );
211
+ ellipse (- 10 , 0 , 3 , 3 );
212
+ ellipse (10 , 0 , 10 , 10 );
213
+ ellipse (10 , 0 , 3 , 3 );
214
+ } else if (blink == 1 ) {
215
+
216
+ ellipse (0 , 0 , 50 , 50 );
217
+
218
+ ellipse (- 10 , 0 , 10 , 1 );
219
+ ellipse (- 10 , 0 , 3 , 3 );
220
+
221
+ ellipse (10 , 0 , 10 , 1 );
222
+ ellipse (10 , 0 , 3 , 3 );
223
+ } else if (blink == 2 ) {
224
+
225
+ ellipse (0 , 0 , 50 , 50 );
226
+
227
+ ellipse (- 10 , 0 , 10 , 15 );
228
+ ellipse (- 10 , 0 , 3 , 1 );
229
+
230
+ ellipse (10 , 0 , 10 , 15 );
231
+ ellipse (10 , 0 , 3 , 1 );
232
+ }
233
+
234
+ arc (0 , 10 , 20 , 20 , radians (30 ), radians (150 )); // randians convert degrees
235
+
236
+ popMatrix ();
237
+ }
0 commit comments