Python 如何制作一个;“退出”;按钮单击事件关闭使用PyGame的程序
我第一次使用Pygame库,在制作一个简单的按钮时遇到了一些麻烦,它的点击事件将关闭我的程序。我已按以下方式对代码进行了分段: App.py,其中包含我的主循环:Python 如何制作一个;“退出”;按钮单击事件关闭使用PyGame的程序,python,pygame,Python,Pygame,我第一次使用Pygame库,在制作一个简单的按钮时遇到了一些麻烦,它的点击事件将关闭我的程序。我已按以下方式对代码进行了分段: App.py,其中包含我的主循环: import pygame from pygame.locals import * from widgets import * class App(): # creates object def __init__(self): self._running = True self._
import pygame
from pygame.locals import *
from widgets import *
class App():
# creates object
def __init__(self):
self._running = True
self._display_surf = None
self.size = self.weight, self.height = 1000, 500
self.button_1 = rect_Button(((255,255,255)),(500,250),"Quit")
# initializes all PyGame modules
# create main display & uses hardware acceleration
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._running = True
def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False
#if event.type == MOUSEBUTTONUP:
#self._running = False
if self.button_1.click_State:
# testing button
print("closing...")
def on_loop(self):
pass
def on_render(self):
rect_button_1 = pygame.draw.rect(self._display_surf,self.button_1.color,self.button_1.container())
self._display_surf.blit(self.button_1.display_Text,rect_button_1)
pygame.display.update()
def on_cleanup(self):
pygame.quit()
def on_click(self):
pass
def on_execute(self):
if self.on_init() == False:
self._running = False
while( self._running ):
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()
pywidgets.py,其中我将为不是Pygame内置的小部件创建类:
import pygame
from pygame.locals import *
pygame.init()
class rect_Button():
def __init__(self, color, pos, text):
self.font = pygame.font.Font('freesansbold.ttf',32)
self.display_Text = self.font.render(text,True,((200,0,0)),None)
self.mouse = pygame.mouse.get_pos()
self.click = pygame.mouse.get_pressed()
self.width = self.font.size(text)[0]
self.height = self.font.size(text)[1]
self.color = color
self.pos_X = pos[0]
self.pos_Y = pos[1]
def container(self):
return pygame.Rect(self.pos_X,self.pos_Y,self.width,self.height)
def click_State(self):
if self.pos_X+self.width > mouse[0] > self.pos_X and self.pos_Y+self.height > mouse[1] > self.pos_Y:
if self.click[0]==True:
return True
else:
return False
最后是game.py,在这里我创建了App对象:
import pygame
from pygame.locals import *
from app import *
if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
我的问题是我想点击我创建的退出按钮并关闭我的程序。为了测试它,我让它在我点击按钮时打印“关闭…”。但是,每当我在程序窗口内移动鼠标时,它就会持续打印。我应该如何告诉我的代码从button对象获取按钮被单击的信息,运行事件并关闭窗口
谢谢很难理解您的代码,但我建议创建一个单独的函数来处理事件检查。当您想检查是否按下按钮时,在您的检查
pygame.event.get()
loop下使用以下命令更容易:
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
button_clicked = easy_button.rect.collidepoint(mouse_x, mouse_y)
if button_hard_clicked and self._running: # checks of active program/click
# Whatever you want to do if the button is clicked
self._running = False
看看我的星球大战pygame射击程序,它也实现了一个按钮。希望这能有所帮助。按钮的代码可在game_functions.py
和main.py
文件中找到。
到目前为止
if self.button_1.click_State:
不会调用单击状态()
要解决此问题,请在click_State的末尾添加()以调用该函数。
此外,当按上述方式写入行时,语句的其余部分将自动进行。(因此所有的“结束…”)
上面的一行比较接近,但是程序没有检查它是否匹配任何东西。要解决此问题,请更改为:
if self.button_1.click_State() == True:
从那里你可以插入其余的代码来关闭程序。
希望我能帮上忙 事实上,我还没有对我的代码发表评论,我为它当前的混乱状态道歉!同时,我更新了文本,解释了我是如何解决这个问题的。另外,非常感谢您分享您的计划和帮助。非常感谢您分享您的答案!我更新了文本,在这里我解释了我是如何解决这个问题的。
如果
本身就很好,而与真
和假
的明确比较实际上通常是个坏主意。在英语中,我们不会说“如果下雨是真的”,尽管我们可以;我们说“如果下雨”。
if self.button_1.click_State() == True: