我想允许调整这个应用程序的大小,我把调整大小的标志,但是当我尝试调整大小时,它搞砸了!试试我的代码。允许调整大小的窗口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()
请告诉我什么是错的,谢谢。
代码的工作,但你应该阅读PEP 8风格指南。你已经用许多不同类型的约定,例如不是类的'CreateWindow',不是常量的'helloWorld',*是*和'DISPLAYSURF'。另外,避免在任何地方发送'from ... import *'垃圾邮件,特别是因为您没有使用它们(无论如何,您都是在所有'pygame'调用的前缀) – MestreLion 2014-08-17 19:15:19