Advertisement
Stonewow

Ball Game 2.0

Nov 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.34 KB | None | 0 0
  1. # Program description:
  2. # A game in which the player controls the platform and must move it under the ball to reflect it and prevent it
  3. # from hitting the bottom of the screen, the ball speeds up with each time it hits the board
  4.  
  5. import curses, time, random, linecache
  6.  
  7. g_scr = None
  8. board_direction = "N"  # Direction of the board, "N" stands for "None"
  9. ball_direction = random.choice(["UR", "UL"])  # Direction of the ball
  10. board_shape = "="  # Shape of the board
  11. hits = 0  # Number of times the ball hit the board
  12. lives = 2  # Number of tries left before the player loses
  13. lose_freeze = 0  # Time to wait after each life loss
  14. pause = 65.0  # 3 / pause = time to wait between screen refreshes
  15. time_score = 1  # Additional score based on time survived
  16. selection = "Hard"  # Used to determine difficulty selection
  17. max_speed = 140.0  # The maximum speed the ball aim to get to
  18. pace = 20  # The pace is which the ball speed rises
  19. record = False  # 'True' if score is a new highest score record
  20. game_over = False
  21.  
  22.  
  23. def init_curses():
  24.     global g_scr
  25.     g_scr = curses.initscr()
  26.     curses.noecho()
  27.     g_scr.nodelay(1)
  28.  
  29.     curses.cbreak()
  30.     g_scr.keypad(1)
  31.     curses.curs_set(0)
  32.  
  33.  
  34. def clean_curses():
  35.     curses.curs_set(1)
  36.     curses.endwin()
  37.  
  38.  
  39. def draw_board():
  40.     # Draws the board
  41.     for i in range(board_size):
  42.         g_scr.addch(my - 2, board_x + i, ord(board_shape), curses.A_BOLD)
  43.  
  44.  
  45. def erase_board():
  46.     # Erases the board
  47.     for i in range(board_size):
  48.         g_scr.addch(my - 2, board_x + i, ord(" "))
  49.  
  50.  
  51. def print_message(msg, y, duration, x=-1, color=0):
  52.     if x == -1:
  53.         x = (mx - len(msg)) / 2
  54.     i = 0
  55.     for ch in msg:
  56.         g_scr.addch(y, x + i, ord(ch), color)
  57.         g_scr.refresh()
  58.         i += 1
  59.         time.sleep(float(duration) / len(msg))
  60.  
  61.  
  62. def small_window():
  63.     # Window is big enough to display message at center:
  64.     if mx >= len("Window is too small to play the game"):
  65.         print_message("Window is too small to play the game", my / 2, 2)
  66.         time.sleep(3)
  67.     # Window is not big enough to display message at center:
  68.     else:
  69.         print " " * mx * my
  70.         print "Window is too small to play the game"
  71.         time.sleep(5)
  72.  
  73.  
  74. def clean_screen():
  75.     for y in range(my):
  76.         g_scr.addstr(y, 0, " " * (mx - 1))
  77.     g_scr.refresh()
  78.  
  79.  
  80. def player_loses():
  81.     choice = "Play Again"  # User's choice: 'Play Again' or 'Exit'
  82.     global game_over
  83.     global record
  84.     clean_screen()
  85.     print_message("Game Over", my / 2 - 2, 0.5, color=curses.A_BOLD)
  86.     time.sleep(0.5)
  87.     print_message("You scored " + str(score) + " Points on " + selection, my / 2, 1)  # Score
  88.     time.sleep(0.5)
  89.     if selection == "Hard":
  90.         if score > int(scores[0]):
  91.             scores[0] = str(score) + "\n\r"
  92.             f = open('scores.txt', 'w')
  93.             f.write(scores[0])
  94.             f.seek(len(scores[0]))
  95.             f.write(scores[1])
  96.             f.close()
  97.         print_message("Highest score on Hard: " + scores[0], my / 2 + 1, 1)
  98.     else:
  99.         if score > int(scores[1]):
  100.             scores[1] = str(score) + "\n\r"
  101.             scores[0] += "\n"
  102.             f = open('scores.txt', 'w')
  103.             f.write(scores[0])
  104.             f.seek(len(scores[0]))
  105.             f.write(scores[1])
  106.             f.close()
  107.         print_message("Highest score on Easy: " + scores[1], my / 2 + 1, 1)
  108.     time.sleep(0.5)
  109.     print_message(" Play Again ", my / 2 + 3, 0.3)
  110.     print_message("    Exit    ", my / 2 + 4, 0.3)
  111.  
  112.     while True:
  113.         k = g_scr.getch()
  114.         if k == curses.KEY_DOWN or k == curses.KEY_UP:
  115.             if choice == "Play Again":
  116.                 choice = "Exit"
  117.             else:
  118.                 choice = "Play Again"
  119.         if choice == "Play Again":
  120.             print_message(" Play Again ", my / 2 + 3, 0, color=curses.A_REVERSE)
  121.             print_message("    Exit    ", my / 2 + 4, 0)
  122.             if k == 10:  # Detects key 'Enter'
  123.                 clean_screen()
  124.                 break
  125.         else:
  126.             print_message("    Exit    ", my / 2 + 4, 0, color=curses.A_REVERSE)
  127.             print_message(" Play Again ", my / 2 + 3, 0)
  128.             if k == 10:  # Detects key 'Enter'
  129.                 game_over = True
  130.                 clean_screen()
  131.                 break
  132.  
  133.  
  134. init_curses()
  135. curses.start_color()
  136. curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
  137. curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_WHITE)
  138. my, mx = g_scr.getmaxyx()
  139. if mx < 60 or my < 16:
  140.     small_window()
  141.     game_over = True
  142. board_size = mx / 8
  143.  
  144. # Get highest score records from file 'scores.txt'
  145. scores = open('scores.txt', 'r').readlines()
  146. open('scores.txt', 'r').close()
  147.  
  148. while True:
  149.     if game_over:
  150.         break
  151.     # Initialize game variables:
  152.     board_x = (mx - board_size) / 2
  153.     ball_x = random.randint(5, mx - 5)
  154.     ball_y = random.randint(5, my / 2)
  155.  
  156.     # Print difficulty selection screen:
  157.     print_message("Choose difficulty:",my / 2 - 2, 0.75)
  158.     print_message(" Hard ", my / 2, 0.3)
  159.     print_message(" Easy ", my / 2 + 1, 0.3)
  160.  
  161.     while True:
  162.         k = g_scr.getch()
  163.         if k == curses.KEY_DOWN or k == curses.KEY_UP:
  164.             if selection == "Hard":
  165.                 selection = "Easy"
  166.             else:
  167.                 selection = "Hard"
  168.         if selection == "Hard":
  169.             print_message(" Hard ", my / 2, 0, color=curses.A_REVERSE)
  170.             print_message(" Easy ", my / 2 + 1, 0)
  171.             if k == 10: # Detects key 'Enter'
  172.                 clean_screen()
  173.                 break
  174.         else:
  175.             print_message(" Easy ", my / 2 + 1, 0, color=curses.A_REVERSE)
  176.             print_message(" Hard ", my / 2, 0)
  177.             if k == 10: # Detects key 'Enter'
  178.                 lives = 3
  179.                 pace = 30
  180.                 max_speed = 110.0
  181.                 pause = 50.0
  182.                 clean_screen()
  183.                 break
  184.     while not game_over:
  185.         time_score += 0.1  # Increase time score
  186.         score = hits * 10 + int(time_score)
  187.  
  188.         # Display messages:
  189.         print_message("Score: " + str(score), my - 1, 0)  # Display score at bottom
  190.         g_scr.addstr(my - 1, mx - 14, "'Esc' to exit")
  191.  
  192.         # Determine user input:
  193.         k = g_scr.getch()
  194.         if k == 27:  # Quits game when 'Esc' is pressed
  195.             clean_screen()
  196.             game_over = True
  197.         if k == curses.KEY_RIGHT:  # Changes board direction to 'Right' when player presses right key
  198.             if board_direction == "N":
  199.                 board_direction = "R"
  200.             else:
  201.                 board_direction = "N"
  202.         elif k == curses.KEY_LEFT:  # Changes board direction to 'Left' when player presses left key
  203.             if board_direction == "N":
  204.                 board_direction = "L"
  205.             else:
  206.                 board_direction = "N"
  207.  
  208.         # Move board:
  209.         if board_x + board_size == mx - 1:
  210.             board_x += 1
  211.         elif board_x == 1:
  212.             board_x = 0
  213.         elif board_direction == "R" and board_x + board_size < mx - 1:
  214.             board_x += 2
  215.         elif board_direction == "L" and board_x > 1:
  216.             board_x -= 2
  217.         else:
  218.             board_direction = "N"
  219.  
  220.         # Move ball
  221.         if ball_direction is "UR":  # Ball direction up - right
  222.             ball_x += 1
  223.             ball_y -= 1
  224.  
  225.         if ball_direction is "DR":  # Ball direction down - right
  226.             ball_x += 1
  227.             ball_y += 1
  228.  
  229.         if ball_direction is "UL":  # Ball direction up - left
  230.             ball_x -= 1
  231.             ball_y -= 1
  232.  
  233.         if ball_direction is "DL":  # Ball direction down - left
  234.             ball_x -= 1
  235.             ball_y += 1
  236.  
  237.         # Determine ball direction:
  238.         if ball_x >= mx - 1 and ball_y <= 0:  # Ball hits upper - right Corner
  239.             ball_direction = "DL"
  240.  
  241.         if ball_x <= 0 and ball_y <= 0:  # Ball hits upper - left Corner
  242.             ball_direction = "DR"
  243.  
  244.         if ball_x <= 0:  # Ball hits left wall
  245.             if "U" in ball_direction:
  246.                 ball_direction = "UR"
  247.             else:
  248.                 ball_direction = "DR"
  249.  
  250.         if ball_x >= mx - 1:  # Ball hits right wall
  251.             if "U" in ball_direction:
  252.                 ball_direction = "UL"
  253.             else:
  254.                 ball_direction = "DL"
  255.  
  256.         if ball_y <= 0:  # Ball hits ceiling
  257.             if "R" in ball_direction:
  258.                 ball_direction = "DR"
  259.             else:
  260.                 ball_direction = "DL"
  261.  
  262.         if ball_y == my - 3:  # Ball at board - level
  263.             if board_x - 1 <= ball_x <= board_x + board_size:
  264.                 hits += 1
  265.                 pause += (max_speed - pause) / pace
  266.                 curses.beep()
  267.                 if "R" in ball_direction:
  268.                     ball_direction = "UR"
  269.                 else:
  270.                     ball_direction = "UL"
  271.         if ball_y == my - 1:  # Player loses 1 life
  272.             lives -= 1
  273.             ball_direction = random.choice(["UR", "UL"])
  274.             ball_x = random.randint(5, mx - 5)
  275.             ball_y = random.randint(my - my / 4, my - 4)
  276.             board_direction = "N"
  277.             board_x = (mx - board_size) / 2
  278.             pause = (50 + pause) / 2
  279.             lose_freeze = 1
  280.  
  281.         if lives == 0:
  282.             player_loses()
  283.             hits = 0  # Number of times the ball hit the board
  284.             lives = 2  # Number of tries left before the player loses
  285.             pause = 65.0  # 3 / pause = time to wait between screen refreshes
  286.             time_score = 1  # Additional score based on time survived
  287.             selection = "Hard"  # Used to determine difficulty selection
  288.             max_speed = 140.0  # The maximum speed the ball aim to get to
  289.             pace = 20  # The pace is which the ball speed rises
  290.             break
  291.  
  292.         # Display Lives:
  293.         g_scr.addstr(my - 1, 0, "Lives:  ")
  294.         g_scr.addstr(my - 1, 6, " {}" * lives + "   ", curses.color_pair(1) | curses.A_BOLD)  # Display lives at bottom
  295.  
  296.         # Draw characters:
  297.         draw_board()
  298.         g_scr.addch(ball_y, ball_x, ord('O'), curses.A_BOLD)
  299.         g_scr.refresh()
  300.         g_scr.addch(ball_y, ball_x, ord(' '))
  301.         erase_board()
  302.         time.sleep(3 / pause + lose_freeze)
  303.         while not k == -1:  # Wipes keyboard memory
  304.             k = g_scr.getch()
  305.         lose_freeze = 0
  306. clean_curses()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement