pygame中无法调用模块对象

时间:2018-07-14 16:37:38

标签: python python-3.x pygame

    import pygame
    import snake
    pygame.init()

    # Set the height and width of the screen
    screen = pygame.display.set_mode((800,600))

    pygame.display.set_caption("Snake")

    quit = False
    clock = pygame.time.Clock()

    snake = snake.Snake()

    while not quit:

        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                quit = True # Flag that we are done so we exit this loop


        # rectangle = pygame.Rect(400, 150, 100, 60)  #first two = x and y coords
        # pygame.draw.rect(screen, [0, 0, 255], rectangle)
        snake.draw(screen)
        pygame.display.update()

    # Be IDLE friendly
    pygame.quit()



import pygame

BODY_DIM = 50 #dimension for each part of the snake's body
RED = [255, 0, 0]

class Snake:
    #represent the snake as a list of squares
    class BodyNode:
        def __init__(self, coords):
            self.body = pygame.Rect(coords, (BODY_DIM, BODY_DIM))

    def __init__(self):
        self.snake_body = [Snake.BodyNode((50, 50))]

    def draw(self, screen):
        for s in self.snake_body:
            pygame.draw().rect(screen, RED, s.body)

这是两个单独的文件,底部带有import pygame语句的文件位于一个名为Snake.py的文件中。这行似乎是问题所在:pygame.draw()。rect(screen,RED,s.body),虽然我似乎找不到原因。 pygame已导入,因此可以正常工作。

1 个答案:

答案 0 :(得分:0)

您需要卸下括号

pygame.draw().rect(screen,RED,s.body)
#          ^^ HERE

那样您就不会调用模块