【问题标题】:python pygame the background image changes with the screen sizepython pygame 背景图片随屏幕大小变化
【发布时间】:2020-01-11 13:35:37
【问题描述】:

我将screen=p.display.set_mode((width,height),flag,0) 设置为我的pygame 屏幕,flag=p.RESIZABLE 使屏幕可以拉伸。

此时我加载了一个图像作为背景,现在当我拉伸屏幕时,背景不会随屏幕改变大小,我该怎么办?

代码如下:

#! /usr/bin/python
import sys
import pygame as p

pic="/home/finals/python/alien/image/muha.png"

def screen_setting(width,height):
    p.init()
    flag=p.RESIZABLE   
    screen=p.display.set_mode((width,height),flag,0)
    bk=p.transform.smoothscale(p.image.load(pic).convert(),(width,height))

    while True:
        for event in p.event.get():
            if event.type == p.QUIT:
                sys.exit()
        screen.blit(bk,(0,0))
        p.display.flip()

screen_setting(1200,800)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您必须实现VIDEORESIZE 事件(请参阅pygame.event)。

    当窗口调整大小时,从偶数属性中获取新的窗口大小:

    width, height = event.w, event.h
    

    创建一个与窗口关联的新 Surface 并将背景缩放到新大小:

    def screen_setting(width,height):
        p.init()
        flag = p.RESIZABLE   
        screen=p.display.set_mode((width, height), flag, 0)
        bk_orig = p.image.load(pic).convert()
        bk = p.transform.smoothscale(bk_orig, (width, height))
    
        while True:
            for event in p.event.get():
                if event.type == p.QUIT:
                    sys.exit()
                elif event.type == p.VIDEORESIZE:
                    width, height = event.w, event.h
                    screen = p.display.set_mode((width, height), flag, 0)
                    bk = p.transform.smoothscale(bk_orig, (width, height))
    
            screen.blit(bk, (0 ,0))
            p.display.flip()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多