单击按钮后如何清除屏幕?

时间:2017-06-11 03:50:35

标签: python python-3.x button pygame

我正在尝试构建一个基于菜单的程序,如果我点击一个按钮,它就对应一个函数;它会执行某些条件。但是,我无法让按钮本身工作。我希望这样,当我点击按钮时,屏幕会填充另一种颜色,这样我就可以拥有另一个屏幕,无论它点击了什么功能。我在下面添加了我的代码;任何帮助都非常感激!

这是我遇到问题的按钮:

def arrayIntro():
intro = True
while intro:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            mousePosition = pygame.mouse.get_pos()                             

    screen.fill(MUTEDBLUE)

    textChoice = pygame.font.SysFont('americantypewriter', 20)

    mousePosition = pygame.mouse.get_pos()
    mouseClick = pygame.mouse.get_pressed()

#Top Row Draw Rects        

    if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
        pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
    else:
        pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))

    enterInt = textChoice.render("enterInt", True, NAVYBLUE)
    screen.blit(enterInt, (135, 90))

    if mouseClick[0] ==1:
        screen.fill(MUTEDBLUE)

但是如果有必要,这是我的所有代码:

 #Import the necessary modules
import pygame
import sys
import os

#Initialize pygame
pygame.init()

# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)

# Set the caption for the screen
pygame.display.set_caption("Array")

#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)   


def selectedFirst():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def enterInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def display():
    runSecond = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def countInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def displayReverse():

    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def sumInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def average():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def findMaxORfindMin():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def search():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def writeToText():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def fillFromText():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def searchReplace():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def shuffleArray():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def wordCount():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def arrayIntro():
    buttonenterInt = pygame.Rect(50, 50, 250, 100)
    buttondisplay =pygame.Rect(325, 50, 250, 100)
    buttoncountInt = pygame.Rect(600, 50, 250, 100)

    buttondisplayReverse = pygame.Rect(50, 250, 250, 100)
    buttonsumInt = pygame.Rect(325, 250, 250, 100)
    buttonaverage = pygame.Rect(600, 250, 250, 100)

    buttonfindMaxORfindMin = pygame.Rect(50, 450, 250, 100)
    buttonsearch = pygame.Rect(325, 450, 250, 100)
    buttonwriteToText= pygame.Rect(600, 450, 250, 100)

    buttonfillFromText =pygame.Rect(50, 640, 250, 100)
    buttonsearchReplace = pygame.Rect(325, 640, 250, 100)
    buttonwordCount = pygame.Rect(600, 640, 250, 100)

    intro = True
    while intro:
        mousePosition = pygame.mouse.get_pos()
        screen.fill(MUTEDBLUE)
        textChoice = pygame.font.SysFont('americantypewriter', 20)

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

            if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
                clicked = pygame.mouse.get_pos()

                if buttonenterInt.collidepoint(clicked):
                    selectedFirst()

                elif buttondisplay.collidepoint(clicked):
                    display()

                elif buttoncountInt.collidepoint(clicked):
                    countInt()

                elif buttondisplayReverse.collidepoint(clicked):
                    displayReverse()

                elif buttonsumInt.collidepoint(clicked):
                    sumInt()

                elif buttonaverage.collidepoint(clicked):
                    average()

                elif buttonfindMaxORfindMin.collidepoint(clicked):
                    findMaxORfindMin()

                elif buttonsearch.collidepoint(clicked):
                    search()

                elif buttonwriteToText.collidepoint(clicked):
                    writeToText()

                elif buttonfillFromText.collidepoint(clicked):
                    fillFromText()

                elif buttonsearchReplace.collidepoint(clicked):
                    searchReplace()

                elif buttonshuffleArray.collidepoint(clicked):
                    shuffleArray()

                elif buttonwordCount.collidepoint(clicked):
                    wordCount() 
#########################################################################################
#Top Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))

        enterInt = textChoice.render("enterInt", True, NAVYBLUE)
        screen.blit(enterInt, (135, 90))

        if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))

        display = textChoice.render("display", True, NAVYBLUE)
        screen.blit(display, (414, 90))

        if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))

        countInt = textChoice.render("countInt", True, NAVYBLUE)
        screen.blit(countInt, (680, 90))

#########################################################################################
#Middle Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))

        displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
        screen.blit(displayReverse, (102, 287))

        if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))

        sumInt = textChoice.render("sumInt", True, NAVYBLUE)
        screen.blit(sumInt, (410, 287))

        if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))

        average = textChoice.render("average", True, NAVYBLUE)
        screen.blit(average, (685, 287))

#########################################################################################
#Middle Bottom Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))

        findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
        screen.blit(findMaxORfindMin, (95, 488))

        if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))

        search = textChoice.render("search", True, NAVYBLUE)
        screen.blit(search, (417, 488))

        if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))

        writeToText = textChoice.render("writeToText", True, NAVYBLUE)
        screen.blit(writeToText, (665, 488))

#########################################################################################
#Bottom Row Draw Rects

        if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))

        fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
        screen.blit(fillFromText, (115, 680))

        if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))

        searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
        screen.blit(searchReplace, (382, 680))

        if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))

        wordCount = textChoice.render("wordCount", True, NAVYBLUE)
        screen.blit(wordCount, (670, 680))

        pygame.display.update()
#########################################################################################
    eval(selected)

arrayIntro()

1 个答案:

答案 0 :(得分:1)

while循环继续无限期运行。在当前代码中,您检测到鼠标单击,然后使屏幕变为蓝色,但只是暂时,然后它继续到下一个功能,直到它到达结束并且循环继续,从而完全发生在上一次迭代中发生的事情。

请看下面的代码。我改了一下

#Import the necessary modules
import pygame
import sys
import os

#Initialize pygame
pygame.init()

# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)

# Set the caption for the screen
pygame.display.set_caption("Array")

#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)

#function here
def selectedFirst():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def arrayIntro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONUP:
                mousePosition = pygame.mouse.get_pos()

        screen.fill(MUTEDBLUE)

        textChoice = pygame.font.SysFont('americantypewriter', 20)

        mousePosition = pygame.mouse.get_pos()
        mouseClick = pygame.mouse.get_pressed()

#Top Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))

        enterInt = textChoice.render("enterInt", True, NAVYBLUE)
        screen.blit(enterInt, (135, 90))

        if mouseClick[0] ==1:
            selectedFirst() #change here. Made a new function temporarily to be executed on the click


        if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))

        display = textChoice.render("display", True, NAVYBLUE)
        screen.blit(display, (414, 90))

        if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))

        countInt = textChoice.render("countInt", True, NAVYBLUE)
        screen.blit(countInt, (680, 90))

#Middle Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))

        displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
        screen.blit(displayReverse, (102, 287))

        if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))

        sumInt = textChoice.render("sumInt", True, NAVYBLUE)
        screen.blit(sumInt, (410, 287))

        if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))

        average = textChoice.render("average", True, NAVYBLUE)
        screen.blit(average, (685, 287))

#Middle Bottom Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))

        findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
        screen.blit(findMaxORfindMin, (95, 488))

        if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))

        search = textChoice.render("search", True, NAVYBLUE)
        screen.blit(search, (417, 488))

        if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))

        writeToText = textChoice.render("writeToText", True, NAVYBLUE)
        screen.blit(writeToText, (665, 488))

#Bottom Row Draw Rects

        if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))

        fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
        screen.blit(fillFromText, (115, 680))

        if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))

        searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
        screen.blit(searchReplace, (382, 680))

        if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))

        wordCount = textChoice.render("wordCount", True, NAVYBLUE)
        screen.blit(wordCount, (670, 680))

        pygame.display.update()

arrayIntro()

它评论我改变的地方是......

定义了一个新的函数

def selectedFirst():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

单击时调用此方法

        if mouseClick[0] ==1:
            selectedFirst()

尝试一下。

http://imgur.com/a/wZkI3

编辑:你不需要写这么大的代码。它可以通过在单个函数中循环blitting和鼠标跟踪来减少

相关问题