Beispiel #1
0
def main():
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load(soundDir + "/musica.ogg")
    pygame.mixer.music.play(loops=0)

    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Pong para 2 jugadores")

    fondo = load_image("fondo.jpg", imgDir, alpha=False).convert()
    sonidoGolpe = loadSound("pelota.ogg", soundDir)
    sonidoPunto = loadSound("punto.ogg", soundDir)

    bola = Pelota(sonidoGolpe, sonidoPunto)
    jugador1 = Palas(15)
    jugador2 = Palas(625)

    reloj = pygame.time.Clock(
    )  #Creamos un reloj que controla el tiempo de juego
    pygame.key.set_repeat(
        1, 12
    )  # Activa repeticion de teclas, primer argumento tiempo de retraso segundo argumento, seungo argumento tiempo entre cada envio en milisegundos
    pygame.mouse.set_visible(
        False)  #Desactiva el cursor dentro de la ventana del juego

    puntos = [0, 0]
    while True:
        #Condicion de victoria
        if (puntos[0] == 5):
            messagebox.showinfo("Victoria", "Ganador el jugador 1")
            sys.exit()
        elif (puntos[1] == 5):
            messagebox.showinfo("Victoria", "Ganador el jugador 2")
            sys.exit()

        reloj.tick(60)  #Seleccionamos los fps en este caso 60

        jugador1.humano(
        )  #Actualizamos la posicion de la paleta del jugador 1 y 2
        jugador2.humano()
        puntos = bola.actualizar(
        )  #Al inicio del bucle actualizamos la posicion de la bola

        bola.colision(jugador1)  #Control de colisiones de la bola
        bola.colision(jugador2)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:  #Cuando se pulsa una tecla
                if event.key == K_UP:  #Si se pulsa la tecla arriba
                    jugador2.rect.centery -= 5
                elif event.key == K_DOWN:  #Si se pula la tecla abajo
                    jugador2.rect.centery += 5
                elif event.key == K_w:
                    jugador1.rect.centery -= 5
                elif event.key == K_s:
                    jugador1.rect.centery += 5
            elif event.type == pygame.KEYUP:  #Cuando se suelta una tecla
                if event.key == K_UP:  #Si se suelta la tecla arriba
                    jugador2.rect.centery += 0
                elif event.key == K_DOWN:  #Si se suelta la tecla abajo
                    jugador2.rect.centery += 0
                elif event.key == K_w:
                    jugador1.rect.centery += 0
                elif event.key == K_s:
                    jugador1.rect.centery += 0
        #Nombre de jugadores
        n_jug1, n_jug1_rect = texto("Jugador 1", width / 4, height / 2,
                                    60)  #Texto, posicionx, posiciony, tamaño
        n_jug2, n_jug2_rect = texto("Jugador 2", width - width / 4, height / 2,
                                    60)

        #Sistema de puntuacion
        p_jug1, p_jug1_rect = texto(str(puntos[0]), width / 4, 40, 60)
        p_jug2, p_jug2_rect = texto(str(puntos[1]), width - width / 4, 40, 60)

        #Actualiza la pantalla por cada iteracion del bucle
        screen.blit(fondo, (0, 0))
        screen.blit(p_jug1, p_jug1_rect)
        screen.blit(p_jug2, p_jug2_rect)
        screen.blit(n_jug1, n_jug1_rect)
        screen.blit(n_jug2, n_jug2_rect)
        screen.blit(bola.image, bola.rect)
        screen.blit(jugador1.image, jugador1.rect)
        screen.blit(jugador2.image, jugador2.rect)
        #Guarda los cambios
        pygame.display.flip()
Beispiel #2
0
            if accion.key == 32:
                pausarJuego()
        if accion.type == pygame.MOUSEBUTTONUP:
            iniciado = True

    # Comprobamos si se ha producido el Game Over
    if gameOver:
        alertaGameOver = fuenteJuego.render('Game Over', True, BLANCO)
        alertaGameOver_posicion = alertaGameOver.get_rect(
            centerx=tableroJuego.get_width() / 2)
        alertaGameOver_posicion.top = 300
        tablero.blit(alertaGameOver, alertaGameOver_posicion)
    else:
        valorPortero.actualizar()
        if iniciado:
            if valorPelota.actualizar():
                if vidas > 0:
                    vidas -= 1
                    valorPelota.posicionar()
                    iniciado = False
                else:
                    gameOver = True
        else:
            valorPelota.posicionar()

    # Comprobamos si la pelota choca contra el portero y produce un rebote
    if pygame.sprite.spritecollide(valorPortero, spPelota, False):
        trayectoria = (valorPortero.rect.x + valorPortero.largoPortero / 2) - (
            valorPelota.rect.x + valorPelota.largoPelota / 2)
        valorPelota.rebotar(trayectoria)