允许调整窗口大小 pyGame [英] Allowing resizing window pyGame

查看:107
本文介绍了允许调整窗口大小 pyGame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许调整此应用程序的大小,我放置了 RESIZABLE 标志,但是当我尝试调整大小时,它搞砸了!试试我的代码.

I am trying to allow resizing for this app, I put the RESIZABLE flag, but when I try to resize, it messes up! Try my code.

这是一个网格程序,当窗口调整大小时,我希望网格也调整大小/缩小.

It is a grid program, when the window resizes I want the grid to also resize/shrink.

import pygame,math
from pygame.locals import *
# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
green    = (   0, 255,   0)
red      = ( 255,   0,   0)

# This sets the width and height of each grid location
width=50
height=20
size=[500,500]
# This sets the margin between each cell
margin=1


# Initialize pygame
pygame.init()

# Set the height and width of the screen

screen=pygame.display.set_mode(size,RESIZABLE)

# Set title of screen
pygame.display.set_caption("My Game")

#Loop until the user clicks the close button.
done=False

# Used to manage how fast the screen updates
clock=pygame.time.Clock()

# -------- Main Program Loop -----------
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
        if event.type == pygame.MOUSEBUTTONDOWN:
            height+=10

    # Set the screen background
    screen.fill(black)

    # Draw the grid
    for row in range(int(math.ceil(size[1]/height))+1):
        for column in range(int(math.ceil(size[0]/width))+1):
            color = white
            pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height])

    # Limit to 20 frames per second
    clock.tick(20)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()

请告诉我有什么问题,谢谢.

Please tell me whats wrong, thanks.

推荐答案

当窗口更改时,您不会更新宽度、高度或大小.

You are not updating your width, height, or size when the window changes.

来自文档:http://www.pygame.org/docs/ref/display.html

如果显示设置为 pygame.RESIZABLE 标志,pygame.VIDEORESIZE 事件将在用户调整窗口尺寸.

If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions.

您可以从事件 VIDEORESIZE 中获取新的 size, w, h http://www.pygame.org/docs/ref/event.html

You can get the new size, w, h from the event VIDEORESIZE http://www.pygame.org/docs/ref/event.html

这篇关于允许调整窗口大小 pyGame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆