pygame窗口全屏设置

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

import sys
import random
import time
import pygame

import os
os.environ['SDL_VIDEO_WINDOW_POS']='100,300'

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

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


icon = pygame.image.load('down.png').convert()
pygame.display.set_icon(icon)


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))
        if event.type == pygame.KEYUP and event.key == pygame.K_F11:
            if not isFullScreen:
                #pygame.display.toggle_fullscreen()
                screen = pygame.display.set_mode((0,0),flags=pygame.FULLSCREEN)
                isFullScreen = True
                continue
            if isFullScreen:
                pygame.display.toggle_fullscreen()
                screen = pygame.display.set_mode((800,600),flags=pygame.RESIZABLE)
                isFullScreen = False
                continue
        if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
            if isFullScreen:
                pygame.display.toggle_fullscreen()
                screen = pygame.display.set_mode((800,600),flags=pygame.RESIZABLE)
                isFullScreen = False
    # 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()

os.environ里面的那个值,用来控制窗口的初始位置。

我的测试结果是,全屏设置不要使用pygame.display.toggle_fullscreen(),这个函数调用减小显示器的分辨率,但是从全屏到小屏,却一定要用这个函数,否则窗口无法关闭(没有顶部工具栏)。

这段代码没有仔细调试过,只是把需要功能表现出来了。对键盘的相应,以UP时为准,感觉这样更专业。

-- EOF --

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

留言区

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


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top