繁体   English   中英

pygame中无法调用模块对象

[英]module object is not callable in 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已导入,因此可以正常工作。

您需要卸下支架

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

这样,您就不会调用模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM