当我单击按钮时,为什么表面上没有显示str?

时间:2020-05-15 23:12:27

标签: python pygame

************为什么我单击按钮时屏幕上不显示字符串(ping)(表面(win))? 它显示了几乎几秒钟,它消失了我的代码,我不知道问题出在哪里,谁可以帮助我? (qfdfqsdfqdqqqqqqqqdqdfqsdsdffsqfqfsfqsdfsqfqfsqfsq)这仅仅是为了发布这个问题的能力**********

import pygame


pygame.init()
win = pygame.display.set_mode((384,539))
win.fill((0,0,0))
black_color = (0,0,0)
white_color = (255,255,255)
gris_color = (59,59,59)
red = (229,67,45)
yellow = (236,199,76)


pygame.display.set_caption("Tunisian Internet Speed")

police = pygame.font.SysFont("arial", 40)



class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,win,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x,self.y,self.width,self.height))

        pygame.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),2)

        if self.text != '':
            font = pygame.font.SysFont('arial', 60)
            text = font.render(self.text, 1, red)
            win.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
        return False

def redrawWindow():
    win.fill(gris_color)
    blueButton.draw(win)


run = True
blueButton = button(yellow,65,200,250,100,"Start")

while run:

    pygame.display.flip()
    redrawWindow()
    rec1 = pygame.Rect(0,400,100,150)
    pygame.draw.rect(win, yellow, rec1,2)
    rec2 = pygame.Rect(100,400,142,155)
    pygame.draw.rect(win, yellow, rec2,2)
    rec3 = pygame.Rect(241,400,142,155)
    pygame.draw.rect(win,yellow, rec3,2)
    rec4 = pygame.Rect(284,0,100,25)
    pygame.draw.rect(win, yellow, rec4,2)

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            text_ping = police.render("Ping", True, white_color)
            win.blit(text_ping,[5,300])
            pygame.display.update()
            pygame.display.flip()
            if blueButton.isOver(pos):
                print('clicked Button')
                pygame.display.flip()
                text_ping = police.render("Ping", True, white_color)
                win.blit(text_ping,[5,300])
                pygame.display.update()
                pygame.display.flip()





        if event.type == pygame.MOUSEMOTION:
            if blueButton.isOver(pos):
                blueButton.color = red
            else:
                blueButton.color = yellow


pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

首先,pygame.display.update()pygame.display.flip()都做相同的事情,您不需要两者。另外,您还应该在游戏循环中仅使用其中之一,因为这可能会导致卡顿。

单击按钮后,您希望“ ping”保持在屏幕上。目前,您正在每帧清除屏幕,因此您需要每帧绘制一次,因此在按钮类中创建一个名为clicked的变量

class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.clicked = False #right here

,当您单击按钮时,将其设置为True

if blueButton.isOver(pos):
    print('clicked Button')
    blueButton.clicked = True

并在“ Ping”为真时绘制

redrawWindow()
rec1 = pygame.Rect(0,400,100,150)
pygame.draw.rect(win, yellow, rec1,2)
rec2 = pygame.Rect(100,400,142,155)
pygame.draw.rect(win, yellow, rec2,2)
rec3 = pygame.Rect(241,400,142,155)
pygame.draw.rect(win,yellow, rec3,2)
rec4 = pygame.Rect(284,0,100,25)
pygame.draw.rect(win, yellow, rec4,2)

if blueButton.clicked:  #if you clicked the button
    text_ping = police.render("Ping", True, white_color) #draw the text
    win.blit(text_ping,[5,300])  

现在,因为我们在这里绘制按钮,再次绘制它没有任何作用,只是稍微降低了计算机速度,所以让它摆脱吧

if event.type == pygame.MOUSEBUTTONDOWN:

    if blueButton.isOver(pos):
        print('clicked Button')
        blueButton.clicked = True

如果您希望按钮进行切换,则可以将blueButton.clicked = True更改为blueButton.clicked = not blueButton.clicked

现在您的程序可以运行了,并且看起来更干净

import pygame


pygame.init()
win = pygame.display.set_mode((384,539))
win.fill((0,0,0))
black_color = (0,0,0)
white_color = (255,255,255)
gris_color = (59,59,59)
red = (229,67,45)
yellow = (236,199,76)


pygame.display.set_caption("Tunisian Internet Speed")

police = pygame.font.SysFont("arial", 40)



class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.clicked = False

    def draw(self,win,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x,self.y,self.width,self.height))

        pygame.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),2)

        if self.text != '':
            font = pygame.font.SysFont('arial', 60)
            text = font.render(self.text, 1, red)
            win.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
        return False

def redrawWindow():
    win.fill(gris_color)
    blueButton.draw(win)


run = True
blueButton = button(yellow,65,200,250,100,"Start")

while run:

    redrawWindow()
    rec1 = pygame.Rect(0,400,100,150)
    pygame.draw.rect(win, yellow, rec1,2)
    rec2 = pygame.Rect(100,400,142,155)
    pygame.draw.rect(win, yellow, rec2,2)
    rec3 = pygame.Rect(241,400,142,155)
    pygame.draw.rect(win,yellow, rec3,2)
    rec4 = pygame.Rect(284,0,100,25)
    pygame.draw.rect(win, yellow, rec4,2)

    if blueButton.clicked:
        text_ping = police.render("Ping", True, white_color)
        win.blit(text_ping,[5,300])        

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:

            if blueButton.isOver(pos):
                print('clicked Button')
                blueButton.clicked = not blueButton.clicked

        if event.type == pygame.MOUSEMOTION:
            if blueButton.isOver(pos):
                blueButton.color = red
            else:
                blueButton.color = yellow


    pygame.display.update()