def selected(key): #A choice from the menu is selected global currentMenu global currentChoice global pauseMode if ((key == pygame.K_ESCAPE) or (key == pygame.K_q)): #go back or quit if (pauseMode == 0): if (len(menuStack) > 0): generatePauseMenu(-1) #clear previous menu generatePauseMenu(0) #go back to main menu del menuStack[-1] elif (pauseMode == 1): #return to game generatePauseMenu(-1) #remove all text if (key == pygame.K_ESCAPE): info.info.setPause(False) #go back to the game else: info.info.quitGame() #quit if key == q music.unpause() elif (((pauseMode == 2) or (pauseMode == 3)) and (key == pygame.K_q)): #game over or game complete menu info.info.quitGame() return if (key == pygame.K_RETURN): #go to the next menu generatePauseMenu(-1) #remove menu text from the screen if (currentMenu == 0): #currently at the main menu menuStack.append(currentMenu) if (currentChoice[0] == 0): #go to level selction generatePauseMenu(3) #generate level selection menu elif (currentChoice[0] == 1): #show Help generatePauseMenu(5) elif (currentChoice[0] == 2): #quit currentChoice = [0,0] info.info.quitGame() elif(currentMenu == 3): #level selection initLevel() #initialized level data info.info.setPause(False) #disble pause mode if (currentChoice[0] == 0): #start level 1 Level1.init(info, music) elif (currentChoice[0] == 1): Level2.init(info, music) else: Level3.init(info, music) elif ((pauseMode == 2) or (pauseMode == 3)): #game over info.info.quitGame()
def onInteract(self): if "mass" not in self.data.keys(): if self.x >= 191 and self.x <= 201: if self.y >= 217 and self.y <= 227: ret = Level3.update(2) if ret == True: self.data["mass"] = 1 self.data["destroyOther"] = ["res/entities/3/Door3_1.xml", "res/entities/3/Door3_2.xml"] return self.data
def onInteract(self): if ('mass' not in self.data.keys()): if (self.x >= 153 and self.x <= 163): if (self.y >= 156 and self.y <= 166): ret = Level3.update(1) if (ret == True): self.data['mass'] = 1 self.data['destroyOther'] = ['res/entities/3/Door3_1.xml', 'res/entities/3/Door3_2.xml'] return self.data
def ghost_vision(map: Maze, current_position: Point): VISION_RANGE = 1 start_1 = current_position.x - VISION_RANGE if current_position.x - VISION_RANGE >= 0 else 0 end_1 = current_position.x + VISION_RANGE if current_position.x + VISION_RANGE < map.N_row else map.N_row - 1 start_2 = current_position.y - VISION_RANGE if current_position.y - VISION_RANGE >= 0 else 0 end_2 = current_position.y + VISION_RANGE if current_position.y + VISION_RANGE < map.M_col else map.M_col - 1 # print ("start 1: ", start_1, "end 1:", end_1) # print ("start 2: ", start_2, "end 2:", end_2) vi = Level3.get_sub_list(start_1, end_1, start_2, end_2, map.maze_data) row_range = range(start_1, end_1 + 1) col_range = range(start_2, end_2 + 1) #new_pos = evaluate_point(current_position, start_1, start_2) return vi, row_range, col_range
def level_screen(): menuscreen = MainScreen("level1.jpg") level1 = textsprite("AngryBirds", "Level 1", 60, 100, 100, 255, 255, 255) level2 = textsprite("AngryBirds", "Level 2", 60, 100, 200, 255, 255, 255) level3 = textsprite("AngryBirds", "Level 3", 60, 100, 300, 255, 255, 255) while True: level1_text = Group(level1) level1_text.draw(menuscreen.display) text_button(level1, menuscreen, "Image", "AngryBirds", "Level 1", 60, 100, 100) level2_text = Group(level2) level2_text.draw(menuscreen.display) text_button(level2, menuscreen, "Image", "AngryBirds", "Level 2", 60, 100, 200) level3_text = Group(level3) level3_text.draw(menuscreen.display) text_button(level3, menuscreen, "Image", "AngryBirds", "Level 3", 60, 100, 300) menu_wait = event.wait() if level1.rect.collidepoint(mouse.get_pos()): Hit = pygame.mixer.Sound('Click2.wav').play() if menu_wait.type == MOUSEBUTTONDOWN: Hit = pygame.mixer.Sound('Click.wav').play() import Level1; Level1.transition_screen1() if level2.rect.collidepoint(mouse.get_pos()): Hit = pygame.mixer.Sound('Click2.wav').play() if menu_wait.type == MOUSEBUTTONDOWN: Hit = pygame.mixer.Sound('Click.wav').play() import Level2; Level2.transition_screen2() if level3.rect.collidepoint(mouse.get_pos()): Hit = pygame.mixer.Sound('Click2.wav').play() if menu_wait.type == MOUSEBUTTONDOWN: Hit = pygame.mixer.Sound('Click.wav').play() import Level3; Level3.transition_screen3() if menu_wait.type == QUIT: pygame.quit() if menu_wait.type == KEYDOWN: if menu_wait.key == K_ESCAPE: menu() display.update()
def run_game2(): #draws the characters and background to the screen screen = pygame.display.set_mode((1120, 560)) display.set_caption("Happy Birds") bg_image = background("level1.jpg") sling = imagesprite("Slingshot1.png", 80, 340) pygame.init() bird = happy(screen, 100, 340, 10) bird_group = Group(bird) sling_group = Group(sling) bool = True while True: bg_image.draw(screen, 0, 0) bird_group.draw(screen) sling_group.draw(screen) level2_wait = pygame.event.wait() if level2_wait.type == QUIT: quit() if level2_wait.type == KEYDOWN: if level2_wait.key == K_ESCAPE: import BirdHub BirdHub.menu() enemy = level2(screen) #if bird is clicked, the sprite can be moved if bird.rect.collidepoint(mouse.get_pos()): if level2_wait.type == MOUSEBUTTONDOWN: while True: bool = False for event1 in pygame.event.get(): if event1.type == pygame.QUIT: pygame.quit() mx, my = pygame.mouse.get_pos() bg_image.draw(screen, 0, 0) enemy.show() x = mx - 10 y = my - 10 #gets the difference betweeen the initial spawn point and point after mouse click x2 = 100 - mx y2 = (400 - my) * -1 #sets the above values to the class bird.setleft(x) bird.settop(y) sling_group.draw(screen) #bird can only be moved at this specific area if x <= 100 and y >= 340: pygame.draw.line(screen, ( 45, 27, 16, ), [mx + 10, my + 10], [120, 370], 5) pygame.draw.line(screen, ( 45, 27, 16, ), [mx + 10, my + 10], [150, 370], 5) arrow = pygame.image.load("arrow1.png") if mx >= 90: img = pygame.transform.rotate(arrow, mx - 130) #arrow rotation screen.blit(img, (130, 280)) else: img = pygame.transform.rotate(arrow, mx - 90) #arrow rotation screen.blit(img, (130, 280)) screen.blit(bird.image, (x, y)) pygame.display.flip() move_wait = pygame.event.wait() if move_wait.type == QUIT: quit() if move_wait.type == KEYDOWN: if move_wait.key == K_ESCAPE: import BirdHub BirdHub.menu() bird.setscalex( 0 ) #gives temporary value for xcoor in birdclass in case x2 and y2 is zero if x <= 100 and y >= 340: if x2 != 0 or y2 != 0: if move_wait.type == MOUSEBUTTONDOWN: Hit = pygame.mixer.Sound('Throw.wav').play() y = sqrt( x2**2 + y2**2) #calculations to get the hypotenuse #cosine and sine is used to manipulate the interpolation cosine = math.cos(y2 / y) sine = math.sin(x2 / y) xscale = x2 / (sine * 5) #yscale must be positive, otherwise the bird will fly up the screen if y2 < 0: yscale = y2 / (cosine * 5) * -1 else: yscale = y2 / (cosine * 5) bird.setscaley(yscale) bird.setscalex(xscale) theClock = pygame.time.Clock() Fps = 60 timer = pygame.time.get_ticks() #loops the animation 300 times for i in range(300): theClock.tick(Fps) Fps += 0.001 bg_image.draw( screen, 0, 0 ) #redraws the background to be constantly blitting bird_group.draw(screen) enemy.show() sling_group.draw(screen) event.get() #line (117 -137)animates the interpolation and the curve #for each interval the bird increases at a set value; intervals are used to create the projectile if i < 10: bird.posi1() elif i < 20: bird.posi2() elif i < 30: bird.posi3() elif i < 40: bird.posi4() elif i < 50: bird.posi5() elif i < 60: bird.nega1() elif i < 70: bird.nega2() elif i < 80: bird.nega3() elif i >= 90: bird.nega4() display.update() #if collide with woodblock, the bird will spawn back at initial spawn point if enemy.woodblock1.rect.colliderect(bird): if enemy.woodblock_call1: Hit = pygame.mixer.Sound( 'Woodhit.wav').play() enemy.remove("woodblock1") bird.rect.left = 100 bird.rect.top = 340 break #collision between the bird and the pig if enemy.pig1.rect.colliderect(bird): if enemy.pig1 in enemy.bird_call: Hit = pygame.mixer.Sound( 'Hit.wav').play() enemy.remove("pig1") if enemy.pig2.rect.colliderect(bird): if enemy.pig2 in enemy.bird_call: Hit = pygame.mixer.Sound( 'Hit.wav').play() enemy.remove("pig2") if enemy.pig3.rect.colliderect(bird): if enemy.pig3 in enemy.bird_call: Hit = pygame.mixer.Sound( 'Hit.wav').play() enemy.remove("pig3") if bird.rect.left >= 1120 or bird.rect.top >= 560: bird.rect.left = 100 bird.rect.top = 340 break if not enemy.bird_call: import Level3 Level3.transition_screen3() display.update()
# Controls which level is called if gamestarted: # Draw Backgrounds/beneath ObjSetter Globals.screen.blit(backgroundImg, (0, 0)) # Controls the movement of the player Movement.checkMovement() # ObjSetter draws in this if Globals.levelnum == 1: Level1.playLevel() elif Globals.levelnum == 2: Level2.playLevel() elif Globals.levelnum == 3: Level3.playLevel() # This will be drawn last # Insert things to be drawn on top of the object setter Globals.screen.blit(playerImg, (Globals.playerX, Globals.playerY)) wireVarRender = Font.render(str(Globals.wires), 1, black) Globals.screen.blit(wireVarRender, (0, 0)) else: introstory.playintro() import ObjSetter #ObjSetter.setMoveObj(0,0,900,500,100,True,playerImg,False) if not Globals.keydelay <= 0: Globals.keydelay -= 1
# Controls which level is called if gamestarted: # Draw Backgrounds/beneath ObjSetter Globals.screen.blit(backgroundImg,(0,0)) # Controls the movement of the player Movement.checkMovement() # ObjSetter draws in this if Globals.levelnum == 1: Level1.playLevel() elif Globals.levelnum == 2: Level2.playLevel() elif Globals.levelnum == 3: Level3.playLevel() # This will be drawn last # Insert things to be drawn on top of the object setter Globals.screen.blit(playerImg,(Globals.playerX,Globals.playerY)) wireVarRender = Font.render(str(Globals.wires), 1, black) Globals.screen.blit(wireVarRender, (0, 0)) else: introstory.playintro() import ObjSetter #ObjSetter.setMoveObj(0,0,900,500,100,True,playerImg,False) if not Globals.keydelay <= 0: Globals.keydelay -= 1
def always(): #The actual level logic global info global music global genLargeAstro global enemyDifficulty global enemyFrequency global textPut info.updateObjects() if (info.stage == 0): #stage 1 if (music.get_pos() > 124000): #go to stage 2 info.gunDelay = 250 info.dps = 50 info.stage = 1 textPut = False info.lb = 1 info.ub = 3 elif (music.get_pos() < 105000): if (not (info.astGen)): info.info.addTimedEvent(info.timeDelay, [timedAstGen], [[info]]) info.astGen = True if (not (genLargeAstro)): #generate large asteroid every 10 seconds genLargeAstro = True info.info.addTimedEvent(2500, [timedAstGen], [(info, 3)]) info.timeDelay -= 5.5555e-3 elif ((not (textPut)) and (music.get_pos() > 113000)): textPut = True textPos = (info.pfSize[0] * 0.3, info.pfSize[1] * 0.5) info.addTimedText("Stage 1 Completed", (255, 0, 0), textPos, 7000) textPos = (info.pfSize[0] * 0.2, info.pfSize[1] * 0.5) info.info.addTimedEvent(7000, [info.addTimedText], [("Stage 2. Enemies Incoming!!!", (255, 0, 0), textPos, 4000)]) elif (info.stage == 1): #stage 2 if (music.get_pos() > 240000): #go to stage 3 info.stage = 2 info.gunDelay = 200 textPut = False info.enemyGen = False info.astGen = False textPos = (info.pfSize[0] * 0.3, info.pfSize[1] * 0.5) info.addTimedText("Stage 2 Completed", (255, 0, 0), textPos, 4000) textPos = (info.pfSize[0] * 0.4, info.pfSize[1] * 0.5) info.info.addTimedEvent(4000, [info.addTimedText], [("Stage 3", (255, 0, 0), textPos, 4000)]) return elif (music.get_pos() <= 214000): #update difficulty and frequency enemyDifficulty = int(2.2223e-5 * (music.get_pos() - 124000)) enemyFrequency += 0.022222 if (music.get_pos() < 225000): if (not (info.enemyGen)): info.enemyGen = True info.info.addTimedEvent(enemyFrequency, [info.generateEnemy], [[enemyDifficulty]]) elif (info.stage == 2): #stage 3 if (music.get_pos() < 330000): #350 if (not (info.astGen) and not (info.enemyGen)): if (random.randrange(0, 2) == 0): #generate asteroid wave info.info.addTimedEvent( 5000, [info.generateWave], [(random.randrange(0, 3), 5, 11, 0)]) info.astGen = True else: #generate enemy info.info.addTimedEvent( 5000, [info.generateWave], [(random.randrange(0, 4), 5, 11, 1)]) info.enemyGen = True else: #Go to level end textPut = False info.stage = 3 elif ((info.stage == 3) and not (textPut) and (music.get_pos() > 340000)): #level end textPos = (info.pfSize[0] * 0.3, info.pfSize[1] * 0.5) textPut = True info.addTimedText("Level 2 Completed", (255, 0, 0), textPos, 7000) info.stage = 4 elif (music.get_pos() > 347000): #Finish level 2 and load level 3 info.info.removeEvent((always, ())) music.stop() Level3.init(info, music) #begin level 3
def level4(map: Maze, cur_pos: Point, path: list, dead_node: list): vision_map, r1, r2 = Level3.vision(map, cur_pos) #print("Vision Map:") #vision_map.print_raw_data() #print("Treats: ", [item.coordinate() for item in vision_map.treats]) ghosts_in_vision = Level3.find_ghost_in_vision(vision_map, cur_pos, r1, r2) up = cur_pos.up() down = cur_pos.down() left = cur_pos.left() right = cur_pos.right() if not vision_map.treats: # food not in vision if ghosts_in_vision: print("Ghost in vision be careful") directions = {} min_dist = 0 # get possible direction if Level3.can_move(map, up, path, dead_node): directions["Up"] = 0 if Level3.can_move(map, down, path, dead_node): directions["Down"] = 0 if Level3.can_move(map, left, path, dead_node): directions["Left"] = 0 if Level3.can_move(map, right, path, dead_node): directions["Right"] = 0 # being corner reset path to find a way out if len(directions) < 2: path.clear() if Level3.can_move(map, up, path, dead_node): directions["Up"] = 0 if Level3.can_move(map, down, path, dead_node): directions["Down"] = 0 if Level3.can_move(map, left, path, dead_node): directions["Left"] = 0 if Level3.can_move(map, right, path, dead_node): directions["Right"] = 0 for ghost in ghosts_in_vision: for dir in directions: if dir == "Up": temp_dist = up.manhattan_distance(ghost) if directions["Up"] < temp_dist: directions["Up"] = temp_dist if dir == "Down": temp_dist = down.manhattan_distance(ghost) if directions["Down"] < temp_dist: directions["Down"] = temp_dist if dir == "Left": temp_dist = left.manhattan_distance(ghost) if directions["Left"] < temp_dist: directions["Left"] = temp_dist if dir == "Right": temp_dist = right.manhattan_distance(ghost) if directions["Right"] < temp_dist: directions["Right"] = temp_dist #print("--------------------------------CALCULATION:", directions) got_stuck = all([min_dist == vl for vl in directions.values()]) # randomly delete 1 direction if more than 1 direction has value equal to max value in directions dup_val = 0 d_list = [] temp_step = max(directions.items(), key=lambda x: x[1])[0] if not got_stuck else "Stuck" for dir in directions: if directions[dir] == temp_step: #print(dir, directions[dir]) d_list.append(dir) dup_val += 1 if dup_val > 1: index = random.randrange(0, len(d_list), 1) del directions[d_list[index]] next_step = max(directions.items(), key=lambda x: x[1])[0] if not got_stuck else "Stuck" return next_step else: directions = [] #print("rand") if Level3.can_move(map, cur_pos.up(), path, dead_node): directions.append("Up") if Level3.can_move(map, cur_pos.down(), path, dead_node): directions.append("Down") if Level3.can_move(map, cur_pos.left(), path, dead_node): directions.append("Left") if Level3.can_move(map, cur_pos.right(), path, dead_node): directions.append("Right") #print(directions) return random.choice(directions) if directions else "Stuck" else: # food in vision if ghosts_in_vision: #print("Ghost in vision be careful") directions = {} min_dist = 0 # get possible direction if Level3.can_move(map, up, path, dead_node): directions["Up"] = 0 if Level3.can_move(map, down, path, dead_node): directions["Down"] = 0 if Level3.can_move(map, left, path, dead_node): directions["Left"] = 0 if Level3.can_move(map, right, path, dead_node): directions["Right"] = 0 # being corner reset path to find a way out if len(directions) < 2: path.clear() if Level3.can_move(map, up, path, dead_node): directions["Up"] = 0 if Level3.can_move(map, down, path, dead_node): directions["Down"] = 0 if Level3.can_move(map, left, path, dead_node): directions["Left"] = 0 if Level3.can_move(map, right, path, dead_node): directions["Right"] = 0 for ghost in ghosts_in_vision: for dir in directions: if dir == "Up": temp_dist = up.manhattan_distance(ghost) if directions["Up"] < temp_dist: directions["Up"] = temp_dist if dir == "Down": temp_dist = down.manhattan_distance(ghost) if directions["Down"] < temp_dist: directions["Down"] = temp_dist if dir == "Left": temp_dist = left.manhattan_distance(ghost) if directions["Left"] < temp_dist: directions["Left"] = temp_dist if dir == "Right": temp_dist = right.manhattan_distance(ghost) if directions["Right"] < temp_dist: directions["Right"] = temp_dist #print("--------------------------------CALCULATION:", directions) got_stuck = all([min_dist == vl for vl in directions.values()]) # randomly delete 1 direction if more than 1 direction has value equal to max value in directions dup_val = 0 d_list = [] temp_step = max(directions.items(), key=lambda x: x[1])[0] if not got_stuck else "Stuck" for dir in directions: if directions[dir] == temp_step: #print(dir, directions[dir]) d_list.append(dir) dup_val += 1 if dup_val > 1: index = random.randrange(0, len(d_list), 1) del directions[d_list[index]] next_step = max(directions.items(), key=lambda x: x[1])[0] if not got_stuck else "Stuck" return next_step else: vision_path = [Level3.evaluate_coordinate(node, r1[0], r2[0]) for node in path if Level3.in_map(r1, r2, node)] vision_dead_nodes = [Level3.evaluate_coordinate(node, r1[0], r2[0]) for node in dead_node if Level3.in_map(r1, r2, node)] return Level1.level1(vision_map, vision_map.pacman_init_position, vision_path, vision_dead_nodes, True)