Python 碰撞检测?

4 投票
1 回答
1666 浏览
提问于 2025-04-17 21:40

我最近在做一个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)

1 个回答

1

你需要在代码中添加一个 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))

我们需要修改上面的内容是因为:你的代码是在背景图上进行 blit,而不是在窗口上;这样做是不太好的。

我猜你是在 inventwithpython.org 学习 Python ;D 我也是这样学的(如果我猜得没错的话 ;D)

如果你需要更多帮助或者有问题,随时在下面评论。祝你好运!

撰写回答