fixed paddles and tone
This commit is contained in:
parent
85e5857a15
commit
68f7e2714f
1 changed files with 39 additions and 24 deletions
|
@ -24,8 +24,8 @@
|
||||||
#define X_MAX 10
|
#define X_MAX 10
|
||||||
#define Y_MAX 20
|
#define Y_MAX 20
|
||||||
|
|
||||||
#define GAME_DELAY 80 // in ms
|
#define GAME_DELAY 40 // in ms
|
||||||
#define BALL_DELAY_MAX 350 // in ms
|
#define BALL_DELAY_MAX 200 // in ms
|
||||||
#define BALL_DELAY_MIN 50 // in ms
|
#define BALL_DELAY_MIN 50 // in ms
|
||||||
#define BALL_DELAY_STEP 5 // 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 );
|
Adafruit_WS2801 pixels = Adafruit_WS2801((uint16_t)10 ,(uint16_t)20, dataPin, clockPin );
|
||||||
bool buttonPressed = false;
|
bool buttonPressed = false;
|
||||||
byte gameState;
|
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 paddles[PLAYER_AMOUNT][PADDLE_WIDTH];
|
||||||
Coords ball;
|
Coords ball;
|
||||||
int ballMovement[2];
|
int ballMovement[2];
|
||||||
|
@ -127,6 +127,8 @@ void setup()
|
||||||
pixels.begin();
|
pixels.begin();
|
||||||
resetLEDs();
|
resetLEDs();
|
||||||
|
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
gameState = GAME_STATE_END;
|
gameState = GAME_STATE_END;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -180,10 +182,18 @@ void updateBall()
|
||||||
lastBallUpdate = millis();
|
lastBallUpdate = millis();
|
||||||
toggleLed(ball.x, ball.y, LED_TYPE_OFF);
|
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
|
// 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<PADDLE_WIDTH; i++) {
|
for(byte i=0; i<PADDLE_WIDTH; i++) {
|
||||||
if (paddles[PLAYER_1][i].x == ball.x) {
|
Serial.printf("x[%d]=%d\n", i, paddles[PLAYER_1][i].x );
|
||||||
|
if (paddles[PLAYER_1][i].x == next_ball_x) {
|
||||||
|
Serial.println("HIT!! Player 1");
|
||||||
hitBall = true;
|
hitBall = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -191,9 +201,12 @@ void updateBall()
|
||||||
}
|
}
|
||||||
|
|
||||||
// collision detection for player 2
|
// collision detection for player 2
|
||||||
if (ballMovement[1] == -1 && ball.y == Y_MAX-2) {
|
if (ballMovement[1] == 1 && next_ball_y == Y_MAX-1) {
|
||||||
|
Serial.println("Check Player 2 ...");
|
||||||
for(byte i=0; i<PADDLE_WIDTH; i++) {
|
for(byte i=0; i<PADDLE_WIDTH; i++) {
|
||||||
if (paddles[PLAYER_2][i].x == ball.x) {
|
Serial.printf("x[%d]=%d\n", i, paddles[PLAYER_1][i].x );
|
||||||
|
if (paddles[PLAYER_2][i].x == next_ball_x) {
|
||||||
|
Serial.println("HIT!! Player 1");
|
||||||
hitBall = true;
|
hitBall = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -201,20 +214,22 @@ void updateBall()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hitBall == true) {
|
if (hitBall == true) {
|
||||||
ballMovement[0] *= -1;
|
ballMovement[1] *= -1;
|
||||||
playTone(TONE_PLAYER);
|
playTone(TONE_PLAYER);
|
||||||
if (ballDelay > BALL_DELAY_MIN) {
|
if (ballDelay > BALL_DELAY_MIN) {
|
||||||
ballDelay -= BALL_DELAY_STEP;
|
ballDelay -= BALL_DELAY_STEP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
if (ball.y <=0 || ball.y >= Y_MAX-1) {
|
||||||
|
endGame();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ball.x += ballMovement[0];
|
ball.x += ballMovement[0];
|
||||||
ball.y += ballMovement[1];
|
ball.y += ballMovement[1];
|
||||||
|
|
||||||
if (ball.y <=0 || ball.y >= Y_MAX-1) {
|
|
||||||
endGame();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ball.x <= 0 || ball.x >= X_MAX-1) {
|
if (ball.x <= 0 || ball.x >= X_MAX-1) {
|
||||||
ballMovement[0] *= -1;
|
ballMovement[0] *= -1;
|
||||||
|
@ -275,17 +290,17 @@ byte getPlayerMovement(byte playerId)
|
||||||
{
|
{
|
||||||
int value = analogRead(joystickPins[playerId]);
|
int value = analogRead(joystickPins[playerId]);
|
||||||
if (value < JOYSTICK_OFFSET_MIN) {
|
if (value < JOYSTICK_OFFSET_MIN) {
|
||||||
if (playerId == PLAYER_2) {
|
//if (playerId == PLAYER_2) {
|
||||||
return PADDLE_MOVE_DOWN;
|
return PADDLE_MOVE_DOWN;
|
||||||
} else {
|
//} else {
|
||||||
return PADDLE_MOVE_UP;
|
// return PADDLE_MOVE_UP;
|
||||||
}
|
//}
|
||||||
} else if (value > JOYSTICK_OFFSET_MAX) {
|
} else if (value > JOYSTICK_OFFSET_MAX) {
|
||||||
if (playerId == PLAYER_2) {
|
//if (playerId == PLAYER_2) {
|
||||||
return PADDLE_MOVE_UP;
|
return PADDLE_MOVE_UP;
|
||||||
} else {
|
//} else {
|
||||||
return PADDLE_MOVE_DOWN;
|
// return PADDLE_MOVE_DOWN;
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
return PADDLE_MOVE_NONE;
|
return PADDLE_MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
@ -349,14 +364,14 @@ void playTone(byte type)
|
||||||
{
|
{
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case TONE_PLAYER:
|
case TONE_PLAYER:
|
||||||
tone(PIN_PIEZO, 440, 50);
|
tone(PIN_PIEZO, NOTE_A4, 50,0);
|
||||||
break;
|
break;
|
||||||
case TONE_WALL:
|
case TONE_WALL:
|
||||||
tone(PIN_PIEZO, 550, 50);
|
tone(PIN_PIEZO, NOTE_CS5, 50,0);
|
||||||
break;
|
break;
|
||||||
case TONE_BUZZ:
|
case TONE_BUZZ:
|
||||||
for(byte i=0; i<20; i++) {
|
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);
|
delay(50);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue