pygame 意外显示的表面

我正在制作一个蛇游戏,到目前为止它大部分进展顺利,但它在左上角显示了一个我无法摆脱的蛇块。我检查过我没有在那里绘制表面(0,0)。我被困住了。请帮帮我,谢谢!!


顺便说一句,这是我第一次提出问题,因此对任何建议也表示赞赏。


编辑:我发现使用常规类而不是精灵解决了这个问题,但我需要精灵中的碰撞和其他函数。


import pygame


class snake(pygame.sprite.Sprite):

    speed=5

    init_length=10

    direction=0

    x=[]

    y=[]

    updateCountMax = 2

    updateCount = 0

    length=10

#    image=pygame.Surface((11,11)).convert().fill((0,128,255))

    def __init__(self,init_x,init_y,image,screen):

        pygame.sprite.Sprite.__init__(self)


        for i in range(0,self.init_length):

            self.x.append(init_x)

            self.y.append(init_y)

#        for i in range(0,self.length):

#            print(f"{self.x[i]},{self.y[i]}")

        for x in self.x:

            print(x)

        for y in self.y:

            print(y)

        self.image=image

        self.screen=screen

        self.rect=self.image.get_rect()

#        self.rect.center=(self.x,self.y)


    def move_R(self):

#        self.x+=self.speed

        self.direction=0

    def move_L(self):

#        self.x-=self.speed

        self.direction=1

    def move_U(self):

#        self.y-=self.speed

        self.direction=2

    def move_D(self):

#        self.y+=self.speed

        self.direction=3


    def update(self):

#        self.updateCount = self.updateCount + 1

#        if self.updateCount < self.updateCountMax:

        for i in range(self.length-1,0,-1):

#                print("self.x[" + str(i) + "] = self.x[" + str(i-1) + "]")

            self.x[i] = self.x[i-1]

            self.y[i] = self.y[i-1]


        if(self.direction==0):

            self.x[0]+=self.speed

        elif(self.direction==1):

            self.x[0]-=self.speed

        elif(self.direction==2):

            self.y[0]-=self.speed

        elif(self.direction==3):

            self.y[0]+=self.speed

#        self.rect.center=(self.x,self.y)

#        self.updateCount = 0

#        for i in range(0,self.length):

#            print(f"{self.x[i]},{self.y[i]}")

        self.draw()


一只名叫tom的猫
浏览 133回答 2
2回答

Smart猫小萌

您在左上角看到的是self.imageplayer1 精灵的 。draw精灵组的方法在精灵image的rect.topleft坐标处blit s并且由于您从不移动player1.rect,图像将在默认的 (0, 0) 坐标处 blit。因此,只需删除该行self.snakes.draw(self.screen)即可解决此问题。我还建议您使用pygame.Rects 而不是self.xandself.y列表。您可以使用 theinit_x和init_ycoords 作为topleft属性创建 rect 实例并将它们放入self.rects列表中。这允许您简化更新和绘制方法,并且矩形也可用于碰撞检测。我已经重构了你的代码(它变成了一个小型的代码审查):import pygameclass Snake(pygame.sprite.Sprite):&nbsp; # Use upper camelcase names for classes (see PEP 8).&nbsp; &nbsp; def __init__(self, init_x, init_y, image,screen):&nbsp; &nbsp; &nbsp; &nbsp; pygame.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; # These are instance attributes now (use class attributes if&nbsp; &nbsp; &nbsp; &nbsp; # the values should be shared between the instances).&nbsp; &nbsp; &nbsp; &nbsp; self.speed = 5&nbsp; &nbsp; &nbsp; &nbsp; self.init_length = 10&nbsp; &nbsp; &nbsp; &nbsp; self.direction = 0&nbsp; &nbsp; &nbsp; &nbsp; self.updateCountMax = 2&nbsp; &nbsp; &nbsp; &nbsp; self.updateCount = 0&nbsp; &nbsp; &nbsp; &nbsp; self.length = 10&nbsp; &nbsp; &nbsp; &nbsp; # The body parts are rects now.&nbsp; &nbsp; &nbsp; &nbsp; self.rects = []&nbsp; &nbsp; &nbsp; &nbsp; for i in range(self.init_length):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Append pygame.Rect instances.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects.append(pygame.Rect(init_x, init_y, 11, 11))&nbsp; &nbsp; &nbsp; &nbsp; self.image = image&nbsp; &nbsp; &nbsp; &nbsp; self.screen = screen&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.rects[0]&nbsp; # I use the first rect as the self.rect.&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; for i in range(self.length-1, 0, -1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Update the topleft (x, y) positions of the rects.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects[i].topleft = self.rects[i-1].topleft&nbsp; &nbsp; &nbsp; &nbsp; if self.direction == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects[0].x += self.speed&nbsp; &nbsp; &nbsp; &nbsp; elif self.direction == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects[0].x -= self.speed&nbsp; &nbsp; &nbsp; &nbsp; elif self.direction == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects[0].y -= self.speed&nbsp; &nbsp; &nbsp; &nbsp; elif self.direction == 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rects[0].y += self.speed&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; # Iterate over the rects to blit them (I draw the outlines as well).&nbsp; &nbsp; &nbsp; &nbsp; for rect in self.rects:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.screen.blit(self.image, rect)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.rect(self.screen, (0, 255, 0), rect, 1)class App:&nbsp; &nbsp; width = 1200&nbsp; &nbsp; height = 900&nbsp; &nbsp; title = "Snake"&nbsp; &nbsp; done = False&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pygame.init()&nbsp; &nbsp; &nbsp; &nbsp; self.image = pygame.Surface((11, 11))&nbsp; &nbsp; &nbsp; &nbsp; self.image.fill((0, 128, 255))&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_caption(self.title)&nbsp; &nbsp; &nbsp; &nbsp; self.screen = pygame.display.set_mode((self.width, self.height))&nbsp; &nbsp; &nbsp; &nbsp; self.clock = pygame.time.Clock()&nbsp; &nbsp; &nbsp; &nbsp; self.snakes = pygame.sprite.Group()&nbsp; &nbsp; &nbsp; &nbsp; self.player1 = Snake(500, 10, self.image, self.screen)&nbsp; &nbsp; &nbsp; &nbsp; self.snakes.add(self.player1)&nbsp; &nbsp; def loop(self):&nbsp; &nbsp; &nbsp; &nbsp; while not self.done:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Handle the events.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.done = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keys = pygame.key.get_pressed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # In Python we simply set the values of the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # attributes directly instead of using getter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # and setter methods.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_RIGHT]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.direction = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_LEFT]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.direction = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_UP]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.direction = 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_DOWN]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.direction = 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_ESCAPE]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.done = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Update the game.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Draw everything.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.screen.fill((0, 0, 0))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.player1.draw()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.rect(self.screen, (255, 0, 0), self.player1.rect, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.clock.tick(60)&nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()if __name__ == "__main__" :&nbsp; &nbsp; the_app = App()&nbsp; &nbsp; the_app.loop()

米脂

您添加player1到snakes精灵组并使用self.snakes.draw(self.screen).&nbsp;但是,您还在self.player1.update(), 最后一行中绘制了播放器。移除self.snakes.draw(self.screen)以摆脱幻影蛇。顺便说一句:您创建并设置了 aself.background但您立即用 覆盖了它self.screen.fill((0,0,0)),因此您根本不需要背景。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python