如何在Pygame中创建级别系统?

发布于 2025-01-23 05:00:58 字数 2553 浏览 3 评论 0原文

我正在制作一个简单的数学游戏,其中有5个问题的设定顺序。我目前仅将前两个问题作为功能以及基于玩家答案所调用的“胜利”和“损失”功能实现。但是,我无法弄清楚如何建立适当的级别系统。这是我实施的四个函数的当前代码:

def victory():
    global num_correct
    global time

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        victory_text = font.render("Correct!", True, dark_blue)

        time -= 1
        timer.tick(100)
        if time <= 0:
            second_problem()

        score_display(30, 25)
        screen.blit(victory_text, (325, 240))
        pygame.display.update()

# when incorrect answer is chosen, call this function 
def loss():
    global num_correct
    global time

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        loss_text = font.render("Incorrect!", True, dark_blue)

        time -= 1
        timer.tick(100)
        if time <= 0:
            second_problem()

        score_display(30, 25)
        screen.blit(loss_text, (310, 240))
        pygame.display.update()

# first math problem (9 - 6) + 3 = 6
def first_problem():
    global num_correct

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        gametext = font.render("(9 - 6) + 3 = ", True, dark_blue)
        if six.draw(screen):
            print("true")
            num_correct += 1
            victory()

        if eight.draw(screen):
            print("false")
            loss()

        score_display(30, 25)
        screen.blit(gametext, (290, 75))
        pygame.display.update()

# second math problem 3 + (4 * 1) = 7
def second_problem():
    global num_correct

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        gametext = font.render("3 + (4 * 1) = ", True, dark_blue)
        if seven.draw(screen):
            print("true")
            num_correct += 1
            victory()

        if nine.draw(screen):
            print("false")
            loss()

        score_display(30, 25)
        screen.blit(gametext, (290, 75))
        pygame.display.update()

当前“胜利”和“损失”仅涉及第二个问题。我计划总共实施5个。如何创建一个级别的系统来调用设定顺序的问题?

I am making a simple math game that has a set order of 5 questions. I currently only have the first two problems implemented as functions as well as "victory" and "loss" functions that are called based on the player answer. However, I cannot figure out how to make a proper level system. Here is the current code for the four functions I have implemented:

def victory():
    global num_correct
    global time

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        victory_text = font.render("Correct!", True, dark_blue)

        time -= 1
        timer.tick(100)
        if time <= 0:
            second_problem()

        score_display(30, 25)
        screen.blit(victory_text, (325, 240))
        pygame.display.update()

# when incorrect answer is chosen, call this function 
def loss():
    global num_correct
    global time

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        loss_text = font.render("Incorrect!", True, dark_blue)

        time -= 1
        timer.tick(100)
        if time <= 0:
            second_problem()

        score_display(30, 25)
        screen.blit(loss_text, (310, 240))
        pygame.display.update()

# first math problem (9 - 6) + 3 = 6
def first_problem():
    global num_correct

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        gametext = font.render("(9 - 6) + 3 = ", True, dark_blue)
        if six.draw(screen):
            print("true")
            num_correct += 1
            victory()

        if eight.draw(screen):
            print("false")
            loss()

        score_display(30, 25)
        screen.blit(gametext, (290, 75))
        pygame.display.update()

# second math problem 3 + (4 * 1) = 7
def second_problem():
    global num_correct

    while True:
        screen.fill((150, 200, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        gametext = font.render("3 + (4 * 1) = ", True, dark_blue)
        if seven.draw(screen):
            print("true")
            num_correct += 1
            victory()

        if nine.draw(screen):
            print("false")
            loss()

        score_display(30, 25)
        screen.blit(gametext, (290, 75))
        pygame.display.update()

Currently "victory" and "loss" only advance to the second problem. I plan to implement 5 in total. How can I create a level system of sorts to call upon the problems in a set order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2025-01-30 05:00:58

您可以减少代码。而不是first_problem()second_problem()您可以使用一个具有不同值的函数

problem("3 + (4 * 1) = ", "7", "9")
problem("(9 - 6) + 3 = ", "6", "9")

,并且函数应返回 true 或false代码>代替运行victor()lose()

,然后您可以将值保留在列表上,并使用 loop - loop,

all_problems =  [
    # (question, answer1, answer2)
    ("(9 - 6) + 3 = ", "6", "9"),
    ("3 + (4 * 1) = ", "7", "9"),
]

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        victory()
    else:
        loss()
        break   # exit when wrong answer

您将减少<<。代码> victor()和lose()带有参数消息('corment!')message('Corrent!'),然后您可以进行

all_problems =  [
    # (question, answer1, answer2)
    ("(9 - 6) + 3 = ", "6", "9"),
    ("3 + (4 * 1) = ", "7", "9"),
]

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        message('Corrent!')
    else:
        message('Incorrent!')
        break   # exit when wrong answer

# - end -

message(f"Final Result: {score}")

完整的工作代码,并进行更多更改。

import pygame

# --- constants ---

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

RED   = (255, 0, 0)
GREEM = (0, 255, 0)
BLUE   = (0, 0, 255)

DARK_BLUE = (0, 0, 255)

# --- classes ---

class Button:
    
    def __init__(self, answer, correct, x, y):
        self.answer = answer
        self.correct = correct
        
        self.image = pygame.Surface((100, 100))
        self.image.fill(WHITE)
        self.rect  = self.image.get_rect(centerx=x, centery=y)
        
        self.text_image = font.render(answer, True, DARK_BLUE)
        self.text_rect  = self.text_image.get_rect(center=self.rect.center)
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        screen.blit(self.text_image, self.text_rect)
    
    def on_mouse_clicked(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            return self.rect.collidepoint(event.pos)
        
    def on_mouse_motion(self, event):
        if event.type == pygame.MOUSEMOTION:
            if self.rect.collidepoint(event.pos):
                self.image.fill(RED)
            else:
                self.image.fill(WHITE)
        
# --- functions ---

def score_display(screen, value, x, y):
    
    text = f'Score: {value}'
    
    text_image = font.render(text, True, DARK_BLUE)
    text_rect  = text_image.get_rect(x=x, y=y)

    screen.blit(text_image, text_rect)

def message(text, time=2):
    
    text_image = font.render(text, True, DARK_BLUE)
    text_rect  = text_image.get_rect()
    text_rect.center = screen.get_rect().center

    screen.fill((150, 200, 255))
    score_display(screen, score, 30, 25)
    screen.blit(text_image, text_rect)
    pygame.display.update()

    time = time * 10
    
    while time >= 0:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return

        time -= 1
        timer.tick(10)  # 20*10 = 2 seconds


def problem(question, answer1, answer2, correct1, correct2):

    centerx, centery = screen.get_rect().center

    text_image = font.render(question, True, DARK_BLUE)
    text_rect  = text_image.get_rect()
    
    text_rect.centerx = centerx
    text_rect.centery = centery - 100
    
    button1 = Button(answer1, correct1, centerx-100, centery+100)
    button2 = Button(answer2, correct2, centerx+100, centery+100)
    
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if button1.on_mouse_clicked(event):
                    return button1.correct
                if button2.on_mouse_clicked(event):
                    return button2.correct
            elif event.type == pygame.MOUSEMOTION:
                button1.on_mouse_motion(event)
                button2.on_mouse_motion(event)
                    
        screen.fill((150, 200, 255))
        score_display(screen, score, 30, 25)
        screen.blit(text_image, text_rect)
        button1.draw(screen)
        button2.draw(screen)
        pygame.display.update()

        timer.tick(10)

# --- main ---

all_problems =  [
    # (question, answer1, answer2, correct1, correct2)
    ("(9 - 6) + 3 = ", "6", "9", True, False),
    ("3 + (4 * 1) = ", "7", "9", True, False),
]

pygame.init()

screen = pygame.display.set_mode((800,600))
font = pygame.font.SysFont(None, 50)

timer = pygame.time.Clock()

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        message('Corrent!')
    else:
        message('Incorrent!')
        break

message(f"Final Result: {score}")
pygame.quit()

”在此处输入图像说明”

You could reduce your code. Instead of first_problem() and second_problem() you could use one function with different values

problem("3 + (4 * 1) = ", "7", "9")
problem("(9 - 6) + 3 = ", "6", "9")

And function should return True or False instead of running victory() or loss()

And then you could keep values on list and use for-loop

all_problems =  [
    # (question, answer1, answer2)
    ("(9 - 6) + 3 = ", "6", "9"),
    ("3 + (4 * 1) = ", "7", "9"),
]

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        victory()
    else:
        loss()
        break   # exit when wrong answer

You would reduce victory() and loss() to one function with parameter message('Corrent!'), message('Corrent!') and then you could do

all_problems =  [
    # (question, answer1, answer2)
    ("(9 - 6) + 3 = ", "6", "9"),
    ("3 + (4 * 1) = ", "7", "9"),
]

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        message('Corrent!')
    else:
        message('Incorrent!')
        break   # exit when wrong answer

# - end -

message(f"Final Result: {score}")

Full working code with more changes.

import pygame

# --- constants ---

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

RED   = (255, 0, 0)
GREEM = (0, 255, 0)
BLUE   = (0, 0, 255)

DARK_BLUE = (0, 0, 255)

# --- classes ---

class Button:
    
    def __init__(self, answer, correct, x, y):
        self.answer = answer
        self.correct = correct
        
        self.image = pygame.Surface((100, 100))
        self.image.fill(WHITE)
        self.rect  = self.image.get_rect(centerx=x, centery=y)
        
        self.text_image = font.render(answer, True, DARK_BLUE)
        self.text_rect  = self.text_image.get_rect(center=self.rect.center)
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        screen.blit(self.text_image, self.text_rect)
    
    def on_mouse_clicked(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            return self.rect.collidepoint(event.pos)
        
    def on_mouse_motion(self, event):
        if event.type == pygame.MOUSEMOTION:
            if self.rect.collidepoint(event.pos):
                self.image.fill(RED)
            else:
                self.image.fill(WHITE)
        
# --- functions ---

def score_display(screen, value, x, y):
    
    text = f'Score: {value}'
    
    text_image = font.render(text, True, DARK_BLUE)
    text_rect  = text_image.get_rect(x=x, y=y)

    screen.blit(text_image, text_rect)

def message(text, time=2):
    
    text_image = font.render(text, True, DARK_BLUE)
    text_rect  = text_image.get_rect()
    text_rect.center = screen.get_rect().center

    screen.fill((150, 200, 255))
    score_display(screen, score, 30, 25)
    screen.blit(text_image, text_rect)
    pygame.display.update()

    time = time * 10
    
    while time >= 0:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return

        time -= 1
        timer.tick(10)  # 20*10 = 2 seconds


def problem(question, answer1, answer2, correct1, correct2):

    centerx, centery = screen.get_rect().center

    text_image = font.render(question, True, DARK_BLUE)
    text_rect  = text_image.get_rect()
    
    text_rect.centerx = centerx
    text_rect.centery = centery - 100
    
    button1 = Button(answer1, correct1, centerx-100, centery+100)
    button2 = Button(answer2, correct2, centerx+100, centery+100)
    
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if button1.on_mouse_clicked(event):
                    return button1.correct
                if button2.on_mouse_clicked(event):
                    return button2.correct
            elif event.type == pygame.MOUSEMOTION:
                button1.on_mouse_motion(event)
                button2.on_mouse_motion(event)
                    
        screen.fill((150, 200, 255))
        score_display(screen, score, 30, 25)
        screen.blit(text_image, text_rect)
        button1.draw(screen)
        button2.draw(screen)
        pygame.display.update()

        timer.tick(10)

# --- main ---

all_problems =  [
    # (question, answer1, answer2, correct1, correct2)
    ("(9 - 6) + 3 = ", "6", "9", True, False),
    ("3 + (4 * 1) = ", "7", "9", True, False),
]

pygame.init()

screen = pygame.display.set_mode((800,600))
font = pygame.font.SysFont(None, 50)

timer = pygame.time.Clock()

score = 0

for data in all_problems:
    result = problem(*data)
    if result:
        score += 1
        message('Corrent!')
    else:
        message('Incorrent!')
        break

message(f"Final Result: {score}")
pygame.quit()

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文