class GameLoop: """Game Loop.""" def __init__(self): self.things = Items() self.character = MacGyver() self.draw = Display() def game_loop(self): """Launch the game loop and check the victory conditions""" start = True while start: time.Clock().tick(30) self.draw.maze_create() self.things.items_create(self.draw.maze) while self.character.win is False and self.character.lose is False: self.draw.update_display(self.things, self.character) self.character.mg_move(self.draw, self.things) if len(self.things.backpack) <= 3: self.things.items_colleted(self.character) if self.character.lose is True: self.draw.gameover() self.draw = Display() self.character = MacGyver() self.things = Items() else: exit()
def __init__(self, json_file="labyrinth.json"): """ GameText constructor. Instantiates objects from Labyrinth, MacGyver and Syringe classes. """ self.labyrinth = Labyrinth(json_file) self.macgyver = MacGyver(self.labyrinth) self.syringe = Syringe(self.labyrinth)
def __init__(self): """ Initialize the game. """ pygame.init() self.m = Maze(constants.FILENAME) self.mg = MacGyver(self.m) # display the maze with pygame MazeScreen(self.m.paths, self.m.walls, self.m.start, self.m.goal, self.m.items)
def main(): labyrinth = Labyrinth() labyrinth.load_labyrinth_from_file("labyrinth.txt") macgyver = MacGyver(labyrinth) running = True while running: print(labyrinth.display()) response = input("Where do you want to go ? ( Q to quit the game)") if response == "q" or response == "Q": running = False elif response in ("h", "b", "d", "g"): macgyver.move(directions[response])
def __init__(self): """ Initialize the game. """ pygame.init() self.settings = Settings() self.window = pygame.display.set_mode(self.settings.maze_dimensions) self.admin = GameAdmin(self.window) self.m = Maze(self.settings.FILENAME) self.mg = MacGyver(self.m) # display the maze with pygame DisplayMaze(self.m.paths, self.m.walls, self.m.start, self.m.goal, self.m.item1, self.m.item2, self.m.item3) self.start = self.m.start self.display_mac() self.state = True
def parse_file(self): """Method that manages the parsing of the .txt file representing the maze""" content = {} coo_x, coo_y = 0, 0 spider = open("labyrinthe.txt", "r") # We open the file in playback and assign its contents to spider for line in spider: # We walk the lines of spider for caracter in line: # We're going through the characters of line if caracter == 'd': # if the character is a d then create the pair of keys MacGyver self.mac_gyver = MacGyver([coo_x, coo_y]) # if the character is a back to the line if caracter == "\n": # we iron y to 0, and x to x+1 coo_y = 0 coo_x = coo_x + 1 # Otherwise the character is not a back to the line else: # We create the pair key: value in the dictionary content content[(coo_x, coo_y)] = caracter coo_y = coo_y + 1 return content
class GameText: """ Sets GameText class. The GameText class consists of 2 methods : - __init__() - run() """ def __init__(self, json_file="labyrinth.json"): """ GameText constructor. Instantiates objects from Labyrinth, MacGyver and Syringe classes. """ self.labyrinth = Labyrinth(json_file) self.macgyver = MacGyver(self.labyrinth) self.syringe = Syringe(self.labyrinth) def run(self): """ Manages the game progress. Sets : "carry_on" boolean variable user interaction labyrinth display """ carry_on = True print("\nHelp MacGyver to escape from the labyrinth : he needs to pick\ up a needle, a tube and ether before fighting the guard !") while carry_on: self.labyrinth.display() user_answer = input("In which direction do you want MacGyver to \ move ? Please enter 'u' for up, 'd' for down, 'l' for left and 'r' for \ right, or 'q' to quit : ") success = None if user_answer not in ["q", "u", "d", "l", "r"]: print("\nInvalid choice !") continue elif user_answer == "q": carry_on = False else: success = self.macgyver.move(user_answer) # 'success' becomes True or False after fighting the guard if success is not None: carry_on = False self.labyrinth.display()
class Main: """ Game management. """ def __init__(self): """ Initialize the game. """ pygame.init() self.m = Maze(constants.FILENAME) self.mg = MacGyver(self.m) # display the maze with pygame MazeScreen(self.m.paths, self.m.walls, self.m.start, self.m.goal, self.m.items) def run_game(self): """ Start the main loop for the game. """ while True: self.check_events() # self.check_special(cell_pos) def check_events(self): """ respond to keypresses. """ for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) def _check_keydown_events(self, event): """ respond to keypresses """ if event.key == pygame.K_RIGHT: # move MacGyver to the right self.mg.move('RIGHT') elif event.key == pygame.K_LEFT: self.mg.move('LEFT') elif event.key == pygame.K_DOWN: self.mg.move('DOWN') elif event.key == pygame.K_UP: self.mg.move('UP') elif event.key == pygame.K_q: sys.exit()
from macgyver import MacGyver from maze import Maze from position import Position import settings as constants instance_maze = Maze(constants.FILENAME) instance_macgyver = MacGyver(instance_maze) mg_pos = instance_macgyver.mg_position paths = instance_maze.paths walls = instance_maze.walls # value = input('une touche ') value = 'z' if value == 'z': print( f'MG essaye d\'aller vers la droite, à la coordonnée : {Position.up(mg_pos)}' ) if Position.up(mg_pos) in paths: new_mg_pos = Position.up(mg_pos) print( f'La case est valide, nouvelle position de MacGyver : {new_mg_pos}' ) if value == 's': print( f'MG essaye d\'aller vers le bas, à la coordonnée : {Position.down(mg_pos)}' ) if Position.down(mg_pos) in paths: new_mg_pos = Position.down(mg_pos) print(
def __init__(self): self.things = Items() self.character = MacGyver() self.draw = Display()
class Main: """ I'm managing the game. """ def __init__(self): """ Initialize the game. """ pygame.init() self.settings = Settings() self.window = pygame.display.set_mode(self.settings.maze_dimensions) self.admin = GameAdmin(self.window) self.m = Maze(self.settings.FILENAME) self.mg = MacGyver(self.m) # display the maze with pygame DisplayMaze(self.m.paths, self.m.walls, self.m.start, self.m.goal, self.m.item1, self.m.item2, self.m.item3) self.start = self.m.start self.display_mac() self.state = True def run_game(self): """ Start the main loop for the game. """ while self.state is True: self.check_events() def check_events(self): """ respond to keypresses. """ for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) def _check_keydown_events(self, event): """ respond to keypresses """ if event.key == pygame.K_RIGHT: # move MacGyver to the right validate = self.mg.move('RIGHT') if validate is True: self.move_mac(((self.settings.cell_dimension_x), 0)) self.check_end() elif event.key == pygame.K_LEFT: validate = self.mg.move('LEFT') if validate is True: self.move_mac(((self.settings.cell_dimension_x * -1), 0)) self.check_end() elif event.key == pygame.K_DOWN: validate = self.mg.move('DOWN') if validate is True: self.move_mac((0, (self.settings.cell_dimension_x))) self.check_end() elif event.key == pygame.K_UP: validate = self.mg.move('UP') if validate is True: self.move_mac((0, (self.settings.cell_dimension_x * -1))) self.check_end() elif event.key == pygame.K_q: sys.exit() def display_mac(self): """ Display MacGyver on the maze. """ self.img_mg = pygame.image.load(self.settings.macgyver).convert() position_mac = list(self.start)[0] pos_x = position_mac.y * self.settings.cell_dimension_x pos_y = position_mac.x * self.settings.cell_dimension_y self.position_perso = pygame.Rect( (pos_x, pos_y, self.settings.img_dimension_x, self.settings.img_dimension_y)) self.window.blit(self.img_mg, self.position_perso) pygame.display.flip() def move_mac(self, move): """ I'm moving the image of MacGYver. """ self.img_floor = pygame.image.load(self.settings.floor).convert() self.window.blit(self.img_floor, self.position_perso) self.position_perso = self.position_perso.move(move) self.window.blit(self.img_mg, self.position_perso) pygame.display.flip() def check_end(self): """ I'm checking if this is the end cell. """ if self.mg.mac_goal is True: if 'item1' in self.mg.bag and 'item2' in self.mg.bag and 'item3' in self.mg.bag: self.state = self.admin.win_game() else: self.state = self.admin.loose_game()
def __init__(self, carte): self.carte = carte self.dico_laby = {"aiguille": 'A', "tube": 'T', "ether": 'E'} self.map = [] self.load_labyrinth() self.mac_gyver = MacGyver(self.map)
def main(): screen = pygame.display.set_mode((15 * sp_size, 15 * sp_size)) Lab = Labyrinth("mappy.txt", sp_size) Lab.convert_file_txt() Lab.get_pos() objects = Objects(Lab) objects.random() macgyver = MacGyver("mappy.txt", sp_size, objects, Lab) guardian = Guardian(sp_size) pygame.display.flip() current_game = True while current_game: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: macgyver.move_right() elif event.key == pygame.K_LEFT: macgyver.move_left() elif event.key == pygame.K_UP: macgyver.move_up() elif event.key == pygame.K_DOWN: macgyver.move_down() macgyver.get_objects() Lab.get_pos() arbitror(macgyver, guardian) objects.display_objects() screen.blit(guardian.image, (guardian.rect.x, guardian.rect.y)) screen.blit(macgyver.image, (macgyver.rect.x, macgyver.rect.y)) macgyver.score_count() pygame.display.flip()
class GameGUI: """ Sets GameGUI class. The GameGUI class consists of 2 methods : - __init__() - run() """ def __init__(self, json_file="labyrinth.json"): """ GameGUI constructor. Instantiates objects from Labyrinth, MacGyver and Syringe classes. """ self.labyrinth = Labyrinth(json_file) self.macgyver = MacGyver(self.labyrinth) self.syringe = Syringe(self.labyrinth) def run(self): """ Manages the game progress with Pygame.""" pygame.init() window = pygame.display.set_mode((600, 600)) # Window icon icone = pygame.image.load("images/macgyver_32_40.png") pygame.display.set_icon(icone) # Window title pygame.display.set_caption("Help MacGyver to escape from the \ labyrinth !") # Background background = pygame.image.load( "images/background_800_545.jpg").convert() window.blit(background, (0, 0)) window.blit(background, (0, 100)) # MacGyver macgyver = pygame.image.load( "images/macgyver_32_40.png").convert_alpha() macgyver_position = self.labyrinth.get_position("M") window.blit(macgyver, (macgyver_position[0] * 40 + 4, macgyver_position[1] * 40)) # Guard guard = pygame.image.load("images/guard_32_36.png").convert_alpha() guard_position = self.labyrinth.get_position("G") window.blit(guard, (guard_position[0] * 40 + 4, guard_position[1] * 40 + 2)) # Syringe elements needle = pygame.image.load("images/needle_20_20.png").convert_alpha() tube = pygame.image.load("images/tube_20_20.png").convert_alpha() ether = pygame.image.load("images/ether_20_20.png").convert_alpha() syringe_elements_positions = self.syringe.positions window.blit(needle, (syringe_elements_positions[0][0] * 40 + 10, syringe_elements_positions[0][1] * 40 + 10)) window.blit(tube, (syringe_elements_positions[1][0] * 40 + 10, syringe_elements_positions[1][1] * 40 + 10)) window.blit(ether, (syringe_elements_positions[2][0] * 40 + 10, syringe_elements_positions[2][1] * 40 + 10)) # Walls wall = pygame.image.load("images/wall_40_40.png").convert() walls_positions = self.labyrinth.get_walls_positions() for wall_position in walls_positions: window.blit(wall, (wall_position[0] * 40, wall_position[1] * 40)) pygame.display.flip() # Allows to hold down a key and move faster pygame.key.set_repeat(400, 30) carry_on = True success = None while carry_on: # Loop speed pygame.time.Clock().tick(30) for event in pygame.event.get(): if event.type == QUIT: carry_on = False elif event.type == KEYDOWN: if event.key == K_UP: success = self.macgyver.move("u") elif event.key == K_DOWN: success = self.macgyver.move("d") elif event.key == K_LEFT: success = self.macgyver.move("l") elif event.key == K_RIGHT: success = self.macgyver.move("r") # New display window.blit(background, (0, 0)) window.blit(background, (0, 100)) # Get new MacGyver position macgyver_position = self.macgyver.position window.blit( macgyver, (macgyver_position.x * 40 + 4, macgyver_position.y * 40)) window.blit( guard, (guard_position[0] * 40 + 4, guard_position[1] * 40 + 2)) # Items are no longer displayed once they have been picked up if "N" not in self.macgyver.collected_syringe_elements: window.blit(needle, (syringe_elements_positions[0][0] * 40 + 10, syringe_elements_positions[0][1] * 40 + 10)) if "T" not in self.macgyver.collected_syringe_elements: window.blit(tube, (syringe_elements_positions[1][0] * 40 + 10, syringe_elements_positions[1][1] * 40 + 10)) if "E" not in self.macgyver.collected_syringe_elements: window.blit(ether, (syringe_elements_positions[2][0] * 40 + 10, syringe_elements_positions[2][1] * 40 + 10)) walls_positions = self.labyrinth.get_walls_positions() for wall_position in walls_positions: window.blit(wall, (wall_position[0] * 40, wall_position[1] * 40)) pygame.display.flip() # 'success' becomes True or False after fighting the guard if success: you_win = pygame.image.load( "images/you_win_200_200.png").convert() window.blit(you_win, (200, 200)) pygame.display.flip() carry_on = False elif success == False: game_over = pygame.image.load( "images/game_over_320_256.jpg").convert() window.blit(game_over, (140, 172)) pygame.display.flip() carry_on = False pygame.quit() # Wait 4 seconds before closing the game window pygame.time.wait(4000)