我以为我会试试pygame并且遇到了一个我希望有人可以帮助我的问题。我想有一个类def来创建退出按钮,但是当我这样做时,我无法让鼠标碰撞。我很可能做错了,但我真的很感激帮助。
它在我下面的状态下工作,但是当我把它放在def中时没有!
pygame.init()
class Window:
def __init__(self):
self.screen_width = 800
self.screen_height = 600
self.screen = (pygame.display.set_mode((self.screen_width, self.screen_height)))
self.FPS = 30
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont('Arial', 25)
self.menu_open = True
self.colors = {"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255),
"white": (255, 255, 255),
"black": (0, 0, 0),
"brown": (153, 76, 0),
"grey": (100, 100, 100)}
def setup(self):
self.screen.fill(self.colors["black"])
pygame.display.set_caption("Menu Test!")
def text(self, message, text_color, x_pos, y_pos):
text = self.font.render(message, True, (self.colors[text_color]))
text_rect = text.get_rect(center=(x_pos, y_pos))
self.screen.blit(text, text_rect)
def exit(self):
self.screen.fill(self.colors["black"])
text = self.font.render("Thank you for playing. Goodbye!", True,
(self.colors["white"]))
text_rect = text.get_rect(center=(self.screen_width / 2,
self.screen_height / 2))
self.screen.blit(text, text_rect)
pygame.display.update()
sleep(3)
pygame.quit()
sys.exit()
def main():
window = Window()
window.setup()
这是我想要的一个类def
quit_button = pygame.draw.rect(window.screen, window.colors["white"],
(window.screen_width / 2 - 100,
window.screen_height / 1.5 - 25, 200, 50), 0)
window.text("QUIT", "red", window.screen_width / 2, window.screen_height / 1.5)
pygame.display.update()
while window.menu_open == 1:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
这就是它在矩形上点击鼠标的位置。
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
pos = pygame.mouse.get_pos()
if quit_button.collidepoint(pos):
window.exit()
else:
print("Error Line 48")
if __name__ == "__main__":
main()
答案 0 :(得分:0)
使用import re
c=r"([0-9]{1,2}\s+)(2003-09-07).+(2006-12-25)\s+\w+"
with open("event.txt","r") as f:
file_data=f.readlines()
regex_search=re.search(c,str(file_data))
print(regex_search.group())
和pygame.sprite.Sprite
属性创建一个类(在本例中我使用image
子类)并将文本blit到图像上。
rect
更高级的解决方案是处理import sys
import pygame as pg
pg.init()
class Window:
def __init__(self):
self.screen = pg.display.set_mode((800, 600))
self.rect = self.screen.get_rect()
self.FPS = 30
self.clock = pg.time.Clock()
self.font = pg.font.SysFont("Arial", 25)
self.menu_open = True
self.colors = {"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255),
"white": (255, 255, 255),
"black": (0, 0, 0),
"brown": (153, 76, 0),
"grey": (100, 100, 100)}
def setup(self):
self.screen.fill(self.colors["black"])
pg.display.set_caption("Menu Test!")
def text(self, message, text_color, x_pos, y_pos):
text = self.font.render(message, True, (self.colors[text_color]))
text_rect = text.get_rect(center=(x_pos, y_pos))
self.screen.blit(text, text_rect)
def exit(self):
self.screen.fill(self.colors["black"])
text = self.font.render("Thank you for playing. Goodbye!", True,
(self.colors["white"]))
text_rect = text.get_rect(center=(self.rect.w/2, self.rect.h/2))
self.screen.blit(text, text_rect)
pg.display.update()
pg.time.wait(1000)
pg.quit()
sys.exit()
class Button(pg.sprite.Sprite):
def __init__(self, pos, text, window):
super().__init__() # Call __init__ of the parent class.
# Render the text.
self.text_surf = window.font.render(text, True, window.colors["black"])
self.image = pg.Surface((self.text_surf.get_width()+40,
self.text_surf.get_height()+20))
self.image.fill(window.colors["white"])
# Now blit the text onto the self.image.
self.image.blit(self.text_surf, (20, 10))
self.rect = self.image.get_rect(topleft=pos)
def main():
window = Window()
window.setup()
clock = pg.time.Clock()
# gui is a sprite group which will contain the button sprites.
gui = pg.sprite.Group()
# Instantiate some buttons.
quit_button = Button(
pos=(window.rect.w/2 - 100, window.rect.h/1.5 - 25),
text="QUIT",
window=window,
)
hello_button = Button(
pos=(window.rect.w/8, window.rect.h/2),
text="hello",
window=window,
)
# Add the buttons to the gui group.
gui.add(quit_button, hello_button)
while window.menu_open == True:
for event in pg.event.get():
if event.type == pg.QUIT:
window.exit()
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
# Handle button events.
if quit_button.rect.collidepoint(event.pos):
window.exit()
elif hello_button.rect.collidepoint(event.pos):
print("hello")
gui.update() # Call update methods of contained sprites.
gui.draw(window.screen) # Draw all sprites.
pg.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
类中的事件,然后调用在实例化期间必须传递的回调函数。
Button