设置pygame窗口大小可调节

2021年2月17日 / 39次阅读 / Last Modified 2021年2月17日
pygame

import sys
import random
import time
import pygame


size = width, height = 800, 600
speed = [1,1]

pygame.init()
screen = pygame.display.set_mode(size, flags=pygame.RESIZABLE)
pygame.display.set_caption('Here is topleft title')
clock = pygame.time.Clock()


bg = pygame.image.load('bz.jpg').convert()
screen.blit(pygame.transform.scale(bg,size), (0,0))
imgrect = pygame.draw.rect(screen, (100,20,30,50), (0,0,100,100))


while True:
    # frame rate per second
    clock.tick(240)
    # event list
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
        if event.type == pygame.VIDEORESIZE:
            print(event.dict['size'])
            width = event.dict['w']
            height = event.dict['h']
            size = width, height
            screen.blit(pygame.transform.scale(bg,size), (0,0))
            imgrect = pygame.draw.rect(screen, (100,20,30,50), (0,0,100,100))
    # adjust speed
    if imgrect.left<0 or imgrect.right>width:
        speed[0] = -speed[0]
    if imgrect.top<0 or imgrect.bottom>height:
        speed[1] = -speed[1]
    # draw and flip
    screen.blit(pygame.transform.scale(bg,size), imgrect, imgrect)
    imgrect = imgrect.move(speed)
    pygame.draw.rect(screen, (100,20,30,50), imgrect)
    pygame.display.flip()

screen = pygame.display.set_mode(size, flags=pygame.RESIZABLE),flags参数用来控制窗口是否可以resize。

可resize的窗口,在resize的时候,去处理pygame.VIDEORESIZE事件!

如何让背景图片自适应窗口大小?

pygame.transform.scale(bg,size)

-- EOF --

本文链接:https://www.pynote.net/archives/3526

留言区

您的电子邮箱地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top