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: -
import pygame, importedfunctions, random
-
pygame.init()
-
clock = pygame.time.Clock()
-
screen = pygame.display.set_mode([640,480])
-
class BallClass(pygame.sprite.Sprite):
-
def __init__(self, x,y):
-
pygame.sprite.Sprite.__init__(self)
-
self.image = pygame.image.load("ball.png")
-
self.rect = self.image.get_rect()
-
self.rect.left = x
-
self.rect.top = y
-
self.speed = [6,6]
-
def move(self):
-
global ballgroup
-
x = self.rect.left
-
self.rect.left += self.speed[0]
-
y = self.rect.top
-
self.rect.top += self.speed[1]
-
if self.rect.left < 0 or self.rect.right > screen.get_width():
-
self.speed[0] *= -1
-
self.rect.left = x
-
if self.rect.top < 0 or self.rect.bottom > screen.get_height():
-
self.rect.top = y
-
self.speed[1] *= -1
-
for i in ballgroup:
-
z = importedfunctions.PixelPerfectCollision(self, i)
-
if z == True:
-
self.speed[0] *= -1
-
self.speed[1] *= -1
-
print "collide!"
-
else: print z
-
ballgroup = pygame.sprite.Group()
-
ball1 = BallClass(20,20)
-
ballgroup.add(ball1)
-
ball2 = BallClass(200,20)
-
ballgroup.add(ball2)
-
-
def blitting():
-
global ballgroup, screen
-
screen.fill([0,0,0])
-
for i in ballgroup:
-
i.move()
-
for i in ballgroup:
-
screen.blit(i.image,i.rect)
-
stop = False
-
def while_loop():
-
while stop == False:
-
clock.tick(30)
-
blitting()
-
for event in pygame.event.get():
-
if event.type == pygame.QUIT:
-
sys.exit()
-
pygame.display.flip()
-
while_loop()
-
-
it uses pygame and this script: -
def PixelPerfectCollision(obj1, obj2):
-
"""
-
If the function finds a collision, it will return True;
-
if not, it will return False. If one of the objects is
-
not the intended type, the function instead returns None.
-
"""
-
try:
-
#create attributes
-
rect1, mask1, blank1 = obj1.rect, obj1.hitmask, obj1.blank
-
rect2, mask2, blank2 = obj2.rect, obj2.hitmask, obj2.blank
-
#initial examination
-
if rect1.colliderect(rect2) is False:
-
return False
-
except AttributeError:
-
return None
-
-
#get the overlapping area
-
clip = rect1.clip(rect2)
-
-
#find where clip's top-left point is in both rectangles
-
x1 = clip.left - rect1.left
-
y1 = clip.top - rect1.top
-
x2 = clip.left - rect2.left
-
y2 = clip.top - rect2.top
-
-
#cycle through clip's area of the hitmasks
-
for x in range(clip.width):
-
for y in range(clip.height):
-
#returns True if neither pixel is blank
-
if mask1[x1+x][y1+y] is not blank1 and \
-
mask2[x2+x][y2+y] is not blank2:
-
return True
-
-
#if there was neither collision nor error
-
return False
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
|
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...
|
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...
|
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...
|
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{
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| | |