pygame意外显示的表面

时间:2018-11-02 12:08:39

标签: python pygame

我正在制作蛇游戏,到目前为止,它运行得很顺利,但是它在左上方显示了一条我无法摆脱的蛇块。 我检查了是否没有在此处绘制表面(0,0)。我被卡住了。请帮帮我,谢谢!


顺便说一句,这是我第一次问一个问题,因此对此的任何建议也将受到赞赏。


编辑:我发现使用常规类而不是精灵可以解决问题,但是我需要在精灵中使用碰撞和其他功能。

document.addEventListener("click",function(event){
var target = event.target;
if(target.tagName.toLowerCase() == "rect"){
alert("rect clicked");
} 
},false);

2 个答案:

答案 0 :(得分:2)

您将player1添加到snakes子画面组,并使用self.snakes.draw(self.screen)进行绘制。但是,您self.player1.update()的最后一行中绘制播放器。

删除self.snakes.draw(self.screen)以摆脱幻影蛇。

顺便说一句:您创建并设置了self.background,但是立即用self.screen.fill((0,0,0))覆盖了它,因此根本不需要背景。

答案 1 :(得分:0)

您在左上角看到的是player1精灵的self.image。精灵组的draw方法会在精灵的image坐标处使rect.topleft变白,并且由于您从不移动player1.rect,因此图像会默认变白( 0,0)座标。因此,只需删除行self.snakes.draw(self.screen)即可解决此问题。

我还建议您使用pygame.Rect而不是self.xself.y列表。您可以使用init_xinit_y坐标作为topleft属性创建rect实例,并将它们放入self.rects列表中。这使您可以简化更新和绘制方法,并且rect也可以用于碰撞检测。我已经重构了您的代码(它变成了迷你代码审查):

import pygame


class Snake(pygame.sprite.Sprite):  # Use upper camelcase names for classes (see PEP 8).

    def __init__(self, init_x, init_y, image,screen):
        pygame.sprite.Sprite.__init__(self)
        # These are instance attributes now (use class attributes if
        # the values should be shared between the instances).
        self.speed = 5
        self.init_length = 10
        self.direction = 0
        self.updateCountMax = 2
        self.updateCount = 0
        self.length = 10
        # The body parts are rects now.
        self.rects = []
        for i in range(self.init_length):
            # Append pygame.Rect instances.
            self.rects.append(pygame.Rect(init_x, init_y, 11, 11))

        self.image = image
        self.screen = screen
        self.rect = self.rects[0]  # I use the first rect as the self.rect.

    def update(self):
        for i in range(self.length-1, 0, -1):
            # Update the topleft (x, y) positions of the rects.
            self.rects[i].topleft = self.rects[i-1].topleft

        if self.direction == 0:
            self.rects[0].x += self.speed
        elif self.direction == 1:
            self.rects[0].x -= self.speed
        elif self.direction == 2:
            self.rects[0].y -= self.speed
        elif self.direction == 3:
            self.rects[0].y += self.speed

    def draw(self):
        # Iterate over the rects to blit them (I draw the outlines as well).
        for rect in self.rects:
            self.screen.blit(self.image, rect)
            pygame.draw.rect(self.screen, (0, 255, 0), rect, 1)


class App:
    width = 1200
    height = 900
    title = "Snake"
    done = False

    def __init__(self):
        pygame.init()
        self.image = pygame.Surface((11, 11))
        self.image.fill((0, 128, 255))
        pygame.display.set_caption(self.title)
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.clock = pygame.time.Clock()
        self.snakes = pygame.sprite.Group()
        self.player1 = Snake(500, 10, self.image, self.screen)
        self.snakes.add(self.player1)

    def loop(self):
        while not self.done:
            # Handle the events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        self.done = True

            keys = pygame.key.get_pressed()
            # In Python we simply set the values of the
            # attributes directly instead of using getter
            # and setter methods.
            if keys[pygame.K_RIGHT]:
                self.player1.direction = 0
            if keys[pygame.K_LEFT]:
                self.player1.direction = 1
            if keys[pygame.K_UP]:
                self.player1.direction = 2
            if keys[pygame.K_DOWN]:
                self.player1.direction = 3
            if keys[pygame.K_ESCAPE]:
                self.done = True

            # Update the game.
            self.player1.update()

            # Draw everything.
            self.screen.fill((0, 0, 0))
            self.player1.draw()
            pygame.draw.rect(self.screen, (255, 0, 0), self.player1.rect, 1)
            pygame.display.update()
            self.clock.tick(60)
        pygame.quit()

if __name__ == "__main__" :
    the_app = App()
    the_app.loop()