蛇游戏:蛇与自身碰撞

时间:2018-12-31 13:11:00

标签: python pygame

嗨,我目前正在编写蛇游戏代码,但我快要写完了,但是我很难编写代码,如果蛇的头部与身体碰撞,它将导致游戏结束,我以为我可以创造一个碰撞功能类似于对蛇和苹果的碰撞功能:

pygame.sprite.collide_rect(h, a)

但是,蛇的所有单独部分都以相同的方式起作用,因此蛇将始终不断与自身碰撞。有没有解决的办法?

这是我完整的蛇代码:

import pygame
import random


BLACK = (0, 0, 0)
GREEN = (0, 250, 0)
RED = (250, 0, 0)
Width = 15
Space = 3
Xspeed = 18
Yspeed = 0
Factor = 18
clock = pygame.time.Clock()
segments = 2
HitLoop = 0
ScreenWidth = 800
AppleCount = 1

#creating initial snake
class HEAD(pygame.sprite.Sprite):
    def __init__(self, x, y, colour = GREEN):
        super().__init__()
        self.image = pygame.Surface([Width, Width])
        self.image.fill(colour)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class APPLE(pygame.sprite.Sprite):
    def __init__(self, z, q):
        super().__init__()
        self.image = pygame.Surface([Width, Width])
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = z
        self.rect.y = q

pygame.init()
screen = pygame.display.set_mode([ScreenWidth, ScreenWidth])
pygame.display.set_caption('Snake')
allspriteslist = pygame.sprite.Group()


SnakeSegments = []
for i in range(segments):
    x = 250 - (Width + Space) * i
    y = 30
    h = HEAD(x, y)
    SnakeSegments.append(h)
    allspriteslist.add(h)

AppleList = []
for i in range(0,AppleCount):
    z = random.randint(10,ScreenWidth-25)
    q = random.randint(10,ScreenWidth-25)
    a = APPLE(z, q)
    AppleList.append(a)
    allspriteslist.add(a)

#main loop
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if Xspeed == -Factor:
                    Xspeed = 0
                if Xspeed == Factor:
                    Xspeed = Factor
                else:
                    Xspeed = Xspeed - Factor
                    Yspeed = 0
            elif event.key == pygame.K_RIGHT:
                if Xspeed == Factor:
                    Xspeed = 0
                if Xspeed == -Factor:
                    Xspeed = -Factor
                else:
                    Xspeed = Xspeed + Factor
                    Yspeed = 0
            elif event.key == pygame.K_UP:
                if Yspeed == -Factor:
                    Yspeed = 0
                if Yspeed == Factor:
                    Yspeed = Factor
                else:
                    Yspeed = Yspeed - Factor
                    Xspeed = 0
            elif event.key == pygame.K_DOWN:
                if Yspeed == Factor:
                    Yspeed = 0
                if Yspeed == -Factor:
                    Yspeed = -Factor
                else:
                    Yspeed = Yspeed + Factor
                    Xspeed = 0
    clock.tick(10)
    #snake builder
    OldSegment = SnakeSegments.pop(-1)
    allspriteslist.remove(OldSegment)

    x = SnakeSegments[0].rect.x + Xspeed
    y = SnakeSegments[0].rect.y + Yspeed

    h = HEAD(x, y)
    SnakeSegments.insert(0, h)
    allspriteslist.add(h,a)
    allspriteslist.update()

    # collision had to create apples own list for respawn
    if pygame.sprite.collide_rect(h, a) == True and HitLoop == 0:
        SnakeSegments.append(h)
        AppleList.append(a)
        HitLoop = HitLoop + 1
        z = random.randint(10, ScreenWidth - 25)
        q = random.randint(10, ScreenWidth - 25)
        OldApple = AppleList.pop()
        allspriteslist.remove(OldApple)
        a = APPLE(z, q)
        allspriteslist.update()

# collision had to create a new class
    if pygame.sprite.collide_rect(h, h) == True:
        pass


    # hit timer
    if HitLoop > 0:
        HitLoop += 1
    if HitLoop > 4:
        HitLoop = 0

    screen.fill(BLACK)

    #game walls
    pygame.draw.rect(screen, GREEN, [0, 0, ScreenWidth, 10])
    pygame.draw.rect(screen, GREEN, [0, 0, 10, ScreenWidth])
    pygame.draw.rect(screen, GREEN, [0, ScreenWidth - 10, ScreenWidth, 10])
    pygame.draw.rect(screen, GREEN, [ScreenWidth - 10, 0, 10, ScreenWidth])
    if x <= 10:
        done = True
    if x >= ScreenWidth - Width:
        done = True
    if y <= 10:
        done = True
    if y >= ScreenWidth - Width:
        done = True


    allspriteslist.draw(screen)
    pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

在您的代码中,好像您正在检查蛇的头部是否与自身碰撞,这将始终返回True。您需要单独检查蛇的头部是否不与任何尾随段碰撞:

for segment in SnakeSegments[2:]:
    if pygame.sprite.collide_rect(h, segment):
        pass # collision detected, game-over

预计蛇的头部会与蛇的正后方碰撞,这就是为什么我们需要从列表中的第3个元素开始。