如何使用播放器的坐标和数组中任何块的坐标来检测碰撞?

时间:2016-03-06 06:13:38

标签: pygame collision-detection python-3.4

我有一系列列表:

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]

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

表示列表组中的哪个列表。 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

我确实尝试了其他一些方法,但是当他们没有工作时我就把它们除掉了。

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

由于

1 个答案:

答案 0 :(得分:0)

(我已将块大小更改为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()