Uw e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd *

#include
#include
#include
#include

#define SCREEN_WIDTH 128 // OLED-weergavebreedte, in pixels
#define SCREEN_HEIGHT 64 // OLED-weergavehoogte, in pixels
#define OLED_RESET 4 // Reset pin # (of -1 als je Arduino reset pin deelt)

Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET); // Verklaring voor een SSD1306-display aangesloten op I2C (SDA-, SCL-pinnen)

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

// Prototipo das funcoes
ongeldig movePlayer ();
ongeldig moveAi ();
ongeldig drawScore ();
void eraseScore ();
nietig drawNet ();
leegte drawPixel (int posX, int posY, int afmetingen);
leegte erasePixel (int posX, int posY, int afmetingen);
ongeldig drawPlayerPaddle (int rij);


void erasePlayerPaddle (int rij);
leegte drawAiPaddle (int rij);
leegte eraseAiPaddle (int rij);
ongeldig drawBall (int x, int y);
leegte eraseBall (int x, int y);

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

// SSD1306_SWITCHCAPVCC = genereer intern displayspanning van 3.3V
if (! display.begin (SSD1306_SWITCHCAPVCC, 0x3C)) {// Adres 0x3D voor 128x64
Serial.println (F ("SSD1306-toewijzing mislukt"));
voor (;;); // Ga niet verder, loop voor altijd
}
display.display ();
vertraging (2000); // Pauzeer 2 seconden

// Wis de buffer
display.clearDisplay ();
}

leegte lus () {
if (aiScore> 3 || playerScore> 3) {// controleer de spelstatus
inProgress = false;

// iemand heeft gewonnen
display.clearDisplay ();
display.setTextSize (4);
display.setTextColor (WIT);
display.setCursor (0, 0); // zoek uit wie
if (aiScore> playerScore)
display.println ("JE VERLIEST!");
anders als (playerScore> aiScore)
display.println ("JE WINT!");
}

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

if (ballDirectionVerti == 'U') // beweeg de bal diagonaal omhoog
bal [1] = bal [1] - SNELHEID;

if (ballDirectionVerti == 'D') // beweeg de bal diagonaal naar beneden
bal [1] = bal [1] + SNELHEID;

if (bal [1] = resolutie [1]) // Stuiter de bal van de bodem
ballDirectionVerti = 'U';

if (ballDirectionHori == 'R') {
bal [0] = bal [0] + SNELHEID; // beweeg de bal
if (ball [0]> = (resolutie [0] - 6)) {// bal bevindt zich aan de AI-rand van het scherm
if ((aiPos + 12)> = ball [1] && (aiPos - 12) (aiPos + 2)) // deflecteer de bal naar beneden
ballDirectionVerti = 'D';
anders als (bal [1] ballDirectionVerti = 'U';
anders // de bal recht buigen
ballDirectionVerti = 'S';

ballDirectionHori = 'L'; // verander de balrichting
} anders {// DOEL!
bal [0] = 6; // verplaats de bal naar de andere kant van het scherm
ballDirectionVerti = 'S'; // zet de bal terug op rechtuit rijden
bal [1] = resolutie [1] / 2; // verplaats de bal naar het midden van het scherm
++ playerScore; // verhoog de score van de speler
}
}
}

if (ballDirectionHori == 'L') {
bal [0] = bal [0] - SNELHEID; // beweeg de bal
if (ball [0] = ball [1] && (playerPos - 12) (playerPos + 2)) // deflecteer de bal naar beneden
ballDirectionVerti = 'D';
anders als (bal [1] aiPos)
++ aiPos;
anders als (bal [1] --aiPos;

drawAiPaddle (aiPos);
}

ongeldig drawScore () {// Draw AI en speler scores
display.setTextSize (2);
display.setTextColor (WIT);
display.setCursor (45, 0);
display.println (playerScore);

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

void eraseScore () {// Wis AI en speler scores
display.setTextSize (2);
display.setTextColor (ZWART);
display.setCursor (45, 0);
display.println (playerScore);

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

void drawNet () {// Tekenlijnveld
voor (int i = 0; i drawPixel (((resolutie [0] / 2) - 1), i * (WALL_WIDTH) + (WALL_WIDTH * i), WALL_WIDTH);
}

leegte drawPixel (int posX, int posY, int afmetingen) {// Teken een groep pixels
voor (int x = 0; x voor (int y = 0; y display.drawPixel ((posX + x), (posY + y), WIT);
}

void erasePixel (int posX, int posY, int afmetingen) {// Wis groep pixels
voor (int x = 0; x voor (int y = 0; y display.drawPixel ((posX + x), (posY + y), ZWART);
}

ongeldig drawPlayerPaddle (int rij) {
drawPixel (0, rij - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (0, rij - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, rij, PADDLE_WIDTH);
drawPixel (0, rij + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, rij + (PADDLE_WIDTH + 2), PADDLE_WIDTH);
}

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

ongeldig drawAiPaddle (int rij) {
int column = resolutie [0] - PADDLE_WIDTH;
drawPixel (kolom, rij - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (kolom, rij - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (kolom, rij, PADDLE_WIDTH);
drawPixel (kolom, rij + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (kolom, rij + (PADDLE_WIDTH * 2), PADDLE_WIDTH);
}

void eraseAiPaddle (int rij) {
int column = resolutie [0] - PADDLE_WIDTH;
erasePixel (kolom, rij - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
erasePixel (kolom, rij - PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (kolom, rij, PADDLE_WIDTH);
erasePixel (kolom, rij + PADDLE_WIDTH, PADDLE_WIDTH);
erasePixel (kolom, rij + (PADDLE_WIDTH * 2), PADDLE_WIDTH);
}

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

leegte eraseBall (int x, int y) {
display.drawCircle (x, y, BALL_SIZE, ZWART);
}