How i show the number on screen in pygame and check answer of operation

Viewed 32

This is Code:

import random
import pygame 
import sys
#setting
pygame.init()
screen = pygame.display.set_mode((1000,600))
pygame.display.set_caption("Math Game")
icon = pygame.image.load("img/bg.jpg")
pygame.display.set_icon(icon)
bg = pygame.image.load("img/back2.jpg")
playerImage = pygame.image.load("play.png")
player1 = pygame.transform.scale(playerImage,(150,190))
#ans_text
base_font = pygame.font.Font(None, 32)
user_text = ''
input_rect = pygame.Rect(445, 420, 140, 32)
color_active = pygame.Color('olive')
color_passive = pygame.Color('brown')
color = color_passive
active = False
#ran_num
operater='+','-','*','/'
x=random.randint(1,100)
y=random.randint(1,100)
w=random.randint(1,5)
z=random.randint(1,50)
op=random.sample(operater,1)

while True:
    for event in pygame.event.get():

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False

        if event.type == pygame.KEYDOWN:

            
            if event.key == pygame.K_BACKSPACE:

                
                user_text = user_text[:-1]

            else:
                user_text += event.unicode

    if active:
        color = color_active
    else:
        color = color_passive

    pygame.draw.rect(screen, color, input_rect)

    text_surface = base_font.render(user_text, True, (0, 0, 0))

    screen.blit(player1,(420,430))
    screen.blit(text_surface, (input_rect.x+5, input_rect.y+5))
    pygame.draw.rect(screen,(255,178,102),(350,70,300,50))
    pygame.display.flip()
    screen.fill((190,180,225))
    input_rect.w = max(100, text_surface.get_width()+10)

    

My code is not look beautiful sorry

this code is made for fun, I want it to run

0 Answers
Related