2016-03-06 30 views
0

我有解釋的數組:如何使用我的播放器的座標和陣列中任意塊的座標來檢測碰撞?

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表示的塊。 A 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 

我曾嘗試一些其他的方式,但我擺脫他們的時候,他們沒有工作。

任何幫助表示讚賞。 任何問題都將盡快回復。

感謝

+0

雖然我不確定如何回答這個問題,但在實際的pygame窗口中,您可以使用pygame組來檢測碰撞。查看此鏈接,瞭解可用於檢測精靈組之間衝突的方法的詳細信息。 http://www.pygame.org/docs/ref/sprite.html –

+0

我看了一下,但是,我沒有看到我可以如何將它納入我的代碼。不管怎麼說,還是要謝謝你。 – Ruarri

回答

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()