[英]python collision detection?
所以我一直在為一個項目開發一個python游戲。我現在遇到了麻煩,如果我在游戲中設置了一個障礙,我無法讓它像我的圖片碰撞時那樣回應我的圖片,游戲結束。 我已經有很長一段時間了,但是我無法弄清楚代碼。任何幫助都會非常感激。我非常需要幫助。請注意,我不是初學者,而是剛開始學習python一個月前。所以請盡量理解。 我已附上以下代碼。
import pygame, random, sys
from pygame.locals import *
BACKGROUNDCOLOR = (181, 230, 29)
FPS = 30
pixels = 5
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((990, 557))
pygame.display.set_caption('Clumsy Claire :D')
background = pygame.image.load('grass.jpg')
backgroundRect = background.get_rect()
size = (990, 557)
background.get_size()
image = pygame.image.load('snail 2.png')
imageRect = image.get_rect()
stone1 = pygame.image.load('rock.PNG')
stone1Rect = stone1.get_rect()
stone2 = pygame.image.load('rock.PNG')
stone2Rect = stone2.get_rect()
BROWN = (128,64,0)
pygame.draw.line(background, BROWN, (98, 555), (98,69), 12)
pygame.draw.line(background, BROWN, (98, 16), (98,1), 12)
pygame.draw.line(background, BROWN, (94, 3), (283, 3),12)
pygame.draw.line(background, BROWN, (278, 457), (278, 3),12)
pygame.draw.line(background, BROWN, (278, 554), (278, 512),12)
pygame.draw.line(background, BROWN, (274, 554), (470, 554),12)
pygame.draw.line(background, BROWN, (465, 554), (465, 90),12)
pygame.draw.line(background, BROWN, (465, 35), (465, 0),12)
pygame.draw.line(background, BROWN, (465, 3), (657, 3),12)
pygame.draw.line(background, BROWN, (652,555 ), (652, 502),12)
pygame.draw.line(background, BROWN, (652, 449), (652, 0),12)
pygame.draw.line(background, BROWN, (648, 553), (844, 553),12)
pygame.draw.line(background, BROWN, (838, 553 ), (838, 138),12)
pygame.draw.line(background, BROWN, (838, 84 ), (838, 0),12)
while True:
imageRect.topleft = (10,488)
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
moveLeft = True
if event.key == K_RIGHT:
moveRight = True
if event.key == K_UP:
moveUp = True
if event.key == K_DOWN:
moveDown = True
if event.type == KEYUP:
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUp = False
if event.key == K_DOWN:
moveDown = False
if moveLeft and imageRect.left > 0:
imageRect.move_ip(-1 * pixels, 0)
if moveRight and imageRect.right < 990:
imageRect.move_ip(pixels, 0)
if moveUp and imageRect.top > 0:
imageRect.move_ip(0, -1 * pixels)
if moveDown and imageRect.bottom < 557:
imageRect.move_ip(0, pixels)
windowSurface.blit(background, backgroundRect)
windowSurface.blit(image, imageRect)
rock1 = background.blit(stone1,(658,337))
rock2 = background.blit(stone2,(225,150))
pygame.display.update()
mainClock.tick(FPS)
您需要在代碼中添加colliderect
函數等。 現在你無法測試碰撞。 將其粘貼到您的blit代碼下方:
if imageRect.colliderect(stone1Rect):
print('Game Over')
pygame.quit()
if imageRect.colliderect(stone2Rect):
print('Game Over')
pygame.quit()
這段代碼在這里:
rock1 = background.blit(stone1,(658,337))
rock2 = background.blit(stone2,(225,150))
還需要改為:
windowSurface.blit(stone1, (658, 337))
windowSurface.blit(stone2, (225, 150))
我們需要更改上述內容的原因是:您的代碼在背景圖像而不是窗口上顯示為blitting; 這是一種不好的做法。
出於某種原因,我猜你正在從inventwithpython.org學習python; D這就是我學習它的方式(如果我猜的是對的; D)
如果您需要更多幫助或有疑問,請在下面發表評論。 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.