2016-02-27 103 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