def init_level(self): self.screen = config.get_value('screen') level = self.load_level(config.get_value('level')) level = [list(line) for line in level] player_group = PlayerGroup(level) self.groups = { 'floor': FloorGroup(level), 'box': BoxGroup(level), 'player': player_group, 'lava': LavaGroup(level), 'enemy': EnemyGroup(level, player_group.player), 'finish': FinishGroup(level) } self.groups['player'].set_walls(self.groups['box']) self.groups['player'].set_finish(self.groups['finish']) camera.update(self.groups['player'].player)
def next_level(self): level = config.get_value('level') new_level_index = self.levels.index(level) + 1 if new_level_index >= len(self.levels): # все уровни пройдены, в этом случае надо что-то сделать, сейчас просто зациклил уровни new_level_index = 0 config.set_value('level', self.levels[new_level_index]) self.init_level()
def can_move(self, direction): result = True self.move_action(direction) if not (0 <= self.row < self.height and 0 <= self.col < self.width): result = False if self.walls and pygame.sprite.spritecollideany(self, self.walls): result = False if pygame.sprite.spritecollideany(self, self.finish): print('finish!') pygame.event.post( pygame.event.Event(config.get_value('level_end_event'))) self.move_action(direction, reverse=True) return result
def update_world(self): if self.can_move(pygame.K_DOWN): self.remove_active_piece() self.active_piece.down() self.render_active_piece() else: self.active_piece_to_block() self.check_complete_lines() if self.check_for_lose(): self.knight = Animation(self.screen, False, False, True) size = 100 normal_size = 40 color = pygame.Color('red') main_font = pygame.font.SysFont('Arial', size) font = pygame.font.SysFont('Arial', normal_size) text = ['Вы проиграли!', 'Нажмите любую кнопку чтобы выйти'] rendered = [ main_font.render(text[0], True, color), font.render(text[1], True, color) ] rects = [rendered[0].get_rect(), rendered[1].get_rect()] width = max(rects[0].width, rects[1].width) height = 10 + rects[0].height + rects[1].height surface = pygame.Surface((width, height), pygame.SRCALPHA) rects[1].y += 110 for i in range(2): text = rendered[i] rect = rects[i] rect.x = width // 2 - rect.width // 2 surface.blit(text, rect) x = config.get_value('width') // 2 - width // 2 y = config.get_value('height') // 2 - height // 2 self.screen.blit(surface, pygame.Rect(x, y, width, height)) self.running = False self.create_active_piece() self.render_active_piece()
def on_event(self, event): if event.type == pygame.KEYUP: self.groups['player'].on_key_pressed(event.key) if event.type == config.get_value('level_end_event'): self.next_level()
def __init__(self): self.screen = config.get_value('screen') self.levels = ['lvl1', 'lvl2', 'lvl4', 'lvl5', 'lvl6'] config.set_value('level', self.levels[0]) self.init_level()
def get_coords(self): tile_size = config.get_value('tile_size') x = self.col * tile_size + tile_size // 2 - self.rect.width // 2 y = self.row * tile_size + tile_size // 2 - self.rect.height // 2 return x, y
def get_coords(self): tile_size = config.get_value('tile_size') return self.col * tile_size, self.row * tile_size
import pygame from game_config import config from GameMain import GameMain import NumberLevel if __name__ == '__main__': pygame.init() pygame.display.set_caption('') width, height = config.get_list_values('width', 'height') size = width, height screen = pygame.display.set_mode(size) config.set_value('screen', screen) clock = pygame.time.Clock() running = True game = GameMain() fps = 60 dt = 0 while running: for event in pygame.event.get(): game.on_event(event) if event.type == pygame.QUIT: running = False game.draw(dt) game.update() NumberLevel.draw_number_level(screen, config.get_value('level')) pygame.display.flip() dt = clock.tick(fps) pygame.quit()