In this blog, i will show u how to make ping pong game in Arduino.
Requirements:
Arduino Uno, Arduino IDE, Potentiometer, LCD OLED (0.92 inch, SSD1306), Buzzer
connection (image soon):
code:
#include "ssd1306.h"
//function "handleDirection" not working properly, fix it
const unsigned char playerLineIMG [] PROGMEM = {
0xFF
};
const unsigned char ballIMG [] PROGMEM = {
0b00000011,
0b00000011
};
const int plrWidth = sizeof(playerLineIMG);
const int ballWidth = sizeof(ballIMG);
const int ballSpeed = 1;
const int toneDuration = 100;
const int toneFreq = 1046;
class Player {
public:
SPRITE playerTop = ssd1306_createSprite( 0, 0, plrWidth, playerLineIMG );
SPRITE playerBottom = ssd1306_createSprite( 0, 0, plrWidth, playerLineIMG );
void setPlrX(int x){
playerTop.x = x;
playerBottom.x = x;
}
void setPlrY(int y){
playerTop.y = y;
playerBottom.y = y + 8;
}
void drawPlr(){
playerTop.draw();
playerBottom.draw();
}
int getX(){
return playerTop.x;
}
int getY(){
return playerTop.y;
}
void erasePlrTrace(){
playerTop.eraseTrace();
playerBottom.eraseTrace();
}
};
String handleDirection(String directionOfSprite, int spriteBounceFrom){
if(directionOfSprite == "SE" && spriteBounceFrom == 3){
return "NE";
}else if (directionOfSprite == "SE" && spriteBounceFrom == 2){
return "SW";
}
if(directionOfSprite == "SW" && spriteBounceFrom == 3){
return "NW";
}else if (directionOfSprite == "SW" && spriteBounceFrom == 4){
return "SE";
}
if(directionOfSprite == "NE" && spriteBounceFrom == 1){
return "SE";
}else if (directionOfSprite == "NE" && spriteBounceFrom == 2){
return "NW";
}
if(directionOfSprite == "NW" && spriteBounceFrom == 1){
return "SW";
}else if (directionOfSprite == "NW" && spriteBounceFrom == 4){
return "NE";
}
}
const int potPin1 = A0;
const int potPin2 = A1;
void setup() {
Serial.begin(9600);
ssd1306_setFixedFont(ssd1306xled_font6x8);
ssd1306_128x64_i2c_init();
ssd1306_clearScreen();
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
SPRITE ball;
ball = ssd1306_createSprite(0, 0, ballWidth, ballIMG);
Player plr1, plr2;
plr1.setPlrX(127);
plr1.setPlrY(0);
plr1.drawPlr();
plr2.setPlrX(0);
plr2.setPlrY(0);
plr2.drawPlr();
ball.x = 64;
ball.y = 32;
String BallDir = "SE";
short int ballBounceFrom = 0; //0 == unknown, 1 == top, 2 == right, 3 == bottom, 4 == left
short int pointPlr1 = 0, pointPlr2 = 0;
for(;;){
ssd1306_clearScreen();
ball.eraseTrace();
char buffer1[10];
char buffer2[10];
sprintf(buffer1, "%d", pointPlr1);
sprintf(buffer2, "%d", pointPlr2);
if(pointPlr2 >= 10){
ssd1306_printFixed(41, 0, buffer2, STYLE_BOLD);
}else{
ssd1306_printFixed(47, 0, buffer2, STYLE_BOLD);
}
ssd1306_printFixed(55, 0, "-", STYLE_BOLD);
ssd1306_printFixed(61, 0, buffer1, STYLE_BOLD);
Serial.println(BallDir);
if(ball.y >= 63){
if(BallDir == "SE"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 3;
BallDir = handleDirection(BallDir, ballBounceFrom);
}else if(BallDir == "SW"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 3;
BallDir = handleDirection(BallDir, ballBounceFrom);
}
}
if(ball.y == 0){
if(BallDir == "NE"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 1;
BallDir = handleDirection(BallDir, ballBounceFrom);
}else if(BallDir == "NW"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 1;
BallDir = handleDirection(BallDir, ballBounceFrom);
}
}
if(ball.x >= plr1.getX()){
if(ball.y <= plr1.getY() + 16 && !(ball.y < plr1.getY())){
if(BallDir == "NE"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 2;
BallDir = handleDirection(BallDir, ballBounceFrom);
}else if(BallDir == "SE"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 2;
BallDir = handleDirection(BallDir, ballBounceFrom);
}
}else{
tone(6, toneFreq, toneDuration);
pointPlr2 += 1;
ball.x = 63;
ball.y = 31;
BallDir = "SW";
}
}
if(ball.x <= plr2.getX()){
if(ball.y <= plr2.getY() + 16 && !(ball.y < plr2.getY())){
tone(6, toneFreq, toneDuration);
if(BallDir == "NW"){
ballBounceFrom = 4;
BallDir = handleDirection(BallDir, ballBounceFrom);
}else if(BallDir == "SW"){
tone(6, toneFreq, toneDuration);
ballBounceFrom = 4;
BallDir = handleDirection(BallDir, ballBounceFrom);
}
}else{
tone(6, toneFreq, toneDuration);
pointPlr1 += 1;
ball.x = 63;
ball.y = 31;
BallDir = "SE";
}
}
if(BallDir == "SE"){
ball.x += ballSpeed;
ball.y += ballSpeed;
}else if(BallDir == "NE"){
ball.x += ballSpeed;
ball.y -= ballSpeed;
}else if(BallDir == "SW"){
ball.x -= ballSpeed;
ball.y += ballSpeed;
}else if(BallDir == "NW"){
ball.x -= ballSpeed;
ball.y -= ballSpeed;
}
ball.draw();
int potVal1 = analogRead(potPin1);
int potVal2 = analogRead(potPin2);
plr1.setPlrY(map(potVal1, 0, 1023, 0, 52)); //Y max = 56
plr1.drawPlr();
plr2.setPlrY(map(potVal2, 0, 1023, 0, 52));
plr2.drawPlr();
}
}
void loop() {
}

kerenn
ReplyDelete