1) Увеличение ширины карты: устанавливаем GAME_WIDTH на 70% экрана, MAP_WIDTH на 30%
GAME_WIDTH = int(WIDTH * 0.7) # 70% ширины для игры
MAP_WIDTH = WIDTH - GAME_WIDTH # 30% для карты и инвентаря
GAME_HEIGHT = HEIGHT - 100 # Оставляем место для консоли
Устанавливаем оконный режим для отображения кнопок управления окном
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) # Оконный режим
pygame.display.set_caption("Математическая Игра с NPC")
Цвета
BLUE = (0, 0, 255)
GREEN = (0, 200, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
LIGHT_BLUE = (173, 216, 230)
GRAY = (200, 200, 200)
DARK_GRAY = (50, 50, 50)
LIGHT_GRAY = (230, 230, 230)
ORANGE = (255, 165, 0) # Цвет для 'обиженных' NPC
DUNGEON_COLOR = (128, 0, 128) # Фиолетовый цвет для подземелий
WORLD_SIZE = 30 # 30x30 регионов
MAP_TILE_SIZE = 8 # Размер клетки карты
Инициализация переменных для карты и инвентаря
map_area_height = 280 # Высота области карты
inventory_scroll_offset = 0
SCROLL_STEP = 20
Уровни сложности задач и соответствующие территории
def calculate_level(x, y):
center = WORLD_SIZE // 2
distance = max(abs(x - center), abs(y - center))
# Уровень увеличивается по мере удаления от центра
return min(1 + distance // 3, 10) # Ограничение до 10
npc_levels = {}
for x in range(WORLD_SIZE):
for y in range(WORLD_SIZE):
level = calculate_level(x, y)
if level <= 3:
math_range = (1, 10)
territory = "Засушливые земли"
elif level <= 6:
math_range = (10, 50)
territory = "Великие равнины"
elif level <= 8:
math_range = (50, 100)
territory = "Горные пастбища"
else:
math_range = (100, 1000)
territory = "Туманная долина"
npc_levels[(x, y)] = {"math_range": math_range, "territory": territory}
Assistant
Здравствуйте! Спасибо, что поделились своим кодом игры на Python с использованием Pygame. Вы создали достаточно сложную игру с NPC, инвентарём, системой уровней и подземельями.
Могу ли я чем-то помочь?
Если у вас есть конкретные вопросы или вы сталкиваетесь с какими-либо проблемами в коде, пожалуйста, сообщите мне, и я постараюсь вам помочь. Например:
Возникают ли ошибки при запуске игры?
Хотели бы вы добавить новые функции или изменить существующие?
Не работает ли какая-то часть игры так, как вы ожидаете?
Если вы предоставите больше деталей о том, что именно вам нужно, я смогу дать более точные рекомендации или помочь с отладкой.
Структура региона с добавленным cooldown для NPC и возможными подземельями
class Region:
def init(self, x, y):
self.coord = (x, y)
self.npcs = [] # Список NPC в регионе
self.dungeon_entrances = [] # Список позиций входов в подземелье
self.dungeons = {} # Хранение подземелий
self.generate_npcs()
self.generate_dungeon_entrances()
code
def generate_npcs(self):
num_npcs = random.randint(1, 5) # Ограничение от 1 до 5 NPC
for _ in range(num_npcs):
profession_entry = random.choice(npc_professions)
profession = profession_entry['profession']
name = random.choice(profession_entry['names'])
# Генерация уровня NPC на основе региона
level = calculate_level(*self.coord)
# Генерация позиции NPC с учётом предотвращения наложения и отступа от границ
pos = generate_npc_position(existing_npcs=self.npcs)
# Название местности
territory = npc_levels[self.coord]['territory']
self.npcs.append({
'pos': pos,
'has_interacted': False,
'profession': profession,
'name': name,
'level': level,
'task_solved': False,
'cooldown': 0 # Время, до которого NPC не может взаимодействовать после отказа
})
def generate_dungeon_entrances(self):
# С вероятностью 20% регион может содержать подземелье
if random.random() < 0.2:
# Добавляем 1 или 2 входа в подземелье
num_entrances = random.randint(1, 2)
for _ in range(num_entrances):
pos = generate_dungeon_entrance_position(existing_entrances=self.dungeon_entrances)
self.dungeon_entrances.append(pos)
def generate_npc_position(existing_npcs=None):
if existing_npcs is None:
existing_npcs = []
attempts = 0
margin = 50 # 50 пикселей от каждой стороны
while attempts < 100:
x = random.randint(margin, GAME_WIDTH - 200 - 50) # 50 - размер NPC
y = random.randint(margin, GAME_HEIGHT - 150 - 50)
new_rect = pygame.Rect(x, y, 50, 50)
overlap = False
for npc in existing_npcs:
existing_rect = pygame.Rect(npc['pos'][0], npc['pos'][1], 50, 50)
if new_rect.colliderect(existing_rect.inflate(50, 50)):
overlap = True
break
if not overlap:
return [x, y]
attempts += 1
return [random.randint(margin, GAME_WIDTH - 200 - 50), random.randint(margin, GAME_HEIGHT - 150 - 50)]
def generate_dungeon_entrance_position(existing_entrances=None):
if existing_entrances is None:
existing_entrances = []
attempts = 0
margin = 60 # Отступ для входов
while attempts < 100:
x = random.randint(margin, GAME_WIDTH - 60) # Размер входа = 40, добавляем паддинг
y = random.randint(margin, GAME_HEIGHT - 60)
new_rect = pygame.Rect(x, y, 40, 40)
overlap = False
for entrance in existing_entrances:
existing_rect = pygame.Rect(entrance[0], entrance[1], 40, 40)
if new_rect.colliderect(existing_rect.inflate(50, 50)):
overlap = True
break
if not overlap:
return [x, y]
attempts +=1
return [random.randint(margin, GAME_WIDTH - 60), random.randint(margin, GAME_HEIGHT - 60)]
Генерация и хранение регионов
world_regions = {}
def get_region(x, y):
if (x, y) not in world_regions and 0 <= x < WORLD_SIZE and 0 <= y < WORLD_SIZE:
world_regions[(x, y)] = Region(x, y)
return world_regions.get((x, y), None)
Текущий регион игрока
current_region_coord = (WORLD_SIZE // 2, WORLD_SIZE // 2) # Начинаем в центре
current_region = get_region(*current_region_coord)
Инициализация имен местностей
def assign_territories(x, y):
if (x, y) not in terrain_map:
region_level = npc_levels[(x, y)]['territory']
terrain_map[(x, y)] = region_level
attack = 0
defense = 0
knowledge_level = 1
tasks_solved = 0
tasks_total = 0 # Общее количество решенных задач
tasks_needed = 10 # Необходимое количество для повышения уровня
# Рисуем территорию сверху
territory_title = large_font.render(territory, True, BLACK)
WIN.blit(territory_title, (GAME_WIDTH//2 - territory_title.get_width()//2, 10))
# Рисуем NPC
for npc in current_region.npcs:
# Цвет NPC зависит от состояния
current_time = time.time()
if current_time < npc['cooldown']:
color = ORANGE
else:
color = YELLOW if npc['task_solved'] else GREEN
pygame.draw.rect(WIN, color, (npc['pos'][0], npc['pos'][1], 50, 50))
# Отображаем имя, уровень и профессию
npc_text = npc_font.render(f"{npc['profession']} (ур.{npc['level']}): {npc['name']}", True, BLACK)
WIN.blit(npc_text, (npc['pos'][0], npc['pos'][1] - 20))
# Если игрок рядом и можно получить предмет, показываем информацию
if state == 'move' and not npc['task_solved'] and current_time >= npc['cooldown']:
player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
npc_rect = pygame.Rect(npc['pos'][0], npc['pos'][1], 50, 50)
# Взаимодействие только при близком расстоянии
if player_rect.colliderect(npc_rect.inflate(6, 6)): # Расстояние примерно 3 пикселя
# Каждый NPC может дать один случайный предмет
possible_item = generate_item(npc['level'])
item_type = possible_item['type']
item_name = possible_item['name']
item_stat = possible_item['stat']
stat_value = possible_item['value']
info_text = f"{item_type} {item_name} ({item_stat} +{stat_value})"
info_render = font.render(info_text, True, YELLOW)
WIN.blit(info_render, (npc['pos'][0], npc['pos'][1] + 60))
# Рисуем входы в подземелья как черные квадраты
for entrance in current_region.dungeon_entrances:
pygame.draw.rect(WIN, BLACK, (entrance[0], entrance[1], 40, 40))
# Опционально: можно добавить иконку или символ для входа
entrance_text = font.render("Подземелье", True, WHITE)
WIN.blit(entrance_text, (entrance[0]+5, entrance[1]+10))
# Рисуем игрока
pygame.draw.rect(WIN, player_color, (player_pos[0], player_pos[1], player_size, player_size))
# Отображаем параметры игрока
attack_text = font.render(f"Атака: {attack}", True, BLACK)
defense_text = font.render(f"Защита: {defense}", True, BLACK)
percentage_text = font.render(f"Процент верных: {calculate_percentage()}%", True, BLACK)
knowledge_text = font.render(f"Уровень Знаний: {knowledge_level} ({tasks_solved}/{tasks_needed})", True, BLACK)
WIN.blit(attack_text, (GAME_WIDTH - 200, 20))
WIN.blit(defense_text, (GAME_WIDTH - 200, 50))
WIN.blit(percentage_text, (GAME_WIDTH - 200, 80))
WIN.blit(knowledge_text, (GAME_WIDTH - 200, 110))
# Рисуем кнопку инвентаря под параметрами (больше не нужна, инвентарь всегда отображается)
# inventory_button = pygame.Rect(GAME_WIDTH - 120, 140, 100, 30)
# pygame.draw.rect(WIN, GRAY, inventory_button)
# inventory_text = font.render("Инвентарь", True, BLACK)
# WIN.blit(inventory_text, (inventory_button.x + 15, inventory_button.y + 8))
# Проверка наведения на кнопку
# mouse_pos = pygame.mouse.get_pos()
# mouse_pressed = pygame.mouse.get_pressed()
# if inventory_button.collidepoint(mouse_pos):
# pygame.draw.rect(WIN, YELLOW, inventory_button, 2)
# if mouse_pressed[0]:
# open_inventory()
Функция для рисования карты
def draw_map():
# Отрисовка карты на верхней части правого блока
pygame.draw.rect(WIN, DARK_GRAY, (GAME_WIDTH, 0, MAP_WIDTH, map_area_height))
map_title = large_font.render("Карта", True, WHITE)
WIN.blit(map_title, (GAME_WIDTH + MAP_WIDTH//2 - map_title.get_width()//2, 10))
code
# Отрисовка квадратов регионов
for y in range(WORLD_SIZE):
for x in range(WORLD_SIZE):
region_coord = (x, y)
if region_coord in terrain_map:
# Отображаем только открытые регионы
if region_coord == current_region_coord:
color = YELLOW # Текущий регион
else:
color = WHITE # Посещённые регионы
# Проверяем, есть ли подземелье
region = world_regions[region_coord]
if region.dungeon_entrances:
pygame.draw.rect(WIN, DUNGEON_COLOR, (GAME_WIDTH + 10 + x * MAP_TILE_SIZE, 40 + y * MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE))
else:
pygame.draw.rect(WIN, color, (GAME_WIDTH + 10 + x * MAP_TILE_SIZE, 40 + y * MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE))
else:
# Незаполненные регионы отображаются чёрным
pygame.draw.rect(WIN, BLACK, (GAME_WIDTH + 10 + x * MAP_TILE_SIZE, 40 + y * MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE))
# Отображение координат текущего региона
coord_text = map_font.render(f"Координаты: {current_region_coord}", True, WHITE)
WIN.blit(coord_text, (GAME_WIDTH + 10, map_area_height - 20))
question_parts = []
operations_sequence = []
num1 = random.randint(*num_range)
question_parts.append(str(num1))
for _ in range(num_operations):
operation = random.choice(operations)
# Если операция деления, гарантируем целочисленный результат
if operation == '/':
num = random.randint(*num_range)
# Чтобы деление было целочисленным
num1 = num1 * num
question_parts = [str(num1)] # Пересчитываем первый операнд
question_parts.append('/')
question_parts.append(str(num))
break # Ограничиваем до одного деления для безопасности
else:
num = random.randint(*num_range)
question_parts.append(operation)
question_parts.append(str(num))
num1 = eval(f"{num1} {operation} {num}")
question = ' '.join(question_parts)
try:
# Безопасное вычисление, ограничим операции
allowed_ops = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv}
expr = question
# Рассчитываем ответ
answer = eval(expr, {"__builtins__": None}, allowed_ops)
if isinstance(answer, float):
if answer.is_integer():
answer = int(answer)
else:
# Если результат не целый, повторяем генерацию
return generate_math_question(npc_level)
except:
# В случае ошибки повторяем генерацию
return generate_math_question(npc_level)
return question, answer
Функция для генерации предмета на основе уровня NPC
def generate_item(npc_level):
# Выбираем случайный тип предмета
item_type_entry = random.choice(items_types)
item_type = item_type_entry['type']
item_name = random.choice(item_type_entry['category_names'])
stat = item_type_entry['stat']
# Увеличиваем значения параметров на основе уровня NPC
stat_value = random.randint(
item_type_entry['stat_value_range'][0] + npc_level,
item_type_entry['stat_value_range'][1] + npc_level
)
return {
'type': item_type,
'name': item_name,
'stat': stat,
'value': stat_value
}
Функция обработки диалога с NPC
def handle_dialog(npc):
global state, current_question, correct_answer, player_answer, inventory, console_message, tasks_solved, tasks_total
# Генерируем вопрос
current_question, correct_answer = generate_math_question(npc['level'])
state = 'dialog'
player_answer = ''
# Определяем предмет, который даст NPC в зависимости от уровня знания
received_item = generate_item(npc['level'])
console_message = f"{npc['name']} предлагает вам {received_item['type']} {received_item['name']} ({received_item['stat']} +{received_item['value']}). Решите задачу, чтобы получить его."
code
# Главный цикл диалога
while state == 'dialog':
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
console_message = "Диалог завершён без решения задачи."
state = 'move'
npc['cooldown'] = time.time() + 60 # Устанавливаем 1 минуту cooldown
elif event.key == pygame.K_RETURN:
if player_answer:
try:
user_answer = int(player_answer)
if user_answer == correct_answer:
inventory.append(received_item)
console_message = f"Вы заслужили этот предмет: {received_item['name']}."
tasks_solved +=1
tasks_total +=1
# Обновление процента верных
update_level()
# Обновляем статус NPC
npc['task_solved'] = True
state = 'move'
else:
console_message = f"Неправильный ответ! Правильный ответ: {correct_answer}."
tasks_total +=1
state = 'move'
npc['cooldown'] = time.time() + 60 # Устанавливаем 1 минуту cooldown
except:
console_message = "Пожалуйста, введите корректный ответ."
player_answer = ''
else:
# Если нет ответа, выходим из диалога
console_message = "Диалог завершён без решения задачи."
state = 'move'
npc['cooldown'] = time.time() + 60 # Устанавливаем 1 минуту cooldown
elif event.key == pygame.K_BACKSPACE:
player_answer = player_answer[:-1]
else:
if event.unicode.isdigit() or (event.unicode == '-' and len(player_answer)==0):
player_answer += event.unicode
# Рисуем основной экран
draw_window()
# Отображаем информацию диалога в консоли
pygame.draw.rect(WIN, LIGHT_BLUE, (0, GAME_HEIGHT, WIDTH, HEIGHT - GAME_HEIGHT))
# Первая строка: предложение предмета
dialog_line1 = console_font.render(f"{npc['name']} предлагает вам {received_item['type']} {received_item['name']} ({received_item['stat']} +{received_item['value']}). Решите задачу, чтобы получить его.", True, BLACK)
WIN.blit(dialog_line1, (10, GAME_HEIGHT + 10))
# Вторая строка: сама задача
if current_question and correct_answer is not None:
dialog_line2 = console_font.render(f"Задача: {current_question}", True, BLACK)
WIN.blit(dialog_line2, (10, GAME_HEIGHT + 40))
# Третья строка: поле ввода ответа
input_prompt = console_font.render("Ответ:", True, BLACK)
input_field = console_font.render(player_answer, True, BLACK)
WIN.blit(input_prompt, (10, GAME_HEIGHT + 70))
WIN.blit(input_field, (70, GAME_HEIGHT + 70))
pygame.display.update()
Функция для взаимодействия с NPC
def interact_with_npc(npc):
global state, current_npc
current_npc = npc
handle_dialog(npc)
Функция для входа в подземелье
def enter_dungeon(entrance_pos):
global state, current_dungeon, player_pos, current_dungeon_coord, overworld_player_pos, dungeon_player_pos
state = 'dungeon'
current_dungeon_coord = current_region_coord
if current_dungeon_coord not in active_dungeons:
active_dungeons[current_dungeon_coord] = {} # Мы не генерируем карту подземелья
current_dungeon = active_dungeons[current_dungeon_coord]
# Сохраняем позицию игрока в надземном мире
overworld_player_pos = player_pos.copy()
# Устанавливаем позицию игрока в подземелье
player_pos = entrance_pos.copy()
dungeon_player_pos = player_pos.copy()
Функция для выхода из подземелья
def exit_dungeon():
global state, player_pos, overworld_player_pos
state = 'move'
# Восстанавливаем позицию игрока в надземном мире
player_pos = overworld_player_pos.copy()
Функция для входа в подземелье при приближении к входу
def check_dungeon_entry():
global state
for entrance in current_region.dungeon_entrances:
entrance_rect = pygame.Rect(entrance[0], entrance[1], 40, 40)
player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
if player_rect.colliderect(entrance_rect.inflate(10, 10)): # Приближение
EnterDungeonPrompt(entrance)
break # Только один вход за раз
Функция для отображения приглашения войти в подземелье
def EnterDungeonPrompt(entrance_pos):
global state
prompt_running = True
while prompt_running and state == 'move':
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
# Войти в подземелье
enter_dungeon(entrance_pos)
prompt_running = False
elif event.key == pygame.K_n or event.key == pygame.K_ESCAPE:
# Отказаться
console_message = "Вы отказались войти в подземелье."
prompt_running = False
def equip_item(idx):
global equipped, inventory, console_message, attack, defense
if idx >= len(inventory):
return
item = inventory.pop(idx)
if item['type'] == 'Меч':
if len(equipped['Меч']) < 2:
equipped['Меч'].append(item)
console_message = f"Вы экипировали меч: {item['name']} (Атака +{item['value']})"
else:
console_message = "Максимум два меча уже экипированы."
inventory.insert(idx, item) # Возвращаем предмет обратно
elif item['type'] == 'Щит':
if equipped['Щит']:
console_message = f"Сейчас экипирован щит: {equipped['Щит']['name']}. Смените его перед экипировкой нового щита."
inventory.insert(idx, item) # Возвращаем предмет обратно
else:
equipped['Щит'] = item
console_message = f"Вы экипировали щит: {item['name']} (Защита +{item['value']})"
elif item['type'] == 'Доспех':
if equipped['Доспех']:
console_message = f"Сейчас экипированы доспехи: {equipped['Доспех']['name']}. Смените их перед экипировкой новых доспехов."
inventory.insert(idx, item) # Возвращаем предмет обратно
else:
equipped['Доспех'] = item
console_message = f"Вы экипировали доспехи: {item['name']} (Защита +{item['value']})"
recalculate_stats()
Функция для перехода между регионами
def move_to_region(new_x, new_y, direction):
global current_region_coord, current_region, player_pos, console_message, state
# Проверяем границы мира
if not (0 <= new_x < WORLD_SIZE and 0 <= new_y < WORLD_SIZE):
console_message = "Вы не можете выйти за пределы мира."
return
code
# Устанавливаем новую позицию игрока в зависимости от направления
if direction == 'right':
player_pos[0] = 0
elif direction == 'left':
player_pos[0] = GAME_WIDTH - player_size
elif direction == 'down':
player_pos[1] = 0
elif direction == 'up':
player_pos[1] = GAME_HEIGHT - player_size
current_region_coord = (new_x, new_y)
current_region = get_region(*current_region_coord)
assign_territories(new_x, new_y)
console_message = "Вы перешли в новый регион."
Основной игровой цикл
def main():
global state, player_pos, current_dungeon, inventory_scroll_offset
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Обработка прокрутки инвентаря
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4: # Прокрутка вверх
inventory_scroll_offset = max(inventory_scroll_offset - SCROLL_STEP, 0)
elif event.button == 5: # Прокрутка вниз
inventory_scroll_offset += SCROLL_STEP
code
keys = pygame.key.get_pressed()
if state == 'move':
movement = False
direction = None
new_x, new_y = current_region_coord
# Перемещение игрока
if keys[pygame.K_LEFT]:
if player_pos[0] - player_speed > 0:
player_pos[0] -= player_speed
else:
# Переход в левый регион
new_x = current_region_coord[0] -1
new_y = current_region_coord[1]
direction = 'left'
movement = True
if keys[pygame.K_RIGHT]:
if player_pos[0] + player_speed + player_size < GAME_WIDTH:
player_pos[0] += player_speed
else:
# Переход в правый регион
new_x = current_region_coord[0] +1
new_y = current_region_coord[1]
direction = 'right'
movement = True
if keys[pygame.K_UP]:
if player_pos[1] - player_speed > 0:
player_pos[1] -= player_speed
else:
# Переход в верхний регион
new_x = current_region_coord[0]
new_y = current_region_coord[1] -1
direction = 'up'
movement = True
if keys[pygame.K_DOWN]:
if player_pos[1] + player_speed + player_size < GAME_HEIGHT:
player_pos[1] += player_speed
else:
# Переход в нижний регион
new_x = current_region_coord[0]
new_y = current_region_coord[1] +1
direction = 'down'
movement = True
if movement:
move_to_region(new_x, new_y, direction)
# Проверка на вход в подземелье
check_dungeon_entry()
# Проверяем близость к NPC
for npc in current_region.npcs:
player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
npc_rect = pygame.Rect(npc['pos'][0], npc['pos'][1], 50, 50)
# Взаимодействие только при близком расстоянии
if player_rect.colliderect(npc_rect.inflate(6, 6)) and not npc['task_solved'] and time.time() >= npc['cooldown']:
interact_with_npc(npc)
break # Один NPC за раз
elif state == 'dungeon':
# Управление внутри подземелья
if keys[pygame.K_LEFT] and player_pos[0] - player_speed > 10:
player_pos[0] -= player_speed
if keys[pygame.K_RIGHT] and player_pos[0] + player_speed + player_size < GAME_WIDTH - 10:
player_pos[0] += player_speed
if keys[pygame.K_UP] and player_pos[1] - player_speed > 10:
player_pos[1] -= player_speed
if keys[pygame.K_DOWN] and player_pos[1] + player_speed + player_size < GAME_HEIGHT - 10:
player_pos[1] += player_speed
draw_window()
pygame.quit()
sys.exit()