按钮仅绘制图像一秒钟

窗人

所以,我一直在努力使它所以当我按下按钮,它吸引我的形象永远,但什么也没发生: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()

安禅

在这段代码中:

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

您告诉pygame仅A在单击时才绘制只需设置一个变量,像self.clicked = False,并添加self.clicked = Trueif声明。然后,而不是绘制Aif块,将其移出:

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

如果只希望在鼠标悬停在按钮上时显示图像,则只需更改event.type == pygame.MOUSEBUTTONDOWNevent.type == pygame.MOUSEMOTION


问题已编辑;新答案:

主要的问题是您Abutton redrawwindow函数内部定义因为您在循环的每次迭代过程中都调用了该函数for,所以self.clicked变量的目的就消失了,就像在循环的每次迭代过程中一样while,将设置为来Abutton重新定义self.clickedFalse

提示:在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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

骨干视图仅渲染一秒钟

来自分类Dev

如何显示图像一秒钟?SWIFTUI

来自分类Dev

呼叫按钮仅滴答一秒钟,无法显示文字?

来自分类Dev

一秒钟的JavaScript真的是一秒钟吗?

来自分类Dev

一秒钟的JavaScript真的是一秒钟吗?

来自分类Dev

使用X11窗口的R脚本仅打开一秒钟

来自分类Dev

使用X11窗口的R脚本仅打开一秒钟

来自分类Dev

Javascript验证-错误消息仅闪烁一秒钟

来自分类Dev

每隔一秒钟从Observable获取数据,使用“开始”和“停止”按钮

来自分类Dev

Android,Glide显示错误的图像大约一秒钟

来自分类Dev

一秒钟后如何在Android Studio中自动更改图像?

来自分类Dev

一秒钟内查询数量很高。

来自分类Dev

一秒钟后边界半径消失

来自分类Dev

一秒钟后悬停消失

来自分类Dev

需要循环睡眠一秒钟的时间

来自分类Dev

一秒钟后边界半径消失

来自分类Dev

花费一秒钟锁定屏幕

来自分类Dev

显示登录页面一秒钟

来自分类Dev

录制一秒钟的视频Swift?

来自分类Dev

“不要保留活动”-恢复应用程序后,该片段仅显示一秒钟

来自分类Dev

如何在Java中仅一秒钟更改文本框的背景颜色?

来自分类Dev

tmux状态栏消息仅持续一秒钟:我可以扩展它吗?

来自分类Dev

软件在任务栏中仅闪烁一秒钟。如何找出它是什么软件?

来自分类Dev

仅当用户在页面上停留一秒钟以上时,才检索数据

来自分类Dev

如何在Java中仅一秒钟更改文本框的背景颜色?

来自分类Dev

“不保留活动”-恢复应用程序后,该片段仅显示一秒钟

来自分类Dev

从CSS角度来看,单击,按住一秒钟然后拖走时,输入按钮处于什么状态?

来自分类Dev

SwiftUi-隐藏“后退”按钮和导航栏(显示时间不到一秒钟)

来自分类Dev

如果检索时间超过一秒钟,如何将按钮更改为“禁用”?

Related 相关文章

  1. 1

    骨干视图仅渲染一秒钟

  2. 2

    如何显示图像一秒钟?SWIFTUI

  3. 3

    呼叫按钮仅滴答一秒钟,无法显示文字?

  4. 4

    一秒钟的JavaScript真的是一秒钟吗?

  5. 5

    一秒钟的JavaScript真的是一秒钟吗?

  6. 6

    使用X11窗口的R脚本仅打开一秒钟

  7. 7

    使用X11窗口的R脚本仅打开一秒钟

  8. 8

    Javascript验证-错误消息仅闪烁一秒钟

  9. 9

    每隔一秒钟从Observable获取数据,使用“开始”和“停止”按钮

  10. 10

    Android,Glide显示错误的图像大约一秒钟

  11. 11

    一秒钟后如何在Android Studio中自动更改图像?

  12. 12

    一秒钟内查询数量很高。

  13. 13

    一秒钟后边界半径消失

  14. 14

    一秒钟后悬停消失

  15. 15

    需要循环睡眠一秒钟的时间

  16. 16

    一秒钟后边界半径消失

  17. 17

    花费一秒钟锁定屏幕

  18. 18

    显示登录页面一秒钟

  19. 19

    录制一秒钟的视频Swift?

  20. 20

    “不要保留活动”-恢复应用程序后,该片段仅显示一秒钟

  21. 21

    如何在Java中仅一秒钟更改文本框的背景颜色?

  22. 22

    tmux状态栏消息仅持续一秒钟:我可以扩展它吗?

  23. 23

    软件在任务栏中仅闪烁一秒钟。如何找出它是什么软件?

  24. 24

    仅当用户在页面上停留一秒钟以上时,才检索数据

  25. 25

    如何在Java中仅一秒钟更改文本框的背景颜色?

  26. 26

    “不保留活动”-恢复应用程序后,该片段仅显示一秒钟

  27. 27

    从CSS角度来看,单击,按住一秒钟然后拖走时,输入按钮处于什么状态?

  28. 28

    SwiftUi-隐藏“后退”按钮和导航栏(显示时间不到一秒钟)

  29. 29

    如果检索时间超过一秒钟,如何将按钮更改为“禁用”?

热门标签

归档