from machine import *
import utime
import ssd1306
import uasyncio
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
player_1: int = 25
player_2: int = 25
ball: list = [
[64, 32], [65, 32],
[64, 33], [65, 33]
]
ball_direction: list = [-1, -1]
ball_speed: int = 2
speed: int = 2
aButton = Pin(22, Pin.IN, Pin.PULL_DOWN)
bButton = Pin(21, Pin.IN, Pin.PULL_DOWN)
leftButton = Pin(27, Pin.IN, Pin.PULL_DOWN)
rightButton = Pin(26, Pin.IN, Pin.PULL_DOWN)
start = False
mode: int
def Title():
for a in range(21): # PRIMERA ANIMACION
oled.fill(0)
oled.hline(0, a * 3, 128, 1)
oled.show()
utime.sleep(0.025)
oled.fill(0)
oled.show()
utime.sleep(0.5)
oled.text("PONG", 48, 30) # TITULO
oled.show()
utime.sleep(1)
# Inicio
def SelectMode():
oled.fill(0)
oled.show()
utime.sleep(0.5)
oled.fill(0)
oled.rect(0, 0, 128, 64, 1)
oled.text("Single", 26, 20)
oled.text("Multiplayer", 25, 40)
oled.text(">", 15, 20)
oled.show()
mode = 0
while True:
if rightButton.value() == 1:
oled.fill_rect(15, 20, 7, 10, 0)
oled.text(">", 15, 40)
oled.show()
if leftButton.value() == 1:
oled.fill_rect(15, 40, 7, 20, 0)
oled.text(">", 15, 20)
oled.show()
if aButton.value() == 1 or bButton.value() == 1:
if mode == 0:
SinglePlayer()
if mode == 1:
MultiPlayer()
utime.sleep(0.05)
# CODIGO DEL MODO SINGLE
def SinglePlayer():
global player_1
global player_2
global ball_direction
player_2_direction: int = 1
oled.fill_rect(5, 5, 120, 55, 0)
oled.vline(15, player_2, 15, 1)
oled.vline(108, player_1, 15, 1)
oled.text("1", 60, 30)
oled.show()
utime.sleep(1)
oled.fill_rect(20, 30, 60, 30, 0)
oled.text("2", 60, 30)
oled.show()
utime.sleep(1)
oled.fill_rect(20, 30, 60, 30, 0)
oled.text("3", 60, 30)
oled.show()
utime.sleep(1)
oled.fill_rect(20, 30, 60, 30, 0)
for a in ball:
oled.pixel(a[0], a[1], 1)
oled.show()
while True:
# MOVIMIENTO PLAYER 1
if leftButton.value() == 1 and player_1 < 47: # MOVIMIENTO HACIA ABAJO
player_1 = player_1 + speed
oled.vline(108, 1, 62 , 0)
oled.vline(108, player_1, 15, 1)
oled.show()
if rightButton.value() == 1 and player_1 > 2: # MOVIMIENTO HACIA ARRIBA
player_1 = player_1 - speed
oled.vline(108, 1, 62 , 0)
oled.vline(108, player_1, 15, 1)
oled.show()
# MOVIMIENTO PLAYER 2
if player_2 >= 47 or player_2 <= 2:
player_2_direction = -player_2_direction
player_2 += player_2_direction * speed
oled.vline(15, 1, 62 , 0)
oled.vline(15, player_2, 15, 1)
oled.show()
# BALL COLISIONS
if ball_direction[0] == -1: # LEFT
for a in [0, 2]:
for p in range(ball[a][0] - speed, ball[a][0]):
if oled.pixel(p, ball[a][1]) == 1:
ball_direction[0] = 1
if ball_direction[1] == -1: # UP
for a in [0, 1]:
for p in range(ball[a][1] - ball_speed, ball[a][1]):
if oled.pixel(ball[a][0], p) == 1:
ball_direction[1] = 1
if ball_direction[0] == 1: # RIGHT
for a in [1, 3]:
for p in range(ball[a][0] + 1, ball[a][0] + ball_speed + 1):
if oled.pixel(p, ball[a][1]) == 1:
ball_direction[0] = -1
if ball_direction[1] == 1: # DOWN
for a in [2, 3]:
for p in range(ball[a][1] + 1, ball[a][1] + ball_speed + 1):
if oled.pixel(ball[a][0], p) == 1:
ball_direction[1] = -1
# BALL MOVEMENT
for p in ball:
oled.pixel(p[0], p[1], 0)
for p in ball:
p[0] += speed * ball_direction[0]
p[1] += speed * ball_direction[1]
oled.pixel(p[0], p[1], 1)
oled.show()
utime.sleep(0.025)
# CODIGO DEL MULTIPLAYER
def MultiPlayer():
while True:
utime.sleep(0.025)
# INICIO DEL JUEGO
Title()
while start == False:
for a in range(40):
if a == 0:
for p in [
[49, 50, 49, 48, 47, 46], [50, 49, 46], [51, 47, 48], # P
[53, 50, 49, 48, 47, 46], [54, 47], [55, 46], [56, 47], # R
[58, 49, 48, 47], [59, 50, 48, 46], [60, 50, 48, 46], [61, 48, 47], # E
[63, 50, 47], [64, 50, 48, 46], [65, 50, 48, 46], [66, 49, 46], #S
[68, 50, 47], [69, 50, 48, 46], [70, 50, 48, 46], [71, 49, 46], #S
[75, 50, 49, 48, 47], [76, 48, 46], [77, 48, 46], [78, 50, 49, 48, 47] # A
]:
for a in range(len(p) - 1):
oled.pixel(p[0], p[a + 1], 1)
oled. show()
if a == 20:
for p in [
[49, 50, 49, 48, 47, 46], [50, 49, 46], [51, 47, 48], # P
[53, 50, 49, 48, 47, 46], [54, 47], [55, 46], [56, 47], # R
[58, 49, 48, 47], [59, 50, 48, 46], [60, 50, 48, 46], [61, 48, 47], # E
[63, 50, 47], [64, 50, 48, 46], [65, 50, 48, 46], [66, 49, 46], #S
[68, 50, 47], [69, 50, 48, 46], [70, 50, 48, 46], [71, 49, 46], #S
[75, 50, 49, 48, 47], [76, 48, 46], [77, 48, 46], [78, 50, 49, 48, 47] # A
]:
for a in range(len(p) - 1):
oled.pixel(p[0], p[a + 1], 0)
oled. show()
if aButton.value() == 1 or bButton.value() == 1:
start = True
SelectMode()
utime.sleep(0.025)