Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Program description:
- # A game in which the player controls the platform and must move it under the ball to reflect it and prevent it
- # from hitting the bottom of the screen, the ball speeds up with each time it hits the board
- import curses, time, random, linecache
- g_scr = None
- board_direction = "N" # Direction of the board, "N" stands for "None"
- ball_direction = random.choice(["UR", "UL"]) # Direction of the ball
- board_shape = "=" # Shape of the board
- hits = 0 # Number of times the ball hit the board
- lives = 2 # Number of tries left before the player loses
- lose_freeze = 0 # Time to wait after each life loss
- pause = 65.0 # 3 / pause = time to wait between screen refreshes
- time_score = 1 # Additional score based on time survived
- selection = "Hard" # Used to determine difficulty selection
- max_speed = 140.0 # The maximum speed the ball aim to get to
- pace = 20 # The pace is which the ball speed rises
- record = False # 'True' if score is a new highest score record
- game_over = False
- def init_curses():
- global g_scr
- g_scr = curses.initscr()
- curses.noecho()
- g_scr.nodelay(1)
- curses.cbreak()
- g_scr.keypad(1)
- curses.curs_set(0)
- def clean_curses():
- curses.curs_set(1)
- curses.endwin()
- def draw_board():
- # Draws the board
- for i in range(board_size):
- g_scr.addch(my - 2, board_x + i, ord(board_shape), curses.A_BOLD)
- def erase_board():
- # Erases the board
- for i in range(board_size):
- g_scr.addch(my - 2, board_x + i, ord(" "))
- def print_message(msg, y, duration, x=-1, color=0):
- if x == -1:
- x = (mx - len(msg)) / 2
- i = 0
- for ch in msg:
- g_scr.addch(y, x + i, ord(ch), color)
- g_scr.refresh()
- i += 1
- time.sleep(float(duration) / len(msg))
- def small_window():
- # Window is big enough to display message at center:
- if mx >= len("Window is too small to play the game"):
- print_message("Window is too small to play the game", my / 2, 2)
- time.sleep(3)
- # Window is not big enough to display message at center:
- else:
- print " " * mx * my
- print "Window is too small to play the game"
- time.sleep(5)
- def clean_screen():
- for y in range(my):
- g_scr.addstr(y, 0, " " * (mx - 1))
- g_scr.refresh()
- def player_loses():
- choice = "Play Again" # User's choice: 'Play Again' or 'Exit'
- global game_over
- global record
- clean_screen()
- print_message("Game Over", my / 2 - 2, 0.5, color=curses.A_BOLD)
- time.sleep(0.5)
- print_message("You scored " + str(score) + " Points on " + selection, my / 2, 1) # Score
- time.sleep(0.5)
- if selection == "Hard":
- if score > int(scores[0]):
- scores[0] = str(score) + "\n\r"
- f = open('scores.txt', 'w')
- f.write(scores[0])
- f.seek(len(scores[0]))
- f.write(scores[1])
- f.close()
- print_message("Highest score on Hard: " + scores[0], my / 2 + 1, 1)
- else:
- if score > int(scores[1]):
- scores[1] = str(score) + "\n\r"
- scores[0] += "\n"
- f = open('scores.txt', 'w')
- f.write(scores[0])
- f.seek(len(scores[0]))
- f.write(scores[1])
- f.close()
- print_message("Highest score on Easy: " + scores[1], my / 2 + 1, 1)
- time.sleep(0.5)
- print_message(" Play Again ", my / 2 + 3, 0.3)
- print_message(" Exit ", my / 2 + 4, 0.3)
- while True:
- k = g_scr.getch()
- if k == curses.KEY_DOWN or k == curses.KEY_UP:
- if choice == "Play Again":
- choice = "Exit"
- else:
- choice = "Play Again"
- if choice == "Play Again":
- print_message(" Play Again ", my / 2 + 3, 0, color=curses.A_REVERSE)
- print_message(" Exit ", my / 2 + 4, 0)
- if k == 10: # Detects key 'Enter'
- clean_screen()
- break
- else:
- print_message(" Exit ", my / 2 + 4, 0, color=curses.A_REVERSE)
- print_message(" Play Again ", my / 2 + 3, 0)
- if k == 10: # Detects key 'Enter'
- game_over = True
- clean_screen()
- break
- init_curses()
- curses.start_color()
- curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
- curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_WHITE)
- my, mx = g_scr.getmaxyx()
- if mx < 60 or my < 16:
- small_window()
- game_over = True
- board_size = mx / 8
- # Get highest score records from file 'scores.txt'
- scores = open('scores.txt', 'r').readlines()
- open('scores.txt', 'r').close()
- while True:
- if game_over:
- break
- # Initialize game variables:
- board_x = (mx - board_size) / 2
- ball_x = random.randint(5, mx - 5)
- ball_y = random.randint(5, my / 2)
- # Print difficulty selection screen:
- print_message("Choose difficulty:",my / 2 - 2, 0.75)
- print_message(" Hard ", my / 2, 0.3)
- print_message(" Easy ", my / 2 + 1, 0.3)
- while True:
- k = g_scr.getch()
- if k == curses.KEY_DOWN or k == curses.KEY_UP:
- if selection == "Hard":
- selection = "Easy"
- else:
- selection = "Hard"
- if selection == "Hard":
- print_message(" Hard ", my / 2, 0, color=curses.A_REVERSE)
- print_message(" Easy ", my / 2 + 1, 0)
- if k == 10: # Detects key 'Enter'
- clean_screen()
- break
- else:
- print_message(" Easy ", my / 2 + 1, 0, color=curses.A_REVERSE)
- print_message(" Hard ", my / 2, 0)
- if k == 10: # Detects key 'Enter'
- lives = 3
- pace = 30
- max_speed = 110.0
- pause = 50.0
- clean_screen()
- break
- while not game_over:
- time_score += 0.1 # Increase time score
- score = hits * 10 + int(time_score)
- # Display messages:
- print_message("Score: " + str(score), my - 1, 0) # Display score at bottom
- g_scr.addstr(my - 1, mx - 14, "'Esc' to exit")
- # Determine user input:
- k = g_scr.getch()
- if k == 27: # Quits game when 'Esc' is pressed
- clean_screen()
- game_over = True
- if k == curses.KEY_RIGHT: # Changes board direction to 'Right' when player presses right key
- if board_direction == "N":
- board_direction = "R"
- else:
- board_direction = "N"
- elif k == curses.KEY_LEFT: # Changes board direction to 'Left' when player presses left key
- if board_direction == "N":
- board_direction = "L"
- else:
- board_direction = "N"
- # Move board:
- if board_x + board_size == mx - 1:
- board_x += 1
- elif board_x == 1:
- board_x = 0
- elif board_direction == "R" and board_x + board_size < mx - 1:
- board_x += 2
- elif board_direction == "L" and board_x > 1:
- board_x -= 2
- else:
- board_direction = "N"
- # Move ball
- if ball_direction is "UR": # Ball direction up - right
- ball_x += 1
- ball_y -= 1
- if ball_direction is "DR": # Ball direction down - right
- ball_x += 1
- ball_y += 1
- if ball_direction is "UL": # Ball direction up - left
- ball_x -= 1
- ball_y -= 1
- if ball_direction is "DL": # Ball direction down - left
- ball_x -= 1
- ball_y += 1
- # Determine ball direction:
- if ball_x >= mx - 1 and ball_y <= 0: # Ball hits upper - right Corner
- ball_direction = "DL"
- if ball_x <= 0 and ball_y <= 0: # Ball hits upper - left Corner
- ball_direction = "DR"
- if ball_x <= 0: # Ball hits left wall
- if "U" in ball_direction:
- ball_direction = "UR"
- else:
- ball_direction = "DR"
- if ball_x >= mx - 1: # Ball hits right wall
- if "U" in ball_direction:
- ball_direction = "UL"
- else:
- ball_direction = "DL"
- if ball_y <= 0: # Ball hits ceiling
- if "R" in ball_direction:
- ball_direction = "DR"
- else:
- ball_direction = "DL"
- if ball_y == my - 3: # Ball at board - level
- if board_x - 1 <= ball_x <= board_x + board_size:
- hits += 1
- pause += (max_speed - pause) / pace
- curses.beep()
- if "R" in ball_direction:
- ball_direction = "UR"
- else:
- ball_direction = "UL"
- if ball_y == my - 1: # Player loses 1 life
- lives -= 1
- ball_direction = random.choice(["UR", "UL"])
- ball_x = random.randint(5, mx - 5)
- ball_y = random.randint(my - my / 4, my - 4)
- board_direction = "N"
- board_x = (mx - board_size) / 2
- pause = (50 + pause) / 2
- lose_freeze = 1
- if lives == 0:
- player_loses()
- hits = 0 # Number of times the ball hit the board
- lives = 2 # Number of tries left before the player loses
- pause = 65.0 # 3 / pause = time to wait between screen refreshes
- time_score = 1 # Additional score based on time survived
- selection = "Hard" # Used to determine difficulty selection
- max_speed = 140.0 # The maximum speed the ball aim to get to
- pace = 20 # The pace is which the ball speed rises
- break
- # Display Lives:
- g_scr.addstr(my - 1, 0, "Lives: ")
- g_scr.addstr(my - 1, 6, " {}" * lives + " ", curses.color_pair(1) | curses.A_BOLD) # Display lives at bottom
- # Draw characters:
- draw_board()
- g_scr.addch(ball_y, ball_x, ord('O'), curses.A_BOLD)
- g_scr.refresh()
- g_scr.addch(ball_y, ball_x, ord(' '))
- erase_board()
- time.sleep(3 / pause + lose_freeze)
- while not k == -1: # Wipes keyboard memory
- k = g_scr.getch()
- lose_freeze = 0
- clean_curses()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement