【问题标题】:Button draws image for only a second按钮仅绘制图像一秒钟
【发布时间】:2020-11-25 04:57:08
【问题描述】:

所以我一直在尝试这样做,所以当我按下按钮时,它会永远绘制我的图像,但什么都没有发生:https://gyazo.com/83017705fa2b3f40ca26112a153791c5 我正在按下我的按钮但没有任何反应,我希望它绘制我的图像并制作我的当我按下按钮时图像会停留

这是我尝试过的

    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
            
    if Abutton.clicked:
        Abutton.clicked = True
        window.blit(A,(0,0))

我的完整代码

import pygame
pygame.init()

# Drawing window screen
window = pygame.display.set_mode((700,500))
# The Name of my window
pygame.display.set_caption("StickMan")
# Drawing the buttons
A = pygame.image.load("A.png")
A2 = pygame.image.load("A1.png")

# Button class
class button1():
    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,window,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
 
        pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
 
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0,0,0))
            window.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 playSoundIfMouseIsOver(self, pos, sound):
        if self.isOver(pos):            
            if not self.over:
                eat.play()
                self.over = True
        else:
            self.over = False

# Color
white = (255,255,255)


def redrawwindow():
    window.fill((0,0,0))

    

    
    Abutton = button1((0,255,0),287,310,55,55, '')

    Abutton.draw(window)

    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
            
    if Abutton.clicked:
        Abutton.clicked = True
        window.blit(A,(0,0))



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


        

    redrawwindow()
    pygame.display.update()
pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    在这部分代码中:

        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
                
            if Abutton.isOver(pos):
                window.blit(A,(0,0))
    

    您是在告诉 pygame 仅在您单击时绘制 A。只需设置一个变量,如self.clicked = False,然后在if 语句中添加self.clicked = True。然后,不要在if 块中绘制A,而是将其移出:

    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
            
    if self.clicked:
        window.blit(A,(0,0))
    

    如果您只想在鼠标悬停在按钮上时显示图像,只需将event.type == pygame.MOUSEBUTTONDOWN 更改为event.type == pygame.MOUSEMOTION


    问题已编辑;新答案:

    主要问题是您在redrawwindow 函数内部定义了Abutton。 因为您在 for 循环的每次迭代期间调用该函数,所以 self.clicked 变量丢失,因为在 while 循环的每次迭代中,Abutton 都被重新定义 将self.clicked 设置为False

    提示:在您的isOver 函数中,您可以直接返回表达式,因为表达式返回一个布尔值。

    工作代码:

    import pygame
    pygame.init()
    
    # Drawing window screen
    window = pygame.display.set_mode((700,500))
    # The Name of my window
    pygame.display.set_caption("StickMan")
    # Drawing the buttons
    A = pygame.image.load("A.png")
    A2 = pygame.image.load("A1.png")
    
    # Button class
    class button1():
        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,window,outline=None):
            #Call this method to draw the button on the screen
            if outline:
                pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
     
            pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
     
            if self.text != '':
                font = pygame.font.SysFont('comicsans', 60)
                text = font.render(self.text, 1, (0,0,0))
                window.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
            return pos[0] > self.x and pos[0] < self.x + self.width and pos[1] > self.y and pos[1] < self.y + self.height
     
        def playSoundIfMouseIsOver(self, pos, sound):
            if self.isOver(pos):            
                if not self.over:
                    eat.play()
                    self.over = True
            else:
                self.over = False
    
    # Color
    white = (255,255,255)
    Abutton = button1((0,255,0),287,310,55,55, '')
    
    def redrawwindow():
        window.fill((0,0,0))
        Abutton.draw(window)
        if Abutton.clicked:
            window.blit(A,(0,0))
    
    
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if Abutton.isOver(pos):
                    Abutton.clicked = True
        redrawwindow()
        pygame.display.update()
    pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多