问题
I have a menu (main_menu), that has a Button and I want this button to stay in the middle after I resize the window. The program does draw a new button in the middle of the window but the old button is still visible and I cant figure out why. I tried to fill the screen with the background color again and then draw a new button, but the old one was still visible... Any Ideas ?
WINDOW_W , WINDOW_H = 1200, 800
SCREEN = pg.display.set_mode((WINDOW_W,WINDOW_H),pg.RESIZABLE)
main_menu = Main_Menu(SCREEN)
main_menu.render()
while True:
pg.event.pump()
event = pg.event.wait()
if event.type == pg.VIDEORESIZE:
new_w, new_h = event.dict['size']
SCREEN = pg.display.set_mode((new_w, new_h), pg.RESIZABLE)
main_menu = Main_Menu(SCREEN)
main_menu.render()
class Main_Menu:
def __init__(self, screen):
self.screen = screen
screen_width, screen_height = screen.get_size()
play_button = Button((255,0,0), screen_width/2 - 100, screen_height/2 - 25, 200, 50, "Test")
self.button = play_button
def render(self):
self.button.draw(self.screen, 1)
pg.display.update()
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)))
回答1:
You would want to save the original ratio between the size of the button and the size of the screen. After that you want to check in a loop wether the screen size changed. If so, change the size of the button back to the same ratio.
And same with the position of the button, just make sure that the ratio is consistent.
回答2:
Here's a minimal example drawing an image that stays in the centre of the window as the window is resized:
import pygame
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
image = pygame.Surface((120, 100), pygame.SRCALPHA)
image.fill(pygame.color.Color("royalblue"))
center_pos = (width // 2, height // 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.VIDEORESIZE:
width, height = event.dict["size"]
# recreate screen object required for pygame version 1
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
# TODO: resize image if needed
center_pos = (width // 2, height // 2)
screen.fill(0)
screen.blit(image, image.get_rect(center=center_pos))
pygame.display.update()
If you're using the development release of pygame 2 then you won't need to recreate screen by calling pygame.display.set_mode(…).
来源:https://stackoverflow.com/questions/64230345/how-to-resize-button-after-resizing-pygame-window