所以,我一直在努力使它所以当我按下按钮,它吸引我的形象永远,但什么也没发生: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 = True
在if
声明。然后,而不是绘制A
的if
块,将其移出:
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
在redrawwindow
函数内部定义了。因为您在循环的每次迭代过程中都调用了该函数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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句