Din e-postadress kommer inte att publiceras. behövliga fält är markerade *
#inkludera
#inkludera
#inkludera
#inkludera
#definiera SCREEN_WIDTH 128 // OLED-skärmbredd, i pixlar
#definiera SCREEN_HEIGHT 64 // OLED-skärmhöjd, i pixlar
#definiera OLED_RESET 4 // Återställ stift # (eller -1 om du delar Arduino-återställningsstift)
Adafruit_SSD1306-display (SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET); // Förklaring för en SSD1306-skärm ansluten till I2C (SDA, SCL-stift)
int-upplösning [2] = {SCREEN_WIDTH, SCREEN_HEIGHT}
, boll [2] = {20, (upplösning [1] / 2)};
const int PIXEL_SIZE = 8
, WALL_WIDTH = 4
, PADDLE_WIDTH = 4
, BALL_SIZE = 4
, HASTIGHET = 3;
int playerScore = 0
, aiScore = 0
, playerPos = 0
aiPos = 0;
char ballDirectionHori = 'R'
, ballDirectionVerti = 'S';
boolean inProgress = true;
// Prototipo das funcoes
void movePlayer ();
ogiltigt dragAi ();
void drawScore ();
void eraseScore ();
void drawNet ();
void drawPixel (int posX, int posY, int dimensioner);
void erasePixel (int posX, int posY, int dimensioner);
void drawPlayerPaddle (int rad);
void erasePlayerPaddle (int rad);
void drawAiPaddle (int rad);
void eraseAiPaddle (int rad);
void drawBall (int x, int y);
void eraseBall (int x, int y);
ogiltig installation () {
Serial.begin (9600);
// SSD1306_SWITCHCAPVCC = generera visningsspänning från 3,3V internt
if (! display.begin (SSD1306_SWITCHCAPVCC, 0x3C)) {// Adress 0x3D för 128x64
Serial.println (F ("SSD1306-allokering misslyckades"));
för (;;); // Fortsätt inte, slinga för alltid
}
display.display ();
försening (2000); // Pausa i 2 sekunder
// Rensa bufferten
display.clearDisplay ();
}
void loop () {
if (aiScore> 3 || playerScore> 3) {// kontrollera speltillståndet
inProgress = falsk;
// någon har vunnit
display.clearDisplay ();
display.setTextSize (4);
display.setTextColor (VIT);
display.setCursor (0, 0); // ta reda på vem
if (aiScore> playerScore)
display.println ("DU Tappar!");
annat om (playerScore> aiScore)
display.println ("DU VINNA!");
}
if (inProgress) {
eraseScore ();
radera Ball (boll [0], boll [1]);
if (ballDirectionVerti == 'U') // flytta bollen diagonalt
boll [1] = boll [1] - HASTIGHET;
if (ballDirectionVerti == 'D') // flytta bollen diagonalt
boll [1] = boll [1] + HASTIGHET;
if (boll [1] = upplösning [1]) // studsar bollen från botten
ballDirectionVerti = 'U';
if (ballDirectionHori == 'R') {
boll [0] = boll [0] + HASTIGHET; // flytta bollen
om (boll [0]> = (upplösning [0] - 6)) {// boll är vid skärmens AI-kant
om ((aiPos + 12)> = boll [1] && (aiPos - 12) (aiPos + 2)) // avled boll ner
ballDirectionVerti = 'D';
annars om (boll [1] ballDirectionVerti = 'U';
annars // avböj bollen rakt
ballDirectionVerti = 'S';
ballDirectionHori = 'L'; // ändra kulriktning
} annars {// MÅL!
boll [0] = 6; // flytta bollen till andra sidan av skärmen
ballDirectionVerti = 'S'; // återställ bollen till rakt resande
boll [1] = upplösning [1] / 2; // flytta bollen till mitten av skärmen
++ playerScore; // öka spelarens poäng
}
}
}
if (ballDirectionHori == 'L') {
boll [0] = boll [0] - HASTIGHET; // flytta bollen
if (boll [0] = boll [1] && (playerPos - 12) (playerPos + 2)) // avböj boll ner
ballDirectionVerti = 'D';
annars om (boll [1] aiPos)
++ aiPos;
annars om (boll [1]
drawAiPaddle (aiPos);
}
void drawScore () {// Draw AI and player scores
display.setTextSize (2);
display.setTextColor (VIT);
display.setCursor (45, 0);
display.println (playerScore);
display.setCursor (75, 0);
display.println (aiScore);
}
void eraseScore () {// Radera AI och spelarens poäng
display.setTextSize (2);
display.setTextColor (SVART);
display.setCursor (45, 0);
display.println (playerScore);
display.setCursor (75, 0);
display.println (aiScore);
}
void drawNet () {// Draw line field
för (int i = 0; i drawPixel (((upplösning [0] / 2) - 1), i * (WALL_WIDTH) + (WALL_WIDTH * i), WALL_WIDTH);
}
void drawPixel (int posX, int posY, int-dimensioner) {// Rita pixelgrupp
för (int x = 0; x
}
void erasePixel (int posX, int posY, int dimensions) {// Radera grupp av pixlar
för (int x = 0; x
}
void drawPlayerPaddle (int rad) {
drawPixel (0, rad - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (0, rad - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, rad, PADDLE_WIDTH);
drawPixel (0, rad + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (0, rad + (PADDLE_WIDTH + 2), PADDLE_WIDTH);
}
void erasePlayerPaddle (int rad) {
radera Pixel (0, rad - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
radera Pixel (0, rad - PADDLE_WIDTH, PADDLE_WIDTH);
radera Pixel (0, rad, PADDLE_WIDTH);
radera Pixel (0, rad + PADDLE_WIDTH, PADDLE_WIDTH);
radera Pixel (0, rad + (PADDLE_WIDTH + 2), PADDLE_WIDTH);
}
void drawAiPaddle (int rad) {
int kolumn = upplösning [0] - PADDLE_WIDTH;
drawPixel (kolumn, rad - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
drawPixel (kolumn, rad - PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (kolumn, rad, PADDLE_WIDTH);
drawPixel (kolumn, rad + PADDLE_WIDTH, PADDLE_WIDTH);
drawPixel (kolumn, rad + (PADDLE_WIDTH * 2), PADDLE_WIDTH);
}
void eraseAiPaddle (int rad) {
int kolumn = upplösning [0] - PADDLE_WIDTH;
radera Pixel (kolumn, rad - (PADDLE_WIDTH * 2), PADDLE_WIDTH);
radera Pixel (kolumn, rad - PADDLE_WIDTH, PADDLE_WIDTH);
radera Pixel (kolumn, rad, PADDLE_WIDTH);
radera Pixel (kolumn, rad + PADDLE_WIDTH, PADDLE_WIDTH);
radera Pixel (kolumn, rad + (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, SVART);
}