473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Pixelperfect Collision, hitmask/blank error

I am trying to understand the way that pixelperfect collisions works, I have made a program using two circles(images) bouncing around in the pygame window. This is the script:
Expand|Select|Wrap|Line Numbers
  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.  
it uses pygame and this script:
Expand|Select|Wrap|Line Numbers
  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.  
The problem is that it always returns "None"(which makes the balls running through eachothers), the only thing I have determinated is that it should be something with the obj.hitmask and the obj.blank
Hope you understand the problem:/

i would be very grateful to the one/ones who can help
please help
Jun 1 '12 #1
1 2027
Uh dude there isn't an obj.hitmask. If you read the entire page on this subject INCLUDING the code example at the bottom you will see that you have to defing it using the other functions they give you somewhere in the middle. Apart from that nothing looks wrong so you could just copy and paste the functions in as well as the other section of code.
Oct 16 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John | last post by:
Anyone know why blank records would be input into the database from a form? Everytime I click submit I get 3 entries at a time entered in for no reason on top of the original data.
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
8
by: Dave Smith | last post by:
I am running 7.2 and when doing a vacuum I am getting the following error.... ERROR: Cannot insert a duplicate key into unique index pg_statistic_relid_att_index Where do I start to fix...
4
by: Brian Basquille | last post by:
Hello all, Well, we've gotten to it: the real meaty area of my Air Hockey game.. Any help or suggestions for the following would be much appreciated. In plain terms: My paddle needs to be...
12
by: news.coderaka | last post by:
I got a compliation error when compiling this programs using g++ 3.4.2. Who can tell me why? Thank you. #include<iostream> using namespace std; template<class T> class list{ class iterator{
4
by: Neo Geshel | last post by:
I am experiencing a collision between two scripts, but I cannot find where it is. I am hoping that someone here can help me. I have two external JS files that I call into each web page. One...
6
by: adam | last post by:
ACE+TAO-1.5a gcc3.4.4 linux I'm trying to compile an app which pulls in some TAO code. However, I get lots of name collisions on methods called 'nil' in TAO, e.g. the following error is one of...
7
by: Jan | last post by:
Hi: When I searched the newsgroup for this problem, I saw two or three instances of the question being asked, but it was never answered. Not too promising, but here goes: I have a form with...
1
by: May Amor | last post by:
Helu gurus!!! I have a code below about hashing method with collision resolution...My problem is how to use the collsion resolution again if the hash index though has already a value. Please kinda...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.