【问题标题】:python collision detection?python碰撞检测?
【发布时间】:2014-04-13 23:25:31
【问题描述】:

所以我一直在为一个项目开发 Python 游戏。我现在遇到的问题是,如果我在游戏中设置障碍物,我无法让它响应我的图片,就像我的图片与它发生碰撞一样,游戏结束。我已经有一段时间了,但我无法弄清楚代码。任何帮助将不胜感激。我真的需要帮助。请不要说我是初学者,我刚开始学习 python a一个月前。所以请试着理解。我附上了下面的代码。

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 呢?

标签: python python-2.7 pygame


【解决方案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))

我们需要更改上述内容的原因是:您的代码在背景图像上而不是窗口上进行 blitting;这是一种不好的做法。

出于某种原因,我猜你正在从 inventwithpython.org 学习 python ;D 我也是这样学习的(如果我的猜测是正确的 ;D)

如果您需要更多帮助或有任何疑问,请在下方发表评论。祝你好运!

【讨论】:

  • 是的!你猜对了。我正在向 inventwithpython.org 学习,这才刚过了两周,现在我们应该用它来制作一个游戏:/ & 嗯,是的,我也一直在搞乱这个 colliderect 函数但仍然发生的是蜗牛只是从它下面钻了进去。我似乎无法弄清楚:(
  • 所以你是说我给的代码不起作用?我回家后可以再看一遍。
  • 是的,它不起作用,例如如果玩家撞到岩石,我想退出游戏,但它没有这样做,而是只是在岩石中移动。是的,如果你能帮助我,那就太好了。一天后我有我的项目提交,我只是不知道该怎么办了:(
猜你喜欢
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多