Está en la página 1de 4

Ping-pong en Arduino

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int buttonPinPlayer1 = 2; // Pin del pulsador para el jugador 1


const int buttonPinPlayer2 = 3; // Pin del pulsador para el jugador 2
const int potentiometerPin = A0; // Pin del potenciómetro

const int paddleWidth = 5; // Ancho de las paletas de los jugadores


const int paddleHeight = 20; // Alto de las paletas de los jugadores
const int paddleSpeed = 2; // Velocidad de movimiento de las paletas

int player1Y = 32; // Posición inicial del jugador 1 en el eje Y


int player2Y = 32; // Posición inicial del jugador 2 en el eje Y
const int playerHeight = 20; // Alto de los jugadores

int ballX = 64; // Posición inicial de la pelota en el eje X


int ballY = 32; // Posición inicial de la pelota en el eje Y
const int ballSize = 4; // Tamaño de la pelota
int ballSpeedX = -2; // Velocidad inicial de la pelota en el eje X
int ballSpeedY = 1; // Velocidad inicial de la pelota en el eje Y

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 del jugador 2


int potValue = analogRead(potentiometerPin);
int mappedPotValue = map(potValue, 0, 1023, 0, display.height() - paddleHeight);
player2Y = mappedPotValue;

// Movimiento de la pelota
ballX += ballSpeedX;
ballY += ballSpeedY;

// Rebote de la pelota en las paletas


if (ballX <= paddleWidth && ballY >= player1Y && ballY <= player1Y +
paddleHeight) {
ballSpeedX = abs(ballSpeedX);
}
if (ballX >= display.width() - paddleWidth - ballSize && ballY >= player2Y && ballY
<= player2Y + paddleHeight) {
ballSpeedX = -abs(ballSpeedX);
}

// Rebote de la pelota en los bordes de la pantalla


if (ballY <= 0 || ballY >= display.height() - ballSize) {
ballSpeedY = -ballSpeedY;
}

// Verificar si la pelota sale de la pantalla


if (ballX <= 0 || ballX >= display.width() - ballSize) {
// Reiniciar la posición de la pelota
ballX = 64;
ballY = 32;
// Invertir la dirección de la pelota en el eje X
ballSpeedX = -ballSpeedX;
}

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

# Configuración de la pantalla OLED


i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1))
display = ssd1306.SSD1306_I2C(128, 64, i2c)

# Configuración de los pines de los pulsadores y el potenciómetro


buttonPinPlayer1 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
buttonPinPlayer2 = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)
potentiometerPin = machine.ADC(26)

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 del jugador 2


potValue = potentiometerPin.read_u16()
mappedPotValue = int(potValue / 65535 * (display.height() - paddleHeight))
player2Y = mappedPotValue

# Movimiento de la pelota
ballX += ballSpeedX
ballY += ballSpeedY

# Rebote de la pelota en las paletas


if ballX <= paddleWidth and ballY >= player1Y and ballY <= player1Y +
paddleHeight:
ballSpeedX = abs(ballSpeedX)
if ballX >= display.width() - paddleWidth - ballSize and ballY >= player2Y and
ballY <= player2Y + paddleHeight:
ballSpeedX = -abs(ballSpeedX)

# Rebote de la pelota en los bordes de la pantalla


if ballY <= 0 or ballY >= display.height() - ballSize:
ballSpeedY = -ballSpeedY

# Verificar si la pelota sale de la pantalla


if ballX <= 0 or ballX >= display.width() - ballSize:
# Reiniciar la posición de la pelota
ballX = 64
ballY = 32
# Invertir la dirección de la pelota en el eje X
ballSpeedX = -ballSpeedX

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.

Asegúrate de conectar correctamente la pantalla OLED y los pines de los


pulsadores y el potenciómetro de acuerdo a los pines especificados en el código.

También podría gustarte