如何解决如何使用 pygame 使启动、停止和重置按钮在 python 中工作?
如何使用 pygame 使启动、停止和重置按钮在 python 中工作?这是我的按钮的代码,但我不确定为什么它们没有出现在我的窗口中。每当我在单独的 Python 文件中编写这些代码时,它们都可以工作,但是当我尝试将它们全部放入一个程序时,它们却永远不会出现。
class button():
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
def draw(self,GameOfLifeMain,outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(GameOfLifeMain,outline,(self.x - 2,self.y - 2,self.width + 4,self.height + 4),0)
pygame.draw.rect(GameOfLifeMain,self.color,(self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans',40)
text = font.render(self.text,1,(0,0))
GameOfLifeMain.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
run = True
greenButton = button((0,255,0),80,2,100,50,'Start')
while run:
gameOfLifeMain()
pygame.display.update()
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if greenButton.isOver(pos):
print('clicked')
if event.type == pygame.MOUSEMOTION:
if greenButton.isOver(pos):
greenButton.color = (0,0)
else:
greenButton.color = (0,0)
# ExtraRequirement
***# STOP***
def draw(self,self.y + (self.height / 2 - text.get_height() / 2)))
def isOver(self,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
run = True
redButton = button((255,200,'Stop')
while run:
pygame.display.update()
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if redButton.isOver(pos):
print('clicked')
if event.type == pygame.MOUSEMOTION:
if redButton.isOver(pos):
redButton.color = (255,127,80)
else:
redButton.color = (255,0)
***# Restart***
def draw(self,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
run = True
yellowButton = button(("Yellow 3"),300,'Restart')
while run:
pygame.display.update()
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = True
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if yellowButton.isOver(pos):
print('clicked')
if event.type == pygame.MOUSEMOTION:
if yellowButton.isOver(pos):
yellowButton.color = ("orange")
else:
yellowButton.color = ("Yellow 3")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。