Toggle Navigation
Hatchery
Eggs
card1o-pong
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import buttons import display import utime import vibra import leds import color BOARD_WIDTH = 159 BOARD_HEIGHT = 79 PADDLE_SIZE = BOARD_HEIGHT // 4 COLOR = (255, 255, 255) FPS = 10 SCORE_WIN = 5 ball_x = BOARD_WIDTH // 2 ball_y = BOARD_HEIGHT // 2 ball_speed_x = 3 ball_speed_y = 2 paddle_speed_y = 2 right_player_x = BOARD_WIDTH - 1 right_player_y = BOARD_HEIGHT // 2 cpu_player_y = BOARD_HEIGHT // 2 cpu_player_x = 1 status_lights = [] points_player = 0 points_cpu = 0 def draw_board(disp): disp.clear() disp.rect(ball_x, ball_y, ball_x + 2, ball_y + 2, filled=True) disp.line(right_player_x , right_player_y, right_player_x, right_player_y + PADDLE_SIZE, col=COLOR) disp.line(cpu_player_x, cpu_player_y, cpu_player_x, cpu_player_y + PADDLE_SIZE, col=COLOR) disp.update() def paddle_cpu_move(): global cpu_player_y paddle_cpu_center = cpu_player_y + PADDLE_SIZE // 2 if paddle_cpu_center <= ball_y: cpu_player_y += 1 else: cpu_player_y -= 1 def paddle_player_move(): global right_player_y pressed = buttons.read( buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT ) if pressed & buttons.BOTTOM_LEFT != 0: right_player_y += paddle_speed_y if pressed & buttons.BOTTOM_RIGHT != 0: right_player_y -= paddle_speed_y if right_player_y < 1: right_player_y = 1 elif right_player_y >= BOARD_HEIGHT - PADDLE_SIZE: right_player_y = BOARD_HEIGHT - PADDLE_SIZE def advance(): global ball_x, ball_y, ball_speed_x, ball_speed_y if ball_y <= abs(ball_speed_y) or ball_y >= BOARD_HEIGHT - ball_speed_y: ball_speed_y = -ball_speed_y if ball_x <= cpu_player_x + abs(ball_speed_x): if ball_y >= cpu_player_y and ball_y <= cpu_player_y + PADDLE_SIZE: ball_speed_x = -ball_speed_x else: point_player() reset_board() if ball_x >= BOARD_WIDTH - ball_speed_x: if ball_y >= right_player_y and ball_y <= right_player_y + PADDLE_SIZE: ball_speed_x = -ball_speed_x else: reset_board() point_cpu() ball_x += ball_speed_x ball_y += ball_speed_y def player_won(): disp.clear() disp.print("You won!", fg=color.GREEN) disp.print("Restart for new game", posx=20, posy=40) disp.update() def cpu_won(): disp.clear() disp.print("You lost!", fg=color.RED) disp.print("Restart for new game", posx=20, posy=40) disp.update() def point_player(): global status_ligths, points_player points_player += 1 vibra.vibrate(200) utime.sleep_ms(300) vibra.vibrate(200) status_lights.append(color.GREEN) def point_cpu(): global status_ligths, points_cpu points_cpu += 1 vibra.vibrate(500) utime.sleep_ms(300) status_lights.append(color.RED) def reset_board(): global ball_x, ball_y, ball_speed_x ball_x = BOARD_WIDTH // 2 ball_y = BOARD_HEIGHT // 2 ball_speed_x = abs(ball_speed_x) def get_results(): if points_player >= SCORE_WIN: player_won() stop() elif points_cpu >= SCORE_WIN: cpu_won() stop() def stop(): while True: utime.sleep_ms(1000) with display.open() as disp: while True: draw_board(disp) paddle_player_move() paddle_cpu_move() advance() leds.set_all(status_lights) get_results() utime.sleep_ms(1000 // FPS)