简体   繁体   English

python碰撞检测?

[英]python collision detection?

So I've been working on a python game for a project.I'm having the trouble now that If I put an obstacle in the game I can't make it respond to my picture like when my picture collides with it, the game ends. 所以我一直在为一个项目开发一个python游戏。我现在遇到了麻烦,如果我在游戏中设置了一个障碍,我无法让它像我的图片碰撞时那样回应我的图片,游戏结束。 I've been at it for quite sometime now but I'm unable to figure out the code.Any help will be greatly appreciated.I seriously need help with this.Please not that I'm a beginner and I just started studying python a month ago.So please try to understand. 我已经有很长一段时间了,但是我无法弄清楚代码。任何帮助都会非常感激。我非常需要帮助。请注意,我不是初学者,而是刚开始学习python一个月前。所以请尽量理解。 I've attached the code below. 我已附上以下代码。

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)

You need to add a colliderect function to your code among other things. 您需要在代码中添加colliderect函数等。 Right now you have no way to test collisions. 现在你无法测试碰撞。 Paste this below your blit code: 将其粘贴到您的blit代码下方:

if imageRect.colliderect(stone1Rect):
    print('Game Over')
    pygame.quit()
if imageRect.colliderect(stone2Rect):
    print('Game Over')
    pygame.quit()

This code here: 这段代码在这里:

rock1 = background.blit(stone1,(658,337))
rock2 = background.blit(stone2,(225,150))

also needs to be changed to this: 还需要改为:

windowSurface.blit(stone1, (658, 337))
windowSurface.blit(stone2, (225, 150))

The reason we need to change the above is this: your code is blitting on your background image instead of the window; 我们需要更改上述内容的原因是:您的代码在背景图像而不是窗口上显示为blitting; which is a bad practice. 这是一种不好的做法。

For some reason, I'm guessing you're learning python from inventwithpython.org ;D That's the way I learned it too (if my guess is right ;D) 出于某种原因,我猜你正在从inventwithpython.org学习python; D这就是我学习它的方式(如果我猜的是对的; D)

If you need any more help or have questions just comment below. 如果您需要更多帮助或有疑问,请在下面发表评论。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM