首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >与元组的博弈碰撞

与元组的博弈碰撞
EN

Stack Overflow用户
提问于 2020-10-25 18:52:29
回答 3查看 117关注 0票数 1

所以我正在制作一个蛇游戏,我很难发现它是自碰撞的,我需要理解的是:我的蛇是一个元组。

代码语言:javascript
运行
复制
snake = [(s_pos, s_pos),(s_pos + size, s_pos),(s_pos + size * 2, s_pos)]

就像这样,当它生长时,它在列表上创建了另一个东西,就像上面的代码直到snake[3]为止,我知道如何检测与蛇的前3部分的碰撞,但是当它上升时,它说索引超出了范围,这是碰撞代码

代码语言:javascript
运行
复制
def collision (c1,c2):
    return (c1[0] == c2[0]) and (c1[1] == c2[1])

if collision(snake[0], snake[1]):
        print("self collision")

有没有办法像

代码语言:javascript
运行
复制
if collision(snake[0], snake[>1]):
        print("self collision")

没有超出范围的指数?

当蛇生长时,它会创建snake4,我不知道如何创建一个代码,在这里我可以检测到冲突,对不起,如果我不清楚,那就是使蛇成长的代码:snake.append((0,0))

我所有的代码(认为我对编码非常陌生):

代码语言:javascript
运行
复制
import pygame, random as rand, os

from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1020, 585))
pygame.display.set_caption('2snakes!')
#files location
current_path = os.path.dirname(__file__)
data_path = os.path.join(current_path, 'data')
icon = pygame.image.load(os.path.join(data_path, 'icon.png'))
pygame.display.set_icon(icon)

#variables
direction = 'RIGHT'
direction2 = 'RIGHT'
change_to = direction
change2_to = direction2
fps = 15


#snake
size = 15
s_pos = 60
snake = [(s_pos, s_pos),(s_pos + size, s_pos),(s_pos + size * 2, s_pos)]
s_skin = pygame.Surface((size, size))
s_skin.fill((82,128,208))
#snake2
size2 = 15
s2_pos = 90
snake2 = [(s2_pos, s2_pos),(s2_pos + size2, s2_pos),(s2_pos + size2 * 2, s2_pos)]
s2_skin = pygame.Surface((size2, size2))
s2_skin.fill((208,128,82))
#apple
apple = pygame.Surface((size, size))
apple_pos = (165, 150)
#collission
def collision (c1,c2):
    return (c1[0] == c2[0]) and (c1[1] == c2[1])

def selfColliding(snake: list):
        for i in snake:
            if snake.count(i) > 1:
                return True
        return False

def gameOver():
    #pygame.quit()
    print("gameover")

def selfColliding(snake: list):
    return True if snake.count(snake[0]) > 1 else False

while True:
    #fps
    pygame.time.Clock().tick(fps)
    #basic stuff
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        #input
        elif event.type == pygame.KEYDOWN:
            #snake
            if event.key == ord('w'):
                change_to = 'UP'
            if event.key == ord('s'):
                change_to = 'DOWN'
            if event.key == ord('a'):
                change_to = 'LEFT'
            if event.key == ord('d'):
                change_to = 'RIGHT'           
            #snake2
            if event.key == pygame.K_UP:
                change2_to = 'UP'
            if event.key == pygame.K_DOWN:
                change2_to = 'DOWN'
            if event.key == pygame.K_LEFT:
                change2_to = 'LEFT'
            if event.key == pygame.K_RIGHT:
                change2_to = 'RIGHT'
            #quit game
            if event.key == pygame.K_ESCAPE:
                pygame.event.post(pygame.event.Event(pygame.QUIT))
    #noice smooth snake movement
        #snake
    if change_to == 'UP' and direction != 'DOWN':
        direction = 'UP'
    if change_to == 'DOWN' and direction != 'UP':
        direction = 'DOWN'
    if change_to == 'LEFT' and direction != 'RIGHT':
        direction = 'LEFT'
    if change_to == 'RIGHT' and direction != 'LEFT':
        direction = 'RIGHT'
        #snake2
    if change2_to == 'UP' and direction2 != 'DOWN':
        direction2 = 'UP'
    if change2_to == 'DOWN' and direction2 != 'UP':
        direction2 = 'DOWN'
    if change2_to == 'LEFT' and direction2 != 'RIGHT':
        direction2 = 'LEFT'
    if change2_to == 'RIGHT' and direction2 != 'LEFT':
        direction2 = 'RIGHT'
    #movement
        #snake
    if direction == 'DOWN':
        snake[0] = (snake[0][0], snake[0][1] + size)
    if direction == 'UP':
        snake[0] = (snake[0][0], snake[0][1] - size)
    if direction == 'LEFT':
        snake[0] = (snake[0][0] - size, snake[0][1])
    if direction == 'RIGHT':
        snake[0] = (snake[0][0] + size, snake[0][1])
        #snake2
    if direction2 == 'DOWN':
        snake2[0] = (snake2[0][0], snake2[0][1] + size2)
    if direction2 == 'UP':
        snake2[0] = (snake2[0][0], snake2[0][1] - size2)
    if direction2 == 'LEFT':
        snake2[0] = (snake2[0][0] - size2, snake2[0][1])
    if direction2 == 'RIGHT':
        snake2[0] = (snake2[0][0] + size2, snake2[0][1])
    #snake apple collision
    if collision(snake[0], apple_pos):
        snake.append((0,0))
    #snake2 apple collision
    if collision(snake2[0], apple_pos):
        snake2.append((0,0))
    #snake wall collisison
    if snake[0][0] < 0 or snake[0][1] < 0:
        gameOver()
    elif snake[0][0] > 1020 or snake[0][1] > 585:
        gameOver()
    #snake2 wall collisison
    #if snake2[0][0] < 0 or snake2[0][1] < 0:
        #gameOver()
    #elif snake2[0][0] > 1020 or snake2[0][1] > 585:
        #gameOver()

    #all blocks follow first
        #snake
    for i in range(len(snake) -1, 0, -1):
        snake[i] = (snake[i-1][0], snake[i-1][1])
        #snake2
    for i in range(len(snake2) -1, 0, -1):
        snake2[i] = (snake2[i-1][0], snake2[i-1][1])

    if selfColliding:
        print("self colliding")
    

    #rendering
    screen.fill((0,0,0))
    apple.fill((255,0,0))
    screen.blit(apple,apple_pos)
    for pos in snake:
        screen.blit(s_skin,pos)
    for pos2 in snake2:
        screen.blit(s2_skin,pos2)
    pygame.display.update()
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-10-25 19:08:01

如果要测试列表中的第一个元素是否与列表中的任何其他元素相同,可以执行以下操作:

代码语言:javascript
运行
复制
if snake[0] in snake[1:]:
    print("self collision")
    print(snake[0], "is in", snake[1:])

第一个元素似乎包含在您的列表中两次。请参阅How do I chain the movement of a snake's body?并将解决方案应用于您的代码:

代码语言:javascript
运行
复制
direction = 'RIGHT'
direction2 = 'RIGHT'

# [...]

snake = [(s_pos + size * 2, s_pos),(s_pos + size, s_pos),(s_pos, s_pos)]
snake2 = [(s2_pos + size2 * 2, s2_pos),(s2_pos + size2, s2_pos),(s2_pos, s2_pos)]

# [...]

while True:
   # [...]

    new_pos = None
    if direction == 'DOWN':
        new_pos = (snake[0][0], snake[0][1] + size)
    if direction == 'UP':
        new_pos = (snake[0][0], snake[0][1] - size)
    if direction == 'LEFT':
        new_pos = (snake[0][0] - size, snake[0][1])
    if direction == 'RIGHT':
        new_pos = (snake[0][0] + size, snake[0][1])
    if new_pos:
        snake = [new_pos] + snake
        del snake[-1]
    
    new_pos2 = None
    if direction2 == 'DOWN':
        new_pos2 = (snake2[0][0], snake2[0][1] + size2)
    if direction2 == 'UP':
        new_pos2 = (snake2[0][0], snake2[0][1] - size2)
    if direction2 == 'LEFT':
        new_pos2 = (snake2[0][0] - size2, snake2[0][1])
    if direction2 == 'RIGHT':
        new_pos2 = (snake2[0][0] + size2, snake2[0][1])
    if new_pos2:
        snake2 = [new_pos2] + snake2
        del snake2[-1]
    
    #snake apple collision
    if collision(snake[0], apple_pos):
        snake.append((-20,-20))
    #snake2 apple collision
    if collision(snake2[0], apple_pos):
        snake2.append((-20,-20))
    #snake wall collisison
    if snake[0][0] < 0 or snake[0][1] < 0:
        gameOver()
    elif snake[0][0] > 1020 or snake[0][1] > 585:
        gameOver()
        
    if snake[0] in snake[1:]:
        print("self collision")
        print(snake2[0], "is in", snake2[1:])
    if snake2[0] in snake2[1:]:
        print("self collision")
        print(snake2[0], "is in", snake2[1:])
票数 1
EN

Stack Overflow用户

发布于 2020-10-25 19:02:42

在列表中多次检查是否存在任何元组的可能解决方案:

代码语言:javascript
运行
复制
def selfColliding(snake: list):
    for i in snake:
        if snake.count(i) > 1:
            return True
    return False

或者,正如@Rabbid76 76所指出的,如果您只想检查头部是否发生碰撞:

代码语言:javascript
运行
复制
def selfColliding(snake: list):
    return True if snake.count(snake[0]) > 1 else False
票数 0
EN

Stack Overflow用户

发布于 2020-10-25 19:38:20

我发现了(实际上我并没有发现,我只是使用@rabbid76 76代码,我尝试了一些修改,并且成功了!)如何做到:

代码语言:javascript
运行
复制
 if snake[0] in snake[1:][1:]:
        print("self collision")
        print(snake[0], "is in", snake[1:])

我使用了@Rabbid76 76代码,只做了一点小小的改动,它就能工作了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64527543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档