2016-04-12 75 views
0
import pygame 
import time 
import random 

pygame.init() 

white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,155,0) 
blue = (0, 0, 200) 

#img = pygame.image.load("rectangle.png") 

displayW = 500 
displayH = 500 

display = pygame.display.set_mode((displayW, displayH)) 
pygame.display.update() 

clock = pygame.time.Clock() 

rectSize = 40 
obstacleSize = 30 

FPS = 30 

def left(obstaclex,obstacley): 
    pygame.draw.rect(display, blue, [0, obstacley, rectSize, rectSize]) 

def right(rectx,recty): 
    pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize]) 

def middleL(obstaclex, obstacley): 
    obstacleSize = -230 
    obstaclex = 500 
    pygame.draw.rect(display, red, [obstaclex,obstacley, obstacleSize, rectSize]) 

def middleR(obstaclex, obstacley): 
    obstacleSize = 210 
    obstaclex = 0 
    #display.blit(img, (obstaclex, obstacley)) 
    pygame.draw.rect(display, red, [obstaclex, obstacley, obstacleSize, 40]) 
def middle(obstaclex, obstacley): 

    middleL(obstaclex, obstacley) 
    middleR(obstaclex, obstacley) 



def gameLoop(): 
    gameExit = False 
    gameOver = False 

    obstacley = 10 
    obstaclex = 1 

    rectx = 220 
    recty = 400 

    rect1 = (rectx, recty, rectSize, rectSize) 


    obstacleRectX = 0 
    obstacleRectY = 20 

    obstacleChangeX = 0 
    obstaleChageY = 0 
    randrect = random.randrange(0,4) 

    myRight = rectx + rectSize 
    myLeft = rectx 
    myTop = recty 
    myBottom = recty + rectSize 

    otherRight = obstaclex + obstacleSize 
    otherLeft = obstaclex 
    otherTop = obstacley 
    otherBottom = obstacley + rectSize 

    randObstacle = random.randrange(0,2) 

    while not gameExit: 

     pressed = pygame.key.get_pressed() 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 

     display.fill(black) 
     pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize]) 

     if pressed[pygame.K_RIGHT]: 
      display.fill(black) 
      rectx = displayW/2 + 70 + rectSize 
      right(rectx,recty) 

     if pressed[pygame.K_LEFT]: 
      display.fill(black) 
      rectx = displayW/2 - 170 
      left(rectx,recty) 

     if pressed[pygame.K_LEFT] and pressed[pygame.K_RIGHT]: 
      display.fill(black) 
      rectx = displayW/2 + 100 
      right(rectx,recty) 
      rectx = displayW/2 - 170 
      left(rectx,recty) 

     else: 
      display.fill(black) 
      pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize]) 
      rectx = displayW/2 - 30 

##  if randObstacle == 0: 
##    
##   middleL(obstaclex, obstacley) 
##   obstacley += 5 
##   randObstacle = random.randrange(0,2) 
##  elif randObstacle == 1: 
##    
##   middleR(obstaclex, obstacley) 
##   obstacley += 5 
##   randObstacle = random.randrange(0,2) 
##  elif randObstacle == 2: 
##    
##   middle(obstaclex, obstacley) 
##   obstacley += 5 
##   randObstacle = random.randrange(0,2) 


     middleR(obstaclex, obstacley) 
     obstacley += 5 
     collision = True 
     if ((myRight < otherLeft) and (myLeft > otherRight) and (myBottom < otherTop) and (myTop > otherBottom)): 
      # i replaced the OR's with AND's 
      collision = False 
      print collision 


     pygame.display.update() 

     clock.tick(FPS) 
    pygame.quit() 
    quit() 
gameLoop() 

我想使一個矩形碰撞一個矩形落下朝着它,但我似乎無法讓他們碰撞正確。我想用if語句做這件事,而不是諸如sprite.collide()pygame:我不能讓一個矩形通知時,其他接觸

此代碼不是很精緻,我會修復它,但暫時幫助我更多地瞭解代碼。

**這是編輯的代碼**

回答

0

做這樣的事情,當你開始使用的方法是使8個變量(這僅僅是僞代碼)

myRight = x + width 
myLeft = x 
myTop = y 
myBottom = y + height 

otherRight = x + width 
otherLeft = x 
otherTop = y 
otherBottom = y + height 

的最簡單的方法是開始碰撞,並試圖推翻它

collision = True 

if ((myRight < otherLeft) or (myLeft > otherRight) or (myBottom < otherTop) or (myTop > otherBottom)): 
    collision = False 

你必須要實現這個你自己的,但我發現它有用的,當我漸漸開始使用8個變量,MYR ight,myLeft等,因爲它們可以幫助您理解所有x +寬度實際上意味着什麼

好吧,我不打算在此發佈整個代碼,因爲它會使此評論非常長。我刪除了這個動作和很多無關的東西。你需要先計算一切,然後再繪製。

這是你的比賽下來與工作衝突梗概: http://pastebin.com/P0HyPGY0

+0

謝謝!讓變量像這樣設置確實有助於理解,但是當他們接觸時,我仍然無法得到反應。 – Petras99

+0

所以實現這個到您的代碼,然後發佈新的代碼在操作,所以我可以告訴你哪裏出錯了,並留下評論這裏當你更新,所以我得到一個通知 – Keatinge

+0

好吧,我做到了。謝謝你的回覆。 – Petras99

相關問題