Ejemplo n.º 1
0
class CentralFigure(object):
    def __init__(self, aristas):
        self.aristas = aristas
        self.objeto = None
        self.color = (255, 255, 255)
        self.create()

    def rotate(self, angle):
        self.objeto.rotate(angle)

    def scale(self, scale):
        self.objeto.scale(scale)

    def draw(self):
        self.objeto.draw()

    def create(self):
        center_square = [320, 240]
        angle = (360.0 / self.aristas) * pi / 180.0
        vertices = []
        for x in xrange(self.aristas):
            vertices.append(
                (1.0 * sin(x * angle % 360), 1.0 * cos(x * angle % 360)))
        self.objeto = CenteredFigure(vertices, center_square, color=self.color)

    def set_surface(self, surface):
        self.objeto.set_surface(surface)
Ejemplo n.º 2
0
 def create(self):
     center_square = [320, 240]
     angle = (360.0 / self.aristas) * pi / 180.0
     vertices = []
     for x in xrange(self.aristas):
         vertices.append(
             (1.0 * sin(x * angle % 360), 1.0 * cos(x * angle % 360)))
     self.objeto = CenteredFigure(vertices, center_square, color=self.color)
Ejemplo n.º 3
0
    def create(self):
        int = random.randint(0, self.aristas - 1)
        for x in xrange(self.aristas):
            center_square = [320, 240]
            color = self.color
            angle = (360.0 / self.aristas) * pi / 180.0

            ataque = CenteredFigure([(0, 9), (0, 10),
                                     (10 * sin(angle), 10 * cos(angle)),
                                     (9 * sin(angle), 9 * cos(angle))],
                                    center_square,
                                    color=color)
            ataque.rotate((x * (360 / self.aristas)) % 360)
            if x is not int:
                self.ataque.append(ataque)
Ejemplo n.º 4
0
    def create(self):
        for x in xrange(self.aristas):
            center_square = [320, 240]
            color = None
            trianguloBG = None
            angle = (360.0 / self.aristas) * pi / 180.0
            if self.aristas < 5:
                color = self.colors[x % 2]
            elif self.aristas >= 5:
                color = self.colors[x % 3]

            trianguloBG = CenteredFigure([(0, 0), (0, 10),
                                          (10 * sin(angle), 10 * cos(angle))],
                                         center_square,
                                         color=color)
            trianguloBG.rotate((x * (360 / self.aristas)) % 360)

            self.arreglo.append(trianguloBG)
Ejemplo n.º 5
0
 def __init__(self, x, y, vel, color, surface, sounds):
     """
     Crea la paleta en una posición x, y
     :param x: Posición en x
     :param y: Posición en y
     :param vel: Velocidad
     :param color: Color
     :param surface: Superficie del canvas
     :param sounds: Sonidos
     """
     self.center = [x, y]
     self.center_backup = [x, y]
     self.vel = vel
     self.color = color
     self.figure = CenteredFigure([(0, 50), (20, 50), (20, -50), (0, -50)],
                                  self.center,
                                  color=color,
                                  pygame_surface=surface,
                                  width=0)
     self.vida = 3
     self.sounds = sounds
Ejemplo n.º 6
0
 def __init__(self, center, color, surface, player1, player2, sounds):
     """
     Bolita
     :param center: Posición del centro
     :param color: Color de la bolita
     :param surface: Canvas del juego
     :param player1: Jugador 1
     :type player1: Player
     :param player2: Jugador 2
     :type player2: Player
     :param sounds: Sonidos
     """
     self.center = copy.copy(center)
     self.center_backup = copy.copy(center)
     self.vel = [0, 0]
     self.random_velocity()
     self.figure = CenteredFigure([(-1, 1), (1, 1), (1, -1), (-1, -1)],
                                  center=self.center,
                                  color=color,
                                  pygame_surface=surface)
     self.figure.scale(15)
     self.p1 = player1
     self.p2 = player2
     self.sounds = sounds
Ejemplo n.º 7
0
class Ball(object):
    def __init__(self, center, color, surface, player1, player2, sounds):
        """
        Bolita
        :param center: Posición del centro
        :param color: Color de la bolita
        :param surface: Canvas del juego
        :param player1: Jugador 1
        :type player1: Player
        :param player2: Jugador 2
        :type player2: Player
        :param sounds: Sonidos
        """
        self.center = copy.copy(center)
        self.center_backup = copy.copy(center)
        self.vel = [0, 0]
        self.random_velocity()
        self.figure = CenteredFigure([(-1, 1), (1, 1), (1, -1), (-1, -1)],
                                     center=self.center,
                                     color=color,
                                     pygame_surface=surface)
        self.figure.scale(15)
        self.p1 = player1
        self.p2 = player2
        self.sounds = sounds

    def draw(self):
        """
        Dibujamos la bolita
        :return: 
        """
        self.figure.draw()

    def random_velocity(self):
        """
        Crea una velocidad aleatoria.
        
        :return: 
        """

        self.vel[0] = random.randrange(1, 10) * random.choice([-1, 1])
        self.vel[1] = random.randrange(1, 5) * random.choice([-1, 1])

    def reset(self):
        """
        Resetea la velocidad y la posición.
        
        :return: 
        """
        self.random_velocity()
        self.center[0] = self.center_backup[0]
        self.center[1] = self.center_backup[1]

        # Resteamos los jugadores
        self.p1.reset()
        self.p2.reset()

    def stop(self):
        """
        Detiene la bolita.
        
        :return: 
        """
        self.vel[0] = 0
        self.vel[1] = 0

    def move(self):
        """
        Movemos la bolita y detectamos colisiones
        :return: 
        """
        self.center[0] = self.center[0] + self.vel[0]
        self.center[1] = self.center[1] + self.vel[1]

        # Vemos colisión con techos
        if self.center[1] < 0:
            self.center[1] = 0
            self.vel[1] *= -1
            self.sounds.rebote()
        if self.center[1] > SHEIGHT - 15:
            self.center[1] = SHEIGHT - 15
            self.vel[1] *= -1
            self.sounds.rebote()

        # Vemos colisión con jugadores
        if self.vel[0] > 0:  # Jugador 2 debe tocar
            if self.figure.intersect(self.p2.get_figure()):
                self.vel[0] = -self.vel[0] - 2
                self.vel[1] = self.vel[1] + random.randrange(-1, 1)
                self.sounds.pong()
            if self.center[0] > SWIDTH:
                self.p2.lose()
                self.reset()
        if self.vel[0] < 0:  # Jugador 1 debe tocar
            if self.figure.intersect(self.p1.get_figure()):
                self.vel[0] = -self.vel[0] + 2
                self.vel[1] = self.vel[1] + random.randrange(-1, 1)
                self.sounds.pong()
            if self.center[0] < 0:
                self.p1.lose()
                self.reset()
Ejemplo n.º 8
0
class Player(object):
    def __init__(self, x, y, vel, color, surface, sounds):
        """
        Crea la paleta en una posición x, y
        :param x: Posición en x
        :param y: Posición en y
        :param vel: Velocidad
        :param color: Color
        :param surface: Superficie del canvas
        :param sounds: Sonidos
        """
        self.center = [x, y]
        self.center_backup = [x, y]
        self.vel = vel
        self.color = color
        self.figure = CenteredFigure([(0, 50), (20, 50), (20, -50), (0, -50)],
                                     self.center,
                                     color=color,
                                     pygame_surface=surface,
                                     width=0)
        self.vida = 3
        self.sounds = sounds

    def draw(self):
        """
        Dibuja al jugador
        :return: 
        """
        self.figure.draw()

    def reset(self):
        """
        Resetea la posición del jugador
        :return: 
        """
        self.center[0] = self.center_backup[0]
        self.center[1] = self.center_backup[1]

    def stop(self):
        """
        Detiene la bolita.

        :return: 
        """
        self.vel = 0

    def get_life(self):
        """
        Retorna la vida del jugador
        :return: 
        """
        return str(self.vida)

    def lose(self):
        """
        Pierde una vida y suena musica
        :return: 
        """
        self.vida -= 1
        self.sounds.die()

    def move_up(self):
        """
        Sube al jugador
        :return: 
        """
        self.center[1] = max(50, self.center[1] - self.vel)

    def move_down(self):
        """
        Baja al jugador
        :return: 
        """
        self.center[1] = min(SHEIGHT - 50, self.center[1] + self.vel)

    def get_figure(self):
        """
        Retorna la figura
        :return: 
        """
        return self.figure

    def is_dead(self):
        """
        Retorna true/false si está muerto o no
        :return: 
        """
        return self.vida < 0
Ejemplo n.º 9
0
from centered_figure import CenteredFigure

# Colors
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)

# Init pygame modules
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'

# Create figure
center_square = [400, 300]
center_triangle = [200, 200]

square = CenteredFigure([(-1, -1), (-1, 1), (1, 1), (1, -1)],
                        center_square,
                        color=COLOR_WHITE)
triangle = CenteredFigure([(-1, -0.5), (0, 1.0), (-1, 0.5)],
                          center_triangle,
                          color=COLOR_WHITE)

# Create pygame window
surface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('CenteredFigure example')

# Create pygame timer
clock = pygame.time.Clock()

# Set figure surfaces
square.set_surface(surface)
triangle.set_surface(surface)
figure_list = []  # Stores CenteredFigure objects
figure_angvel = []  # Store angular velocity to each figure
for i in range(TOTAL_FIGURES):
    # Create center of figure
    center_square = [
        random.randrange(0, S_WIDTH),
        random.randrange(0, S_HEIGHT)
    ]

    # Create random color
    color = (random.randrange(0, 255), random.randrange(0, 255),
             random.randrange(0, 255))

    # Create object
    square = CenteredFigure([(-1, -1), (-1, 1), (1, 1), (1, -1)],
                            center_square,
                            color=color)

    # Set figure surface
    square.set_surface(surface)

    # Create figure scale
    scale = random.randrange(1, 50)
    square.scale(scale)

    # Create angular velocity between -2 and 2
    angvel = random.random() * random.choice([-2, 2])

    # Append figure to list
    figure_list.append(square)
    figure_angvel.append(angvel)
Ejemplo n.º 11
0
def main(speed):
    # surface BG
    aristas = randint(4,6)
    # aristas = 5
    background = Background(aristas)
    central = CentralFigure(aristas)
    cursor = CenteredFigure([(0, 6), (-1, 5), (1, 5)], center_square, color=COLOR_CURSOR)
    # Set figure surfaces

    central.set_surface(surface)
    cursor.set_surface(surface)
    background.set_surface(surface)

    # Scale figures

    central.scale(50)
    cursor.scale(10)
    background.scale(100)

    ataques = []


    run = True
    timer_anterior = 0

    # Main loop
    while run:
        # Check events
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    run = False
                elif event.key == K_r:
                    run = False
                    main(speed)
        if int(timer[0]) >= 60*abs(speed):
            # Ganaste
            time_string = "You Win!"
            time_blit = timer_font.render(time_string, 1, COLOR_BLACK)
            time_blit_size = time_blit.get_size()
            surface.blit(time_blit, (
                W_SIZE / 2 - time_blit_size[0] / 2, H_SIZE / 2 - time_blit_size[1] / 2))
        else:
            # Ver teclas presionadas
            keys=pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                cursor.rotate(3*abs(speed))
            if keys[pygame.K_RIGHT]:
                cursor.rotate(-3*abs(speed))

            # Fill surface
            surface.fill(COLOR_BLACK)
            
            cambio_segundo = 0 if int(timer[0])-timer_anterior == 0 else 1
            timer_anterior = int(timer[0])
            # print(clock.get_time())


            if cambio_segundo == 1 and timer_anterior % 2 == 0:
                ataques.append(crear_ataque(aristas, surface))

            if cambio_segundo == 1 and timer_anterior % (6/speed) == 0:
                speed = -speed

            # Rotar Items
            central.rotate(1*speed)
            background.rotate(1*speed)

            # Draw
            background.draw()
            cursor.draw()
            # avanzar ataques
            for ataque in ataques:
                ataque.rotate(1*speed)
                ataque.scale(0.99 - abs(speed)/100) 
                if ataque.collide(cursor):
                    run = False           
                ataque.draw()

            central.draw()

            time_string = str(datetime.timedelta(seconds=int(timer[0])))
            time_blit = timer_font.render(time_string, 1, COLOR_WHITE)
            time_blit_size = time_blit.get_size()
            surface.blit(time_blit, (0, 0))

        # Flip display
        pygame.display.flip()

        # Set clock (60FPS)
        clock.tick(60)
        timer[0] += dt