diff options
author | Luiz Irber <luiz.irber@gmail.com> | 2008-04-09 00:48:07 (GMT) |
---|---|---|
committer | Luiz Irber <luiz.irber@gmail.com> | 2008-04-09 00:48:07 (GMT) |
commit | eac28ed9766fa39d8be99c46b2c7e285172d4f13 (patch) | |
tree | a7ed107a678a114b5e750eea9520cd24e911bac8 | |
parent | 6a917f6cbbd8d6ac34320477d5363c8e9f708437 (diff) |
Refactoring of check_collision done
- Now the Thing class has a new method, collide(self, obj),
and the collision treatment is done there.
- Penguin still need to have this method overrided!
-rw-r--r-- | Gambiarra/gambiarra.py | 101 | ||||
-rw-r--r-- | Gambiarra/objects/elastica.py | 11 | ||||
-rw-r--r-- | Gambiarra/objects/esteira.py | 10 | ||||
-rw-r--r-- | Gambiarra/objects/target.py | 3 | ||||
-rw-r--r-- | Gambiarra/objects/things.py | 39 | ||||
-rw-r--r-- | Gambiarra/objects/wall.py | 36 |
6 files changed, 99 insertions, 101 deletions
diff --git a/Gambiarra/gambiarra.py b/Gambiarra/gambiarra.py index 8c15fbf..d52d058 100644 --- a/Gambiarra/gambiarra.py +++ b/Gambiarra/gambiarra.py @@ -37,106 +37,7 @@ from objects.esteira import Esteira from objects.target import Target from objects.wall import * -def check_collision(sprite_list, wall_list): - new_objects = pygame.sprite.RenderPlain() - for obj in sprite_list: - obj.remove(sprite_list) - obj.add(new_objects) - - for s in sprite_list: - """hitbox = obj.rect.inflate(5,5) - if hitbox.colliderect(s.rect): - if obj.rect.left < s.rect.right: - aux = obj.speed[0] - obj.speed[0]=s.speed[0] - s.speed[0]=aux - obj.rect.left = s.rect.right-1 - if obj.rect.left > s.rect.right : - aux = obj.speed[0] - obj.speed[0]=s.speed[0] - s.speed[0]=aux - obj.rect.right = srect.left-1 - if obj.rect.bottom > s.rect.top: - aux = obj.speed[1] - obj.speed[1]=s.speed[1] - s.speed[1]=aux - obj.rect.bottom = s.rect.top-1 - if obj.rect.top < s.rect.bottom: - aux = obj.speed[1] - obj.speed[1]=s.speed[1] - s.speed[1]=aux - obj.rect.top = s.rect.bottom+1 - """ - #TODO: verifica colisao de objetos dinamicos - pass - - for w in wall_list: -# hitbox = obj.rect.inflate(-5,-5) -# if hitbox.colliderect(w.rect): - if obj.rect.colliderect(w.rect): - obj.play() - w.play() - if isinstance(w, DownWall): - if obj.rect.bottom > w.rect.top: - obj.rect.bottom = w.rect.top - 5 - if isinstance(obj,Penguin): - obj.speed[1] = 0 - else: - obj.speed[1] *= -0.75*obj.elasticity/100 - #1. a**n + b**n = c**n ? - if isinstance(w, UpWall): - if obj.rect.top <= w.rect.bottom: - obj.rect.top = w.rect.bottom + 5 - obj.speed[1] *= -0.95*obj.elasticity/100 - - if isinstance(w, LeftWall): - if obj.rect.left <= w.rect.right: - obj.rect.left = w.rect.right + 5 - obj.speed[0] *= -0.95*obj.elasticity/100 - - if isinstance(w, RightWall): - if obj.rect.right >= w.rect.left: - obj.rect.right = w.rect.left - 5 - obj.speed[0] *= -0.95*obj.elasticity/100 - - if isinstance(w, Esteira): - if (obj.rect.bottom >= w.rect.top and obj.speed[1] > 0): - obj.rect.bottom = w.rect.top - 5 - obj.speed[0] = w.sentido*15 - if isinstance(obj,Penguin): - obj.speed[1] = 0 - else: - obj.speed[1] *= -0.75*obj.elasticity/100 - - if isinstance(w, Elastica): - if (obj.rect.bottom >= w.rect.top and obj.speed[1] > 0): - obj.rect.bottom = w.rect.top - 1 - elif (obj.rect.top <= w.rect.bottom): - obj.rect.top = w.rect.bottom + 1 - obj.speed[1] *= -0.99 - - if isinstance(w, Target): - pass - - if isinstance(w,DownWall) : - if obj.rect.bottom >= w.rect.top: - obj.rect.bottom = w.rect.top - 5 - obj.speed[1] *= -0.75*obj.elasticity/100 - if isinstance(w,UpWall) : - if obj.rect.top <= w.rect.bottom: - obj.rect.top = w.rect.bottom + 5 - obj.speed[1] *= -0.95*obj.elasticity/100 - if isinstance(w,LeftWall) : - if obj.rect.left <= w.rect.right: - obj.rect.left = w.rect.right + 5 - obj.speed[0] *= -0.95*obj.elasticity/100 - if isinstance(w,RightWall) : - if obj.rect.right >= w.rect.left: - obj.rect.right = w.rect.left - 5 - obj.speed[0] *= -0.95*obj.elasticity/100 - - return new_objects - +from objects.things import check_collision class Game(object): # controle do jogo diff --git a/Gambiarra/objects/elastica.py b/Gambiarra/objects/elastica.py index 350edab..5af6574 100644 --- a/Gambiarra/objects/elastica.py +++ b/Gambiarra/objects/elastica.py @@ -37,3 +37,14 @@ class Elastica(Thing): editable, snd, initialPosition, elasticity = 100, mobility = False, gravity = 10) + + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if (obj.rect.bottom >= self.rect.top and obj.speed[1] > 0): + obj.rect.bottom = self.rect.top - 1 + elif (obj.rect.top <= self.rect.bottom): + obj.rect.top = self.rect.bottom + 1 + obj.speed[1] *= -0.99 + return True + return False + diff --git a/Gambiarra/objects/esteira.py b/Gambiarra/objects/esteira.py index 88bff1f..b0ffef4 100644 --- a/Gambiarra/objects/esteira.py +++ b/Gambiarra/objects/esteira.py @@ -48,3 +48,13 @@ class Esteira(Thing): screen.blit(self.image_dir, (pos[0],pos[1])) elif self.sentido == -1: screen.blit(self.image_esq, (pos[0],pos[1])) + + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if (obj.rect.bottom >= self.rect.top and obj.speed[1] > 0): + obj.rect.bottom = self.rect.top - 5 + obj.speed[0] = self.sentido*15 + obj.speed[1] *= -0.75*obj.elasticity/100 + return True + return False + diff --git a/Gambiarra/objects/target.py b/Gambiarra/objects/target.py index 8df61b8..a4021c7 100644 --- a/Gambiarra/objects/target.py +++ b/Gambiarra/objects/target.py @@ -32,3 +32,6 @@ class Target(Thing): editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) + + def collide(self, obj): + pass diff --git a/Gambiarra/objects/things.py b/Gambiarra/objects/things.py index f2d0189..cc98e21 100644 --- a/Gambiarra/objects/things.py +++ b/Gambiarra/objects/things.py @@ -76,3 +76,42 @@ class Thing(pygame.sprite.Sprite): def play(self): if self.snd and pygame.mixer.get_init(): self.snd.play() + + def collide(self, obj): + ''' Very basic implementation of elastic collision''' + #TODO: verify mobility attribute + hitbox = self.rect.inflate(5,5) + if hitbox.colliderect(obj.rect): + if self.rect.left < obj.rect.right: + self.speed[0], obj.speed[0] = obj.speed[0], self.speed[0] + self.rect.left = obj.rect.right - 1 + if self.rect.left > obj.rect.right: + self.speed[0], obj.speed[0] = obj.speed[0], self.speed[0] + self.rect.right = obj.rect.left - 1 + if self.rect.bottom > obj.rect.top: + self.speed[1], obj.speed[1] = obj.speed[1], self.speed[1] + self.rect.bottom = obj.rect.top - 1 + if self.rect.top < obj.rect.bottom: + self.speed[1], obj.speed[1] = obj.speed[1], self.speed[1] + self.rect.top = obj.rect.bottom + 1 + return True + return False + +def check_collision(sprite_list, wall_list): + new_objects = pygame.sprite.RenderPlain() + for obj in sprite_list: + obj.remove(sprite_list) + obj.add(new_objects) + +# for s in sprite_list: +# if s.collide(obj) +# obj.play() +# s.play() + + for w in wall_list: + if w.collide(obj): + obj.play() + w.play() + + return new_objects + diff --git a/Gambiarra/objects/wall.py b/Gambiarra/objects/wall.py index 3ec40cc..4a1b32d 100644 --- a/Gambiarra/objects/wall.py +++ b/Gambiarra/objects/wall.py @@ -30,7 +30,16 @@ class LeftWall(Thing): super(LeftWall, self).__init__( pygame.image.load(abspath("../data/images/leftwall.png")), editable, None, - initialPosition, elasticity = 100, mobility = False, gravity = 10) + initialPosition, elasticity = 100, mobility = False, + gravity = 10) + + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if obj.rect.left <= self.rect.right: + obj.rect.left = self.rect.right + 5 + obj.speed[0] *= -0.95*obj.elasticity/100 + return True + return False class RightWall(Thing): def __init__(self, initialPosition = [1185,0], editable=False): @@ -39,6 +48,14 @@ class RightWall(Thing): editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if obj.rect.right >= self.rect.left: + obj.rect.right = self.rect.left - 5 + obj.speed[0] *= -0.95*obj.elasticity/100 + return True + return False + class UpWall(Thing): def __init__(self, initialPosition = [15,0], editable=False): super(UpWall, self).__init__( @@ -47,6 +64,14 @@ class UpWall(Thing): initialPosition, elasticity = 100, mobility = False, gravity = 10) + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if obj.rect.top <= self.rect.bottom: + obj.rect.top = self.rect.bottom + 5 + obj.speed[1] *= -0.95*obj.elasticity/100 + return True + return False + class DownWall(Thing): def __init__(self, initialPosition = [15,755], editable=False): super(DownWall, self).__init__( @@ -54,3 +79,12 @@ class DownWall(Thing): editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) + + def collide(self, obj): + if obj.rect.colliderect(self.rect): + if obj.rect.bottom > self.rect.top: + obj.rect.bottom = self.rect.top - 5 + obj.speed[1] *= -0.75*obj.elasticity/100 + return True + return False + |