[Python] Why is my calculater counting up instead of printing a number? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a calculator in pygame. I made a variables for every key. This is the code:

    import pygame

    pygame.font.init()

    width=1200
    height=600
    win=pygame.display.set_mode((width,height))

    pygame.display.set_caption("calculator")

    bg=pygame.Rect(0,0,width,height)

    Npad_w=75
    Npad_h=75

    font=pygame.font.SysFont("comic sans",50)

    def draw(Npad1,Npad2,Npad3,Npad4,Npad5,Npad6,Npad7,Npad8,Npad9,numbers):

    pygame.draw.rect(win, "dark red", bg)
    pygame.draw.rect(win,"pink",Npad1)
    pygame.draw.rect(win,"pink",Npad2)
    pygame.draw.rect(win,"pink",Npad3)
    pygame.draw.rect(win,"pink",Npad4)
    pygame.draw.rect(win,"pink",Npad5)
    pygame.draw.rect(win,"pink",Npad6)
    pygame.draw.rect(win,"pink",Npad7)
    pygame.draw.rect(win,"pink",Npad8)
    pygame.draw.rect(win,"pink",Npad9)

    text=font.render(numbers,1,"white")
    win.blit(text,(width/2-200,height/2-Npad_h*3))

    pygame.display.update()

    def main():
    run = True

    Npad1=pygame.Rect(width/2-300,height/2-Npad_h,Npad_w,Npad_h)
    Npad2=pygame.Rect(width/2-200,height/2-Npad_h,Npad_w,Npad_h)
    Npad3=pygame.Rect(width/2-100,height/2-Npad_h,Npad_w,Npad_h)

    Npad4=pygame.Rect(width/2-300,height/2-Npad_h+100,Npad_w,Npad_h)
    Npad5=pygame.Rect(width/2-200,height/2-Npad_h+100,Npad_w,Npad_h)
    Npad6=pygame.Rect(width/2-100,height/2-Npad_h+100,Npad_w,Npad_h)

    Npad7=pygame.Rect(width/2-300,height/2-Npad_h+200,Npad_w,Npad_h)
    Npad8=pygame.Rect(width/2-200,height/2-Npad_h+200,Npad_w,Npad_h)
    Npad9=pygame.Rect(width/2-100,height/2-Npad_h+200,Npad_w,Npad_h)

    numbers=""

    clock=pygame.time.Clock()

    while run:
    clock.tick(60)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run=False
    break

    if event.type == pygame.MOUSEBUTTONDOWN:
    mouseX,mouseY=pygame.mouse.get_pos()

    mouse=pygame.Rect(10,10,mouseX,mouseY)

    if Npad1.x + Npad_w>=mouseX and Npad1.y + Npad_h>=mouseY :
    numbers += "1"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad2.x+ Npad_w>=mouseX and Npad2.y + Npad_h>=mouseY :
    numbers += "2"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad3.x+ Npad_w>=mouseX and Npad3.y + Npad_h>=mouseY :
    numbers += "3"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad4.x+ Npad_w>=mouseX and Npad4.y + Npad_h>=mouseY :
    numbers += "4"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad5.x+ Npad_w>=mouseX and Npad5.y + Npad_h>=mouseY :
    numbers += "5"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad6.x+ Npad_w>=mouseX and Npad6.y + Npad_h>=mouseY :
    numbers += "6"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad7.x+ Npad_w>=mouseX and Npad7.y + Npad_h>=mouseY :
    numbers += "7"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad8.x+ Npad_w>=mouseX and Npad8.y + Npad_h>=mouseY :
    numbers += "8"
    print(numbers)
    mouseX=0
    mouseY=0

    if Npad9.x+ Npad_w>=mouseX and Npad9.y + Npad_h>=mouseY :
    numbers += "9"
    print(numbers)
    mouseX=0
    mouseY=0

    draw(Npad1,Npad2,Npad3,Npad4,Npad5,Npad6,Npad7,Npad8,Npad9,numbers)

    pygame.quit()

    if __name__ == "__main__":
    main()


    If I press like 1 it starts counting from 1 to 9. I want it to input only 1 and not the rest. The same happens to the rest.

    I tried using pygame.Rect.colliderect but it starts counting from 3 to 9:

    mouse=pygame.Rect(10,10,mouseX,mouseY)

    if Npad1.x + Npad_w>=mouseX and pygame.Rect.colliderect(mouse,Npad1) :
    numbers += "1"
    print(numbers)
    mouseX=0
    mouseY=0


    I'm still a starter in python and have made only 2 more projects with pygame (1 from a tutorial). So if you have any ideas, please help.

    Continue reading...

Compartilhe esta Página