我正在尝试构建一个带有照片和一些启动选项的游戏菜单,但我遇到了一些麻烦。我尝试使用pygame包无济于事,这只是在它出现时立即关闭了黑色窗口。
#!/usr/bin/python
import pygame
from pygame.locals import *
from PIL import Image, ImageFilter
pygame.init()
class GameMenu():
def __init__(self, screen, items, bg_color=(0, 0, 0), font=None, font_size=30,
font_color=(255, 255, 255)):
self.screen = screen
self.scr_width = self.screen.get_rect().width
self.scr_height = self.screen.get_rect().height
self.bg_color = bg_color
self.clock = pygame.time.Clock()
self.items = items
self.font = pygame.font.SysFont(font, font_size)
self.font_color = font_color
self.items = []
for index, item in enumerate(items):
label = self.font.render(item, 1, font_color)
width = label.get_rect().width
height = label.get_rect().height
posx = (self.scr_width / 2) - (width / 2)
# t_h: total height of text block
t_h = len(items) * height
posy = (self.scr_height / 2) - (t_h / 2) + (index * height)
self.items.append([item, label, (width, height), (posx, posy)])
def run(self):
mainloop = True
while mainloop:
# Limit frame speed to 50 FPS
self.clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False
# Redraw the background
self.screen.fill(self.bg_color)
for name, label, (width, height), (posx, posy) in self.items:
self.screen.blit(label, (posx, posy))
pygame.display.flip()
if __name__ == "__main__":
# Creating the screen
screen = pygame.display.set_mode((1280, 780), 0, 32)
backround = pygame.image.load(path).convert()
image = pygame.image.load(path).convert_alpha()
menu_items = ('Start', 'Quit')
pygame.display.set_caption('Game Menu')
gm = GameMenu(screen, menu_items)
gm.run()
while True:
screen.blit(backround, 5, 5)
screen.blit(image, 10, 10)
pygame.display.update()
非常感谢帮助。
感谢