2013-07-22 155 views
1

Pygame問題!當兩個矩陣相撞時,碰撞函數不會檢測到!雖然沒有給出錯誤信息,但是這些rects只是彼此交錯。爲什麼會這樣以及如何修復?一直在努力解決這個同樣的問題!假設的問題點標有評論。提前致謝!Pygame碰撞不起作用

#Start it up 
import pygame 
pygame.init() 
fpsClock = pygame.time.Clock() 
#surface = pygame.display.set_mode((640,480),pygame.FULLSCREEN) 
surface = pygame.display.set_mode((640,480)) 
pygame.display.set_caption('Game Skeleton') 

#Globals and Misc. 
x = 10 
y = 350 
l = 15 
w = 35 
moveX=0 
moveY=0 
characterRect= pygame.Rect(x,y,l,w) 
ground = pygame.Rect(0,385,700,385) 
ledge1= pygame.Rect(310,330,20,20) 
jump=0 
white = (255,255,255) 
black = (0,0,0) 
firebrick = (178,34,34) 
blockRects = [ground,ledge1] 
contact = False 
playOn = True 
var=0 

#standingLeft = pygame.image.load("images/ 
#standingRight = pygame.image.load("images/ 
#walkingRight = pygame.image.load("images/ 
#walkingLeft = pygame.image.load("images/ 
#straightJumping = pygame.image.load("images/ 
#rightJumping = pygame.image.load("images/ 
#leftJumping = pygame.image.load("images/ 
#inquire = pygame.image.load("images/ 
#climbing = pygame.image.load("images/ 


#Game Loop 
while playOn: 

    #Take user input 
    for event in pygame.event.get(): 

     if(event.type==pygame.KEYDOWN): 

      if(event.key==pygame.K_RIGHT): 
       moveX=1 

      if(event.key==pygame.K_LEFT): 
       moveX=-1 

      if(event.key==pygame.K_UP): 
       moveY=-1 

      if(event.key==pygame.K_DOWN): 
       moveY=1 

      if(event.key==pygame.K_SPACE): 
       jump=1 

      if(event.key==pygame.K_ESCAPE): 
       playOn = False 

     if(event.type==pygame.KEYUP): 

      if(event.key==pygame.K_RIGHT): 
       moveX=0 

      if(event.key==pygame.K_LEFT): 
       moveX=0 

      if(event.key==pygame.K_UP): 
       moveY=0 

      if(event.key==pygame.K_DOWN): 
       moveY=0 

    #If user input said to move 
    x = x + moveX 
    y = y + moveY 

    #Jump Code 
    if(jump>0 and contact==False): 
     y=y-jump - 1 
     var=var+1 
     if(var>45): 
      jump=0 
      var=0 

    if(contact==False): 
     y=y+1 

    if(contact!=True): 
     contact==False 

    #These two "ifs" don't appear to be working D: 
    if (ground.colliderect(characterRect)): 
     y=y-1 
     contact == True 

    if(ledge1.colliderect(characterRect)): 
     y=y-1 
     contact == True 

    #Renderings  
    surface.fill(white) 
    pygame.draw.rect(surface,black,(x,y,l,w)) 
    pygame.draw.rect(surface,firebrick,(0,385,700,385)) 
    pygame.draw.rect(surface,firebrick,(340,350,100,5)) 
    fpsClock.tick(80) 
    pygame.display.flip() 

pygame.quit() 
+0

哦!我明白你的意思了......但是,給我打電話n00b,但是如何在每次迭代中呈現characterRect? pygame.draw.characterRect不起作用,但我敢肯定,這是開放的。我需要更多的變量嗎? – user2597443

+0

'y = y + moveY'與'y + = moveY'相同,'y = y - 1'與'y - = 1'相同。 – ninMonkey

+0

是的,謝謝。我只是覺得使用更長的方法更舒服,因爲我習慣了從Python這個過時的課程中學習。我應該現在應該停止,但是... – user2597443

回答

2

要初始化:

characterRect= pygame.Rect(x,y,l,w) 

則進行更新X,Y,L,W變量獨立,忘了characterRect,所以characterRect RECT總是在相同的位置。

您可以直接在characterRect上進行更新,或在檢查colliderect之前指定新的y值。

@Justin皮爾斯更正也很重要,否則您的代碼將無法正常工作。

另請參閱PEP 8。對於一個好看的python代碼,你應該在if條件周圍去掉括號。

+0

要添加,你應該這樣做:'characterRect.y - = 1'有很多虛擬矩形屬性。請參閱:http://www.pygame.org/docs/ref/rect.html – ninMonkey

+0

非常感謝你!我現在明白了!...除了當我從x - > characterRect.x和y - > characterRect.y切換時,我無法移動。大聲笑我只是很高興這不是同樣的問題!乾杯! – user2597443

+0

也許你動起來,但你仍然在繪製(x,y,l,w)矩形? – pmoleri

1

你有這樣的:

if(contact!=True): 
    contact==False 

#These two "ifs" don't appear to be working D: 
if (ground.colliderect(characterRect)): 
    y=y-1 
    contact == True 

if(ledge1.colliderect(characterRect)): 
    y=y-1 
    contact == True 

應該是不?:

if(contact!=True): 
    contact=False 

#These two "ifs" don't appear to be working D: 
if (ground.colliderect(characterRect)): 
    y=y-1 
    contact = True 

if(ledge1.colliderect(characterRect)): 
    y=y-1 
    contact = True 

雙等於是比較運營商,而不是一個賦值運算符