Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pm:prj2023:tmiu:pongrecreation [2023/05/30 17:29]
Ovidiu.istrate0211 [Software Design]
pm:prj2023:tmiu:pongrecreation [2023/05/30 18:48] (current)
Ovidiu.istrate0211 [Software Design]
Line 48: Line 48:
 const int Player_1_moveButton = A0;  // Joystick Pin for Player 1 const int Player_1_moveButton = A0;  // Joystick Pin for Player 1
 const int Player_2_moveButton = A2;  // Joystick Pin for Player 2 const int Player_2_moveButton = A2;  // Joystick Pin for Player 2
 +const int ScoreBuzzerPin = 9; 
  
 //​---------------------------------------CLASSES------------------------------------------------ //​---------------------------------------CLASSES------------------------------------------------
Line 151: Line 152:
  
 Paddle player1(0, 0, 2);  // Creating Object for Player 1 Paddle Paddle player1(0, 0, 2);  // Creating Object for Player 1 Paddle
-Paddle player2(15, 0, 2); // Creating Object for Player 2 Paddle+Paddle player2(0, 1, 2); // Creating Object for Player 2 Paddle
 Ball ball(7, 1, 1, 1);    // Creating Object for Ball Ball ball(7, 1, 1, 1);    // Creating Object for Ball
  
 //​---------------------------------------SETUP------------------------------------------------ //​---------------------------------------SETUP------------------------------------------------
  
-void setup() +void setup() {
-{+
   lcd.init(); ​            // Initialize the LCD   lcd.init(); ​            // Initialize the LCD
   lcd.backlight(); ​      // Turn on the backlight   lcd.backlight(); ​      // Turn on the backlight
  
-  // Setting the buttons as Inputs 
   pinMode(Player_1_moveButton,​ INPUT);   pinMode(Player_1_moveButton,​ INPUT);
   pinMode(Player_2_moveButton,​ INPUT);   pinMode(Player_2_moveButton,​ INPUT);
 +  pinMode(ScoreBuzzerPin,​ OUTPUT);
  
-  // Printing initial positions 
   player1.PrintPaddle();​   player1.PrintPaddle();​
   player2.PrintPaddle();​   player2.PrintPaddle();​
   ball.PrintBall();​   ball.PrintBall();​
 +
 +  ShowStartingScreen();​
 } }
  
Line 175: Line 176:
 void PrintScoreboard(int player1Score,​ int player2Score) void PrintScoreboard(int player1Score,​ int player2Score)
 { {
 +  lcd.clear();​
   lcd.setCursor(0,​ 0);   lcd.setCursor(0,​ 0);
   lcd.print("​P1:​ ");   lcd.print("​P1:​ ");
Line 182: Line 184:
   lcd.print("​P2:​ ");   lcd.print("​P2:​ ");
   lcd.print(player2Score);​   lcd.print(player2Score);​
 +
 +  delay(500); ​ // Display the scoreboard for 0.5 seconds
 +  lcd.clear();​
 +}
 +
 +//​---------------------------------------SCREENS------------------------------------------------
 +
 +void ShowStartingScreen()
 +{
 +  lcd.clear();​
 +  lcd.setCursor(3,​ 0);
 +  lcd.print("​Pong Game"​);​
 +  delay(2000);​
 +  lcd.clear();​
 +}
 +
 +void ShowGameOverScreen()
 +{
 +  lcd.clear();​
 +  lcd.setCursor(4,​ 0);
 +  lcd.print("​Game Over"​);​
 +  delay(2000);​
 +  lcd.clear();​
 +}
 +void PlayScoreSound() {
 +  tone(ScoreBuzzerPin,​ 1000, 200);  // Play a sound for 200ms at a frequency of 1000Hz
 +  delay(200); ​ // Wait for 200ms to avoid overlapping sounds
 } }
  
Line 219: Line 248:
     player1Score++;​     player1Score++;​
     PrintScoreboard(player1Score,​ player2Score);​     PrintScoreboard(player1Score,​ player2Score);​
-    delay(2000); ​ // Delay to display the scoreboard for 2 seconds 
  
-    if (player1Score >= 8)+    if (player1Score ​>= 8 || player2Score ​>= 8)
     {     {
 +     
 +      ShowGameOverScreen();​
       player1Score = 0;       player1Score = 0;
       player2Score = 0;       player2Score = 0;
 +      ball.ClearBall();​
 +      ball = Ball(7, 1, 1, 1);  // Reset the ball position
 +      ball.PrintBall();​
 +      ShowStartingScreen();​
 +    }
 +    else
 +    {
 +      PlayScoreSound();​
 +      ball.ClearBall();​
 +      ball = Ball(7, 1, 1, 1);  // Reset the ball position
 +      ball.PrintBall();​
     }     }
- 
-    ball.ClearBall();​ 
-    ball = Ball(7, 1, 1, 1);  // Reset the ball position 
-    ball.PrintBall();​ 
   }   }
   else if (ball.Ball_X == 0)  // Replace with actual condition for player 2 scoring   else if (ball.Ball_X == 0)  // Replace with actual condition for player 2 scoring
Line 235: Line 272:
     player2Score++;​     player2Score++;​
     PrintScoreboard(player1Score,​ player2Score);​     PrintScoreboard(player1Score,​ player2Score);​
-    delay(2000); ​ // Delay to display the scoreboard for 2 seconds 
  
-    if (player2Score >= 8)+    if (player1Score >= 8 || player2Score >= 8)
     {     {
 +      PlayScoreSound();​
 +      ShowGameOverScreen();​
       player1Score = 0;       player1Score = 0;
       player2Score = 0;       player2Score = 0;
 +      ball.ClearBall();​
 +      ball = Ball(7, 1, 1, 1);  // Reset the ball position
 +      ball.PrintBall();​
 +      ShowStartingScreen();​
 +    }
 +    else
 +    {
 +      ball.ClearBall();​
 +      ball = Ball(7, 1, 1, 1);  // Reset the ball position
 +      ball.PrintBall();​
     }     }
- 
-    ball.ClearBall();​ 
-    ball = Ball(7, 1, 1, 1);  // Reset the ball position 
-    ball.PrintBall();​ 
   }   }
  
Line 251: Line 295:
   delay(150);   delay(150);
 } }
 +
  
 </​note>​ </​note>​
pm/prj2023/tmiu/pongrecreation.1685456946.txt.gz · Last modified: 2023/05/30 17:29 by Ovidiu.istrate0211
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0