Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Gambiarra/objects/things.py
blob: b008947787eb3b3a12ca36514e1de873d000abc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2007 by ULPM: Alexandre Yukio Harano
#                             Fábio Cassarotti Parronchi Navarro
#                             Gabriel Geraldo França Marcondes
#                             Luiz Carlos Irber Júnior
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Bowling ball bouncing
# http://freesound.iua.upf.edu/samplesViewSingle.php?id=22744
# Updated at September 12th, 2006.
# Published under Creative Commons Sampling Plus 1.0 License.

# Congratulations
# http://freesound.iua.upf.edu/samplesViewSingle.php?id=25293
# Updated at November 09th, 2006.
# Published under Creative Commons Sampling Plus 1.0 License.

# Penguin bouncing
# http://freesound.iua.upf.edu/samplesViewSingle.php?id=34316
# Updated at May 01st, 2007.
# Published under Creative Commons Sampling Plus 1.0 License.

# Trampoline bouncing
# http://freesound.iua.upf.edu/samplesViewSingle.php?id=19347
# Updated at May 30th, 2006.
# Published under Creative Commons Sampling Plus 1.0 License.

import pygame

class Thing(pygame.sprite.Sprite):

    img = None
    initialPosition = None
    mobility = None
    editable = None
    speed = None
    gravity = None
    snd = None
    elasticity = None # * 1%, from 0 up to 100

    def __init__(self, image, editable, snd, initialPosition=None,
                 elasticity = 70, mobility = False, gravity = 5 ):
        pygame.sprite.Sprite.__init__(self) #call Sprite intializer
        self.image = image
        self.rect = image.get_rect()

        if initialPosition:
            self.initialPosition = initialPosition
            self.rect.topleft = initialPosition[0], initialPosition[1]
        self.elasticity = elasticity
        self.editable = editable
        self.speed = [0,0]
        self.mobility = mobility
        self.gravity = gravity
        self.snd = snd

    def draw(self, screen, pos ):
        # temos a imagem na variavel <img> e
        # o 'zero' (ponto onde deve ser desenhado <pos>
        screen.blit(self.image, (pos[0],pos[1]))

    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 update(self):
        if self.mobility:
            newpos = self.rect.move((self.speed[0],self.speed[1]))
            self.rect = newpos
            self.speed[0] *= 0.99
            if self.speed[1] <= self.rect[3]*0.04 and self.speed[1] > 0:
                self.speed[1] = 0
            else:
                self.speed[1] += self.gravity

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