def render_settings(self): """Установка изображений фона, кнопок и т. п. Parameter: Returns: """ # установка фона self.screen.blit(load_image(PATHS[30])[0], load_image(PATHS[30])[1]) # установка текста и кнопок, ползунков self.font = pygame.font.Font( 'fonts/comic _sans_ms_pixel_rus_eng.ttf', 40) self.font_count = pygame.font.Font( 'fonts/comic _sans_ms_pixel_rus_eng.ttf', 35) self.screen.blit(self.font.render('НАСТРОЙКИ', True, (250, 250, 250)), (420, 62, 188, 572)) # установка текста и ползунков, относящихся к музыке self.screen.blit(self.font.render('музыка:', True, (250, 250, 250)), (92, 200, 188, 572)) music_slider_way = load_setting_image((275, 196), PATHS[28], (512, 64)) self.screen.blit(music_slider_way[0], music_slider_way[1]) self.music_slider = load_setting_image( (self.return_slider_location('melody'), 204), PATHS[29], (64, 64)) self.screen.blit(self.music_slider[0], self.music_slider[1]) # установка текста и ползунков, относящихся к звукам self.screen.blit(self.font.render('звук:', True, (250, 250, 250)), (92, 296, 188, 572)) sound_slider_way = load_setting_image((275, 292), PATHS[28], (512, 64)) self.screen.blit(sound_slider_way[0], sound_slider_way[1]) self.sound_slider = load_setting_image( (self.return_slider_location('sound'), 300), PATHS[29], (64, 64)) self.screen.blit(self.sound_slider[0], self.sound_slider[1]) # установка текста и кнопок, относящихся к полноэкранному режиму self.screen.blit(self.font.render('полноэкранный режим:', True, (250, 250, 250)), (92, 392, 188, 572)) if self.get_button_from_db() == 'yes': yes_button = load_setting_image((540, 388), PATHS[27], (64, 64)) self.screen.blit(yes_button[0], yes_button[1]) no_button = load_setting_image((700, 388), PATHS[26], (64, 64)) self.screen.blit(no_button[0], no_button[1]) else: yes_button = load_setting_image((540, 388), PATHS[26], (64, 64)) self.screen.blit(yes_button[0], yes_button[1]) no_button = load_setting_image((700, 388), PATHS[27], (64, 64)) self.screen.blit(no_button[0], no_button[1]) self.screen.blit(self.font.render('да', True, (250, 250, 250)), (620, 392, 188, 572)) self.screen.blit(self.font.render('нет', True, (250, 250, 250)), (780, 392, 188, 572))
def health_bar_render(self, screen): health_level = self.health // 10 health_frame = 163 + health_level if self.health == 0: health_frame = 164 screen.blit(func.load_image(PATHS[health_frame])[0], func.load_image(PATHS[health_frame])[1]) font = pg.font.SysFont('agencyfb', 40) text = font.render(f'{self.health}', True, (250, 250, 250)) screen.blit(text, (HEALTH_PLACE[0], HEALTH_PLACE[1]))
def light_render(self, screen): if self.light_frame % 6 == 0: # установление порядкового номера кадра для теней и света if not self.pause_trigger: self.number_of_frame = self.number_of_frame % 3 + 1 self.frame_for_shadow = 33 + self.number_of_frame self.frame_for_light = 37 + self.number_of_frame else: self.frame_for_shadow = 34 self.frame_for_light = 38 # установка теней screen.blit(func.load_image(PATHS[self.frame_for_shadow])[0], func.load_image(PATHS[self.frame_for_shadow])[1]) # установка свет screen.blit(func.load_image(PATHS[self.frame_for_light])[0], func.load_image(PATHS[self.frame_for_light])[1])
def shops_render(self, screen): if self.visible_guns: screen.blit(func.load_image(PATHS[179])[0], func.load_image(PATHS[179])[1]) elif self.visible_food: screen.blit(func.load_image(PATHS[180])[0], func.load_image(PATHS[180])[1]) elif self.visible_skills: screen.blit(func.load_image(PATHS[181])[0], func.load_image(PATHS[181])[1]) else: screen.blit(func.load_image(PATHS[182])[0], func.load_image(PATHS[182])[1])
def names_render(self, screen): if self.level_names_frame == 47: self.basic_level_names_list[0][1] = True if self.level_names_frame == 54: self.basic_level_names_list[1][1] = True if self.level_names_frame == 63: self.basic_level_names_list[2][1] = True if self.level_names_frame == 70: self.basic_level_names_list[3][1] = True if self.level_names_frame == 77: self.basic_level_names_list[4][1] = True if self.level_names_frame == 83: self.basic_level_names_list[5][1] = True if self.level_names_frame == 91: self.basic_level_names_list[6][1] = True if self.level_names_frame == 98: self.basic_level_names_list[7][1] = True for i in self.basic_level_names_list: if i[1]: screen.blit(func.load_image(PATHS[i[0]])[0], func.load_image(PATHS[i[0]])[1])
def render_map(self, screen): # установка фона screen.blit(func.load_image(PATHS[32])[0], func.load_image(PATHS[32])[1]) # отрисовка теней и света self.light_render(screen) # установка путей if not self.ways_already_here_flag: if self.way_frame != self.end_of_way_frames: self.way_frame += 1 if self.way_frame not in ACTION_WAY_FRAMES: screen.blit(func.load_image(PATHS[self.way_frame])[0], func.load_image(PATHS[self.way_frame])[1]) else: screen.blit(func.load_image(PATHS[self.way_frame - 1])[0], func.load_image(PATHS[self.way_frame - 1])[1]) else: self.ways_already_here_flag = True # установка надписей if not self.all_levels_already_here: if self.level_names_frame != self.end_of_level_names_frames: self.level_names_frame += 1 self.names_render(screen) if self.level_names_frame not in ACTION_BUTTONS_FRAMES: screen.blit(func.load_image( PATHS[self.level_names_frame])[0], func.load_image( PATHS[self.level_names_frame])[1]) else: self.all_levels_already_here = True # закрепление уровней if self.ways_already_here_flag: screen.blit(func.load_image(PATHS[self.way_frame])[0], func.load_image(PATHS[self.way_frame])[1]) if self.all_levels_already_here: for i in self.basic_level_names_list: if i[1]: screen.blit(func.load_image(PATHS[i[0]])[0], func.load_image(PATHS[i[0]])[1]) self.health_bar_render(screen)
def render_pause(self, screen, pause_condition): if pause_condition == 0: screen.blit(func.load_image(PATHS[184])[0], func.load_image(PATHS[184])[1]) if pause_condition == 1: screen.blit(func.load_image(PATHS[185])[0], func.load_image(PATHS[185])[1]) if pause_condition == 2: screen.blit(func.load_image(PATHS[186])[0], func.load_image(PATHS[186])[1]) if pause_condition == 3: screen.blit(func.load_image(PATHS[187])[0], func.load_image(PATHS[187])[1]) if pause_condition == 4: screen.blit(func.load_image(PATHS[188])[0], func.load_image(PATHS[188])[1]) if pause_condition == 5: screen.blit(func.load_image(PATHS[189])[0], func.load_image(PATHS[189])[1]) if pause_condition == 6: screen.blit(func.load_image(PATHS[190])[0], func.load_image(PATHS[190])[1]) screen.blit(func.load_pause_slider_way(PAUSE_SLIDER_WAY_POSITION1)[0], func.load_pause_slider_way(PAUSE_SLIDER_WAY_POSITION1)[1]) screen.blit(func.load_pause_slider_way(PAUSE_SLIDER_WAY_POSITION2)[0], func.load_pause_slider_way(PAUSE_SLIDER_WAY_POSITION2)[1]) screen.blit(func.load_pause_slider(PAUSE_SLIDER_POSITION1)[0], func.load_pause_slider(PAUSE_SLIDER_POSITION1)[1]) screen.blit(func.load_pause_slider(PAUSE_SLIDER_POSITION2)[0], func.load_pause_slider(PAUSE_SLIDER_POSITION2)[1])
def exp_shop_render(self, screen, condition): if condition == 0: screen.blit(func.load_image(PATHS[192])[0], func.load_image(PATHS[192])[1]) if condition == 1: screen.blit(func.load_image(PATHS[193])[0], func.load_image(PATHS[193])[1]) if condition == 2: screen.blit(func.load_image(PATHS[194])[0], func.load_image(PATHS[194])[1]) if condition == 3: screen.blit(func.load_image(PATHS[195])[0], func.load_image(PATHS[195])[1]) if condition == 4: screen.blit(func.load_image(PATHS[196])[0], func.load_image(PATHS[196])[1]) if condition == 5: screen.blit(func.load_image(PATHS[197])[0], func.load_image(PATHS[197])[1]) if condition == 6: screen.blit(func.load_image(PATHS[198])[0], func.load_image(PATHS[198])[1]) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 27) text = font.render('меткость', True, (250, 250, 250)) screen.blit(text, (ACCURACY_NAME[0], ACCURACY_NAME[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 27) text = font.render('защита', True, (250, 250, 250)) screen.blit(text, (DEFENCE_NAME[0], DEFENCE_NAME[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 27) text = font.render('скорость', True, (250, 250, 250)) screen.blit(text, (SPEED_NAME[0], SPEED_NAME[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 30) text = font.render(f'{self.accuracy}', True, (250, 250, 250)) screen.blit(text, (ACCURACY_LVL[0], ACCURACY_LVL[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 30) text = font.render(f'{self.defence}', True, (250, 250, 250)) screen.blit(text, (DEFENCE_LVL[0], DEFENCE_LVL[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 30) text = font.render(f'{self.speed}', True, (250, 250, 250)) screen.blit(text, (SPEED_LVL[0], SPEED_LVL[1])) font = pg.font.Font('fonts/comic _sans_ms_pixel_rus_eng.ttf', 40) text = font.render(f'{self.exp_count}', True, (250, 250, 250)) screen.blit(text, (EXP_BANK[0], EXP_BANK[1]))
def money_and_pauseBtn_render(self, screen): screen.blit(func.load_image(PATHS[self.pause_button_frame])[0], func.load_image(PATHS[self.pause_button_frame])[1]) font = pg.font.SysFont('agencyfb', 40) text = font.render(f'{self.money}', True, (200, 200, 200)) screen.blit(text, (MONEY_PLACE[0], MONEY_PLACE[1]))
def run(self, screen): """Игровой цикл Parameter screen: surface Returns: """ self.connect_to_db() running = True # Установка музыки главного меню music_menu = Music('main_menu_melody.ogg') music_menu.run() music_menu.set_volume(self.get_from_db()) while running: # Установка спрайта фона screen.blit(load_image(PATHS[2])[0], load_image(PATHS[2])[1]) # Установка спрайта кнопок в зависимости от выбранной кнопки # Переменная зависимости - buttons_condition screen.blit(load_image(self.buttons_condition)[0], load_image(self.buttons_condition)[1]) """Кнопки: Button-0 = Играть, Button-1 = Сохранения, Button-2 = Настройки, Button-3 = Выход. """ button = None # Кнопка которую нажали(если нажали) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Обработка наведения на кнопки if event.type == pygame.MOUSEMOTION: mouse_x, mouse_y = event.pos coords = MAIN_MENU_BUTTONS_COORDINATES if coords[0][0] < mouse_x < coords[0][2] and \ coords[0][1] < mouse_y < coords[0][3]: if self.is_pressed: self.buttons_condition = PATHS[5] else: self.buttons_condition = PATHS[4] elif coords[1][0] < mouse_x < coords[1][2] and \ coords[1][1] < mouse_y < coords[1][3]: if self.is_pressed: self.buttons_condition = PATHS[7] else: self.buttons_condition = PATHS[6] elif coords[2][0] < mouse_x < coords[2][2] and \ coords[2][1] < mouse_y < coords[2][3]: if self.is_pressed: self.buttons_condition = PATHS[9] else: self.buttons_condition = PATHS[8] elif coords[3][0] < mouse_x < coords[3][2] and \ coords[3][1] < mouse_y < coords[3][3]: if self.is_pressed: self.buttons_condition = PATHS[11] else: self.buttons_condition = PATHS[10] else: self.buttons_condition = PATHS[3] # Обработка нажатия кнопки if event.type == pygame.MOUSEBUTTONDOWN: self.is_pressed = True mouse_x, mouse_y = event.pos coords = MAIN_MENU_BUTTONS_COORDINATES if coords[0][0] < mouse_x < coords[0][2] and \ coords[0][1] < mouse_y < coords[0][3]: self.buttons_condition = PATHS[5] elif coords[1][0] < mouse_x < coords[1][2] and \ coords[1][1] < mouse_y < coords[1][3]: self.buttons_condition = PATHS[7] elif coords[2][0] < mouse_x < coords[2][2] and \ coords[2][1] < mouse_y < coords[2][3]: self.buttons_condition = PATHS[9] elif coords[3][0] < mouse_x < coords[3][2] and \ coords[3][1] < mouse_y < coords[3][3]: self.buttons_condition = PATHS[11] else: self.buttons_condition = PATHS[3] # Обработка нажатия кнопки(отпуск клавиши) if event.type == pygame.MOUSEBUTTONUP: self.is_pressed = False mouse_x, mouse_y = event.pos coords = MAIN_MENU_BUTTONS_COORDINATES if coords[0][0] < mouse_x < coords[0][2] and \ coords[0][1] < mouse_y < coords[0][3]: self.buttons_condition = PATHS[4] button = 0 # Кнопка "Играть" elif coords[1][0] < mouse_x < coords[1][2] and \ coords[1][1] < mouse_y < coords[1][3]: button = 1 # Кнопка "Сохранения" self.buttons_condition = PATHS[6] elif coords[2][0] < mouse_x < coords[2][2] and \ coords[2][1] < mouse_y < coords[2][3]: self.buttons_condition = PATHS[8] button = 2 # Кнопка "Настройки" elif coords[3][0] < mouse_x < coords[3][2] and \ coords[3][1] < mouse_y < coords[3][3]: self.buttons_condition = PATHS[10] running = False else: self.buttons_condition = PATHS[3] pygame.display.flip() if button == 0: # Выключение музыки главного меню music_menu.stop() LevelHub().run(screen) elif button == 1: SaveHub().run(screen) elif button == 2: Settings().run(screen, music_menu)
def run(self, screen): running = True while running: screen.blit(load_image(PATHS[14])[0], load_image(PATHS[14])[1]) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(0) if event.type == pygame.KEYDOWN: # Обработчик клавиш if event.key == pygame.K_ESCAPE: running = False if event.type == pygame.MOUSEBUTTONUP: mouse_x, mouse_y = pygame.mouse.get_pos() if 50 <= mouse_x <= 974 and 50 <= mouse_y <= 110: self.cell = 1 elif 50 <= mouse_x <= 974 and 113 <= mouse_y <= 173: self.cell = 2 elif 50 <= mouse_x <= 974 and 176 <= mouse_y <= 236: self.cell = 3 elif 50 <= mouse_x <= 974 and 239 <= mouse_y <= 299: self.cell = 4 elif 50 <= mouse_x <= 974 and 302 <= mouse_y <= 362: self.cell = 5 elif 50 <= mouse_x <= 974 and 365 <= mouse_y <= 425: self.cell = 6 elif 50 <= mouse_x <= 974 and 428 <= mouse_y <= 488: self.cell = 7 else: self.cell = -1 # Наведение мышью на одну из ячеек сохранения if self.cell == -1: mouse_x, mouse_y = pygame.mouse.get_pos() if 50 <= mouse_x <= 974 and 50 <= mouse_y <= 110: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_1, 3) elif 50 <= mouse_x <= 974 and 113 <= mouse_y <= 173: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_2, 3) elif 50 <= mouse_x <= 974 and 176 <= mouse_y <= 236: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_3, 3) elif 50 <= mouse_x <= 974 and 239 <= mouse_y <= 299: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_4, 3) elif 50 <= mouse_x <= 974 and 302 <= mouse_y <= 362: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_5, 3) elif 50 <= mouse_x <= 974 and 365 <= mouse_y <= 425: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_6, 3) elif 50 <= mouse_x <= 974 and 428 <= mouse_y <= 488: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_7, 3) # выделение выбранной ячейки elif self.cell == 1: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_1, 3) elif self.cell == 2: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_2, 3) elif self.cell == 3: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_3, 3) elif self.cell == 4: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_4, 3) elif self.cell == 5: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_5, 3) elif self.cell == 6: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_6, 3) elif self.cell == 7: pygame.draw.rect(screen, pygame.Color('white'), SAVE_PROFILE_7, 3) pygame.display.flip()