def reinit(self): self.macgyver = Macgyver(40, 520, "m") # create Macgyver object self.murdock = Murdock(520, 560, "murdock") # create Murdock object self.win = False self.end = False
def run(self): map = Map("level.txt") map.load_maze() map.pos_items() map.pos_guardian() map.display(0, 0) macg = Macgyver(map) continued = True while continued: for event in pygame.event.get(): '''Seeking every events happening while the game is running''' if event.type == pygame.QUIT or \ event.type == KEYDOWN and event.key == K_ESCAPE: '''If any of these events is QUIT type''' continued = False '''Loop is stopped and the game windows is closed''' if event.type == pygame.KEYDOWN: '''Keyboard touch used to moove MacGyver''' if event.key == pygame.K_DOWN: '''If ARROW DOWN pressed''' macg.move_down() if event.key == pygame.K_UP: macg.move_up() if event.key == pygame.K_RIGHT: macg.move_right() if event.key == pygame.K_LEFT: macg.move_left() map.display(macg.x, macg.y) '''Re-pasting after the events''' macg.check_win() '''Check the victory conditions'''
def set_grid(self): """set_grid defined the maze.""" with open('maze.txt') as maze: datas = maze.read() # je parcours toutes mes lettres # le retour a la ligne est le caractère "\n" count_x = 0 count_y = 0 for letter in datas: print(count_x, count_y) if letter == "M": self.grid[(count_x, count_y)] = "wall" self.wall.append((count_x, count_y)) count_x = count_x + 1 if letter == "A": self.grid[(count_x, count_y)] = "macgyver" self.macgyver = Macgyver(count_x, count_y) count_x = count_x + 1 if letter == "B": self.grid[(count_x, count_y)] = "guardian" self.guardian = Guardian(count_x, count_y) count_x = count_x + 1 if letter == "C": self.grid[(count_x, count_y)] = "floor" self.floor.append((count_x, count_y)) count_x = count_x + 1 if letter == "S": self.grid[(count_x, count_y)] = "exit" count_x = count_x + 1 if letter == "\n": print("line break") count_x = 0 count_y = count_y + 1
def game(): level = Labyrinth() level.open_csv(constants.CSV_LEVEL, constants.NSPRITE) hero = Macgyver() hero.create(level.structure) guard = Guard() guard.create(level.structure) ether = Item() ether.create(level.ground_list, level.structure, 'E') thumb = Item() thumb.create(level.ground_list, level.structure, 'T') needle = Item() needle.create(level.ground_list, level.structure, 'N') screen = View() screen.load() screen.create(level.structure) return level, hero, guard, ether, thumb, needle, screen
def main(labychoice, surface): score = 0 level = Level(labychoice) # Display the labyrinth level.display(surface, score) # initialise Macgyver's position in the labyrinth mcgyver = Macgyver(level.pos_mcgyver[0], level.pos_mcgyver[1], level) while True: pygame.time.Clock().tick(30) # Initialise the personage for event in pygame.event.get(): # Display mcgyver position after every movement if event.type == QUIT: quit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: quit() # if mcgyver move to the right elif event.key == K_RIGHT: mcgyver.move("d", level.structure) # if mcgyver goes left in the laby elif event.key == K_LEFT: mcgyver.move("q", level.structure) # if mcgyver move up in the laby elif event.key == K_UP: mcgyver.move("z", level.structure) # if mcgyver move down in the laby elif event.key == K_DOWN: mcgyver.move("s", level.structure) # display the score of mcgyver on the screen level.display(surface, mcgyver.score) level.read_coordinates() # print("Position x: ", mcgyver.x, "Position y: ", mcgyver.y) pygame.display.flip() # if macgyver end the game the user can choose # to replay or quit the game if mcgyver.finish: mcgyver.check_victory_or_defeat() while True: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # to leave the game after reaching the guardian quit() elif event.type == KEYDOWN: # if the user tap space the game restart with laby1 if event.key == K_SPACE: return True # if the user tap the return button he restart laby2 elif event.key == K_RETURN: return True
def __init__(self): self.sprite = Sprite() self.macgyver = Macgyver(40, 520, "m") # create Macgyver object self.murdock = Murdock(520, 560, "murdock") # create Murdock object self.win = False self.end = False self.objects = random.sample(self.way(), 3) # 3 random positions in list from way function self.needle = self.objects[0] # needle correspond to first object in random list self.tube = self.objects[1] # tube correspond to second object in random list self.ether = self.objects[2] # ether correspond to third object in random list self.pos_needle = Loot(self.needle.x * 40, self.needle.y * 40, "needle") # create needle as Loot object self.pos_tube = Loot(self.tube.x * 40, self.tube.y * 40, "tube") # create tube as Loot object self.pos_ether = Loot(self.ether.x * 40, self.ether.y * 40, "ether") # create ether as Loot object
def run(self): """ -tc- Ajouter une docstring.""" # -tc- les 6 lignes ci-dessous sont d'excellentes # -tc- candidates pour une méthode __init__ map = Map("level.txt") map.load_maze() map.pos_items() map.pos_guardian() map.display(0, 0) macg = Macgyver(map) continued = True while continued: for event in pygame.event.get(): # -tc- Attention: un commentaire en python s'exprime avec un #, pas avec # -tc- guillemets ou des apostrophes '''Seeking every events happening while the game is running''' if event.type == pygame.QUIT or \ event.type == KEYDOWN and event.key == K_ESCAPE: '''If any of these events is QUIT type''' continued = False '''Loop is stopped and the game windows is closed''' if event.type == pygame.KEYDOWN: '''Keyboard touch used to moove MacGyver''' if event.key == pygame.K_DOWN: '''If ARROW DOWN pressed''' macg.move_down() if event.key == pygame.K_UP: macg.move_up() if event.key == pygame.K_RIGHT: macg.move_right() if event.key == pygame.K_LEFT: macg.move_left() map.display(macg.x, macg.y) '''Re-pasting after the events''' macg.check_win() '''Check the victory conditions'''
def __init__(self): """Constructor of the class""" pygame.init() # inivible cursor pygame.mouse.set_cursor((8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0)) # set size of window WINDOW = pygame.display.set_mode((450, 450)) # set icon of window ICO = pygame.image.load(IMG_ICO) # load the level LEVEL = Maze(MAZE) # generate the level LEVEL.generate() # load BACKGROUND BACKGROUND = pygame.image.load(IMG_BACKGROUND).convert() # load BAG and IN_BAG BAG = pygame.image.load(IMG_BAG).convert_alpha() IN_BAG = pygame.image.load(IMG_IN_BAG).convert_alpha() # display BACKGROUND WINDOW.blit(BACKGROUND, (0, 0)) # display maze LEVEL.display(WINDOW) # display BAG and IN_BAG WINDOW.blit(BAG, (0, 418)) WINDOW.blit(IN_BAG, (37, 418)) # load MAZE_SONG MAZE_SONG = pygame.mixer.Sound("maze_song.ogg") # play song MAZE_SONG.play(loops=-1, maxtime=0, fade_ms=0) # load Macgyver sprites according to the axis MG = Macgyver("images/macgyver_right.png", "images/macgyver_left.png", "images/macgyver_up.png", "images/macgyver_down.png", LEVEL) # Load loot TUBE_IMG = pygame.image.load(C_TUBE).convert_alpha() NEEDLE_IMG = pygame.image.load(C_NEEDLE).convert_alpha() ETHER_IMG = pygame.image.load(C_ETHER).convert_alpha() # Sprite of loot TUBE = Loot(TUBE_IMG, LEVEL) TUBE.display(TUBE_IMG, WINDOW) NEEDLE = Loot(NEEDLE_IMG, LEVEL) NEEDLE.display(NEEDLE_IMG, WINDOW) ETHER = Loot(ETHER_IMG, LEVEL) ETHER.display(ETHER_IMG, WINDOW) # Var game = True Tube_Picked = False Ether_Picked = False Needle_Picked = False YOU_WIN = False YOU_LOSE = False # Load win and lose img WIN = pygame.image.load(IMG_WIN).convert() LOSE = pygame.image.load(IMG_LOSE).convert() THREE = pygame.image.load(IMG_3).convert_alpha() TWO = pygame.image.load(IMG_2).convert_alpha() ONE = pygame.image.load(IMG_1).convert_alpha() END = pygame.image.load(IMG_END).convert_alpha() while game: pygame.time.Clock().tick(30) pygame.display.set_icon(ICO) pygame.display.set_caption(TITLE_WINDOW) for event in pygame.event.get(): if event.type == KEYDOWN: # display BACKGROUND WINDOW.blit(BACKGROUND, (0, 0)) # display maze LEVEL.display(WINDOW) # display BAG and IN_BAG WINDOW.blit(BAG, (0, 418)) WINDOW.blit(IN_BAG, (37, 418)) if Tube_Picked is False: TUBE.display(TUBE_IMG, WINDOW) if Tube_Picked is True: WINDOW.blit(TUBE_IMG, (40, 418)) if Ether_Picked is False: ETHER.display(ETHER_IMG, WINDOW) if Ether_Picked is True: WINDOW.blit(ETHER_IMG, (75, 418)) if Needle_Picked is False: NEEDLE.display(NEEDLE_IMG, WINDOW) if Needle_Picked is True: WINDOW.blit(NEEDLE_IMG, (110, 418)) if event.key == K_RIGHT: MG.move('right') elif event.key == K_LEFT: MG.move('left') elif event.key == K_UP: MG.move('up') elif event.key == K_DOWN: MG.move('down') # Display MacGyver WINDOW.blit(MG.axe, (MG.var_x, MG.var_y)) # Refresh the display pygame.display.flip() # take the TUBE if Macgyver is on the TUBE if (MG.var_x, MG.var_y) == (TUBE.var_x, TUBE.var_y): Tube_Picked = True # take ETHER if Macgyver is on the ETHER if (MG.var_x, MG.var_y) == (ETHER.var_x, ETHER.var_y): Ether_Picked = True # take NEEDLE if Macgyver is on the NEEDLE if (MG.var_x, MG.var_y) == (NEEDLE.var_x, NEEDLE.var_y): Needle_Picked = True if (event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE): game = False # EndGame Victory or lose # if macgyver arrives in front of the guard if LEVEL.is_end(MG.square_y, MG.square_x): # loot check if (Tube_Picked is True and Needle_Picked is True and Ether_Picked is True): YOU_WIN = True else: YOU_LOSE = True if YOU_WIN is True: WINDOW.blit(WIN, (0, 0)) WINDOW.blit(THREE, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(WIN, (0, 0)) pygame.display.flip() WINDOW.blit(TWO, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(WIN, (0, 0)) pygame.display.flip() WINDOW.blit(ONE, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(WIN, (0, 0)) pygame.display.flip() WINDOW.blit(END, (10, 300)) pygame.display.flip() game = False if YOU_LOSE is True: WINDOW.blit(LOSE, (0, 0)) WINDOW.blit(THREE, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(LOSE, (0, 0)) pygame.display.flip() WINDOW.blit(TWO, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(LOSE, (0, 0)) pygame.display.flip() WINDOW.blit(ONE, (200, 280)) pygame.display.flip() time.sleep(1) WINDOW.blit(LOSE, (0, 0)) pygame.display.flip() WINDOW.blit(END, (10, 300)) pygame.display.flip() game = False
screen.blit(image_font, (0, 0)) # Win image image_win = pygame.image.load(constants.IMAGE_WIN) # Death image image_death = pygame.image.load(constants.IMAGE_DEATH) # Main loop run = True print('Game start') # Instances of classes labyrinth = Labyrinth(screen) items = Items(labyrinth) macgyver = Macgyver(labyrinth) macgyver.collect(items) while run: # Player action that allows interaction with the game for e in pygame.event.get(): # Quit the game if e.type == pygame.QUIT or e.type == KEYDOWN and e.key == K_SPACE: run = False print('you quit the game') # Move the hero if e.type == pygame.KEYDOWN: if e.key == pygame.K_RIGHT: macgyver.move_right() if e.key == pygame.K_LEFT: