Feld gedreht

This commit is contained in:
nimeissn 2022-02-16 19:34:30 +01:00
parent a88eab9ee5
commit 85e5857a15
6 changed files with 58 additions and 47 deletions

View file

@ -3,5 +3,8 @@
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -9,8 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[env:nanoatmega328new]
platform = atmelavr
board = nanoatmega328
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit NeoMatrix@^1.2.0

View file

@ -33,8 +33,8 @@ Example sketch for driving Adafruit WS2801 pixels!
// Can be any valid output pins.
// The colors of the wires may be totally different so
// BE SURE TO CHECK YOUR PIXELS TO SEE WHICH WIRES TO USE!
uint8_t dataPin = 6; // Yellow wire on Adafruit Pixels
uint8_t clockPin = 3; // Green wire on Adafruit Pixels
uint8_t dataPin = 2; // Yellow wire on Adafruit Pixels
uint8_t clockPin = 4; // Green wire on Adafruit Pixels
// Don't forget to connect the ground wire to Arduino ground,
// and the +5V wire to a +5V supply

View file

@ -3,5 +3,8 @@
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -9,9 +9,10 @@
; https://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = nanoatmega328
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit NeoPixel@^1.10.4
adafruit/Adafruit WS2801 Library@^1.1.1
lbernstone/Tone32@^1.0.0
platform = espressif32

View file

@ -1,5 +1,6 @@
#include <Arduino.h>
#include <Adafruit_WS2801.h>
#include <Tone32.h>
/**
* Player 1: x = 0
@ -7,14 +8,16 @@
*/
#define PIN_LED_MATRIX 2
#define PIN_JOYSTICK_1_BUTTON 6
#define PIN_JOYSTICK_2_BUTTON 7
#define PIN_PIEZO 11
#define PIN_JOYSTICK_1_Y A0
#define PIN_JOYSTICK_2_Y A1
#define PIN_JOYSTICK_1_BUTTON 12
#define PIN_JOYSTICK_2_BUTTON 26
#define PIN_PIEZO 17
#define PIN_JOYSTICK_1_Y 13
#define PIN_JOYSTICK_2_Y 27
#define PIN_JOYSTICK_1_X 14
#define PIN_JOYSTICK_2_X 25
#define JOYSTICK_OFFSET_MIN 200
#define JOYSTICK_OFFSET_MAX 700
#define JOYSTICK_OFFSET_MAX 3500
#define DEBOUNCE_TIME 300 // in ms
@ -50,8 +53,8 @@
#define GAME_STATE_INIT 3
#include <Adafruit_NeoPixel.h>
uint8_t dataPin = 6;
uint8_t clockPin = 3;
uint8_t dataPin = 2;
uint8_t clockPin = 4;
struct Coords {
byte x;
@ -123,7 +126,9 @@ void setup()
pinMode(PIN_JOYSTICK_2_BUTTON, INPUT_PULLUP);
pixels.begin();
resetLEDs();
gameState = GAME_STATE_END;
}
void loop()
@ -150,17 +155,17 @@ void initGame()
resetLEDs();
lastButtonClick = millis();
ball.x = 1;
ball.y = (Y_MAX/2) - (PADDLE_WIDTH/2) + 1;
ballMovement[0] = 1;
ballMovement[1] = -1;
ball.y = 1;
ball.x = (X_MAX/2) - (PADDLE_WIDTH/2) + 1;
ballMovement[0] = -1;
ballMovement[1] = 1;
ballDelay = BALL_DELAY_MAX;
for(byte i=0; i<PADDLE_WIDTH; i++) {
paddles[PLAYER_1][i].x = 0;
paddles[PLAYER_1][i].y = (Y_MAX/2) - (PADDLE_WIDTH/2) + i;
paddles[PLAYER_2][i].x = X_MAX - 1;
paddles[PLAYER_2][i].y = paddles[PLAYER_1][i].y;
paddles[PLAYER_1][i].y = 0;
paddles[PLAYER_1][i].x = (X_MAX/2) - (PADDLE_WIDTH/2) + i;
paddles[PLAYER_2][i].y = Y_MAX - 1;
paddles[PLAYER_2][i].x = paddles[PLAYER_1][i].x;
}
gameState = GAME_STATE_RUNNING;
@ -176,9 +181,9 @@ void updateBall()
toggleLed(ball.x, ball.y, LED_TYPE_OFF);
// collision detection for player 1
if (ballMovement[0] == -1 && ball.x == 1) {
if (ballMovement[1] == 1 && ball.y == 1) {
for(byte i=0; i<PADDLE_WIDTH; i++) {
if (paddles[PLAYER_1][i].y == ball.y) {
if (paddles[PLAYER_1][i].x == ball.x) {
hitBall = true;
break;
}
@ -186,9 +191,9 @@ void updateBall()
}
// collision detection for player 2
if (ballMovement[0] == 1 && ball.x == X_MAX-2) {
if (ballMovement[1] == -1 && ball.y == Y_MAX-2) {
for(byte i=0; i<PADDLE_WIDTH; i++) {
if (paddles[PLAYER_2][i].y == ball.y) {
if (paddles[PLAYER_2][i].x == ball.x) {
hitBall = true;
break;
}
@ -206,13 +211,13 @@ void updateBall()
ball.x += ballMovement[0];
ball.y += ballMovement[1];
if (ball.x <=0 || ball.x >= X_MAX-1) {
if (ball.y <=0 || ball.y >= Y_MAX-1) {
endGame();
return;
}
if (ball.y <= 0 || ball.y >= Y_MAX-1) {
ballMovement[1] *= -1;
if (ball.x <= 0 || ball.x >= X_MAX-1) {
ballMovement[0] *= -1;
playTone(TONE_WALL);
}
@ -245,14 +250,14 @@ void updateGame()
// move paddles
for(byte p=0; p<PLAYER_AMOUNT; p++) {
byte movement = getPlayerMovement(p);
if (movement == PADDLE_MOVE_UP && paddles[p][PADDLE_WIDTH-1].y < (Y_MAX-1)) {
if (movement == PADDLE_MOVE_UP && paddles[p][PADDLE_WIDTH-1].x < (X_MAX-1)) {
for(byte i=0; i<PADDLE_WIDTH; i++) {
paddles[p][i].y++;
paddles[p][i].x++;
}
}
if (movement == PADDLE_MOVE_DOWN && paddles[p][0].y > 0) {
if (movement == PADDLE_MOVE_DOWN && paddles[p][0].x > 0) {
for(byte i=0; i<PADDLE_WIDTH; i++) {
paddles[p][i].y--;
paddles[p][i].x--;
}
}
}