I said 100, got it done with 80 lines of "Game code".
Could be cleaner, the whole sketch is about double and the padal placement needs a bit of work.
- Adding a ball Spin or acceleration when a hit is made with the padal moving.
- a ball Split extra if you match the ball speed and hit on the corner of the padal.
Pong Arduino Oled Code Below...
void drawrect(int x, int y, int l, bool filled) {
if(filled) display.fillRect(x, y, 4, l, SSD1306_INVERSE);
else display.drawRect(x, y, 4, l, SSD1306_WHITE);
}
int p1 = 0, p2 = 0;
int tik = 0;
int bx = 32, by = 32;
int vx = 1, vy = 1;
int bs = 6, pl = 10;
int s1 =0, s2 = 0;
int hp = bs+6;
bool paused = false;
void one(){
paused = true;
s1++;
char l1[12];
sprintf(l1," %02d : %02d ", s1, s2);
testdrawoled( "POne Wins!", l1);
delay(511);
}
void two(){
paused = true;
s2++;
char l1[12];
sprintf(l1," %02d : %02d ", s1, s2);
testdrawoled( "pTWO Wins!", l1);
delay(511);
}
void drawBall() {
int hit = -1;
//check for points.
if(bx > display.width()-bs) one();
else if(bx < bs) two();
else if(bx > display.width()-hp and vx > 0){
//if the ball is on p2 side.
if((by < (p2+pl)) and (by > p2) ) vx*=-1;
hit = 1;
}
else if(bx < hp and vx < 0){
//p1 side
if((by < (p1+pl)) and (by > p1) ) vx*=-1;
hit = 2;
}
if( (hit ==-1) and (bx > display.width()-bs or bx < bs) ) vx *= -1;
if(by > display.height()-bs/2 or by < bs/2) vy *= -1;
bx += vx;
by += vy;
display.drawCircle(bx, by, bs, SSD1306_WHITE);
display.drawCircle(bx, by, bs/2, SSD1306_WHITE);
//double ball for spin?/acceleration.
}
void drawGame() {
display.clearDisplay();
drawrect(2, p1, pl, false);
drawrect(display.width()-4, p2, pl, false);
char score[12];
sprintf(score,"%02d : %02d", s1, s2);
display.setTextSize(1);
display.setCursor(38,0); // Start at top-left corner
display.println(score);
drawrect(62, 0, 64, false);//net
drawBall();
display.display();
delay(1);
if(!paused)
if(tik++ > 1000) tik = 0;
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.