Skip to content

Commit cafea38

Browse files
author
Kirk
committed
moved pong to use XOR
1 parent 9ec808a commit cafea38

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

examples/Example-08_Multi/Example-08_Multi.ino

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
//
6161
// The default is Micro OLED
6262

63-
#define MICRO
63+
//#define MICRO
6464
//#define NARROW
65-
//#define TRANSPARENT
65+
#define TRANSPARENT
6666

6767
//////////////////////////////////////////////////////////////////////////////////////////
6868

@@ -187,7 +187,7 @@ void shapeExample()
187187
// Paddle 1 (right) position coordinates
188188
int paddle1_Y = ( height / 2) - (paddleH / 2);
189189
int paddle1_X = width - 3 - paddleW;
190-
int ball_rad = 2; // Ball radius
190+
int ball_rad = 4; // Ball radius
191191

192192
// Ball position coordinates
193193
int ball_X = paddle0_X + paddleW + ball_rad;
@@ -197,9 +197,28 @@ void shapeExample()
197197
int paddle0Velocity = -1; // Paddle 0 velocity
198198
int paddle1Velocity = 1; // Paddle 1 velocity
199199

200-
//while(ball_X >= paddle0_X + paddleW - 1)
200+
// Draw the Pong Field
201+
myOLED.erase();
202+
203+
// Draw an outline of the screen:
204+
myOLED.rectangle(0, 0, width-1, height-1);
205+
206+
// Draw the center line
207+
myOLED.rectangleFill( width/ 2 - 1, 0, width/2 + 1, height-1);
208+
209+
bool firstLoop = true;
210+
201211
while((ball_X - ball_rad > 1) && (ball_X + ball_rad < width - 2)){
202212

213+
if(!firstLoop){
214+
215+
// Erase the old ball. In XOR mode, so just draw old values again!
216+
// Draw the Paddles:
217+
myOLED.rectangleFill(paddle0_X, paddle0_Y, paddle0_X+paddleW, paddle0_Y+paddleH);
218+
myOLED.rectangleFill(paddle1_X, paddle1_Y, paddle1_X+paddleW, paddle1_Y+paddleH);
219+
// Draw the ball: - use rect - xor and circle fails b/c of circle algorithm overdraws
220+
myOLED.rectangleFill(ball_X, ball_Y, ball_X+ball_rad, ball_Y+ball_rad);
221+
}
203222
// Increment ball's position
204223
ball_X += ballVelocityX;
205224
ball_Y += ballVelocityY;
@@ -227,7 +246,7 @@ void shapeExample()
227246
}
228247

229248
// Check if the ball hit the top or bottom
230-
if((ball_Y <= ball_rad) || (ball_Y >= (height - ball_rad - 1))){
249+
if((ball_Y <= 1) || (ball_Y >= (height - ball_rad - 1))){
231250

232251
// Change up/down velocity direction
233252
ballVelocityY = -ballVelocityY;
@@ -245,28 +264,27 @@ void shapeExample()
245264
if((paddle1_Y <= 1) || (paddle1_Y > height - 2 - paddleH))
246265
paddle1Velocity = -paddle1Velocity;
247266

248-
// Draw the Pong Field
249-
myOLED.erase();
250-
251-
// Draw an outline of the screen:
252-
myOLED.rectangle(0, 0, width-1, height-1);
253-
254-
// Draw the center line
255-
myOLED.rectangleFill( width/ 2 - 1, 0, width/2 + 1, height-1);
256-
257267
// Draw the Paddles:
258268
myOLED.rectangleFill(paddle0_X, paddle0_Y, paddle0_X+paddleW, paddle0_Y+paddleH);
259269
myOLED.rectangleFill(paddle1_X, paddle1_Y, paddle1_X+paddleW, paddle1_Y+paddleH);
260270

261271
// Draw the ball:
262-
myOLED.circle(ball_X, ball_Y, ball_rad);
272+
myOLED.rectangleFill(ball_X, ball_Y, ball_X+ball_rad, ball_Y+ball_rad);
263273

264274
// Actually draw everything on the screen:
265275
myOLED.display();
266276

277+
// Once the first loop is done, switch to XOR mode. So we just update our
278+
// moving parts
279+
if(firstLoop){
280+
firstLoop = false;
281+
myOLED.setDrawMode(grROPXOR);
282+
}
283+
267284
delay(25); // Delay for visibility
268285
}
269286
delay(1000);
287+
myOLED.setDrawMode(grROPCopy);
270288
}
271289
///////////////////////////////////////////////////////////////////////////////////////////////
272290

0 commit comments

Comments
 (0)