为什么pygame中的这个功能不起作用

时间:2016-02-27 07:53:56

标签: pygame

此代码应该打印多米诺骨牌[4,2],但它只填充表面白色并且不打印任何东西。 我没有收到错误,也无法找出我做错了什么。

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()

1 个答案:

答案 0 :(得分: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))
相关问题