2012-07-22 183 views
11

我想允许调整这个应用程序的大小,我把调整大小的标志,但是当我尝试调整大小时,它搞砸了!试试我的代码。允许调整大小的窗口pyGame

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

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

请告诉我什么是错的,谢谢。

回答

0

是可调整大小的一个简单的Hello World窗口新size, w, h,再加上我是带班玩弄。
分解成两个文件,一个用于定义颜色常量。

import pygame, sys 
from pygame.locals import * 
from colors import * 


# Data Definition 
class helloWorld: 
    '''Create a resizable hello world window''' 
    def __init__(self): 
     pygame.init() 
     self.width = 300 
     self.height = 300 
     DISPLAYSURF = pygame.display.set_mode((self.width,self.height), RESIZABLE) 
     DISPLAYSURF.fill(WHITE) 

    def run(self): 
     while True: 
      for event in pygame.event.get(): 
       if event.type == QUIT: 
        pygame.quit() 
        sys.exit() 
       elif event.type == VIDEORESIZE: 
        self.CreateWindow(event.w,event.h) 
      pygame.display.update() 

    def CreateWindow(self,width,height): 
     '''Updates the window width and height ''' 
     pygame.display.set_caption("Press ESC to quit") 
     DISPLAYSURF = pygame.display.set_mode((width,height),RESIZABLE) 
     DISPLAYSURF.fill(WHITE) 


if __name__ == '__main__': 
    helloWorld().run() 

colors.py:

BLACK = (0, 0,0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
YELLOW = (255, 255, 0) 
BLUE = (0,0,255) 

GREEN = (0,255,0) 
+7

代码的工作,但你应该阅读PEP 8风格指南。你已经用许多不同类型的约定,例如不是类的'CreateWindow',不是常量的'helloWorld',*是*和'DISPLAYSURF'。另外,避免在任何地方发送'from ... import *'垃圾邮件,特别是因为您没有使用它们(无论如何,您都是在所有'pygame'调用的前缀) – MestreLion 2014-08-17 19:15:19

5

ninMonkey的描述是正确的(https://stackoverflow.com/a/11604661/3787376)。

当窗口 更改时,您没有更新宽度,高度或大小。

因此,答案是简单地重新Pygame的窗口,更新其大小
(此重新调整当前窗口和删除其表面上的所有先前的内容)。

>>> import pygame 
>>> 
>>> pygame.display.set_mode.__doc__ 
'set_mode(resolution=(0,0), flags=0, depth=0) -> Surface\nInitialize a window or screen for display' 
>>> 

这需要在pygame.VIDEORESIZE事件,当用户改变可调整大小的窗口的尺寸将其发送完成。另外,可能需要使用以下保留当前窗口内容的方法。

一些示例代码:

import pygame, sys 
# from pygame.locals import * # This would make Pygame constants (in capitals) not need the prefix "pygame." 

pygame.init() 

# Create the window, saving it to a variable. 
surface = pygame.display.set_mode((350, 250), pygame.RESIZABLE) 
pygame.display.set_caption("Example resizable window") 

while True: 
    surface.fill((255,255,255)) 

    # Draw a red rectangle that resizes with the window as a test. 
    pygame.draw.rect(surface, (200,0,0), (surface.get_width()/3, 
              surface.get_height()/3, 
              surface.get_width()/3, 
              surface.get_height()/3)) 

    pygame.display.update() 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
     if event.type == pygame.VIDEORESIZE: 
      # The main code that resizes the window: 
      # (recreate the window with the new size) 
      surface = pygame.display.set_mode((event.w, event.h), 
               pygame.RESIZABLE) 

,以避免失去先前的内容的风险的方法:
这里的一些步骤,以留下您的GUI的部分不变:

  1. 创建第二个变量,设置为旧窗口表面变量的值。
  2. 创建新窗口,将其存储为旧变量。
  3. 将第二个曲面绘制到第一个曲面上(旧变量) - 使用blit功能
  4. 如果您不想浪费内存,请使用此变量并删除新变量(可选,使用del)。

对上述方案的一些示例代码(云在pygame.VIDEORESIZE事件if声明,在开始):

  old_surface_saved = surface 
      surface = pygame.display.set_mode((event.w, event.h), 
               pygame.RESIZABLE) 
      # On the next line, if only part of the window needs to be copied, there's some other options. 
      surface.blit(old_surface_saved, (0,0)) 
      del old_surface_saved # This line may not be needed.