Documentos de Académico
Documentos de Profesional
Documentos de Cultura
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
pinMode(buttonPinPlayer1, INPUT_PULLUP);
pinMode(buttonPinPlayer2, INPUT_PULLUP);
}
void loop() {
// Movimiento del jugador 1
if (digitalRead(buttonPinPlayer1) == LOW && player1Y > 0) {
player1Y -= paddleSpeed;
}
if (digitalRead(buttonPinPlayer1) == HIGH && player1Y < display.height() -
paddleHeight) {
player1Y += paddleSpeed;
}
// Movimiento de la pelota
ballX += ballSpeedX;
ballY += ballSpeedY;
display.clearDisplay();
// Dibujar jugadores
display.fillRect(0, player1Y, paddleWidth, paddleHeight, WHITE);
display.fillRect(display.width() - paddleWidth, player2Y, paddleWidth,
paddleHeight, WHITE);
// Dibujar pelota
display.fillCircle(ballX, ballY, ballSize, WHITE);
display.display();
}
Código en raspberry Pi Pico
import machine
import ssd1306
paddleWidth = 5
paddleHeight = 20
paddleSpeed = 2
player1Y = 32
player2Y = 32
playerHeight = 20
ballX = 64
ballY = 32
ballSize = 4
ballSpeedX = -2
ballSpeedY = 1
def draw_game():
display.fill(0)
# Dibujar jugadores
display.fill_rect(0, player1Y, paddleWidth, paddleHeight, 1)
display.fill_rect(display.width() - paddleWidth, player2Y, paddleWidth,
paddleHeight, 1)
# Dibujar pelota
display.fill_circle(ballX, ballY, ballSize, 1)
display.show()
while True:
# Movimiento del jugador 1
if not buttonPinPlayer1.value() and player1Y > 0:
player1Y -= paddleSpeed
if buttonPinPlayer1.value() and player1Y < display.height() - paddleHeight:
player1Y += paddleSpeed
# Movimiento de la pelota
ballX += ballSpeedX
ballY += ballSpeedY
draw_game()
NOTA:
Recuerda que para ejecutar este código en el Raspberry Pi Pico, debes tener
instalada la biblioteca ssd1306 en tu entorno de desarrollo MicroPython.