def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 20 self.score_multiplier = 10 self.level_complete_bonus = 200 self.zone = "City" self.background = pygame.image.load( "./resources/img/city-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [[200, 30, 40, 750], [500, 20, 0, 800], [500, 20, 500, 800], [100, 30, 0, 700], [200, 30, 280, 610], [200, 30, 500, 610], [200, 30, 800, 705], [100, 30, 0, 500], [200, 30, 280, 410], [200, 30, 500, 410], [200, 30, 800, 515], [100, 30, 900, 480], [200, 30, 0, 320], [200, 30, 280, 225], [200, 30, 500, 225], [50, 30, 950, 150], [50, 30, 0, 80]] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(7): target = Cop() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.move_end = random_plat[2] + random_plat[0] # Add the block to the list of objects self.target_list.add(target)
def build(self): layout = load_image('assets/level1layout.png') level = Background((0, 0), 'assets/level1.png', 1, 1) background = Background((0, 0), 'assets/background1.png', 15, 15) #layout = load_image('henesyslayout.png') #level = Background((0,0), 'henesys.png', 1,1) #layout = load_image('citylevellayout.png') #level = Background((0,0), 'citylevel.png',1, 1) #layout = load_image('level3layout.png') #level = Background((0, 0), 'level3.png', 1, 1) #background = Background((0, 0), 'background3.png', 20, 20) self.backgrounds = [background, level] width, height = layout.get_size() self.blocks = [] from blocks import Block, Platform, Step for y in xrange(height): for x in xrange(width): pos = x * 8, y * 8 if layout.get_at((x, y)) == (0, 0, 0, 255): self.blocks.append(Block(pos)) elif layout.get_at((x, y)) == (0, 0, 255, 255): self.blocks.append(Platform(pos)) elif layout.get_at((x, y)) == (255, 0, 0, 255): self.blocks.append(Step(pos)) event = LevelBuildEvent(layout, self.backgrounds) self.evManager.Post(event)
def load_level(entities): with open('level.txt') as f: level = f.readlines() platforms = list() # то, во что мы будем врезаться или опираться x, y = 0, 0 # координаты _, _, w, h = Platform(x, y).rect for row in level: # вся строка for col in row: # каждый символ if col == "*": pf = Platform(x, y) entities.add(pf) platforms.append(pf) x += w # блоки платформы ставятся на ширине блоков y += h # то же самое и с высотой x = 0 total_level_width = len( level[0]) * w # Высчитываем фактическую ширину уровня total_level_height = len(level) * h # и высоту return platforms, total_level_width, total_level_height
def create_level(level): global entities, platforms, enemies, fin, hero, hero_sprite, stamina_bar, fires, clock entities = sprite.Group() platforms = [] fin = [] enemies = [] fires = [] entities.add(fone) clock = time.Clock() stamina_bar = StaminaBar(25, 740) entities.add(stamina_bar) x = y = 0 for row in level: for col in row: if col == "-": pf = Platform(x, y) entities.add(pf) platforms.append(pf) if col == '$': snake = Snake(x, y) entities.add(snake) enemies.append(snake) if col == '!': hero_sprite = Hero_sprite(x, y) hero = Hero(x, y) entities.add(hero_sprite) entities.add(hero) if col == 'f': finish = Finish(x, y) entities.add(finish) fin.append(finish) if col == '%': a_bandit = A_bandit(x, y) entities.add(a_bandit) enemies.append(a_bandit) if col == '&': m_bandit = M_bandit(x, y) entities.add(m_bandit) enemies.append(m_bandit) if col == '*': b_bandit = B_bandit(x, y) entities.add(b_bandit) enemies.append(b_bandit) x += PLATFORM_WIDTH y += PLATFORM_HEIGHT x = 0
def loadlevel(self, levelfile, screen, timer, player): ''' Из levelfile получает информацию об уровне, положени персонажа Создает группу entities Вызывает main_loop с загруженным уровнем ''' file = open(levelfile) line = " " level = [] while line[0] != "/": line = file.readline() if line[0] == "[": # Начало блока с уровнем while line[0] != "]": # Пока не найден конец уровня в файле line = file.readline() # Считываем строку if line[0] != "]": # Если это не конец блока level.append( line[0:-2]) # добавляем строку с данными уровня if line.find("player") != -1: player.setpos( line.split(",")[1:3]) # Перемещает игрока в начало entities = pygame.sprite.Group() # Все объекты entities.add(player) x = y = 0 # координаты for row in level: # вся строка for col in row: # каждый символ if col == "-": # создаем блок, заливаем его цветом и рисеум его pf = Platform(x, y) entities.add(pf) if col == "0": pf = Gate(x, y) entities.add(pf) if col == "3": em = Ember(x, y - 5) entities.add(em) x += Platform.width # блоки платформы ставятся на ширине блоков y += Platform.height # то же самое и с высотой x = 0 # на каждой новой строчке начинаем с нуля self.run = True self.main_loop(screen=screen, player=player, timer=timer, entities=entities, level=level)
def play(player): pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Bounce") background = Surface((WIN_WIDTH, WIN_HEIGHT)) background.fill(Color(BACKGROUND_COLOR)) score_ground = Surface((WIN_WIDTH, WIN_HEIGHT - 3 * 32)) score_ground.fill(Color(SCORE_GROUND_COLOR)) ball = player(64, 32) up = left = right = False my_font = pygame.font.SysFont('Arial', 30) entities_back = pygame.sprite.Group() entities_front = pygame.sprite.Group() platforms = [] level = level_1 x = y = 0 for row in level: for col in row: if col == "-": pf = Platform(x, y) entities_back.add(pf) platforms.append(pf) if col == "i": pf = Spike(x + 7, y) entities_back.add(pf) platforms.append(pf) if col == "s": pf = SavePoint(x, y) entities_back.add(pf) platforms.append(pf) if col == "b": pf = BonusLife(x, y) entities_back.add(pf) platforms.append(pf) if col == "e": pf = Exit(x, y) entities_back.add(pf) platforms.append(pf) if col == "r": ball.ring_count += 1 pf = Ring(x + 17, y) entities_back.add(pf) platforms.append(pf) pf = BackFontRing(x + 17, y - 32) entities_back.add(pf) platforms.append(pf) pf = FrontFontRing(x + 8, y - 32) entities_front.add(pf) platforms.append(pf) pf = Invisible(x + 12, y - 32) platforms.append(pf) pf = Invisible(x + 12, y + 22) platforms.append(pf) if col == "v": ball.ring_count += 1 pf = HRing(x + 32, y + 17) entities_back.add(pf) platforms.append(pf) pf = HBackFontRing(x, y + 9) entities_front.add(pf) platforms.append(pf) pf = HFrontFontRing(x, y) entities_back.add(pf) platforms.append(pf) pf = HInvisible(x + 2, y + 8) platforms.append(pf) pf = HInvisible(x + 54, y + 8) platforms.append(pf) x += PLATFORM_WIDTH y += PLATFORM_HEIGHT x = 0 total_level_width = len(level[0]) * PLATFORM_WIDTH total_level_height = len(level) * PLATFORM_HEIGHT camera = Camera(camera_configure, total_level_width, total_level_height) timer = pygame.time.Clock() while True: timer.tick(60) for e in pygame.event.get(): if e.type == QUIT: raise SystemExit if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYUP and e.key == K_LEFT: left = False if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYUP and e.key == K_UP: up = False screen.blit(background, (0, 0)) ball.update(left, right, up, platforms) camera.update(ball) for e in entities_back: screen.blit(e.image, camera.apply(e)) screen.blit(ball.image, camera.apply(ball)) for e in entities_front: screen.blit(e.image, camera.apply(e)) screen.blit(score_ground, (0, 8 * 32)) score = my_font.render(ball.Score, True, (255, 255, 255)) screen.blit(score, (5 * 32, 8 * 32 + 5)) x = 10 y = 8 * 32 + 3 for i in range(ball.lifes): screen.blit(ball.life_image, (x, y)) x += 26 x = 10 y = 9 * 32 for i in range(ball.ring_count): screen.blit(ball.ring_image, (x, y)) x += 18 if ball.is_game_over(): raise SystemExit pygame.display.update()
def main(): lvl = 0 slot1 = [ image.load( "recource\\textures\\player\\pistols\\strikes\\bananas3.png"), 'Banans', 100, 10, 10 ] slot2 = [ image.load("recource\\textures\\player\\pistols\\pistols\\pistol.png"), 'Gun', 100, 7, 7 ] slot3 = [ image.load("recource\\textures\\player\\pistols\\pistols\\AK47.png"), 'AK47', 100, 30, 30 ] slot4 = [None, None, 0, 0, 0] timess = 0 inventory = [slot1, slot2, slot3, slot4] for leval in levels: reload = False player = Player(leval[1][0], leval[1][1]) player.hp = levels[lvl - 1][2] player.score = levels[lvl - 1][3] player.bananas = levels[lvl - 1][4] level = leval[0] checkp = [55, 55] shift = False speedPlatforms = [] pygame.init() screen = pygame.display.set_mode(display) fon = Surface(display) fon.fill(Color(bgColor)) #fon = image.load('recource\\textures\\bgTextures\\kirpichi.jpg') sprites = pygame.sprite.Group() platforms = [] banans = [] monsters = [] badPlatforms = [] dropped = [] weapones = [] sprites.add(player) left = right = up = False timer = pygame.time.Clock() moneys = [] flymonsters = [] bullets = [] regens = [] chekpoints = [] x = y = 0 boss = None for all in level: for each in all: if each == '-': pf = Platform(x, y) sprites.add(pf) platforms.append(pf) if each == '@': pf = SpeedPlatform(x, y) sprites.add(pf) speedPlatforms.append(pf) if each == 'x': pf = DiePlatform(x, y) sprites.add(pf) badPlatforms.append(pf) if each == '%': pf = DoorPlatform(x, y) sprites.add(pf) door = pf if each == 'M': pf = Monsters(x, y) monsters.append(pf) sprites.add(pf) if each == '$': pf = Money(x, y) moneys.append(pf) sprites.add(pf) if each == 'L': pf = Fly_Monsters(x, y) flymonsters.append(pf) sprites.add(pf) if each == 'B': boss = Boss(x, y) monsters.append(boss) sprites.add(boss) if each == 'b': pf = Banans(x, y) banans.append(pf) sprites.add(pf) if each == 'h': pf = Hp_regen(x, y) sprites.add(pf) regens.append(pf) if each == '+': pf = Chekpoint(x, y) sprites.add(pf) chekpoints.append(pf) if each == 'P': pf = BulletsForGun(x, y) sprites.add(pf) bullets.append(pf) x += platformWidth y += platformHeigh x = 0 total_width = len(level[0]) * 32 total_heigh = len(level) * 32 camera = Camera(camera_config, total_width, total_heigh) black = (0, 0, 0) green = (0, 255, 0) red = (255, 0, 0) font = pygame.font.Font(None, 55) text = font.render("My text", True, black) font2 = pygame.font.Font(None, 100) banani = pygame.font.Font(None, 55) money = image.load('recource\\textures\\money\\money2.png') hpx = 180 hpy = 32 banx = 105 more9 = False more99 = False more92 = False more992 = False bantextx = 143 uminshilos = False times = 0 pistols = [] hps = [] inventory_slots = [ image.load('recource\\textures\\inventory\\inventory.png'), image.load('recource\\textures\\inventory\\inventory.png'), image.load('recource\\textures\\inventory\\inventory.png'), image.load('recource\\textures\\inventory\\inventory.png') ] for i in range(player.hp): hps.append(image.load('recource\\textures\\hp\\hp3.png')) timer.tick(30) banan = None banantext = None pygame.mixer.music.load("recource\\sound-trak\\menu.mp3") #pygame.mixer.music.play(-1, 0.0) now = 0 while not player.next_level: hps = [] for i in range(player.hp): hps.append(image.load('recource\\textures\\hp\\hp3.png')) for e in pygame.event.get(): if e.type == QUIT: raise SystemExit("Quit") if e.type == KEYDOWN and e.key == K_a: left = True if e.type == KEYDOWN and e.key == K_d: right = True if e.type == KEYDOWN and e.key == K_w: up = True if e.type == KEYDOWN and e.key == K_LSHIFT: shift = True if e.type == KEYDOWN and e.key == K_r: reload = True if e.type == KEYDOWN and e.key == K_SPACE: if inventory[player.slot][0] is not None: if inventory[player.slot][1] == 'Banans': if inventory[player.slot][2] > 0: if inventory[player.slot][3] > 0: reload = False inventory[player.slot][3] -= 1 pf = BananPushka(0, player) sprites.add(pf) pistols.append(pf) now = inventory[player.slot][2] if inventory[player.slot][1] == 'Gun': if inventory[player.slot][2] > 0: if inventory[player.slot][3] > 0: reload = False inventory[player.slot][3] -= 1 pf = Gun(1, player) sprites.add(pf) pistols.append(pf) now = inventory[player.slot][2] if inventory[player.slot][1] == 'AK47': if inventory[player.slot][2] > 0: if inventory[player.slot][3] > 0: reload = False inventory[player.slot][3] -= 1 pf = AK47(1, player) sprites.add(pf) pistols.append(pf) now = inventory[player.slot][2] else: player.banas = 0 if e.type == KEYDOWN and e.key == K_q: if inventory[player.slot][1] == 'Gun': gun = Dropped_Gun(player.rect.x, player.rect.y, inventory[player.slot][2], inventory[player.slot][3], inventory[player.slot][4]) inventory[player.slot] = [None, None, 0, 0, 0] dropped.append(gun) sprites.add(gun) if inventory[player.slot][1] == 'AK47': ak47 = Dropped_AK47(player.rect.x, player.rect.y, inventory[player.slot][2], inventory[player.slot][3], inventory[player.slot][4]) inventory[player.slot] = [None, None, 0, 0, 0] dropped.append(ak47) sprites.add(ak47) if e.type == KEYDOWN and e.key == K_1: player.slot = 0 if e.type == KEYDOWN and e.key == K_2: player.slot = 1 if e.type == KEYDOWN and e.key == K_3: player.slot = 2 if e.type == KEYDOWN and e.key == K_4: player.slot = 3 if e.type == KEYUP and e.key == K_d: right = False if e.type == KEYUP and e.key == K_a: left = False if e.type == KEYUP and e.key == K_w: up = False if e.type == KEYUP and e.key == K_LSHIFT: shift = False if player.score > 9: if not more9: hpx += 10 banx += 10 bantextx += 10 more9 = True if player.score > 99: if not more99: hpx += 20 banx += 20 bantextx += 20 more99 = True if player.score < 9: if more9: hpx -= 10 banx -= 10 bantextx -= 10 more9 = False if player.score < 99: if more99: hpx -= 20 banx -= 20 bantextx -= 20 more99 = False if inventory[player.slot][2] > 9: if not more92: hpx += 10 more92 = True if inventory[player.slot][2] > 99: if not more992: hpx += 20 more992 = True if inventory[player.slot][2] < 9: if more92: hpx -= 10 more92 = False if inventory[player.slot][2] < 99: if more992: hpx -= 20 more992 = False for each in dropped: if sprite.collide_rect(player, each): for each2 in range(len(inventory)): if inventory[each2][0] is None: inventory[each2] = [ each.image2, each.name, each.bullets, each.oboima, each.max ] if each in dropped: dropped.remove(each) if each in sprites: sprites.remove(each) break for chekpoint in chekpoints: if sprite.collide_rect(player, chekpoint): checkp = [chekpoint.rect.y, chekpoint.rect.x] print(chekpoint.rect.y) print(chekpoint.rect.x) print('Savepoint') for bullet in bullets: if sprite.collide_rect(player, bullet): for each in range(len(inventory)): if inventory[each][1] == 'Gun': inventory[each][2] += 15 if bullet in bullets: bullets.remove(bullet) if bullet in sprites: sprites.remove(bullet) bullet.kill() for banan1 in banans: if sprite.collide_rect(player, banan1): if banan1 in banans: banans.remove(banan1) if banan1 in sprites: sprites.remove(banan1) banan1.kill() player.bananas += 3 for platform in moneys: if sprite.collide_rect(player, platform): player.score += 1 if platform in platforms: platforms.remove(platform) if platform in sprites: sprites.remove(platform) platform.kill() for each in regens: if sprite.collide_rect(player, each): player.hp += 1 if each in regens: regens.remove(each) if each in sprites: sprites.remove(each) each.kill() screen.blit(fon, (0, 0)) text = font.render(str(player.score), True, black) for each in dropped: each.upadate(platforms) if inventory[player.slot][0] is not None: if inventory[player.slot][1] == 'AK47': banantext = banani.render( '{}/{}'.format(str(inventory[player.slot][3]), str(inventory[player.slot][2])), True, black) banan = image.load( 'recource\\textures\\player\\pistols\\strikes\\bullet.png' ) if inventory[player.slot][1] == 'Gun': banantext = banani.render( '{}/{}'.format(str(inventory[player.slot][3]), str(inventory[player.slot][2])), True, black) banan = image.load( 'recource\\textures\\player\\pistols\\strikes\\bullet.png' ) if inventory[player.slot][1] == 'Banans': banantext = banani.render( '{}/{}'.format(str(inventory[player.slot][3]), str(inventory[player.slot][2])), True, black) banan = image.load( 'recource\\textures\\player\\pistols\\strikes\\bananas2.png' ) else: banan = None banantext = None camera.update(player) for each in flymonsters: fms = each.update(player, platforms, flymonsters, sprites, pistols) if fms is not None: sprites = fms[0] flymonsters = fms[1] if reload: timess += 1 if timess > 10: inventory[player.slot][3] += 1 inventory[player.slot][2] -= 1 timess = 0 if inventory[player.slot][3] == inventory[player.slot][4]: reload = False timess = 0 for each in pistols: sp = each.updata(monsters, sprites, platforms, pistols, flymonsters) if sp is not None: sprites = sp[0] pistols = sp[1] for each in monsters: sm = each.update(player, platforms, monsters, sprites, pistols) if sm is not None: sprites = sm[0] monsters = sm[1] if each == boss: sm = each.updata(player, platforms, monsters, sprites, pistols) if sm is not None: sprites = sm[0] for each in sprites: screen.blit(each.image, camera.apply(each)) for each in range(len(inventory_slots)): screen.blit(inventory_slots[each], [32, 196 + (each * 64)]) screen.blit(image.load('recource\\textures\\inventory\\vibor.png'), [32, 196 + player.slot * 64]) screen.blit(money, [32, 32]) screen.blit(text, [70, 32]) for each in range(len(inventory)): if inventory[each][0] is not None: screen.blit(inventory[each][0], [32, 196 + (each * 64)]) i = 0 if boss != None: bosshps = boss.hp if not boss.died: if boss.hp != 0: if boss.hp < 11: for k in range(10): if bosshps != 0: bosshps -= 1 each = image.load( 'recource\\textures\\hp\\hp3.png') screen.blit(each, [700 + (k * 30), 32]) else: for i in range(2): for v in range(10): if bosshps != 0: bosshps -= 1 each = image.load( 'recource\\textures\\hp\\hp3.png') screen.blit( each, [700 + (v * 30), 32 + (i * 30)]) for each in hps: i += 1 screen.blit(each, [hpx + (35 * i), hpy]) if banan is not None: screen.blit(banan, [banx, 32]) screen.blit(banantext, [bantextx, 32]) if player.hp == 0: player.smert = True sprites_speedplatforms_money = player.updata( left, right, up, shift, platforms, speedPlatforms, badPlatforms, door, sprites, moneys, banans, regens) speedPlatforms = sprites_speedplatforms_money[1] sprites = sprites_speedplatforms_money[0] moneys = sprites_speedplatforms_money[2] banans = sprites_speedplatforms_money[3] regens = sprites_speedplatforms_money[4] levels[lvl][2] = player.hp levels[lvl][3] = player.score levels[lvl][4] = player.bananas if player.smert: if not uminshilos: player.score -= 5 if player.score < 0: player.score = 0 potracheno = font2.render('Потрачено!', True, green) uminshilos = True screen.blit(potracheno, [360, 340]) player.umirat = True times += 1 if times > 100: player.smert = False player.umirat = False player.bananas = player.savepoint[4] player.hp = 5 player.rect.y = checkp[0] player.rect.x = checkp[1] player.faseDie = 0 uminshilos = False print(player.rect.y) print(player.rect.x) times = 0 player.savepoint[0] = player.hp player.savepoint[1] = player.score player.savepoint[2] = player.bananas pygame.display.update() lvl += 1
def __init__(self, x, y): Platform.__init__(self, x, y) self.image = image.load("fire.png")
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 25 self.score_multiplier = 12 self.level_complete_bonus = 300 self.zone = "Swamp" self.background = pygame.image.load( "./resources/img/swamp-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [ [100, 40, 700, 750], [100, 40, 900, 670], [380, 40, 300, 580], [100, 40, 200, 540], [100, 40, 0, 460], [380, 40, 300, 370], [100, 40, 900, 280], [100, 40, 570, 190], [200, 40, 200, 150], [50, 40, 950, 90], ] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(6): target = Pogomonkey() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.jump_start = random_plat[3] # Add the block to the list of objects self.target_list.add(target) # Deliberate target locations for level as per design for i in range(8, 10): target = Pogomonkey() platform = level[i] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound plat_x = platform[2] plat_y = platform[3] target.rect.x = random.randrange( plat_x, plat_x + (platform[0] - target.width)) target.rect.y = plat_y - target.height # Move distance allowed for this plat target.move_start = platform[2] target.jump_start = platform[3] # Add the block to the list of objects self.target_list.add(target)
def __init__(self, FPS): self.level_height = 25 self.level_width = 100 self.levelsize = (self.level_width * BS, self.level_height * BS) self.obstacles = pg.sprite.Group() self.storm_troopers = pg.sprite.Group() self.player_blaster = pg.sprite.Group() self.enemy_blaster = pg.sprite.Group() self.f = open(config.LEVEL, 'r') self.data = [] for i in range(self.level_height): self.data.append(self.f.readline()) self.level_map = self.data self.x = 0 self.y = 0 for row in self.level_map: for col in row: if col == ".": self.obstacles.add( Block(pg.Color("brown"), (self.x * BS, self.y * BS, BS, BS))) if col == "!": self.obstacles.add( Block(pg.Color("red"), (self.x * BS, self.y * BS, BS, BS), kind="danger")) if col == "(": self.player = Player((self.x * BS, self.y * BS), 5) if col == "I": self.obstacles.add( Platform(pg.Color("olivedrab"), (self.x * BS, self.y * BS, BS, BS), axis=1, speed=4, move_dist=BS * 4, direction=1)) if col == "i": self.obstacles.add( Platform(pg.Color("olivedrab"), (self.x * BS, self.y * BS, BS, BS), axis=1, speed=4, move_dist=BS * 4, direction=-1)) if col == "_": self.obstacles.add( Platform(pg.Color("olivedrab"), (self.x * BS, self.y * BS, BS, BS), move_dist=BS * 4)) if col == "S": self.storm_troopers.add( Stormtrooper((self.x * BS, self.y * BS), 1)) self.x += 1 self.y += 1 self.x = 0 self.f.close() self.screen = pg.display.get_surface() self.screen_rect = self.screen.get_rect() self.viewport = self.screen.get_rect() self.clock = pg.time.Clock() self.fps = FPS self.keys = pg.key.get_pressed() self.done = False self.level = pg.Surface( (self.levelsize[0], self.levelsize[1])).convert() self.level_rect = self.level.get_rect() self.death = False self.die = False self.direction = 1 self.level.fill(pg.Color("lightblue")) self.obstacles.draw(self.level)
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 40 self.score_multiplier = 12 self.level_complete_bonus = 600 self.zone = "Underwater" self.background = pygame.image.load( "./resources/img/atlantis-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [ [90, 30, 0, 770], [90, 30, 910, 770], [90, 30, 200, 680], [90, 30, 710, 680], [90, 30, 0, 590], [90, 30, 910, 590], [90, 30, 200, 500], [90, 30, 710, 500], [90, 30, 0, 410], [90, 30, 910, 410], [90, 30, 200, 320], [90, 30, 710, 320], [90, 30, 0, 230], [90, 30, 910, 230], [90, 30, 200, 140], [90, 30, 710, 140], ] walls = [ [40, 120, 290, 680], [40, 120, 670, 680], [40, 120, 290, 560], [40, 120, 670, 560], [40, 120, 290, 440], [40, 120, 670, 440], [40, 120, 290, 320], [40, 120, 670, 320], [40, 120, 290, 200], [40, 120, 670, 200], [40, 120, 290, 80], [40, 120, 670, 80], ] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 350 block.rect.y = 500 block.boundary_bottom = 800 block.boundary_top = 50 block.change_y = 1 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 550 block.rect.y = 500 block.boundary_bottom = 800 block.boundary_top = 50 block.change_y = -1 self.platform_list.add(block) # Deliberate target locations for level as per design for i in range(len(level)): target = Bikefish() platform = level[i] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound plat_x = platform[2] plat_y = platform[3] target.rect.x = random.randrange( plat_x, plat_x + (platform[0] - target.width)) target.rect.y = plat_y - target.height # Add the block to the list of objects self.target_list.add(target) # Generate walls after enemies so none spawn on it for platform in walls: block = Wall(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block)
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 30 self.score_multiplier = 12 self.level_complete_bonus = 500 self.zone = "Underwater" self.background = pygame.image.load( "./resources/img/atlantis-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [[100, 50, 0, 750], [100, 50, 900, 750], [100, 30, 120, 660], [100, 30, 780, 660], [100, 30, 260, 750], [100, 30, 640, 750], [100, 30, 260, 570], [100, 30, 640, 570], [280, 30, 200, 150], [280, 30, 520, 150]] walls = [[40, 120, 220, 680], [40, 120, 740, 680], [40, 110, 220, 570], [40, 110, 740, 570], [40, 90, 480, 0], [40, 90, 480, 90]] # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 450 block.rect.y = 660 block.boundary_left = 370 block.boundary_right = 530 block.change_x = -2 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 300 block.rect.y = 480 block.boundary_left = 150 block.boundary_right = 750 block.change_x = -3 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 50 block.rect.y = 300 block.boundary_bottom = 500 block.boundary_top = 200 block.change_y = -1 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(100, 30, self.zone, player) block.rect.x = 850 block.rect.y = 400 block.boundary_bottom = 500 block.boundary_top = 200 block.change_y = -1 self.platform_list.add(block) # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(10): target = Bikefish() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.move_end = random_plat[2] + random_plat[0] # Add the block to the list of objects self.target_list.add(target) # Deliberate target locations for level as per design for i in range(8, 10): target = Bikefish() platform = level[i] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound plat_x = platform[2] plat_y = platform[3] target.rect.x = random.randrange( plat_x, plat_x + (platform[0] - target.width)) target.rect.y = plat_y - target.height # Move distance allowed for this plat target.move_start = platform[2] target.move_end = platform[2] + platform[0] # Add the block to the list of objects self.target_list.add(target) # Generate walls after enemies so none spawn on it for platform in walls: block = Wall(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block)
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 20 self.score_multiplier = 12 self.level_complete_bonus = 500 self.zone = "Underwater" self.background = pygame.image.load( "./resources/img/atlantis-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [ [200, 30, 800, 615], [300, 30, 0, 115], [300, 30, 700, 115], ] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 40 block.rect.y = 710 block.boundary_left = 40 block.boundary_right = 600 block.change_x = -3 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 100 block.rect.y = 520 block.boundary_left = 50 block.boundary_right = 600 block.change_x = -5 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 300 block.rect.y = 430 block.boundary_left = 0 block.boundary_right = 400 block.change_x = -1 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 200 block.rect.y = 340 block.boundary_left = 0 block.boundary_right = 400 block.change_x = -2 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 300 block.rect.y = 250 block.boundary_left = 200 block.boundary_right = 400 block.change_x = -4 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(150, 30, self.zone, player) block.rect.x = 500 block.rect.y = 160 block.boundary_left = 400 block.boundary_right = 700 block.change_x = -4 self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(4): target = Bikefish() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.move_end = random_plat[2] + random_plat[0] # Add the block to the list of objects self.target_list.add(target)
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 30 self.score_multiplier = 12 self.level_complete_bonus = 400 self.zone = "Swamp" self.background = pygame.image.load( "./resources/img/swamp-bg.png").convert_alpha() self.background = pygame.transform.scale(self.background, (1000, 800)) # Array with width, height, x, and y of platform level = [ [90, 70, 340, 730], [90, 70, 570, 730], [250, 30, 230, 540], [250, 30, 520, 540], [180, 30, 300, 400], # mandatory [180, 30, 520, 400], # mandatory [230, 30, 250, 140], [230, 30, 520, 140], [50, 30, 270, 305], [50, 30, 680, 305], [40, 30, 0, 115], [40, 30, 960, 115] ] walls = [[30, 95, 270, 335], [30, 95, 700, 335], [40, 120, 480, 310], [40, 120, 480, 190], [40, 120, 480, 70], [40, 120, 480, -50], [40, 120, 480, 430], [40, 120, 480, 450]] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(140, 30, self.zone, player) block.rect.x = 50 block.rect.y = 500 block.boundary_bottom = 750 block.boundary_top = 115 block.change_y = -1 self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(140, 30, self.zone, player) block.rect.x = 810 block.rect.y = 500 block.boundary_bottom = 750 block.boundary_top = 115 block.change_y = 1 self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(10): target = Pogomonkey() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.jump_start = random_plat[3] # Add the block to the list of objects self.target_list.add(target) # Generate walls after enemies so none spawn on it for platform in walls: block = Wall(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block)
def main(): ms = 'k' while True: pygame.init() maze = maze_generation() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption('Maze game') bg = Surface(DISPLAY) bg.fill(Color(BACKGROUND_COLOR)) screen.blit(bg, (0, 0)) hero = NPC(maze[1][0] * 10, maze[1][1] * 10) finish = Finish(maze[2][0] * 10, maze[2][1] * 10) entites = pygame.sprite.Group() platforms = [] entites.add(hero) entites.add(finish) level = maze[0] clock = pygame.time.Clock() x = y = 0 for row in level: for col in row: if col == 0: pf = Platform(x, y) entites.add(pf) platforms.append(pf) x += PLATFORM_WIDTH y += PLATFORM_HEIGHT x = 0 stepc = 0 while True: for i in pygame.event.get(): if i.type == KEYDOWN and i.key == K_LEFT: stepc += 1 hero.rect.x -= 10 for p in platforms: if sprite.collide_rect(hero, p): hero.rect.left = p.rect.right if i.type == KEYDOWN and i.key == K_RIGHT: stepc += 1 hero.rect.x += 10 for p in platforms: if sprite.collide_rect(hero, p): hero.rect.right = p.rect.left if i.type == KEYDOWN and i.key == K_UP: stepc += 1 hero.rect.y -= 10 for p in platforms: if sprite.collide_rect(hero, p): hero.rect.top = p.rect.bottom if i.type == KEYDOWN and i.key == K_DOWN: hero.rect.y += 10 stepc += 1 for p in platforms: if sprite.collide_rect(hero, p): hero.rect.bottom = p.rect.top if i.type == QUIT: pygame.quit() exit() pygame.display.update() screen.blit(bg, (0, 0)) entites.draw(screen) clock.tick(60) ms = finish.collaid(hero, stepc) if ms == 'kek': break pygame.display.update()
def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Platformer Title") bg = Surface((WIN_WIDTH, WIN_HEIGHT)) bg.fill(Color(BACKGROUND_COLOR)) hero = Player(55, 55) left = right = False up = False timer = pygame.time.Clock() entities = pygame.sprite.Group() # all objects platforms = [] # object to hit or stay entities.add(hero) level = [ "----------------------------------", "- -", "- -- -", "- -", "- -- -", "- -", "-- -", "- -", "- ---- --- -", "- -", "-- -", "- -", "- --- -", "- -", "- -", "- --- -", "- -", "- ------- ---- -", "- -", "- - -", "- -- -", "- -", "- -", "----------------------------------" ] x = y = 0 for row in level: for col in row: if col == "-": pf = Platform(x, y) entities.add(pf) platforms.append(pf) x += PLATFORM_WIDTH y += PLATFORM_HEIGHT x = 0 total_level_width = len(level[0]) * PLATFORM_WIDTH # real level width total_level_height = len(level) * PLATFORM_HEIGHT # height camera = Camera(camera_configure, total_level_width, total_level_height) while 1: timer.tick(60) for e in pygame.event.get(): if e.type == QUIT: raise SystemExit("QUIT") if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_UP: up = False if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYUP and e.key == K_LEFT: left = False screen.blit(bg, (0, 0)) hero.update(left, right, up, platforms) camera.update(hero) for e in entities: screen.blit(e.image, camera.apply(e)) pygame.display.update()
def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Trump - legend") # Пишем в шапку bg = Surface((WIN_WIDTH, WIN_HEIGHT)) # Создание заднего фона bg.fill(Color(BACKGROUND_COLOR)) # Заливаем фон сплошным цветом hero = Player(70, 70) # создаем героя по (x,y) координатам left = right = False # по умолчанию — стоим up = False is_on_pause = False # проверка на паузу entities = pygame.sprite.Group() # Все объекты obstacles = [] # то, во что мы будем врезаться или опираться coins = [] # список для монет monsters = pygame.sprite.Group() # все монстры entities.add(hero) levels = [load_level(f'level{i + 1}.txt') for i in range(1)] current_level = 0 # уровень на данный момент x = y = 0 # координаты for row in levels[current_level]: # вся строка for col in row: # каждый символ if col == "-": # знак для платформ pf = Platform(x, y) entities.add(pf) obstacles.append(pf) elif col == "*": # для шипов bd = BlockDie(x, y) entities.add(bd) obstacles.append(bd) elif col == "m": # для монет mn = Coin(x, y) entities.add(mn) coins.append(mn) elif col == "e": # для монстров en = Monster(x, y) entities.add(en) obstacles.append(en) monsters.add(en) x += TILE_WIDTH # блоки платформы ставятся на ширине блоков y += TILE_HEIGHT # то же самое и с высотой x = 0 # на каждой новой строчке начинаем с нуля total_level_width = len( levels[current_level] [0]) * TILE_WIDTH # Высчитываем фактическую ширину уровня total_level_height = len(levels[current_level]) * TILE_HEIGHT # высоту camera = Camera(camera_configure, total_level_width, total_level_height) timer = pygame.time.Clock() running = True while running: # Основной цикл программы timer.tick(30) # fps monsters.update(obstacles) for e in pygame.event.get(): # Обрабатываем события if e.type == QUIT: running = False if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYUP and e.key == K_UP: up = False if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYUP and e.key == K_LEFT: left = False if e.type == KEYDOWN and e.key == K_ESCAPE: # Esc - управляющая клавиша паузы is_on_pause = 1 - is_on_pause for obj in obstacles: # останавливаем всех монстров в зависимости от состояния if obj.__class__ == Monster: obj.stop(is_on_pause) screen.blit(bg, (0, 0)) # Каждую итерацию необходимо всё перерисовывать if not is_on_pause: hero.update(left, right, up, obstacles, coins, levels[current_level]) # передвижение camera.update(hero) for e in entities: screen.blit(e.image, camera.apply(e)) if is_on_pause: draw_pause(screen) draw_score(screen, str(hero.scores)) pygame.display.update() # обновление и вывод всех изменений на экран pygame.quit()
def main(): pygame.init() # Инициализация начальных значений WIN_WIDTH = 800 WIN_HEIGHT = 640 DISPLAY = (WIN_WIDTH, WIN_HEIGHT) BACKGROUND_COLOR = "#004400" HERO_COLOR = "#FF6262" #"#440043" background_image = pygame.image.load('images/background.jpg') block_image = image.load("images/blocks.jpg") x=y=0 timer = pygame.time.Clock() timer.tick(60) # время PLATFORM_WIDTH = 32 PLATFORM_HEIGHT = 32 PLATFORM_COLOR = "#FF6262" # цвет платформы level = [ "-------------------------", "- - -", "- -", "- -", "- ----- -", "- -", "--- - -", "- - -", "- -------- -", "- -", "- ---------- -", "- ----- -", "-- -", "- ----------- -", "-- --", "- --- -", "- --- -", "- --", "- ------", "-------------------------"] # настраиваем лвл pygame.display.set_caption("Witmario") bg = Surface((WIN_WIDTH, WIN_HEIGHT)) entities = pygame.sprite.Group() platforms = [] music = pygame.mixer.music.load('muzik/Lazare.mp3') pygame.mixer.music.play(-1, 0.0) screen = pygame.display.set_mode(DISPLAY) # задаём героя hero = Player(55,55) entities.add(hero) left = right = False up = False # Цикл бесконечного ожидания событий (клавиш, мышки, ...) while 1: for e in pygame.event.get(): if e.type == QUIT: raise SystemExit, "QUIT" if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYUP and e.key == K_LEFT: left = False if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYUP and e.key == K_UP: up = False screen.blit(background_image, (0, 0)) #screen.blit(bg, (0,0)) x=y=0 for row in level: # вся строка x=0 # на каждой новой строчке x обнуляется for col in row: # каждый символ if col == "-": # создание блока и заливка цветом if col == "-": pf = Platform(x,y) entities.add(pf) platforms.append(pf) x += PLATFORM_WIDTH # установка блоков плотформы по ширине блоков y += PLATFORM_HEIGHT # установка блоков плотформы по высоте блоков hero.update(left, right, up, platforms) entities.draw(screen) pygame.display.update()
def run_game(self, gravity): ''' Main cycle of the game. ''' # Initialize pygame for this level self.screen = pygame.display.set_mode(self.WIN_SIZE) pygame.display.set_caption("DEADLINE") bg = Surface(self.WIN_SIZE) bg.fill(Color(BACKGROUND_COLOR)) # Directions of the player left = right = False up = False flag = True down = gravity self.start = False # все анимированные объекты, за исключением героя animatedEntities = pygame.sprite.Group() b1, b2 = -1, -1 for row in self.level: # вся строка b1 += 1 for col in row: # каждый символ b2 += 1 self.seed += 1 if col == "#": pf = Platform(self.x, self.y) self.entities.add(pf) self.platforms.append(pf) if col == "!": bd = BlockDie(self.x, self.y) self.entities.add(bd) self.platforms.append(bd) self.fire_list_coords.append([b1, b2]) if col == "C": pf = ClosedDoor(self.x, self.y) self.entities.add(pf) self.platforms.append(pf) if col == "E": pf = Door(self.x, self.y) self.entities.add(pf) self.platforms.append(pf) if col == 0: pf = Space(self.x, self.y) self.entities.add(pf) self.x += PLATFORM_WIDTH # блоки платформы ставятся на ширине блоков b2 = -1 self.y += PLATFORM_HEIGHT # то же самое и с высотой self.x = 0 # на каждой новой строчке начинаем с нуля running = False pygame.mixer.Channel(0).set_volume(0.85) pygame.mixer.Channel(0).play( pygame.mixer.Sound( get_data_path( 'fb_medium.ogg', 'music')), loops=-1) self.exit_code = 0 self.start_time = 0 while self.run: # Основной цикл программы self.timer.tick(60) if not self.paused: self.start_time += 1 if self.start and self.start_time > self.fire_delay: self._fire_cycle() events = pygame.event.get() for e in events: # Обрабатываем события if e.type == QUIT: self.run = False # Player died if not self.player.life: self.run = False self.exit_code = 2 # Player ended this level if self.player.end: self.run = False self.exit_code = 3 if e.type == KEYDOWN and e.key == K_ESCAPE: self.paused = not self.paused if e.type == KEYDOWN and e.key == K_LEFT: left = True self.start = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True self.start = True if e.type == KEYDOWN and e.key == K_UP: up = True self.start = True if e.type == KEYDOWN and e.key == K_DOWN: down = True self.start = True if e.type == KEYUP and e.key == K_UP: up = False self.start = True if e.type == KEYUP and e.key == K_RIGHT: right = False self.start = True if e.type == KEYUP and e.key == K_LEFT: left = False self.start = True if e.type == KEYUP and e.key == K_DOWN: down = False self.start = True if e.type == KEYDOWN and e.key == K_LSHIFT: running = True self.start = True if e.type == KEYUP and e.key == K_LSHIFT: running = False self.start = True if self.paused: self.PAUSE_MENU.update(events) if not self.paused: self.player.update( left, right, up, down, self.platforms, running) else: self.player.update( False, False, False, False, self.platforms, False) # First - backgorund drawing self.screen.blit(bg, (0, 0)) # Next - drawing objects if not self.paused: animatedEntities.update() self.entities.update( left, right, up, down, self.platforms, running) # Centralize camera on player self.camera.update(self.player) for e in self.entities: self.screen.blit(e.image, self.camera.apply(e)) if self.paused: self.PAUSE_MENU.draw(self.screen) pygame.display.update() if self.game_over_func is not None: self.game_over_func() pygame.mixer.Channel(0).stop() pygame.mixer.Channel(1).stop() pygame.mixer.Channel(2).stop() # 1 - leaved from menu ; 2 - died ; 3 - finished return self.exit_code
def __init__(self, player): # Call the parent constructor Level.__init__(self, player) self.time_allowed = 40 self.score_multiplier = 12 self.level_complete_bonus = 300 self.zone = "City" self.background = pygame.image.load( "./resources/img/city-bg.png").convert_alpha() # Array with width, height, x, and y of platform level = [[100, 60, 250, 740], [100, 60, 660, 740], [150, 40, 190, 110], [150, 40, 640, 110], [150, 40, 0, 530], [150, 40, 850, 530], [170, 40, 0, 330], [170, 40, 830, 330]] walls = [[30, 130, 170, 240], [30, 130, 800, 240]] # Go through the array above and add platforms for platform in level: block = Platform(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block) # Moving platform construct and add to platform list block = MovingPlatform(200, 40, self.zone, player) block.rect.x = 400 block.rect.y = 140 block.boundary_bottom = 705 block.boundary_top = 115 block.change_y = -2 self.platform_list.add(block) # Go through and create randomized targets for the level for target in range(10): target = Cop() # Set a random location for the block, but place it on a platform within the list # First select a random plat from the list in the level random_plat = random.randrange(len(level)) random_plat = level[random_plat] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound random_plat_x = random_plat[2] random_plat_y = random_plat[3] target.rect.x = random.randrange( random_plat_x, random_plat_x + (random_plat[0] - target.width)) target.rect.y = random_plat_y - target.height # Move distance allowed for this plat target.move_start = random_plat[2] target.move_end = random_plat[2] + random_plat[0] # Add the block to the list of objects self.target_list.add(target) # Deliberate target locations for level as per design for i in range(6, 8): target = Cop() platform = level[i] # Grab it's X and Y and set this iteration of target's X and Y to be within a "standing" bound plat_x = platform[2] plat_y = platform[3] target.rect.x = random.randrange( plat_x, plat_x + (platform[0] - target.width)) target.rect.y = plat_y - target.height # Move distance allowed for this plat target.move_start = platform[2] target.move_end = platform[2] + platform[0] # Add the block to the list of objects self.target_list.add(target) # Generate walls after enemies so none spawn on it for platform in walls: block = Wall(platform[0], platform[1], self.zone) block.rect.x = platform[2] block.rect.y = platform[3] block.player = self.player block.zone = self.zone self.platform_list.add(block)