我如何使用Pygame get_size()?

时间:2018-12-21 13:53:42

标签: python pygame

我一直在用python编写DVD屏幕保护程序的代码,但是我无法从pygame获取窗口大小。我曾经使用过Info(),get_size(),get_width()和get_height(),但是当我调整窗口大小时,它们都没有改变。

我正在python 3.5.3中对此进行编码,我尝试重新安装pygame,然后尝试切换到python 3.6并为此安装pygame,但没有任何效果。

import pygame as p, random as r, time as t, sys

width, height = 640, 480 #Initial screen width & height
x, y, vel = 0, 0, [1, 1] #Makes coordinates and velocity
DVD = p.image.load('sprites\\w.png') #Loads a sprite
DVDRECT = DVD.get_rect() #Makes object for the sprites to be loaded onto
screen = p.display.set_mode((width, height),p.RESIZABLE) #Sets screen to resizable mode
p.display.set_caption('DVD')#Sets executable capton
p.init() #Initialize Pygame

x, y = r.randint(200, 400), r.randint(200, 400) #sets the start location

#Loads in sprites
wht = p.image.load('sprites\\w.png')
blu = p.image.load('sprites\\b.png')
pnk = p.image.load('sprites\\p2.png')
pur = p.image.load('sprites\\p.png')
grn = p.image.load('sprites\\g.png')
org = p.image.load('sprites\\o.png')
ylw = p.image.load('sprites\\y.png')
p.display.set_icon(wht)

def new_color():
    """
    Function for getting random colors
    """
    return r.choice([wht, blu, pnk, pur, grn, org, ylw])

while True:
    print(screen.get_size())
    for event in p.event.get():
        if event.type == p.QUIT:
            p.quit()
            sys.exit()
    #Makes new coordinates:
    x += vel[0]
    y += vel[1]

    #Checks if logo hits a wall
    if x >= width-29:
        vel[0] = -vel[0] #Makes logo 'bounce' off wall
        DVD = new_color() #Sets a new color to the logo
        p.display.set_icon(DVD)

    if x <= 29:
        vel[0] = -vel[0]
        DVD = new_color()
        p.display.set_icon(DVD)

    if y >= height-19:
        vel[1] = -vel[1]
        DVD = new_color()
        p.display.set_icon(DVD)

    if y <= 19:
        vel[1] = -vel[1]
        DVD = new_color()
        p.display.set_icon(DVD)

    DVDRECT.center = (x, y) #moves the logo
    screen.fill((0, 0, 0)) #sets background to black
    screen.blit(DVD, DVDRECT) #Updates logo
    p.display.flip() #flips screen
    t.sleep(1.0/80.0) #waits certian time between frames

当我调整窗口大小时,我期望print(screen.get_size())的输出发生变化,但是无论我对窗口做什么,它显示的元组仍然(640,480)。

1 个答案:

答案 0 :(得分:0)

我发现我必须使用事件处理程序来获取用户何时调整屏幕大小,他们使屏幕变大然后通过重新定义屏幕变量来重新定义宽度和高度的原因

0000000000000238 l    d  .interp                0000000000000000              .interp
0000000000000254 l    d  .note.gnu.build-id     0000000000000000              .note.gnu.build-id
0000000000000278 l    d  .gnu.hash              0000000000000000              .gnu.hash
0000000000000298 l    d  .dynsym                0000000000000000              .dynsym
00000000000002e0 l    d  .dynstr                0000000000000000              .dynstr
0000000000000302 l    d  .gnu.version           0000000000000000              .gnu.version
0000000000000308 l    d  .gnu.version_r         0000000000000000              .gnu.version_r
0000000000000328 l    d  .rela.plt              0000000000000000              .rela.plt
0000000000000360 l    d  .plt                   0000000000000000              .plt
0000000000000390 l    d  .text                  0000000000000000              .text
00000000000003f0 l    d  .eh_frame_hdr          0000000000000000              .eh_frame_hdr
0000000000000418 l    d  .eh_frame              0000000000000000              .eh_frame
0000000000200e78 l    d  .dynamic               0000000000000000              .dynamic
0000000000200fd8 l    d  .got                   0000000000000000              .got
0000000000000000 l    d  .comment               0000000000000000              .comment
0000000000000000 l    df *ABS*                  0000000000000000              file.c
0000000000000000 l    df *ABS*                  0000000000000000              
0000000000200e78 l     O .dynamic               0000000000000000              _DYNAMIC
00000000000003f0 l       .eh_frame_hdr          0000000000000000              __GNU_EH_FRAME_HDR
0000000000200fd8 l     O .got                   0000000000000000              _GLOBAL_OFFSET_TABLE_
0000000000201000 g       .got                   0000000000000000              _edata
00000000000003d5 g     F .text                  0000000000000019              not_main
0000000000000390 g     F .text                  0000000000000045              program
0000000000201000 g       .got                   0000000000000000              _end
0000000000201000 g       .got                   0000000000000000              __bss_start
0000000000000000       F *UND*                  0000000000000000              atoi@@GLIBC_2.2.5
0000000000000000       F *UND*                  0000000000000000              exit@@GLIBC_2.2.5

从这里开始,代码工作正常。