Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.16 KB | None | 0 0
  1. ###
  2. ### Author : Antoine Scherrer <antoine.scherrer@lecole-ldlc.com>
  3. ### License : GPL
  4. ###
  5.  
  6. # The game map, as a large string
  7. # Be careful not to include useless spaces on the right when you modify the map !
  8. game_map = """
  9. ########
  10. .C ..o #
  11. #.## # #
  12. #.##.# #
  13. #. . .X#
  14. ########
  15. """
  16.  
  17.  
  18. # Definition of each component of the map
  19. PACMAN = 'C'
  20. ENNEMY = 'X'
  21. WALL = '#'
  22. SUPERGUM = 'o'
  23. GUM = '.'
  24. EMPTY = ' '
  25.  
  26. # Compute width and height of the map
  27. width = len(game_map.split('\n')[1])
  28. # we need to subtract 2 because of the newline at beginning and the newline at the end of the string
  29. height = len(game_map.split('\n')) - 2
  30.  
  31.  
  32. # Generate a red colored text
  33. def red_text(txt):
  34.     return '\033[31;1m' + txt + '\033[0m'
  35.  
  36.  
  37. # Generate a green colored text
  38. def green_text(txt):
  39.     return '\033[32;1m' + txt + '\033[0m'
  40.  
  41.  
  42. # Generate a blue colored text
  43. def blue_text(txt):
  44.     return '\033[34;1m' + txt + '\033[0m'
  45.  
  46.  
  47. # Generate a pink colored text
  48. def pink_text(txt):
  49.     return '\033[35;1m' + txt + '\033[0m'
  50.  
  51.  
  52. # Generate a debug text. Use this to show debug messages,
  53. # you can consider the player won't see these messages
  54. def debug_text(txt):
  55.     return '\033[37m' + '[DEBUG]' + txt + '\033[0m'
  56.  
  57.  
  58. # get the index of a position in the game_map string
  59. def get_map_index(position):
  60.     # We need to compute the index of that char in the string.
  61.     # We need to add 1 to the width because the the "new line" characters
  62.     return 1 + position[0] + (width + 1) * position[1]
  63.  
  64.  
  65. # return the character in the game_map at given coordinates
  66. def get_case_content(position):
  67.     # TODO check if position goes outside of the map, return None in that case
  68.     return game_map[get_map_index(position)]
  69.  
  70.  
  71. # remove a gum of the map
  72. def remove_gum_from_map(position):
  73.     # use this line to modify the game_map global variable in the function
  74.     global game_map
  75.     # just in case, check that the case at given position contains a gum !
  76.     if get_case_content(position) != GUM:
  77.         print(debug_text('ERROR: trying to remove a non-existing gum'))
  78.         return
  79.  
  80.     # convert the map into a list (so that we can change a character !)
  81.     game_map_list = list(game_map)
  82.     # remove the gum (put the empty char at the position of the gum)
  83.     game_map_list[get_map_index(position)] = EMPTY
  84.     # convert the list back to a string, that will be the updated game map
  85.     game_map = "".join(game_map_list)
  86.  
  87.  
  88. # move pacman at new position in the map
  89. def move_pacman(current_position, next_position):
  90.     # use this line to modify the game_map global variable in the function
  91.     global game_map
  92.     # TODO : complete this function (you can get inspired by remove_gum_from_map)
  93.     # You will need the two parameters of the function
  94.     # current_position is PACMAN's current position in the map
  95.     # next_position is PACMAN's next position (after move)
  96.  
  97.     game_map_list = list(game_map)
  98.     game_map_list[get_map_index(current_position)] = EMPTY
  99.     game_map = "".join(game_map_list)
  100.  
  101.     game_map_list = list(game_map)
  102.     game_map_list[get_map_index(next_position)] = PACMAN
  103.     game_map = "".join(game_map_list)
  104.     print(debug_text('we are now moving PACMAN'))
  105.  
  106.  
  107. # display the map, with fancy colors !
  108. def show_map(map):
  109.     # for each char of the map
  110.     for char in map:
  111.         if char == WALL:
  112.             print(char, end='')
  113.         elif char == ENNEMY:
  114.             if superpower == False:
  115.                 print(red_text(char), end='')
  116.             else:
  117.                 print(pink_text(char), end='')
  118.         elif char == PACMAN:
  119.             print(green_text(char), end='')
  120.         elif char == GUM:
  121.             print(pink_text(char), end='')
  122.         else:
  123.             print(char, end='')
  124.  
  125.  
  126. # Program starts here !
  127. if __name__ == "__main__":
  128.  
  129.     # Inital positions of PACMAN and ennemy
  130.     pacman_position = [1, 1]
  131.     enemy_position = [6, 4]
  132.     ngum = 0
  133.     superpower = False
  134.  
  135.     while True:
  136.         # Display the game map (this is what "slow refresh game' implies)
  137.         show_map(game_map)
  138.  
  139.         # Ask the user where he wants to go
  140.         # convert user input to uppercase
  141.         move = input('Votre déplacement ?').upper()
  142.  
  143.         # We copy pacman_position in next_position
  144.         next_position = list(pacman_position)
  145.         # Update next_position
  146.         if move == 'L':
  147.             next_position[0] -= 1
  148.         elif move == 'R':
  149.             next_position[0] += 1
  150.         elif move == 'U':
  151.             next_position[1] -= 1
  152.         elif move == 'D':
  153.             next_position[1] += 1
  154.         else:
  155.             print('Move not understood, try again.')
  156.             continue
  157.  
  158.         # Depending of the content of the case, move PACMAN and take required actions
  159.         case = get_case_content(next_position)
  160.         if case == WALL:
  161.             print(red_text('Vous enterred a wall, try again'))
  162.         elif case == ENNEMY:
  163.             if superpower == False:
  164.                 print(red_text('ENNEMY THERE => YOU DIE // GAME OVER'))
  165.                 break
  166.             else:
  167.                 print("MANGE ENNEMI")
  168.         elif case == GUM:
  169.             print(green_text('Yummy !'))
  170.             ngum = ngum + 1
  171.             remove_gum_from_map(next_position)
  172.             # update PACMAN position
  173.             move_pacman(pacman_position, next_position)
  174.             pacman_position = list(next_position)
  175.         elif case == SUPERGUM:
  176.             # TODO Deal with SUPERGUM effect
  177.             print(pink_text('You are now invincible'))
  178.             superpower = True
  179.             # update PACMAN position
  180.             move_pacman(pacman_position, next_position)
  181.             pacman_position = list(next_position)
  182.         elif case == EMPTY:
  183.             print(pink_text('Nothing here, keep moving'))
  184.             # update PACMAN position
  185.             move_pacman(pacman_position, next_position)
  186.             pacman_position = list(next_position)
  187.         elif case == None:
  188.             print(red_text('You went outside of the map, stupid !'))
  189.         else:
  190.             print(debug_text('Something went wrong !!'))
  191.  
  192.         # TODO Check is game is finished, in that case display some messages
  193.  
  194.  
  195.         # TODO Make the ennemy move
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement