Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

python pygame the background image changes with the screen size

I set screen=p.display.set_mode((width,height),flag,0) as my pygame screen and flag=p.RESIZABLE makes the screen can be stretch.

At this time i load an image as the background, now when i stretch the screen, the background wont change the size by the screen, how can i do it?

here's the codes:

#! /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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to implement the VIDEORESIZE event (see pygame.event).

When the window is resized, then get the new size of the window from the even attributes:

width, height = event.w, event.h

Create a new Surface associated to the window and scale the background to the new size:

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...