Python 按钮仅绘制一秒钟的图像
所以我试着让它成为这样,当我按下按钮时,它会永远画出我的图像,但什么都没有发生:我按下按钮,但什么都没有发生,我想让它画出我的图像,当我按下按钮时,它会留下我的图像 这就是我试过的Python 按钮仅绘制一秒钟的图像,python,pygame,Python,Pygame,所以我试着让它成为这样,当我按下按钮时,它会永远画出我的图像,但什么都没有发生:我按下按钮,但什么都没有发生,我想让它画出我的图像,当我按下按钮时,它会留下我的图像 这就是我试过的 if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() if Abutton.clicked: Abutton.clicked = True w
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()
导入pygame
pygame.init()
#绘图窗口屏幕
window=pygame.display.set_模式((700500))
#我窗口的名称
pygame.display.set_标题(“StickMan”)
#绘制按钮
A=pygame.image.load(“A.png”)
A2=pygame.image.load(“A1.png”)
#按钮类
类button1():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
self.clicked=False
def绘图(自身、窗口、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pygame.draw.rect(窗口,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(窗口,self.color,(self.x,self.y,self.width,self.height),0)
如果self.text!='':
font=pygame.font.SysFont('comicsans',60)
text=font.render(self.text,1,(0,0,0))
blit(text,(self.x+(self.width/2-text.get\u width()/2),self.y+(self.height/2-text.get\u height()/2)))
def isOver(自身,位置):
#Pos是鼠标位置或(x,y)坐标的元组
如果位置[0]>self.x和位置[0]self.y和位置[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
问题编辑;新答案: 主要问题是您在
重画窗口
函数中定义了Abutton
。
由于您在for
循环的每次迭代期间调用该函数
self.clicked
变量丢失,就像在循环的每次迭代中一样,Abutton
被重新定义
使用self。单击
设置为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()
导入pygame
pygame.init()
#绘图窗口屏幕
window=pygame.display.set_模式((700500))
#我窗口的名称
pygame.display.set_标题(“StickMan”)
#绘制按钮
A=pygame.image.load(“A.png”)
A2=pygame.image.load(“A1.png”)
#按钮类
类button1():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
self.clicked=False
def绘图(自身、窗口、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pygame.draw.rect(窗口,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(窗口,self.color,(self.x,self.y,self.width,self.height),0)
如果self.text!='':
font=pygame.font.SysFont('comicsans',60)
text=font.render(self.text,1,(0,0,0))
blit(text,(self.x+(self.width/2-text.get\u width()/2),self.y+(self.height/2-text.get\u height()/2)))
def isOver(自身,位置):
#Pos是鼠标位置或(x,y)坐标的元组
返回位置[0]>self.x和位置[0]self.y和位置[1]