像素完美碰撞,命中掩码/空白错误

我正在试图理解完美像素碰撞的工作原理,我已经用pyGame窗口中弹来跳去的两个圆圈(图像)做了一个程序。脚本是这样的:

选择 | 换行 | 行号
  1. import pygame, importedfunctions, random
  2. pygame.init()
  3. clock = pygame.time.Clock()
  4. screen = pygame.display.set_mode([640,480])
  5. class BallClass(pygame.sprite.Sprite):
  6.     def __init__(self, x,y):
  7.         pygame.sprite.Sprite.__init__(self)
  8.         self.image = pygame.image.load("ball.png")
  9.         self.rect = self.image.get_rect()
  10.         self.rect.left = x
  11.         self.rect.top = y
  12.         self.speed = [6,6]
  13.     def move(self):
  14.         global ballgroup
  15.         x = self.rect.left
  16.         self.rect.left += self.speed[0]
  17.         y = self.rect.top
  18.         self.rect.top += self.speed[1]
  19.         if self.rect.left < 0 or self.rect.right > screen.get_width():
  20.             self.speed[0] *= -1
  21.             self.rect.left = x
  22.         if self.rect.top < 0 or self.rect.bottom > screen.get_height():
  23.             self.rect.top = y
  24.             self.speed[1] *= -1
  25.         for i in ballgroup:
  26.             z = importedfunctions.PixelPerfectCollision(self, i)
  27.             if z == True:
  28.                 self.speed[0] *= -1
  29.                 self.speed[1] *= -1
  30.                 print "collide!"
  31.             else: print z
  32. ballgroup = pygame.sprite.Group()
  33. ball1 = BallClass(20,20)
  34. ballgroup.add(ball1)
  35. ball2 = BallClass(200,20)
  36. ballgroup.add(ball2)
  37.  
  38. def blitting():
  39.     global ballgroup, screen
  40.     screen.fill([0,0,0])
  41.     for i in ballgroup:
  42.         i.move()
  43.     for i in ballgroup:
  44.         screen.blit(i.image,i.rect)
  45. stop = False
  46. def while_loop():
  47.     while stop == False:
  48.         clock.tick(30)
  49.         blitting()
  50.         for event in pygame.event.get():
  51.             if event.type == pygame.QUIT:
  52.                 sys.exit()
  53.         pygame.display.flip()
  54. while_loop()
  55.  
  56.  

它使用了PYGAME和以下脚本:

选择 | 换行 | 行号
  1. def PixelPerfectCollision(obj1, obj2):
  2.     """
  3.     If the function finds a collision, it will return True;
  4.     if not, it will return False. If one of the objects is
  5.     not the intended type, the function instead returns None.
  6.     """
  7.     try:
  8.         #create attributes
  9.         rect1, mask1, blank1 = obj1.rect, obj1.hitmask, obj1.blank
  10.         rect2, mask2, blank2 = obj2.rect, obj2.hitmask, obj2.blank
  11.         #initial examination
  12.         if rect1.colliderect(rect2) is False:
  13.             return False
  14.     except AttributeError:
  15.         return None
  16.  
  17.     #get the overlapping area
  18.     clip = rect1.clip(rect2)
  19.  
  20.     #find where clip's top-left point is in both rectangles
  21.     x1 = clip.left - rect1.left
  22.     y1 = clip.top  - rect1.top
  23.     x2 = clip.left - rect2.left
  24.     y2 = clip.top  - rect2.top
  25.  
  26.     #cycle through clip's area of the hitmasks
  27.     for x in range(clip.width):
  28.         for y in range(clip.height):
  29.             #returns True if neither pixel is blank
  30.             if mask1[x1+x][y1+y] is not blank1 and \
  31.                mask2[x2+x][y2+y] is not blank2:
  32.                 return True
  33.  
  34.     #if there was neither collision nor error
  35.     return False
  36.  

问题是它总是返回"None"(这使得球在每个球之间运行),我唯一确定的是它应该是带有obj.hit掩码和obj.Blank的对象
希望你能理解这个问题:/
我将非常感谢能帮上忙的人
请帮帮忙

# 回答1


呃,伙计,这不是什么对象。头盔。如果您阅读了关于这个主题的整个页面,包括底部的代码示例,您会发现您必须使用它们提供给您的其他函数来定义它,位于中间的某个地方。除此之外,看起来没有任何问题,因此您只需将函数复制并粘贴到代码的其他部分即可。

标签: python

添加新评论