【发布时间】: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()
【问题讨论】: