【问题标题】:How can I use the coordinates of my player and the coordinates of any block in an array to detect collision?如何使用我的玩家的坐标和数组中任何块的坐标来检测碰撞?
【发布时间】:2016-03-06 06:13:38
【问题描述】:

我有列表数组:

blocks0 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1]
blocks1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks2 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks3 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks4 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]                   
blocks5 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks6 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks7 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks8 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks9 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks10= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks11= [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ,1 ,1 ,1 ,1]

blockList = [blocks0, blocks1, blocks2, blocks3, blocks4, blocks5, blocks6, blocks7, blocks8, blocks9, blocks10, blocks11]

1 表示一个块。 0 表示开放空间。 每个数字是一个 50x50 像素块/空间。 使用 blockList[y][x] 我可以分别找到每个数字。

y 表示列表组中的哪个列表。 x 表示列表 y 中的哪个数字。

但是我没有在我的代码中使用 x 和 y。

如果我使用箭头键,我有一个 50x50 像素的角色,每个循环移动 10 个像素。

如何检测我的播放器与数字 1 之间的碰撞?

我试过了:

lefX = x
rigX = x + player_width
topY = y
botY = y + player_height

lefX -= int(lefX % 50)
rigX -= int(rigX % 50)
topY -= int(topY % 50)
botY -= int(botY % 50)

lefX = int(lefX/50)
rigX = int(rigX/50)
topY = int(topY/50)
botY = int(botY/50)

if blockList[topY][lefX] == 1:     ##This is just for going left.
    if not((blockList[topY][lefX]*50)+50 >= x):
        x += x_changeLeft
else:
    x += x_changeLeft

我确实尝试了一些其他方法,但是当它们不起作用时我就摆脱了它们。

感谢任何帮助。 任何问题都会尽快得到解答。

谢谢

【问题讨论】:

  • 虽然我不确定如何回答这个问题,但在实际的 pygame 窗口中,您可以使用 pygame Groups 来检测碰撞。查看此链接以获取有关可用于检测精灵组之间冲突的方法的详细信息。 pygame.org/docs/ref/sprite.html
  • 我看过了,但是,我没有看到如何将其合并到我的代码中。无论如何,谢谢。

标签: pygame collision-detection python-3.4


【解决方案1】:

(我已将块大小更改为 20,但您可以轻松调整回来。)使用 z/x/p/l 移动播放器。

import pygame, sys
from pygame.locals import *

blocks0 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1]
blocks1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks2 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks3 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks4 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]                   
blocks5 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks6 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks7 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks8 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks9 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks10= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks11= [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ,1 ,1 ,1 ,1]

blockList = [blocks0, blocks1, blocks2, blocks3, blocks4, blocks5, blocks6, blocks7, blocks8, blocks9, blocks10, blocks11]

class player:

    def __init__(self):
        self.row = 3
        self.col = 4
        self.leftcount = 0
        self.rightcount = 0
        self.upcount = 0
        self.downcount = 0

pygame.init()
size = width, height = 510, 700
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0,0,0))

def init():
    global theplayer, clock
    theplayer = player()
    clock = pygame.time.Clock() 

def run():

    global theplayer

    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                sys.exit()

        keys=pygame.key.get_pressed()

        if keys[K_z]:
            theplayer.leftcount+=1
            if theplayer.leftcount == 5:
                theplayer.leftcount = 0
                if theplayer.col>0:
                    theplayer.col -=1
                    if blockList[theplayer.row][theplayer.col] == 1:
                        print "hit"

        if keys[K_x]:
            theplayer.rightcount+=1
            if theplayer.rightcount == 5:
                theplayer.rightcount = 0
                if theplayer.col<15:
                    theplayer.col +=1
                    if blockList[theplayer.row][theplayer.col] == 1:
                        print "hit"

        if keys[K_p]:
            theplayer.upcount+=1
            if theplayer.upcount == 5:
                theplayer.upcount = 0
                if theplayer.row>0:
                    theplayer.row -=1
                    if blockList[theplayer.row][theplayer.col] == 1:
                        print "hit"

        if keys[K_l]:
            theplayer.downcount+=1
            if theplayer.downcount == 5:
                theplayer.downcount = 0
                if theplayer.row <11:
                    theplayer.row +=1
                    if blockList[theplayer.row][theplayer.col] == 1:
                        print "hit"

        clock.tick(30)  
        update()

def update():

        background.fill((0,0,0))
        size = 20

        for row in range(12):
            for col in range(16):
                if blockList[row][col] == 1:
                    pygame.draw.rect(background, (255,255,255), (size*col, size*row, size, size))

        pygame.draw.rect(background, (255,255,255), (theplayer.col*size, theplayer.row*size, size, size))
        screen.blit(background, (0, 0))
        pygame.display.flip()


if __name__ == "__main__":
    init()
    run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2016-02-17
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多