1. 程式人生 > >pygame之視窗大小調整

pygame之視窗大小調整

對於不同的情況我門需要調整不同的視窗,在pygame中pygame.display.set_mode((640,480),0,32)為調整視窗的函式,例如,我們可以通過設定第二個引數為FULLSCREEN來讓整個視窗全屏,基本的語法我就不說了,這裡我展示可以使自定義調整視窗大小的程式碼,

import pygame
from pygame.locals import *
from sys import exit
SCREEN_SIZE = (640, 480)
pygame.init()
background_image_filename = 'sushiplate.png'
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)
background = pygame.image.load(background_image_filename).convert()
while True:
    event = pygame.event.wait()
    if event.type == QUIT:
        exit()
    if event.type == VIDEORESIZE:
        # get the size of the window
        SCREEN_SIZE = event.size
        # set the mode of the window
        screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)
        pygame.display.set_caption("Window resized to "+str(event.size))
    screen_width, screen_height = SCREEN_SIZE
    # change the background size
    for y in range(0, screen_height, background.get_height()):
        for x in range(0, screen_width, background.get_width()):
            screen.blit(background, (x, y))
    pygame.display.update()