2014-12-24 65 views
1

當我運行此代碼時,它顯示一個空白的黑色窗口,它說x未定義。在pygame中打開一個空白的黑色窗口

import pygame,sys 
from pygame.locals import * 
pygame.init() 
screen = pygame.display.set_mode((800,500)) 
screen.fill((255,255,255)) 

#basic stuff 


pygame.draw.line(screen,(0,0,0),(500,0),(500,500)) 
    pygame.draw.rect(screen, (0,255,0), (20,50,460,420)) 
    #pygame.draw.line(screen, (0,0,0), (500,0),(500,500)) 
    #pygame.draw.line(screen, (0,0,0), (500,500),(0,500)) 

#draw 

while 1: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == MOUSEBUTTONDOWN: 
      pos = pygame.mouse.get_pos() 
      x = int(pos[0]//20) 
      y = int(pos[1]//20) 
      j = int(x*20+20) 
      l = int(y*20+20) 
      print(x+1,' ',y+1) 
     if x > 2 and y > 3 and x < 24 and y < 24: 
      pygame.quit() 

其不是顏色。它的劑量不作出任何強硬的線條給我的命令

myfont = pygame.font.SysFont("monospace", 20) 


    label = myfont.render("Allameh helli 3 stock exchange group",1,0,0,0)) 
    screen.blit(label, (30, 10)) 

什麼是我的錯誤(S)在這裏,使我無法使得畫面白色和提高的錯誤?

回答

0

您需要添加此行您while循環,但不是在你的for循環:

screen.fill([255, 255, 255]) 

外這行代碼和while循環後:

pygame.quit() 

你有不是定義爲x也不是y。您需要重新定義這些變量,包括pos以防止出現錯誤。錯誤在於你的if語句:

 if x > 2 and y > 3 and x < 24 and y < 24: 
      pygame.quit() 

與上面的不一致。該程序然後不能看到什麼xy是,提高了錯誤。順便說一句,你的while循環很好。我希望這可以幫助你!

+0

感謝您的幫助。 – kiant