【问题标题】:How to change the rendered numbers on this code如何更改此代码上呈现的数字
【发布时间】:2021-10-21 08:50:37
【问题描述】:

该程序包括生成 2 个随机数,将它们呈现在屏幕上(使用“+”符号,因为它是一个总和)并期望用户将总和的结果放在框条目上。代码已经有一个函数可以生成 2 个随机数(xy),另一个可以在屏幕上呈现它们(除了分数)。代码如下:

import pygame
import random
from InputBox import InputBox

pygame.init()

clock = pygame.time.Clock()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
base_font = pygame.font.Font(None, 32)
user_text = ''
color_active = pygame.Color('lightskyblue3')
running = True
points = 0

def start_the_game():
    x = random.randint(0, 10)
    y = random.randint(0, 10)
    is_correct = False
    return x, y

def display_the_game(x, y):
    # Variables
    z = x + y
    surface.fill((255, 70, 90))
    text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))

    text_surface = base_font.render(user_text, True, (255, 255, 255))
    surface.blit(text, (260, 120))
    input_box.draw(surface)
    punts = font.render("Puntuació: " +  str(points),True, (255,255,255))
    surface.blit(punts, (350,30))

x, y = start_the_game()
input_box = InputBox(190, 250, 200, 32)

while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        else:
            result = input_box.handle_event(event)
            if result != None:
                if int(result) == int(x) + int(y):
                    # TODO: when the user is right
                    points = points + 5
                    start_the_game()
                    pygame.display.update()
                    display_the_game(x, y)
                    pygame.display.update()
                    
                else:
                    # TODO: when the user is wrong
                    start_the_game
                    pygame.display.update()
                    display_the_game(x, y)
                    pygame.display.update()

    display_the_game(x, y)
    pygame.display.update()

pygame.quit()

如果结果正确,我需要程序生成两个新的随机数并渲染它们,除了在右侧出现的“Puntuació”上添加 5 个点(完成)。如果用户错了,它只需要生成2个新的随机数,并渲染它们(分数不必改变)。

这是导入的 InputBox 中的代码。

import pygame


pygame.init()
surface = pygame.display.set_mode((600, 400))
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
FONT = pygame.font.SysFont('comicsans', 32)
base_font = pygame.font.Font(None, 32)
color_active = pygame.Color('lightskyblue3')
user_text = ''


class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    user_input = self.text
                    self.text = ''
                    self.txt_surface = FONT.render(self.text, True, self.color)
                    return user_input
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 2)



def main():
    clock = pygame.time.Clock()
    input_box2 = InputBox(190, 250, 200, 32)
    input_boxes = [input_box2]
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

        for box in input_boxes:
            box.update()

        surface.fill((255, 70, 90))
        for box in input_boxes:
            box.draw(surface)

        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pygame.quit()

【问题讨论】:

    标签: python python-3.x random pygame


    【解决方案1】:

    您所要做的就是为xy 创建新值并重置输入框:

    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            else:
                result = input_box.handle_event(event)
                if result != None:
                    if int(result) == int(x) + int(y):
                        points = points + 5
    
                    # create new random numbers
                    x, y = start_the_game()
    
                    # reset input box (just create a new box)
                    input_box = InputBox(190, 250, 200, 32)
    
        display_the_game(x, y)
        pygame.display.update()
    
    pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 2022-10-22
      • 1970-01-01
      • 2022-10-06
      • 2018-02-22
      • 1970-01-01
      • 2023-01-19
      • 2018-02-11
      • 1970-01-01
      • 2021-02-14
      相关资源
      最近更新 更多