Пример #1
0
def caza_pajaros(screen,size):
    default_font = pygame.font.get_default_font()
    font = pygame.font.SysFont(default_font, 90, False)

    fondo = pygame.Surface(size)
    fondo.fill((0,0,0))
    dibujo_fondo = carga_fondo('fondopang.png')
    tiempo = 0
    disparos = collections.deque(maxlen=100)
    pajaros = collections.deque(maxlen=100)
    huevos = collections.deque(maxlen=100)
    
    img_personaje = carga_imagen_animada('jugador.png',66,66,7)
    img_disparo = carga_imagen('bala.png',-1)
    img_pajaro = carga_imagen_animada('pajaro.png', 53, 62, 4)
    img_huevo = carga_imagen('huevo.png',-1)
    caja_pajaro = img_pajaro[0].get_bounding_rect()
    caja_jugador = img_personaje[0].get_bounding_rect()
    
    personaje = Jugador(img_personaje, 400, caja_jugador)
    clock = pygame.time.Clock()
    movimiento = PARADO
    anterior = PARADO
    ultimo_lanzado = 0
    score = 0
    
    while True:
        if pygame.time.get_ticks() - ultimo_lanzado >= 500:
            pajaros.append(Pajaro(img_pajaro,-50,random.randint(-10,200),huevos,img_huevo,caja_pajaro))
            ultimo_lanzado = pygame.time.get_ticks()
    
        tiempo += clock.tick(30)
        
        keys = pygame.key.get_pressed()
        mouse = pygame.mouse.get_pos()
        for eventos in pygame.event.get():
            if eventos.type == QUIT:
                return JUEGO_CANCELADO
            if eventos.type == KEYUP:
                if eventos.key == K_RIGHT and movimiento == DERECHA:
                    movimiento = PARADO
                if eventos.key == K_LEFT and movimiento == IZQUIERDA:
                    movimiento = PARADO
            if eventos.type == KEYDOWN:
                if eventos.key == K_ESCAPE:
                    return JUEGO_CANCELADO
                if eventos.key == K_RIGHT:
                    movimiento = DERECHA
                elif eventos.key == K_LEFT:
                    movimiento = IZQUIERDA
                elif eventos.key == K_SPACE and movimiento == PARADO:
                    disparos.append(Disparo(img_disparo,personaje.location[0]))
        
        personaje.realizar_movimiento(movimiento)
            
        screen.blit(fondo,(0,0))
        screen.blit(dibujo_fondo,(0,0))
        
        for huevo in huevos:
            huevo.render(screen)
            if huevo.estado == VIVO:
                if huevo.hay_colision(personaje):
                    return score
            
        for disparo in disparos:
            disparo.render(screen)
            if disparo.estado == VIVO:
                for pajaro in pajaros:
                    if pajaro.estado == VIVO:
                        if disparo.hay_colision(pajaro):
                            score += 1
                            pajaro.estado = MUERTO
                            disparo.estado = MUERTO
                        
        for pajaro in pajaros:
            pajaro.render(screen)
        personaje.render(screen)
        screen.blit(font.render(str(score), True, (255,255,255)), (650,520))
        pygame.display.flip()
    return 0
Пример #2
0
def coche_choques(screen,size):

    
    default_font = pygame.font.get_default_font()
    font = pygame.font.SysFont(default_font, 90, False)
    img_coches = carga_imagen_animada('coches.png',45,100,5)
    cajas = []
    for img in img_coches:
        cajas.append(img.get_bounding_rect())
    jugador = cajas[0]
    img_arbol = carga_imagen('arbol.png',-1)
    fondo = carga_fondo('fondo.png')
   
    coches = collections.deque(maxlen=500)
    arboles = collections.deque(maxlen=500)
    
    clock = pygame.time.Clock()
    tiempo = 0
    ultimo_lanzado = 0
    ultimo_generado = 0
    ultimo_movimiento = pygame.time.get_ticks()
    lado = 0
    
    micoche = img_coches[0]
    miposicion = [400,400]
    pulsado_izquierda = False
    pulsado_derecha = False
    pulsado_arriba = False
    pulsado_abajo = False
    tiempo_adorno = random.randint(100,1000)
    
    velocidad_extra = 0
    
    while True:
        tiempo += clock.tick(30)
        if ultimo_lanzado + 1000 * (0.1 /(0.1+velocidad_extra)) <= tiempo:
            ultimo_lanzado = tiempo
            tipo = random.randint(1,4)
            coches.append(Coche(img_coches[tipo],cajas[tipo],random.randint(150,600)))
        if ultimo_generado + tiempo_adorno * (1-velocidad_extra) <= tiempo:
            ultimo_generado = tiempo
            if lado == 0:
                arboles.append(Adorno(img_arbol,random.randint(-30,100)))
            else:
                arboles.append(Adorno(img_arbol,random.randint(700,820)))
            lado = 1-lado
            tiempo_adorno = random.randint(100,1000)
            
        mouse = pygame.mouse.get_pos()
        for eventos in pygame.event.get():
            if eventos.type == QUIT:
                return JUEGO_CANCELADO
            if eventos.type == MOUSEBUTTONDOWN:
                pass
            if eventos.type == KEYUP:
                if eventos.key == K_LEFT:
                    pulsado_izquierda = False
                elif eventos.key == K_RIGHT:
                    pulsado_derecha = False
                elif eventos.key == K_UP:
                    pulsado_arriba = False
                elif eventos.key == K_DOWN:
                    pulsado_abajo = False
            if eventos.type == KEYDOWN:
                if eventos.key == K_ESCAPE:
                    return JUEGO_CANCELADO
                if eventos.key == K_LEFT:
                    pulsado_izquierda = True
                elif eventos.key == K_RIGHT:
                    pulsado_derecha = True
                elif eventos.key == K_UP:
                    pulsado_arriba = True
                elif eventos.key == K_DOWN:
                    pulsado_abajo = True
                    
        delta = pygame.time.get_ticks() - ultimo_movimiento
        velocidad_extra += delta * 0.000003

        mov_coche = delta * (0.2 + velocidad_extra)
        if pulsado_izquierda and miposicion[0] - mov_coche >= 150:
            miposicion[0] -= mov_coche
        if pulsado_derecha and miposicion[0] + mov_coche <= 600:
            miposicion[0] += mov_coche
        if pulsado_arriba and miposicion[1] - mov_coche >= -50:
            miposicion[1] -= mov_coche
        if pulsado_abajo and miposicion[1] + mov_coche <= 550:
            miposicion[1] += mov_coche
            
        ultimo_movimiento = pygame.time.get_ticks()
        screen.blit(fondo,(0,0))
        for coche in coches:
            coche.render(screen,0.1+velocidad_extra)
            if coche.hay_colision(pygame.Rect(miposicion[0]+jugador[0],miposicion[1]+jugador[1],jugador[2],jugador[3])):
                return int(tiempo/1000)
        for arbol in arboles:
            arbol.render(screen,0.2+velocidad_extra)
        
        screen.blit(micoche,miposicion)
        
        screen.blit(font.render(str(int(tiempo/1000)), True, (255,255,255)), (10,10))
        
        pygame.display.flip()
    return 0