Punktstand hinzugefügt

This commit is contained in:
nimeissn 2022-04-27 20:47:36 +02:00
parent 5938001735
commit d4c822b242

View file

@ -27,7 +27,7 @@
#define GAME_DELAY 40 // in ms #define GAME_DELAY 40 // in ms
#define BALL_DELAY_MAX 200 // 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 10 // in ms
#define PLAYER_AMOUNT 2 #define PLAYER_AMOUNT 2
#define PLAYER_1 0 #define PLAYER_1 0
@ -40,9 +40,12 @@
#define PADDLE_MOVE_DOWN 2 #define PADDLE_MOVE_DOWN 2
#define LED_TYPE_OFF 1 #define LED_TYPE_OFF 1
#define LED_TYPE_PADDLE 2 #define LED_TYPE_PADDLE_BLUE 2
#define LED_TYPE_BALL 3 #define LED_TYPE_PADDLE_RED 3
#define LED_TYPE_BALL_RED 4 #define LED_TYPE_BALL 4
#define LED_TYPE_BALL_YELLOW 5
#define LED_TYPE_POINTS_BLUE 6
#define LED_TYPE_POINTS_RED 7
#define TONE_PLAYER 1 #define TONE_PLAYER 1
#define TONE_WALL 2 #define TONE_WALL 2
@ -61,6 +64,9 @@ struct Coords {
byte y; byte y;
}; };
byte pointsPlayerRed =0;
byte pointsPlayerBlue =0;
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;
@ -138,6 +144,7 @@ void loop()
switch(gameState) { switch(gameState) {
case GAME_STATE_INIT: case GAME_STATE_INIT:
initGame(); initGame();
break; break;
case GAME_STATE_RUNNING: case GAME_STATE_RUNNING:
@ -154,13 +161,36 @@ void loop()
void initGame() void initGame()
{ {
resetLEDs(); resetLEDs();
lastButtonClick = millis(); lastButtonClick = millis();
ball.y = 1; ball.y = random(7,13);
ball.x = (X_MAX/2) - (PADDLE_WIDTH/2) + 1; ball.x = random(3,6);
ballMovement[0] = -1; switch(random(3))
ballMovement[1] = 1; {
case 0:
ballMovement[0] = -1;
ballMovement[1] = 1;
break;
case 1:
ballMovement[0] = 1;
ballMovement[1] = -1;
break;
case 2:
ballMovement[0] = -1;
ballMovement[1] = -1;
break;
case 3:
ballMovement[0] = 1;
ballMovement[1] = 1;
break;
}
ballDelay = BALL_DELAY_MAX; ballDelay = BALL_DELAY_MAX;
for(byte i=0; i<PADDLE_WIDTH; i++) { for(byte i=0; i<PADDLE_WIDTH; i++) {
@ -195,9 +225,16 @@ void updateBall()
if (paddles[PLAYER_1][i].x == next_ball_x) { if (paddles[PLAYER_1][i].x == next_ball_x) {
Serial.println("HIT!! Player 1"); Serial.println("HIT!! Player 1");
hitBall = true; hitBall = true;
break; break;
} }
} }
if(hitBall == false)
{
pointsPlayerRed++;
}
} }
// collision detection for player 2 // collision detection for player 2
@ -208,9 +245,16 @@ void updateBall()
if (paddles[PLAYER_2][i].x == next_ball_x) { if (paddles[PLAYER_2][i].x == next_ball_x) {
Serial.println("HIT!! Player 1"); Serial.println("HIT!! Player 1");
hitBall = true; hitBall = true;
break; break;
} }
} }
if (hitBall == false)
{
pointsPlayerBlue++;
}
} }
if (hitBall == true) { if (hitBall == true) {
@ -243,7 +287,7 @@ void updateBall()
void endGame() void endGame()
{ {
gameState = GAME_STATE_END; gameState = GAME_STATE_END;
toggleLed(ball.x, ball.y, LED_TYPE_BALL_RED); toggleLed(ball.x, ball.y, LED_TYPE_BALL_YELLOW);
pixels.show(); pixels.show();
playTone(TONE_BUZZ); playTone(TONE_BUZZ);
} }
@ -280,9 +324,45 @@ void updateGame()
// show paddle LEDs // show paddle LEDs
for(byte p=0; p<PLAYER_AMOUNT; p++) { for(byte p=0; p<PLAYER_AMOUNT; p++) {
for(byte i=0; i<PADDLE_WIDTH; i++) { for(byte i=0; i<PADDLE_WIDTH; i++) {
toggleLed(paddles[p][i].x, paddles[p][i].y, LED_TYPE_PADDLE); toggleLed(paddles[p][i].x, paddles[p][i].y, LED_TYPE_PADDLE_BLUE+p);
} }
} }
// show points
for(int i = 0; i<pointsPlayerBlue; i++)
{
Serial.printf("Punkte Player Blue %d\n", pointsPlayerBlue);
toggleLed(0,9-i, LED_TYPE_POINTS_BLUE);
}
for(int i = 0; i<pointsPlayerRed; i++)
{
Serial.printf("Punkte Player Red %d\n", pointsPlayerRed);
toggleLed(0,10+i,LED_TYPE_POINTS_RED);
}
if(pointsPlayerBlue == 5 )
{
pointsPlayerBlue = 0;
pointsPlayerRed = 0;
for (int i =0; i<200; i++)
{
pixels.setPixelColor(i, 0,0,255);
}
}
else if(pointsPlayerRed == 5)
{
pointsPlayerBlue = 0;
pointsPlayerRed = 0;
for (int i =0; i<200; i++)
{
pixels.setPixelColor(i, 255,0,0);
}
}
pixels.show(); pixels.show();
} }
@ -343,18 +423,27 @@ void toggleLed(byte x, byte y, byte type)
uint32_t color; uint32_t color;
switch(type) { switch(type) {
case LED_TYPE_PADDLE: case LED_TYPE_PADDLE_RED:
color = Color(0, 255, 255);
break;
case LED_TYPE_BALL_RED:
color = Color(255, 0, 0); color = Color(255, 0, 0);
break; break;
case LED_TYPE_PADDLE_BLUE:
color = Color(0,0,255);
break;
case LED_TYPE_BALL_YELLOW:
color = Color(255, 255, 0);
break;
case LED_TYPE_BALL: case LED_TYPE_BALL:
color = Color(0, 255, 0); color = Color(0, 255, 0);
break; break;
case LED_TYPE_OFF: case LED_TYPE_OFF:
color = Color(0, 0, 0); color = Color(0, 0, 0);
break; break;
case LED_TYPE_POINTS_RED:
color = Color(64,0,0);
break;
case LED_TYPE_POINTS_BLUE:
color = Color(0,0,64);
break;
} }
pixels.setPixelColor(ledIndex, color); pixels.setPixelColor(ledIndex, color);