Игра на PyGame сразу же закрывается, перед этим показывая приветственное сообщение

Рейтинг: 0Ответов: 0Опубликовано: 25.02.2023
import pygame
import random

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)

# Set the screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Define the number of lives
NUM_LIVES = 3

# Initialize Pygame
pygame.init()

# Set the font
font = pygame.font.Font(None, 36)

# Load the questions and answers from files
with open("questions.txt", "r") as qfile:
    questions = qfile.readlines()
with open("answers.txt", "r") as afile:
    answers = afile.readlines()

# Initialize the game
def init_game():
    # Initialize the screen
    screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
    pygame.display.set_caption("Dungeon Quiz")

    # Initialize the game state
    lives = NUM_LIVES
    question_index = 0

    # Shuffle the questions and answers
    combined = list(zip(questions, answers))
    random.shuffle(combined)
    questions, answers = zip(*combined)

    return screen, lives, question_index, questions, answers

# Draw the screen
def draw_screen(screen, lives, question):
    # Clear the screen
    screen.fill(BLACK)

    # Draw the question text
    question_text = font.render(question, True, WHITE)
    question_rect = question_text.get_rect()
    question_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 50)
    screen.blit(question_text, question_rect)

    # Draw the lives text
    lives_text = font.render("Lives: " + str(lives), True, WHITE)
    lives_rect = lives_text.get_rect()
    lives_rect.topright = (SCREEN_WIDTH - 10, 10)
    screen.blit(lives_text, lives_rect)

    # Update the display
    pygame.display.flip()

# Handle input events
def handle_input(lives, question_index, questions, answers):
    # Wait for an event
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # Quit the game
                pygame.quit()
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    # Quit the game
                    pygame.quit()
                    exit()
                elif event.key == pygame.K_RETURN:
                    # Check the answer
                    answer = input_box.text.lower().strip()
                    correct_answer = answers[question_index].lower().strip()
                    if answer == correct_answer:
                        # Correct answer - move on to the next question
                        question_index += 1
                        if question_index == len(questions):
                            # Passed the game
                            print("Congratulations, you passed!")
                            pygame.quit()
                            exit()
                    else:
                        # Incorrect answer - lose a life
                        lives -= 1
                        if lives == 0:
                            # Game over
                            print("Game over")
                            pygame.quit()
                            exit()
                    # Clear the input box
                    input_box.text = ""
                    return lives, question_index
        # Update the input box
        input_box.update()

# Define the input box class
class InputBox:
    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = GRAY
        self.text = text
        self

Ответы

Ответов пока нет.