Deine Email-Adresse wird nicht veröffentlicht. erforderliche Felder sind markiert *

#umfassen
#umfassen
#umfassen
#umfassen

#define SCREEN_WIDTH 128 // OLED-Anzeigebreite in Pixel
#define SCREEN_HEIGHT 64 // OLED-Anzeigehöhe in Pixel
#define OLED_RESET 4 // Pin # zurücksetzen (oder -1, wenn Arduino-Reset-Pin gemeinsam genutzt wird)

Adafruit_SSD1306-Anzeige (SCREEN_WIDTH, SCREEN_HEIGHT & Wire, OLED_RESET); // Deklaration für ein an I2C angeschlossenes SSD1306-Display (SDA-, SCL-Pins)

int Auflösung [2] = {SCREEN_WIDTH, SCREEN_HEIGHT}
, Kugel [2] = {20, (Auflösung [1] / 2)};
const int PIXEL_SIZE = 8
, WALL_WIDTH = 4
, PADDLE_WIDTH = 4
, BALL_SIZE = 4
, GESCHWINDIGKEIT = 3;
int playerScore = 0
, aiScore = 0
, playerPos = 0
aiPos = 0;
char ballDirectionHori = 'R'
, ballDirectionVerti = 'S';
boolean inProgress = true;

// Prototipo das funcoes
void movePlayer ();
void moveAi ();
void drawScore ();
void eraseScore ();
void drawNet ();
void drawPixel (int posX, int posY, int dimensions);
void erasePixel (int posX, int posY, int dimensions);


void drawPlayerPaddle (int row);
void erasePlayerPaddle (int row);
void drawAiPaddle (int row);
void eraseAiPaddle (int row);
void drawBall (int x, int y);
void eraseBall (int x, int y);

void setup () {
Serial.begin (9600);

// SSD1306_SWITCHCAPVCC = Anzeigespannung von 3,3 V intern erzeugen
if (! display.begin (SSD1306_SWITCHCAPVCC, 0x3C)) {// Adresse 0x3D für 128x64
Serial.println (F ("SSD1306-Zuordnung fehlgeschlagen"));
zum (;;); // Nicht weitermachen, für immer schleifen
}
display.display ();
Verzögerung (2000); // Pause für 2 Sekunden

// Lösche den Puffer
display.clearDisplay ();
}

void loop () {
if (aiScore> 3 || playerScore> 3) {// Spielstatus überprüfen
inProgress = false;

// Jemand hat gewonnen
display.clearDisplay ();
display.setTextSize (4);
display.setTextColor (WHITE);
display.setCursor (0, 0); // finde heraus wer
if (aiScore> playerScore)
display.println ("SIE VERLIEREN!");
sonst wenn (playerScore> aiScore)
display.println ("SIE GEWINNEN!");
}

if (inProgress) {
eraseScore ();
eraseBall (Ball [0], Ball [1]);

if (ballDirectionVerti == 'U') // bewege den Ball diagonal nach oben
Kugel [1] = Kugel [1] - GESCHWINDIGKEIT;

if (ballDirectionVerti == 'D') // bewege den Ball diagonal nach unten
Kugel [1] = Kugel [1] + GESCHWINDIGKEIT;

if (ball [1] = auflösung [1]) // hüpfe den ball vom boden
ballDirectionVerti = 'U';

if (ballDirectionHori == 'R') {
Ball [0] = Ball [0] + GESCHWINDIGKEIT; // Ball bewegen
if (ball [0]> = (Auflösung [0] - 6)) {// ball befindet sich am AI-Rand des Bildschirms
if ((aiPos + 12)> = ball [1] && (aiPos - 12) (aiPos + 2)) // Ball nach unten ablenken
ballDirectionVerti = 'D';
sonst wenn (ball [1] ballDirectionVerti = 'U';
sonst // Ball gerade ablenken
ballDirectionVerti = 'S';

ballDirectionHori = 'L'; // Ballrichtung ändern
} else {// ZIEL!
Kugel [0] = 6; // bewege den Ball auf die andere Seite des Bildschirms
ballDirectionVerti = 'S'; // Ball auf Geraden zurücksetzen
Kugel [1] = Auflösung [1] / 2; // bewege den Ball in die Mitte des Bildschirms
++ playerScore; // Spielerpunktzahl erhöhen
}
}
}

if (ballDirectionHori == 'L') {
Ball [0] = Ball [0] - GESCHWINDIGKEIT; // Ball bewegen
if (ball [0] = ball [1] && (playerPos - 12) (playerPos + 2)) // lenke den Ball nach unten ab
ballDirectionVerti = 'D';
sonst wenn (ball [1] aiPos)
++ aiPos;
sonst wenn (ball [1] --aiPos;

drawAiPaddle (aiPos);
}

void drawScore () {// Ziehe KI- und Spielerwerte
display.setTextSize (2);
display.setTextColor (WHITE);
display.setCursor (45, 0);
display.println (playerScore);

display.setCursor (75, 0);
display.println (aiScore);
}

void eraseScore () {// KI- und Spielerergebnisse löschen
display.setTextSize (2);
display.setTextColor (SCHWARZ);
display.setCursor (45, 0);
display.println (playerScore);

display.setCursor (75, 0);
display.println (aiScore);
}

void drawNet () {// Linienfeld zeichnen
für (int i = 0; i drawPixel (((Auflösung [0] / 2) - 1), i * (WALL_WIDTH) + (WALL_WIDTH * i), WALL_WIDTH);
}

void drawPixel (int posX, int posY, int dimensions) {// Zeichne eine Pixelgruppe
für (int x = 0; x für (int y = 0; y display.drawPixel ((posX + x), (posY + y), WEISS);
}

void erasePixel (int posX, int posY, int dimensions) {// Gruppe von Pixeln löschen
für (int x = 0; x für (int y = 0; y display.drawPixel ((posX + x), (posY + y), SCHWARZ);
}

void drawPlayerPaddle (int row) {
drawPixel (0, Zeile - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (0, Zeile - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, Zeile, PADDLE_WIDTH);
drawPixel (0, Zeile + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, Zeile + (PADDLE_WIDTH + 2), PADDLE_WIDTH);
}

void erasePlayerPaddle (int row) {
erasePixel (0, Zeile - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
erasePixel (0, Zeile - PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (0, Zeile, PADDLE_WIDTH);
erasePixel (0, Zeile + PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (0, Zeile + (PADDLE_WIDTH + 2), PADDLE_WIDTH);
}

void drawAiPaddle (int row) {
int column = Auflösung [0] - PADDLE_WIDTH;
drawPixel (Spalte, Zeile - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (Spalte, Zeile - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (Spalte, Zeile, PADDLE_WIDTH);
drawPixel (Spalte, Zeile + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (Spalte, Zeile + (PADDLE_WIDTH * 2), PADDLE_WIDTH);
}

void eraseAiPaddle (int row) {
int column = Auflösung [0] - PADDLE_WIDTH;
erasePixel (Spalte, Zeile - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
erasePixel (Spalte, Zeile - PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (Spalte, Zeile, PADDLE_WIDTH);
erasePixel (Spalte, Zeile + PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (Spalte, Zeile + (PADDLE_WIDTH * 2), PADDLE_WIDTH);
}

void drawBall (int x, int y) {
display.drawCircle (x, y, BALL_SIZE, WHITE);
}

void eraseBall (int x, int y) {
display.drawCircle (x, y, BALL_SIZE, BLACK);
}