Python 我正在尝试为我的主菜单制作两个按钮
我使用了技术部和蒂姆提供的关于如何在我的程序中制作按钮的教程。第一个按钮我做得很好,它是“开始”按钮,但我也需要一个“退出”按钮,但它不起作用。有人能告诉我我的代码出了什么问题吗?我知道的一切我都试过了。代码如下:Python 我正在尝试为我的主菜单制作两个按钮,python,button,pygame,Python,Button,Pygame,我使用了技术部和蒂姆提供的关于如何在我的程序中制作按钮的教程。第一个按钮我做得很好,它是“开始”按钮,但我也需要一个“退出”按钮,但它不起作用。有人能告诉我我的代码出了什么问题吗?我知道的一切我都试过了。代码如下: import pygame from pygame.locals import * pygame.init() win = pygame.display.set_mode((800, 600)) win.fill((0, 180, 210)) class button():
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((800, 600))
win.fill((0, 180, 210))
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, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, 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))
win.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 redrawWindow():
win.fill((0, 180, 210))
greenButton.draw(win, (0, 0, 0))
run = True
greenButton = button((0, 255, 0), 280, 255, 250, 100, "Start")
while run:
redrawWindow()
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 the button")
if event.type == pygame.MOUSEMOTION:
if greenButton.isOver(pos):
greenButton.color = (105, 105, 105)
else:
greenButton.color = (0, 255, 0)
class button2():
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, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != '':
font = pygame.font.SysFont('arial', 60)
text = font.render(self.text, 1, (0, 0, 0))
win.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 redrawWindow():
win.fill((0, 180, 210))
greenButton.draw(win, (0, 0, 0))
redButton.draw(win, (0, 0, 0))
run = True
redButton = button((0, 255, 0), 280, 300, 250, 100, "Quit")
while run:
redrawWindow()
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 the 2button")
if event.type == pygame.MOUSEMOTION:
if redButton.isOver(pos):
redButton.color = (105, 105, 105)
else:
redButton.color = (255, 0, 0)
导入pygame
从pygame.locals导入*
pygame.init()
win=pygame.display.set_模式((800600))
胜利填充((0,180,210))
类按钮():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
def draw(自我、胜利、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pygame.draw.rect(赢,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(win,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]self.x和位置[0]self.y和位置[1]
您不需要类按钮2
。类的诀窍在于它们可以用于多个实例。看
只需创建两个按钮的实例
:
greenButton=按钮((0,255,0),280,190,250,100,“开始”)
红色按钮=按钮((255,0,0),280,310,250,100,“退出”)
并处理两个按钮的事件:
运行时:
重画窗口()
pygame.display.update()
对于pygame.event.get()中的事件:
pos=pygame.mouse.get_pos()
如果event.type==pygame.QUIT:
运行=错误
pygame.quit()
退出
如果event.type==pygame.MOUSEBUTTONDOWN:
如果为绿色按钮,则为isOver(位置):
打印(“单击按钮”)
如果为红色按钮,则为isOver(位置):
打印(“单击2按钮”)
如果event.type==pygame.MOUSEMOTION:
如果为绿色按钮,则为isOver(位置):
greenButton.color=(105、105、105)
其他:
greenButton.color=(0,255,0)
如果为红色按钮,则为isOver(位置):
redButton.color=(105、105、105)
其他:
redButton.color=(255,0,0)
请参见示例:
导入pygame
从pygame.locals导入*
pygame.init()
win=pygame.display.set_模式((800600))
胜利填充((0,180,210))
类按钮():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
def draw(自我、胜利、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pygame.draw.rect(赢,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(win,self.color,(self.x,self.y,self.width,self.h
def button(x, y, w, h, inactive, active, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
gameDisplay.blit(active, (x, y))
if click[0] == 1 and action is not None:
action()
else:
gameDisplay.blit(inactive, (x, y))