Advertisement
zeromega64twenty

Terminal Pong

Aug 27th, 2023 (edited)
106
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | Gaming | 0 0
  1. import curses
  2. import time
  3.  
  4. def main(stdscr):
  5.     curses.curs_set(0)
  6.     stdscr.nodelay(1)
  7.     stdscr.timeout(100)
  8.  
  9.     curses.start_color()
  10.     curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
  11.     curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
  12.     curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
  13.     curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
  14.     curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
  15.     curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK)
  16.  
  17.     height, width = stdscr.getmaxyx()
  18.     paddle_height = 5
  19.     paddle_width = 1
  20.     ball_row = height // 2
  21.     ball_col = width // 2
  22.     ball_velocity_row = 1
  23.     ball_velocity_col = 1
  24.     paddle1_row = (height - paddle_height) // 2
  25.     paddle2_row = (height - paddle_height) // 2
  26.     score1 = 0
  27.     score2 = 0
  28.  
  29.     while True:
  30.         stdscr.clear()
  31.  
  32.         # Draw paddles with colors
  33.         for i in range(paddle_height):
  34.             stdscr.addch(paddle1_row + i, 0, '|', curses.color_pair(1))
  35.             stdscr.addch(paddle2_row + i, width - 1, '|', curses.color_pair(2))
  36.  
  37.         # Draw ball (circle) with color
  38.         stdscr.addstr(ball_row, ball_col, 'o', curses.color_pair(3))
  39.  
  40.         # Display scores with colors
  41.         stdscr.addstr(0, width // 2 - 10, f"Player 1: {score1}  Player 2: {score2}", curses.color_pair(4))
  42.  
  43.         # Move ball
  44.         ball_row += ball_velocity_row
  45.         ball_col += ball_velocity_col
  46.  
  47.         # Ball collision with walls
  48.         if ball_row == 0 or ball_row == height - 1:
  49.             ball_velocity_row = -ball_velocity_row
  50.         if ball_col == 0:
  51.             score2 += 1
  52.             ball_row = height // 2
  53.             ball_col = width // 2
  54.         if ball_col == width - 1:
  55.             score1 += 1
  56.             ball_row = height // 2
  57.             ball_col = width // 2
  58.  
  59.         # Ball collision with paddles
  60.         if ball_col == 1 and paddle1_row <= ball_row < paddle1_row + paddle_height:
  61.             ball_velocity_col = -ball_velocity_col
  62.         if ball_col == width - 2 and paddle2_row <= ball_row < paddle2_row + paddle_height:
  63.             ball_velocity_col = -ball_velocity_col
  64.  
  65.         # Move paddles with boundary checks
  66.         key = stdscr.getch()
  67.         if key == ord('q') or key == ord('Q'):
  68.             break
  69.         if key == curses.KEY_UP and paddle2_row > 0:
  70.             paddle2_row -= 1
  71.         if key == curses.KEY_DOWN and paddle2_row < height - paddle_height - 1:
  72.             paddle2_row += 1
  73.         if key == ord('w') or key == ord('W'):
  74.             if paddle1_row > 0:
  75.                 paddle1_row -= 1
  76.         if key == ord('s') or key == ord('S'):
  77.             if paddle1_row < height - paddle_height:
  78.                 paddle1_row += 1
  79.  
  80.         stdscr.refresh()
  81.  
  82. curses.wrapper(main)
  83.  
  84. """
  85. Copyright (c) 2023 Zeromega
  86. Drop a link or a Sub on one of my videos if this script help you, copy the link below
  87. https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
  88. """
  89.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement