def start_screen(): # начальная заставка игры, ждет нажатия клавиш counter_sec = 0 intro_text = ["PRESS ANY KEY"] fon = pygame.transform.scale(dop_func.load_image(f'fon{str(random.randint(0, 4))}.jpg'), (WIDTH, HEIGHT)) font = pygame.font.Font("data/cosm.ttf", 50) string_rendered = font.render(intro_text[0], 1, pygame.Color('white')) flag_invis = True pygame.mixer.music.play(-1) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() elif event.type == pygame.KEYDOWN: pygame.time.delay(500) menu() return counter_sec += 1 if counter_sec == 8: # техника, чтобы текст мигал fon = pygame.transform.scale(dop_func.load_image(f'fon{str(random.randint(0, 4))}.jpg'), (WIDTH, HEIGHT)) counter_sec = 0 screen.blit(fon, (0, 0)) if flag_invis: screen.blit(string_rendered, (WIDTH // 2 - string_rendered.get_width() // 2, HEIGHT // 4 * 3)) flag_invis = False else: flag_invis = True pygame.display.flip() clock.tick(FPS)
def menu(): # стартовое меню fon = pygame.transform.scale( dop_func.load_image(f'fon{str(random.randint(0, 4))}.jpg'), (WIDTH, HEIGHT)) start_game = Button(350, 80) quit_button = Button(225, 80) resume_game = Button(350, 80, text_size=45) counter_sec = 0 pygame.mixer.music.play(-1) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() counter_sec += 1 if counter_sec == 40: # смена фона fon = pygame.transform.scale( dop_func.load_image(f'fon{str(random.randint(0, 4))}.jpg'), (WIDTH, HEIGHT)) counter_sec = 0 screen.blit(fon, (0, 0)) if dop_func.check_save( ): # если есть сейвы то одна кнопка, если нет, то другая start_game.draw(WIDTH // 2 - start_game.width // 2, HEIGHT // 4 * 2.5 - 300, 'Новая игра', start) resume_game.draw(WIDTH // 2 - start_game.width // 2, HEIGHT // 4 * 2.5 - 200, 'Продолжить', mainField.resume) else: start_game.draw(WIDTH // 2 - start_game.width // 2, HEIGHT // 4 * 2.5 - 200, 'Новая игра', start) quit_button.draw(WIDTH // 2 - quit_button.width // 2, HEIGHT // 4 * 2.5 - 100, 'Выйти', quit) pygame.display.flip() clock.tick(FPS)
def __init__(self, size): self.size = size self.x0, self.y0 = 0, 0 self.x1, self.y1 = 0, 0 self.image = pygame.transform.scale( dop_func.load_image('ship.png', (255, 255, 255)), (size, size)) self.image = pygame.transform.flip(self.image, 1, 0)
def draw_level(number, board): # рисует уровень global screen lst_planet = database.take_planet(number) for i in lst_planet: id, x, y = i image = pygame.transform.scale( dop_func.load_image(f'planet/{id}.jpg', (255, 255, 255)), (board.cell_size, board.cell_size)) screen.blit(image, (board.left + board.cell_size * x + 1, board.top + board.cell_size * y + 1)) return lst_planet
def pause(ret=False): # создает паузу при нажати esc global lst_planet, lvl cont = mainMenu.Button(315, 70, text_size=40) save_game = mainMenu.Button(280, 70, text_size=40) change_team = mainMenu.Button(225, 70, text_size=40) exit_game = mainMenu.Button(220, 70, text_size=40) fon = pygame.transform.scale(dop_func.load_image('pause.jpg'), (WIDTH, HEIGHT)) if ret: return True while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() if event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos if x in range(WIDTH // 2 - cont.width // 2, WIDTH // 2 + cont.width // 2): if y in range(int(HEIGHT // 2.5), int(HEIGHT // 2.5) + cont.height): cont.draw(WIDTH // 2 - cont.width // 2, HEIGHT // 2 + HEIGHT // 3, "Начать игру", 'return') return cont.ret() elif y in range(int(HEIGHT // 2.5 + cont.height + 10), int(HEIGHT // 2.5 + 2 * cont.height + 10)): save_game.draw(WIDTH // 2 - save_game.width // 2, HEIGHT // 2.5 + cont.height + 10, 'Сохранить', dop_func.save_game, (lst_planet, lvl)) return save_game.ret() screen.blit(fon, (0, 0)) cont.draw(WIDTH // 2 - cont.width // 2, HEIGHT // 2.5, 'Продолжить', 'return') save_game.draw(WIDTH // 2 - save_game.width // 2, HEIGHT // 2.5 + cont.height + 10, 'Сохранить', dop_func.save_game, (lst_planet, lvl)) change_team.draw(WIDTH // 2 - change_team.width // 2, HEIGHT // 2.5 + 2 * cont.height + 2 * 10, 'Команда', change) exit_game.draw(WIDTH // 2 - exit_game.width // 2, HEIGHT // 2.5 + 3 * cont.height + 3 * 10, ' Выйти', action=mainMenu.menu) pygame.display.flip()
def congratulations(flag): # выводит результат битвы fon = pygame.transform.scale(dop_func.load_image('battle_fon.jpg'), (WIDTH, HEIGHT)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() if event.type == pygame.MOUSEBUTTONDOWN: return screen.blit(fon, (0, 0)) if flag is None: dop_func.print_text(screen, "Игра завершена", WIDTH // 2 - WIDTH // 13, HEIGHT // 2 - HEIGHT // 15) elif flag: dop_func.print_text(screen, "Вы победили", WIDTH // 2 - WIDTH // 13, HEIGHT // 2 - HEIGHT // 15) else: dop_func.print_text(screen, "Вы проиграли", WIDTH // 2 - WIDTH // 13, HEIGHT // 2 - HEIGHT // 15) pygame.display.flip()
def render(self): # отображает игровое поле global screen for i in range(self.width): for r in range(self.height): pygame.draw.polygon(screen, (255, 255, 255, 255), [(self.left + self.cell_size * i, self.top + self.cell_size * r), (self.left + self.cell_size * (i + 1), self.top + self.cell_size * r), (self.left + self.cell_size * (i + 1), self.top + self.cell_size * (r + 1)), (self.left + self.cell_size * i, self.top + self.cell_size * (r + 1))], 1) screen.blit(self.ship.return_image(), (self.left + self.cell_size * self.ship.x1 + 1, self.top + self.cell_size * self.ship.y1 + 1)) screen.blit( pygame.transform.scale( dop_func.load_image('next.jpg', (255, 255, 255)), (self.cell_size, self.cell_size)), (self.left + self.cell_size * 15 + 1, self.top + self.cell_size * 7 + 1)) return
def choose_captain( LEVEL=1 ): # выбор капитана, очень много костылей, лучше не смотреть код background = pygame.transform.scale(dop_func.load_image(f'board.jpg'), (WIDTH, HEIGHT)) heroes = set() while len(heroes) != 5: hero = database.take_hero() if hero[0] in database.TAKED_HERO or hero[0] in list( map(lambda x: x.name, defaultUnit.groupMain.get_lst())): continue heroes.add(hero) heroes = list(heroes) image0 = pygame.transform.scale( dop_func.load_image("heroes/" + heroes[0][-1]), (5 * WIDTH // 26, HEIGHT // 2)) image1 = pygame.transform.scale( dop_func.load_image("heroes/" + heroes[1][-1]), (5 * WIDTH // 26, HEIGHT // 2)) image2 = pygame.transform.scale( dop_func.load_image("heroes/" + heroes[2][-1]), (5 * WIDTH // 26, HEIGHT // 2)) image3 = pygame.transform.scale( dop_func.load_image("heroes/" + heroes[3][-1]), (5 * WIDTH // 26, HEIGHT // 2)) image4 = pygame.transform.scale( dop_func.load_image("heroes/" + heroes[4][-1]), (5 * WIDTH // 26, HEIGHT // 2)) dct_names = { 0: [heroes[0][0]], 1: [heroes[1][0]], 2: [heroes[2][0]], 3: [heroes[3][0]], 4: [heroes[4][0]] } choose = Button(185, 65, text_size=30) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() screen.fill((255, 255, 255)) dop_func.print_text(screen, 'Выберите героя', (2 * WIDTH) // 6, HEIGHT // 8, color='black') for i in range(0, 5): choose.draw(3 * WIDTH // 30 + i * WIDTH // 6, 3 * HEIGHT // 4, "Выбрать", "set_captain", (dct_names[i][0], LEVEL)) if len(dct_names[i]) == 1: dct_names[i] = [ dct_names[i][0], (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9, HEIGHT // 5), (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9 + image0.get_width(), HEIGHT // 5 + HEIGHT // 2), False ] if dct_names[i][-1] is False: dct_names[i] = [ dct_names[i][0], (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9, HEIGHT // 5), (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9 + image0.get_width(), HEIGHT // 5 + HEIGHT // 2), False ] screen.blit(locals()['image%s' % i], (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9, HEIGHT // 5)) else: dct_names[i] = [ dct_names[i][0], (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9, HEIGHT // 5), (3 * WIDTH // 50 + 8 * i * image0.get_width() // 9 + image0.get_width(), HEIGHT // 5 + HEIGHT // 2), True ] draw_stat(heroes[i], dct_names[i]) if dct_names[i][1][0] <= pygame.mouse.get_pos()[0] <= dct_names[i][2][0] and\ dct_names[i][1][1] <= pygame.mouse.get_pos()[1] <= dct_names[i][2][1] and pygame.mouse.get_pressed()[0]: dct_names[i][-1] = not dct_names[i][-1] pygame.display.flip() clock.tick(FPS)
def run_cycle(captain_name, LEVEL=1, planets=None): # основной цикл if len(groupMain.lst) != LEVEL and LEVEL < 6: groupMain.lst.clear() if groupMain.lst == list(): name, hp, damage, armour, bonus_hp, bonus_damage, bonus_armour, photo = list( database.full_hero(captain_name)) hero = defaultUnit.HeroUnit(name, hp, damage, armour, groupMain, bonus_hp, bonus_damage, bonus_armour, photo) groupMain.append_hero(hero) board = Board(16, 8) global lst_planet, lvl if planets: lst_planet = planets else: lst_planet = [ Planet(i[0], i[1], i[2], LEVEL) for i in database.take_planet(LEVEL) ] lvl = LEVEL cell_s = WIDTH // 16 - 1 board.set_view((WIDTH - cell_s * 16) // 2, (HEIGHT - cell_s * 8) // 2, WIDTH // 16 - 1) fon = pygame.transform.scale(dop_func.load_image('Space.jpg'), (WIDTH, HEIGHT)) flag = True while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() if event.type == pygame.MOUSEBUTTONDOWN: cell = board.get_cell(event.pos) if cell is None: break x, y = cell if x == 15 and y == 7: if LEVEL < 8: stat = True for i in lst_planet: if not i.get_status(): stat = False if stat: return mainMenu.choose_captain(LEVEL + 1) else: congratulations(None) break flag = True for i in range(len(lst_planet)): if lst_planet[i].x == x and lst_planet[i].y == y: if lst_planet[i].get_status() is False: board.get_click(event.pos) if battleUI(lst_planet[i]): lst_planet[i].set_status(True) congratulations(True) else: congratulations(False) else: flag = False break if flag: board.get_click(event.pos) if event.type == pygame.KEYDOWN and event.key == 27: pause() screen.blit(fon, (0, 0)) draw_level(LEVEL, board) board.render() if flag is False: dop_func.print_text(screen, 'Планета уже захвачена', WIDTH // 2 - WIDTH // 5, HEIGHT // 2 - HEIGHT // 20) pygame.display.flip()
def battleUI(planet=None, flag=False, res=None): global screen if flag: return res battle = Battle((groupMain, planet.get_group())) fon = pygame.transform.scale(dop_func.load_image('fone.jpg'), (WIDTH, HEIGHT)) start_but = mainMenu.Button(105, 75, font_type='stat.ttf') while True: for event in pygame.event.get(): if event.type == pygame.QUIT: dop_func.terminate() screen.blit(fon, (0, 0)) not_ours_lst = battle.lst2 our_lst = battle.lst1 hp, damage, armour = 0, 0, 0 for i in range(len(our_lst)): hpp, dmgg, armrr = our_lst[i][0], our_lst[i][1], our_lst[i][2] if hpp < 0: hpp = 0 dop_func.print_text(screen, 'Ваши герои', WIDTH // 15, HEIGHT // 20, font_type='stat.ttf', font_size=60) dop_func.print_text( screen, f'{battle.gr1.get_lst()[i].name} HP {hpp} DMG {dmgg} ARM ' f'{armrr}', WIDTH // 15, i * HEIGHT // 10 + HEIGHT // 5, font_type='stat.ttf', font_size=35) dop_func.print_text(screen, 'Общие статы', WIDTH // 15, HEIGHT // 2 + HEIGHT // 5, font_type='stat.ttf', font_size=45) hp += int(hpp) damage += int(dmgg) armour += int(armrr) dop_func.print_text(screen, f'HP {hp} DMG {damage} ARM {armour}', WIDTH // 15, HEIGHT // 2 + HEIGHT // 4, font_type='stat.ttf', font_size=35) hp, damage, armour = 0, 0, 0 for i in range(len(not_ours_lst)): hpp, dmgg, armrr = not_ours_lst[i][0], not_ours_lst[i][ 1], not_ours_lst[i][2] if hpp < 0: hpp = 0 dop_func.print_text(screen, 'Герои противника', WIDTH // 2, HEIGHT // 20, font_type='stat.ttf', font_size=60) dop_func.print_text( screen, f'{battle.gr2.get_lst()[i].name} HP {hpp} DMG {dmgg} ARM ' f'{armrr}', WIDTH // 2, i * HEIGHT // 10 + HEIGHT // 5, font_type='stat.ttf', font_size=35) dop_func.print_text(screen, 'Общие статы', WIDTH // 2, HEIGHT // 2 + HEIGHT // 5, font_type='stat.ttf', font_size=45) hp += int(hpp) damage += int(dmgg) armour += int(armrr) dop_func.print_text(screen, f'HP {hp} DMG {damage} ARM {armour}', WIDTH // 2, HEIGHT // 2 + HEIGHT // 4, font_type='stat.ttf', font_size=35) start_but.draw(WIDTH // 2 - start_but.width // 2, HEIGHT // 2 + HEIGHT // 3, "Ход", 'battle', battle) pygame.display.flip() if battle.get_result() is not None: return battle.get_result()