Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/12/2008, 17:40
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Tiro parabolico con pygame

Aquí dejo un pequeño código, donde simulo el tiro parabólico:
Las teclas son:

Arriba -> Aumentar el ángulo
Abajo -> Disminuir el ángulo

Izquierda -> Aumentar la velocidad inicial
Derecha -> Disminuir la velocidad inicial

Barra espaciadora -> Empezar la animacion
Escape -> Salir
Código python:
Ver original
  1. #!/usr/bin/env python
  2. #coding: UTF-8
  3.  
  4. def printos(surface, text, x, y, color, font):
  5.     text_in_lines = text.split('\n')
  6.     for line in text_in_lines:
  7.         new = font.render(line, 1, color)
  8.         surface.blit(new, (x, y))
  9.         y += new.get_height()
  10.  
  11. def arrow(screen, color, x, y, ang):
  12.     pygame.draw.line(screen, color, (x, y), (x + 20*math.cos(math.radians(ang + 150.0)), y - 20*math.sin(math.radians(ang + 150.0))))
  13.     pygame.draw.line(screen, color, (x, y), (x + 20*math.cos(math.radians(ang + 210.0)), y - 20*math.sin(math.radians(ang + 210.0))))
  14.  
  15. def vector(screen, color, x, y, ang):
  16.     w, z = x + v0*10*math.cos(math.radians(ang)), y - v0*10*math.sin(math.radians(ang))
  17.     x, y, w, z = int(x), int(y), int(w), int(z)
  18.     arrow(screen, color, w, z, ang)
  19.     pygame.draw.line(screen, blue, (x, y), (w, z))
  20.    
  21.  
  22. import pygame, math
  23. pygame.init()
  24.  
  25. size = width, height = 640, 480
  26.  
  27. black = (0, 0, 0)
  28. blue = (0, 0, 255)
  29. green = (0, 255, 0)
  30. background = (200, 200, 200)
  31.  
  32. screen = pygame.display.set_mode(size)
  33. pygame.display.set_caption("Inicializar")
  34.  
  35.  
  36. radio = 10
  37. x = 10
  38. y = height - radio
  39.  
  40. pygame.font.init()
  41. font = pygame.font.Font(None, 30)
  42.  
  43. clock = pygame.time.Clock()
  44.  
  45. t = 0.0
  46. dt = 0.5
  47.  
  48. v0 = 25.0
  49. a = 1.0
  50. ang = 45.0
  51.  
  52. vx = 0
  53. vy = 0
  54.  
  55. lock = True
  56. lock1 = False
  57. second = False
  58. while 1:
  59.     screen.fill(background)
  60.     for event in pygame.event.get():
  61.         if event.type == pygame.QUIT:
  62.             exit()
  63.     teclado = pygame.key.get_pressed()
  64.     if(tuple(set(teclado)) == (0,1) and lock):
  65.         if (teclado[pygame.K_UP]):
  66.             ang += 1
  67.             if(ang >= 90):
  68.                 ang = 90
  69.         if (teclado[pygame.K_DOWN]):
  70.             ang -= 1
  71.             if(ang < 0):
  72.                 ang = 0
  73.         if (teclado[pygame.K_RIGHT] and v0 < 100):
  74.             v0 += 1
  75.         if (teclado[pygame.K_LEFT] and v0 > 1):
  76.             v0 -= 1
  77.         if (teclado[pygame.K_SPACE]):
  78.             lock = False
  79.             lock1 = True
  80.             vy0 = v0*math.sin(math.radians(ang))
  81.     if (teclado[pygame.K_ESCAPE]):
  82.         break
  83.     vx0 = v0*math.cos(math.radians(ang))
  84.     vy = a*t - v0*math.sin(math.radians(ang))
  85.     if(lock1):
  86.         y = (height - radio) - vy0*t + .5*a*(t**2)
  87.         x = radio + vx0*t
  88.         t += dt
  89.         if(y > (height - radio)):
  90.             y = height - radio
  91.             t = 0
  92.             lock1 = False
  93.             second = True
  94.     if(second):
  95.         printos(screen, "Continue? Y/N", 0, 60, green, font)
  96.         if(teclado[pygame.K_y]):
  97.             lock = True
  98.             second = False
  99.             x = radio
  100.         elif(teclado[pygame.K_n]):
  101.             break
  102.  
  103.     printos(screen, "x = %d y = %d ang = %d v0 = %d vx = %d vy = %d"%(x - radio, height - radio - y, ang, v0, vx0, vy), 0, 0, green, font)
  104.     pygame.draw.circle(screen, blue, (int(x), int(y)), radio)
  105.     vector(screen, blue, x, y, ang)
  106.     pygame.display.flip()
  107.     clock.tick(30)