2017-08-06 142 views
1

作为pygame的新手,我在网上搜索了如何创建矩形。然后我使用该代码,但运行时只显示一个空白屏幕。没有显示错误代码,但代码没有像我想象的那样运行。我究竟做错了什么?使用Pygame创建矩形

import pygame, sys 
import pygame.locals as GAME_GLOBALS 
import pygame.event as GAME_EVENTS 

pygame.init 

#window variables 
WindowHeight = 480 
WindowWidth = 640 


surface = pygame.display.set_mode((WindowWidth,WindowHeight)) 
surface.fill((255,255,255)) 


pygame.draw.rect(surface,(100,0,155),(0,0,100,100)) 




while True: 
    for event in GAME_EVENTS.get(): 
     if event.type == GAME_GLOBALS.QUIT: 
      pygame.quit() 
      sys.exit() 
+0

你不*调用*'pygame.init'。 – jonrsharpe

+0

窗口显示 –

回答

0

你的代码是很漂亮,但写的一个很好的pygame的代码开始:

import pygame, sys 
from pygame.locals import * 


WindowHeight = 480 
WindowWidth = 640 

def main(): 
    pygame.init() 

    DISPLAY=pygame.display.set_mode((WindowWidth,WindowHeight),0,32) 

    WHITE=(255,255,255) 
    blue=(0,0,255) 

    DISPLAY.fill(WHITE) 

    pygame.draw.rect(DISPLAY,blue,(200,150,100,50)) 

    while True: 
     for event in pygame.event.get(): 
      if event.type==QUIT: 
       pygame.quit() 
       sys.exit() 
     pygame.display.update() 

main() 

把一切都在main功能,init pygame的和update显示。

这创建了一个简单的白色窗口。窗口内将是一个蓝色的矩形。查看pygame.org获取更多信息