2016-02-27 102 views
0

這段代碼應該打印多米諾骨牌[4,2],但它只填充表面白色並且不打印任何東西。 我沒有得到一個錯誤,我找不到我做錯了什麼。爲什麼pygame中的這個函數不起作用

import os 
import string 
import random 
import pygame, sys 
from pygame.locals import * 

BLACK = ( 0, 0, 0) 
WHITE = (255, 255, 255) 

def main(): 
    global DISPLAYSURF 
    pygame.init() 
    DISPLAYSURF = pygame.display.set_mode((1300, 600), 0, 32) 
    pygame.display.set_caption('domino test') 

    while True: 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
     DISPLAYSURF.fill(WHITE) 
     printpiece([4,2],(400,200)) 
     pygame.display.update() 

def printpiece(piece, position): 
    def printdots(number, centre): 
     def printdot(dot, centre): 
      if dot == "AA": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]-15), 3, 3) 
      elif dot == "AB": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]), 3, 3) 
      elif dot == "AC": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]+15), 3, 3) 
      elif dot == "BB": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0], centre[1]), 3, 3) 
      elif dot == "CA": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]-15), 3, 3) 
      elif dot == "CB": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]), 3, 3) 
      elif dot == "CC": 
       pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]+15), 3, 3) 

     if number == 1: 
      printdot("BB", centre) 
     elif number == 2: 
      printdot("AC", centre) 
      printdot("CA", centre) 
     elif number == 3: 
      printdot("AC", centre) 
      printdot("BB", centre) 
      printdot("CA", centre) 
     elif number == 4: 
      printdot("AA", centre) 
      printdot("CA", centre) 
      printdot("AC", centre) 
      printdot("CC", centre) 
     elif number == 5: 
      printdot("AA", centre) 
      printdot("CA", centre) 
      printdot("BB", centre) 
      printdot("AC", centre) 
      printdot("CC", centre) 
     elif number == 6: 
      printdot("AA", centre) 
      printdot("AB", centre) 
      printdot("AC", centre) 
      printdot("CA", centre) 
      printdot("CB", centre) 
      printdot("CC", centre) 

     pygame.draw.rect(DISPLAYSURF, BLACK, (position[0]-5, position[1]-5, 110, 60)) 
     pygame.draw.rect(DISPLAYSURF, WHITE, (position[0], position[1], 100, 50)) 
     pygame.draw.line(DISPLAYSURF, BLACK, (position[0]+50, position[1]), (position[0]+50, position[1]+50), 2) 
     printdots(piece[0], (position[0]+25, position[1]+25)) 
     printdots(piece[1], (position[0]+75, position[1]+25)) 

if __name__ == '__main__': 
    main() 

回答

0

您的縮進是錯誤的。 printpiece(piece, position)不執行任何語句。

從這些線的前面刪除一個標籤,它會工作:

pygame.draw.rect(DISPLAYSURF, BLACK, (position[0]-5, position[1]-5, 110, 60)) 
    pygame.draw.rect(DISPLAYSURF, WHITE, (position[0], position[1], 100, 50)) 
    pygame.draw.line(DISPLAYSURF, BLACK, (position[0]+50, position[1]), (position[0]+50, position[1]+50), 2) 
    printdots(piece[0], (position[0]+25, position[1]+25)) 
    printdots(piece[1], (position[0]+75, position[1]+25)) 
+0

謝謝你,我好高興我能夠繼續現在的工作計劃,我定了這個bug! – zom4