首页 > 解决方案 > 如何使我的按钮、对象等大小取决于窗口大小?

问题描述

我知道如何使窗口可调整大小,但如果我运行代码只有窗口得到调整大小。按钮、对象等没有。我该怎么做才能使它们根据窗口调整大小?如果这个问题有点难以理解,你可以去我的问题,我问如何调整窗口大小并阅读那里的评论。这是代码,如果你们需要它:

import pygame
from pygame.locals import *

pygame.init()

WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600

SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), SURFACE)
win.fill((0, 180, 210))

pygame.display.set_caption("Baloon War!")
icon = pygame.image.load("Baloon war icon.png")
pygame.display.set_icon(icon)

centre_point = (WINDOW_WIDTH//2, WINDOW_HEIGHT//2)

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 rescale(self):
        new_size = int(WINDOW_WIDTH * self.scale_factor)
        x, y = self.rect.center
        self.image = pygame.transform.smoothscale(self.original, (new_size, new_size))
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

def redrawMenuWindow():
    win.fill((0, 255, 110))
    greenButton.draw(win, (0, 0, 0))
    redButton.draw(win, (0, 0, 0))

def redrawGameWindow():
    win.fill((0, 150, 210))
    pygame.draw.rect(win, (0, 250, 110),(0, 450, 800, 250))




greenButton = button((0, 255, 0), 280, 255, 250, 100, "Start")
redButton = button ((255, 0, 0), 280, 380, 250, 100, "Quit")

game_state = "menu"
run = True
while run:
    if game_state == "menu":
        redrawMenuWindow()
    elif game_state == "game":
        redrawGameWindow()
    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()
        elif event.type == pygame.VIDEORESIZE:
            WINDOW_WIDTH = event.w
            WINDOW_HEIGHT = event.h
            win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), SURFACE)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenButton.isOver(pos):
                print("clicked the button")
                game_state = "game"
            if redButton.isOver(pos):
                print("clicked the 2button")
                run = False
                pygame.quit()
                quit()


        if event.type == pygame.MOUSEMOTION:
            if greenButton.isOver(pos):
                greenButton.color = (105, 105, 105)
            else:
                greenButton.color = (0, 255, 0)
            if redButton.isOver(pos):
                redButton.color = (105, 105, 105)
            else:
                redButton.color = (255, 0, 0)

        pygame.display.update()

标签: pythonwindowspygameimage-resizing

解决方案


这样的事情有望解决这个问题,在你的Button类(类以大写字母开头)中,添加这个方法:

def resize_button(self, width, height, x, y):
        self.width = width
        self.height = height
        self.x = x
        self.y = y

然后,在main_loop您捕捉VIDEORESIZE事件时:

elif event.type == pygame.VIDEORESIZE:
            win = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
            greenButton.resize_button(event.w*0.3, event.h*0.3, event.w/2, event.h/2)

使用系数乘以参数以获得所需的结果。玩这个。但主要思想是使按钮大小和位置相对于当前窗口大小。还要插入clock.tick(FPS)你的main_loop并且不要忘记clock = pygame.clock.Time()在循环之前添加。通过这种方式,您可以控制 FPS,从而减少处理器使用率。


推荐阅读