def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Super Mario") # 创建马里奥 mario = Mario(screen, ai_settings) # 创建一个用于存储游戏统计信息的实例 status = GameStatus(ai_settings) # 创建一个蘑菇 mushroom = Mushroom(screen, ai_settings, status) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 gf.check_events(mario) if status.game_active: mario.update() gf.check_mario_mushroom_collisions(mario, mushroom) mushroom.update(status) gf.update_screen(ai_settings, screen, mario, mushroom)
def create_mushroom(settings, screen, mushrooms, mushroom_number, row_number): '''Create a mushroom and place it in the row.''' mushroom = Mushroom(settings, screen) mushroom_width = mushroom.rect.width mushroom.x = mushroom_width + 2 * mushroom_width * mushroom_number mushroom.rect.x = mushroom.x mushroom.rect.y = mushroom.rect.height + 2 * mushroom.rect.height * row_number mushrooms.add(mushroom)
def creat_mushroom(screen, ai_settings, mushrooms): if ai_settings.lives == 2: random_num = random.choice(ai_settings.room_choices) mushroom = Mushroom(screen, ai_settings) mushroom.rect.y -= random_num mushrooms.add(mushroom) elif ai_settings.lives == 3: random_num = random.sample(ai_settings.room_choices, ai_settings.lives) for i in range(len(mushrooms), ai_settings.lives - 1): mushroom = Mea(screen, ai_settings) mushroom.rect.y -= random_num[i] mushrooms.add(mushroom) return len(mushrooms)
def enter(): global background, player, mob1s, blocks, pipes, stairs, traproad, mush, muscle, cloud, mob2, mob3 background = Background() player = Player() mob1s = create_mobs() blocks = create_blocks() pipes = create_pipes() stairs = create_stairs() traproad = Traproad() mush = Mushroom() muscle = Muscleman() cloud = Cloud() mob2 = Monster2() mob3 = Monster3() player.set_background(background)
def create_fleet(settings, screen, snail, mushrooms): '''Create fleet of mushrooms.''' # Create a mushroom and find the number of mushrooms in a row. # Spacing between each mushroom is equal to 1/2 mushroom width. mushroom = Mushroom(settings, screen) number_mushrooms_x = get_number_mushrooms_x(settings, mushroom.rect.width) number_rows = get_number_rows(settings, snail.rect.height, mushroom.rect.height) # Create the fleet of mushrooms. for row_number in range(number_rows // 2): # Create the first row of mushrooms. for mushroom_number in range(number_mushrooms_x): # Create a mushroom and place it in the row create_mushroom(settings, screen, mushrooms, mushroom_number, row_number)
def enter(): global map2 map2 = Map() game_world.add_object(map2, 0) key = Key() game_world.add_object(key, 1) global boss boss = Boss() game_world.add_object(boss, 1) global monster monster = Monster() # monsters = Monster() game_world.add_object(monster, 1) global mushroom mushroom = Mushroom() game_world.add_object(mushroom, 1) global character character = Character() game_world.add_object(character, 1)
def crete_new_mushroom(mushrooms, ai_settings, screen): m = Mushroom(ai_settings, screen) set_random_coords_mushroom(m, ai_settings.screen_width, ai_settings.screen_height) mushrooms.add(m)
def make_item_appear(self): # TODO: animate sprite to appear in if/else below # Move sprite up until bottom of sprite is at top of block obj = None if self.stored_item == self.settings.mystery_block_possible_items[ 'MUSHROOM']: print('Mushroom item appears!') obj = Mushroom(self.screen, self.settings) obj.spawn() elif self.stored_item == self.settings.mystery_block_possible_items[ 'FIRE_FLOWER']: print('Flower item appears!') obj = Fire_Flower(self.screen, self.settings) obj.spawn() elif self.stored_item == self.settings.mystery_block_possible_items[ 'COIN']: print('Coin item appears!') obj = Coin(self.screen, self.settings) obj.spawn() elif self.stored_item == self.settings.mystery_block_possible_items[ 'ONE_UP']: print('1UP item appears!') obj = Mushroom(self.screen, self.settings, is_one_up=True) obj.spawn() elif self.stored_item == self.settings.mystery_block_possible_items[ 'STAR']: print('Star item appears!') obj = Star(self.screen, self.settings) obj.spawn() else: # shouldn't be empty! print('WARNING - mystery block missing item') return obj
def check_collisions(screen, mario, platforms_top, platforms_bottom, left_walls, right_walls, fireballs, mystery_tiles, brick_tiles, entity_gamemaster, viewport): # mario is coming down after having jumped, collide with top platform if mario.vel.y > 0: feet_collisions = pygame.sprite.spritecollide(mario, platforms_top, False) if feet_collisions: mario.vel.y = 0 mario.pos.y = feet_collisions[0].rect.top + 1 mario.airborne = False if feet_collisions and mario.finish: pygame.mixer.Channel(5).play( pygame.mixer.Sound('resources/sounds/stage_clear.wav')) mario.finish = False mario.pos.x += 42 # mario collides with his head if mario.vel.y < 0: head_collision = pygame.sprite.spritecollide(mario, platforms_bottom, False) if head_collision: mario.vel.y = 0 head_collision[0].rect.y -= 23 # check what other components this bottom platforms collides with and move them up by the same amount mystery_collision = pygame.sprite.spritecollide( head_collision[0], mystery_tiles, False) if mystery_collision: if mystery_collision[0].status == 'new': mystery_collision[0].rect.y -= 23 if mystery_collision[0].id == 0 or mystery_collision[0].id == 1 or mystery_collision[0].id == 3\ or mystery_collision[0].id == 4 or mystery_collision[0].id == 5\ or mystery_collision[0].id == 7 or mystery_collision[0].id == 10\ or mystery_collision[0].id == 11 or mystery_collision[0].id == 12\ or mystery_collision[0].id == 13: coin = Coin(screen, mystery_collision[0]) pygame.mixer.Channel(6).play( pygame.mixer.Sound('resources/sounds/coin.wav')) entity_gamemaster.coin.add(coin) viewport.add(entity_gamemaster.coin) mario.gui.coin_counter += 1 mario.gui.update_coin_counter_text() mario.gui.score += 200 mario.gui.update_score_text() elif mystery_collision[0].id == 6: mushroom = Mushroom(screen, platforms_top, left_walls, right_walls, mystery_collision[0]) pygame.mixer.Channel(7).play( pygame.mixer.Sound( 'resources/sounds/powerup_appears.wav')) entity_gamemaster.mushrooms.add(mushroom) viewport.add(entity_gamemaster.mushrooms) elif mystery_collision[0].id == 2 or mystery_collision[ 0].id == 8: if mario.status == 'small': mushroom = Mushroom(screen, platforms_top, left_walls, right_walls, mystery_collision[0]) pygame.mixer.Channel(7).play( pygame.mixer.Sound( 'resources/sounds/powerup_appears.wav')) entity_gamemaster.mushrooms.add(mushroom) viewport.add(entity_gamemaster.mushrooms) if not mario.status == 'small': flower = Fireflower(screen, mystery_collision[0]) pygame.mixer.Channel(7).play( pygame.mixer.Sound( 'resources/sounds/powerup_appears.wav')) entity_gamemaster.fireflowers.add(flower) viewport.add(entity_gamemaster.fireflowers) elif mystery_collision[0].id == 9: star = Starman(screen, platforms_top, left_walls, right_walls, mystery_collision[0]) pygame.mixer.Channel(7).play( pygame.mixer.Sound( 'resources/sounds/powerup_appears.wav')) entity_gamemaster.starmen.add(star) viewport.add(entity_gamemaster.starmen) top_collision = pygame.sprite.spritecollide( mystery_collision[0], platforms_top, False) if top_collision: top_collision[0].rect.y -= 23 left_collision = pygame.sprite.spritecollide( mystery_collision[0], left_walls, False) if left_collision: left_collision[0].rect.y -= 23 right_collision = pygame.sprite.spritecollide( mystery_collision[0], right_walls, False) if right_collision: right_collision[0].rect.y -= 23 mystery_collision[0].status = 'used' # check what other components this bottom platforms collides with and move them up by the same amount brick_collision = pygame.sprite.spritecollide( head_collision[0], brick_tiles, False) if brick_collision: brick_collision[0].rect.y -= 23 top_collision = pygame.sprite.spritecollide( brick_collision[0], platforms_top, False) if top_collision: if mario.status == 'big' or mario.status == 'fire': pygame.mixer.Channel(7).play( pygame.mixer.Sound('resources/sounds/break.wav')) top_collision[0].rect.y -= 23 top_collision[0].kill() mario.gui.score += 50 mario.gui.update_score_text() else: pygame.mixer.Channel(7).play( pygame.mixer.Sound('resources/sounds/bump.wav')) top_collision[0].rect.y -= 23 left_collision = pygame.sprite.spritecollide( brick_collision[0], left_walls, False) if left_collision: if mario.status == 'big' or mario.status == 'fire': left_collision[0].rect.y -= 23 left_collision[0].kill() else: left_collision[0].rect.y -= 23 right_collision = pygame.sprite.spritecollide( brick_collision[0], right_walls, False) if right_collision: if mario.status == 'big' or mario.status == 'fire': for r in right_collision: r.kill() else: for r in right_collision: r.rect.y -= 23 if mario.status == 'big' or mario.status == 'fire': brick_collision[0].kill() if mario.status == 'big' or mario.status == 'fire': head_collision[0].kill() # when mario is moving in the right direction, his velocity is greater than 0, # check for collisions with wall # move mario back to position of collision, subtract 16 since pos.x is midbottom center of mario if mario.vel.x > 0: wall_collision = pygame.sprite.spritecollide(mario, left_walls, False) if wall_collision: mario.vel.x = 0 mario.pos.x = wall_collision[0].rect.left - 12 # when mario is moving in the left direction, his velocity is less than 0, # when for collisions with wall # move mario forward to position of collision, add 16 since pos.x is midbottom center of mario if mario.vel.x < 0: wall_collision = pygame.sprite.spritecollide(mario, right_walls, False) if wall_collision: mario.vel.x = 0 mario.pos.x = wall_collision[0].rect.right + 12