From 68f7e2714f2be5f7b30ac178c18851a7bc2e2779 Mon Sep 17 00:00:00 2001 From: jenoack Date: Thu, 17 Feb 2022 08:59:54 +0100 Subject: [PATCH] fixed paddles and tone --- VSCode/Pong/src/main.cpp | 63 +++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/VSCode/Pong/src/main.cpp b/VSCode/Pong/src/main.cpp index a6fb9fc..b04a7f0 100755 --- a/VSCode/Pong/src/main.cpp +++ b/VSCode/Pong/src/main.cpp @@ -24,8 +24,8 @@ #define X_MAX 10 #define Y_MAX 20 -#define GAME_DELAY 80 // in ms -#define BALL_DELAY_MAX 350 // in ms +#define GAME_DELAY 40 // in ms +#define BALL_DELAY_MAX 200 // in ms #define BALL_DELAY_MIN 50 // in ms #define BALL_DELAY_STEP 5 // in ms @@ -64,7 +64,7 @@ struct Coords { Adafruit_WS2801 pixels = Adafruit_WS2801((uint16_t)10 ,(uint16_t)20, dataPin, clockPin ); bool buttonPressed = false; byte gameState; -byte joystickPins[PLAYER_AMOUNT] = {PIN_JOYSTICK_1_Y, PIN_JOYSTICK_2_Y}; +byte joystickPins[PLAYER_AMOUNT] = {PIN_JOYSTICK_1_X, PIN_JOYSTICK_2_X}; Coords paddles[PLAYER_AMOUNT][PADDLE_WIDTH]; Coords ball; int ballMovement[2]; @@ -127,6 +127,8 @@ void setup() pixels.begin(); resetLEDs(); + Serial.begin(9600); + gameState = GAME_STATE_END; } @@ -180,10 +182,18 @@ void updateBall() lastBallUpdate = millis(); toggleLed(ball.x, ball.y, LED_TYPE_OFF); + Serial.printf("ballMovement[1]=%d, ball.y=%d, ball.x=%d\n", ballMovement[1], ball.y, ball.x); + + byte next_ball_x = ball.x + ballMovement[0]; + byte next_ball_y = ball.y + ballMovement[1]; + // collision detection for player 1 - if (ballMovement[1] == 1 && ball.y == 1) { + if (ballMovement[1] == -1 && next_ball_y == 0) { + Serial.println("Check Player 1 ..."); for(byte i=0; i BALL_DELAY_MIN) { ballDelay -= BALL_DELAY_STEP; } } + else{ + if (ball.y <=0 || ball.y >= Y_MAX-1) { + endGame(); + return; + } + } ball.x += ballMovement[0]; ball.y += ballMovement[1]; - - if (ball.y <=0 || ball.y >= Y_MAX-1) { - endGame(); - return; - } + if (ball.x <= 0 || ball.x >= X_MAX-1) { ballMovement[0] *= -1; @@ -275,17 +290,17 @@ byte getPlayerMovement(byte playerId) { int value = analogRead(joystickPins[playerId]); if (value < JOYSTICK_OFFSET_MIN) { - if (playerId == PLAYER_2) { + //if (playerId == PLAYER_2) { return PADDLE_MOVE_DOWN; - } else { - return PADDLE_MOVE_UP; - } + //} else { + // return PADDLE_MOVE_UP; + //} } else if (value > JOYSTICK_OFFSET_MAX) { - if (playerId == PLAYER_2) { + //if (playerId == PLAYER_2) { return PADDLE_MOVE_UP; - } else { - return PADDLE_MOVE_DOWN; - } + //} else { + // return PADDLE_MOVE_DOWN; + //} } return PADDLE_MOVE_NONE; } @@ -349,14 +364,14 @@ void playTone(byte type) { switch(type) { case TONE_PLAYER: - tone(PIN_PIEZO, 440, 50); + tone(PIN_PIEZO, NOTE_A4, 50,0); break; case TONE_WALL: - tone(PIN_PIEZO, 550, 50); + tone(PIN_PIEZO, NOTE_CS5, 50,0); break; case TONE_BUZZ: for(byte i=0; i<20; i++) { - tone(PIN_PIEZO, 220-i*10, 50); + tone(PIN_PIEZO, NOTE_A3-i*10, 50,0); delay(50); } break;