释放双眼,带上耳机,听听看~!
目录
步骤一 材料准备
硬件准备:
arduino uno和genuino uno
12mm 按钮开关
220欧姆电阻
跳线
oled显示屏
电烙铁
软件准备:
Arduino IDE
步骤二 电路搭建
根据下面原理图搭建电路
步骤四 编写程序
#include // Include Wire if you're using I2C #include // Include SPI if you're using SPI #include // Include the SFE_MicroOLED library ////////////////////////// // MicroOLED Definition // ////////////////////////// #define PIN_RESET 9 // Connect RST to pin 9 #define PIN_DC 8 // Connect DC to pin 8 #define PIN_CS 10 // Connect CS to pin 10 #define DC_JUMPER 0 ////////////////////////////////// // MicroOLED Object Declaration // ////////////////////////////////// MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration //MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration int a= 0; int b=0; void setup(){ digitalWrite(7, HIGH); delay(200); digitalWrite(7, LOW); delay(100); digitalWrite(7, HIGH); delay(200); digitalWrite(7, LOW); oled.clear(PAGE); oled.begin(); // Initialize the OLED oled.clear(ALL); // Clear the display's internal memory oled.setFontType(1); oled.setCursor(0, 0); oled.print("PONG :)"); oled.display(); // Display what's in the buffer (splashscreen) delay(1000); // Delay 1000 ms oled.clear(PAGE); // Clear the buffer. randomSeed(analogRead(A0) + analogRead(A1)); pinMode(7,OUTPUT); pinMode(2,INPUT); pinMode(3,INPUT); pinMode(4,INPUT); pinMode(5,INPUT); } void loop() { // put your main code here, to run repeatedly: int paddleW = 3; // Paddle width int paddleH = 15; // Paddle height // Paddle 0 (left) position coordinates int paddle0_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); int paddle0_X = 2; // Paddle 1 (right) position coordinates int paddle1_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); int paddle1_X = oled.getLCDWidth() - 3 - paddleW; int ball_rad = 2; // Ball radius // Ball position coordinates int ball_X = paddle0_X + paddleW + ball_rad; int ball_Y = random(1 + ball_rad, oled.getLCDHeight() - ball_rad);//paddle0_Y + ball_rad; int ballVelocityX = 1; // Ball left/right velocity int ballVelocityY = 1; // Ball up/down velocity int paddle0Velocity = -1; // Paddle 0 velocity int paddle1Velocity = 1; // Paddle 1 velocity //while(ball_X >= paddle0_X + paddleW - 1) while ((ball_X - ball_rad > 1) && (ball_X + ball_rad < oled.getLCDWidth() - 2)) { int aa= digitalRead(2); int ab= digitalRead(3); int ba= digitalRead(4); int bb= digitalRead(5); // Increment ball's position ball_X+=ballVelocityX; ball_Y+=ballVelocityY; // Check if the ball is colliding with the left paddle if(aa == HIGH){ paddle1_Y++; paddle1_Y++; } if(ab == HIGH){ paddle1_Y--; paddle1_Y--; } if(ba == HIGH){ paddle0_Y++; paddle0_Y++; } if(bb == HIGH){ paddle0_Y--; paddle0_Y--; } if (ball_X - ball_rad < paddle0_X + paddleW) { // Check if ball is within paddle's height if ((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y + paddleH)) { ball_X++; // Move ball over one to the right ballVelocityX = -ballVelocityX; // Change velocity } } // Check if the ball hit the right paddle if (ball_X + ball_rad > paddle1_X) { // Check if ball is within paddle's height if ((ball_Y > paddle1_Y) && (ball_Y < paddle1_Y + paddleH)) { ball_X--; // Move ball over one to the left ballVelocityX = -ballVelocityX; // change velocity } } // Check if the ball hit the top or bottom if ((ball_Y <= ball_rad) || (ball_Y >= (oled.getLCDHeight() - ball_rad - 1))) { // Change up/down velocity direction ballVelocityY = -ballVelocityY; } // Move the paddles up and down // Change paddle 0's direction if it hit top/bottom if ((paddle0_Y <= 1) || (paddle0_Y > oled.getLCDHeight() - 2 - paddleH)) { paddle0Velocity = -paddle0Velocity; } // Change paddle 1's direction if it hit top/bottom if ((paddle1_Y <= 1) || (paddle1_Y > oled.getLCDHeight() - 2 - paddleH)) { paddle1Velocity = -paddle1Velocity; } // Draw the Pong Field oled.clear(PAGE); // Clear the page // Draw an outline of the screen: oled.rect(0, 0, oled.getLCDWidth() - 1, oled.getLCDHeight()); // Draw the center line oled.rectFill(oled.getLCDWidth()/2 - 1, 0, 2, oled.getLCDHeight()); // Draw the Paddles: oled.rectFill(paddle0_X, paddle0_Y, paddleW, paddleH); oled.rectFill(paddle1_X, paddle1_Y, paddleW, paddleH); // Draw the ball: oled.circle(ball_X, ball_Y, ball_rad); oled.setFontType(1); oled.setCursor(20, 0); oled.print(b); oled.print("-"); oled.print(a); // Actually draw everything on the screen: oled.display(); delay(25); // Delay for visibility } digitalWrite(7, HIGH); delay(200); digitalWrite(7, LOW); delay(100); digitalWrite(7, HIGH); delay(200); digitalWrite(7, LOW); delay(1000); if(ball_X + ball_rad < oled.getLCDWidth() - 2){ a++; }else{ b++; } }
步骤五 验证结果
连接好如上图,下好程序后就可以游戏了。