Share Pastebin
Guest
Public paste!

Esteban

By: a guest | Dec 7th, 2009 | Syntax: Python | Size: 4.44 KB | Hits: 213 | Expires: Never
Copy text to clipboard
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import sys
  4. from math import tan, sqrt, log, sin, cos, pi, atan
  5.  
  6. def getvel(x, y, angle, v0, l):
  7.     w = v0/(l*sin(angle)) if angle else 0
  8.     return (v0-w*y, w*(x-l*cos(angle)))
  9.  
  10. def modulo(vector):
  11.     return sqrt(vector[0]**2 + vector[1]**2)
  12.  
  13. def choose(vector):
  14.     if modulo(vector) == 0:
  15.         return (0,0,0)
  16.     else:
  17.         return (0, abs(int(255*vector[0]/modulo(vector))), abs(int(255*vector[1]/modulo(vector))))
  18.  
  19. def scale(modulo):
  20.     return modulo
  21.  
  22. def p2s(point):
  23.     ''' Point to Screen point '''
  24.     return (30+point[0], 400-point[1])
  25.  
  26. def s2p(point):
  27.     ''' Screen point to coordinate point '''
  28.     return (point[0]-30, 400-point[1])
  29.  
  30. def closest(p1, p2, p3):
  31.     if (p2[0]-p1[0])**2+(p2[1]-p1[1])**2 == 0:
  32.         return (0,0) # This shouldn't happen: means that p1 == p2 (bar length = 0)
  33.     u = ((p3[0]-p1[0])*(p2[0]-p1[0])+(p3[1]-p1[1])*(p2[1]-p1[1]))/((p2[0]-p1[0])**2+(p2[1]-p1[1])**2)
  34.     return (p1[0]+u*(p2[0]-p1[0]), p1[1]+u*(p2[1]-p1[1]))
  35.  
  36. if __name__ == '__main__':
  37.     pygame.init()
  38.     screen = pygame.display.set_mode((800, 600))
  39.     font = pygame.font.Font(None, 17)
  40.     l = 400
  41.     theta = pi/4
  42.     v0 = 15
  43.     play = False
  44.     clock = pygame.time.Clock()
  45.     sumticks = 0
  46.     Qstate = True
  47.  
  48.     while 1:
  49.         for event in pygame.event.get():
  50.             if event.type == pygame.KEYDOWN:
  51.                 if event.key == pygame.K_s:
  52.                     play = True
  53.                     clock.tick()
  54.                     sumticks = 0
  55.                 if event.key == pygame.K_KP_PLUS or event.key == pygame.K_RIGHTBRACKET:
  56.                     v0 += 1
  57.                 if event.key == pygame.K_KP_MINUS or event.key == pygame.K_LEFTBRACKET:
  58.                     v0 -= 1
  59.                 if event.key == pygame.K_KP_MULTIPLY or event.key == pygame.K_COMMA:
  60.                     theta += pi/24
  61.                 if event.key == pygame.K_KP_DIVIDE or event.key == pygame.K_PERIOD:
  62.                     theta -= pi/24
  63.                 if event.key == pygame.K_r:
  64.                     theta = pi/4
  65.                     play = False
  66.                 if event.key == pygame.K_SPACE:
  67.                     play = False if play else True
  68.                 if event.key == pygame.K_q:
  69.                     Qstate = False if Qstate else True
  70.                 if event.key == pygame.K_ESCAPE:
  71.                     sys.exit()
  72.         if play:
  73.             sumticks += clock.tick()
  74.             if sumticks > 100:
  75.                 theta -= 0.01
  76.                 sumticks = 0
  77.                 if theta <= 0:
  78.                     play = False
  79.                     theta = 0
  80.  
  81.         screen.fill((0,0,0))
  82.  
  83.         # Axis
  84.         pygame.draw.aaline(screen, (255,255,255), (30, 0), (30, 460))
  85.         pygame.draw.aaline(screen, (255,255,255), (0, 400), (640, 400))
  86.  
  87.         # Some text
  88.         text_vel = font.render('Velocidad = %d'%v0, True, (255,255,255), (0,0,0))
  89.         text_theta = font.render('Angulo = %f'%theta, True, (255,255,255), (0,0,0))
  90.         screen.blit(text_vel, (200, 0))
  91.         screen.blit(text_theta, (200, 15))
  92.  
  93.         if Qstate:
  94.             # In the "Q State", we draw speed lines for each value of x in the bar (cheap effect)
  95.             # Q state is named after the key that activates this mode. Chosen by the Qwerty inventor
  96.             x = int(l*cos(theta))
  97.             for i in range(x):
  98.                 inicial = (i, l*sin(theta)-i*tan(theta))
  99.                 v = getvel(inicial[0], inicial[1], theta, v0, l)
  100.                 m = modulo(v)
  101.                 # Can also choose color = (0,255,i*255/x)
  102.                 color = choose(v)
  103.                 escala = scale(m)
  104.                 pygame.draw.aaline(screen, color, p2s(inicial), p2s((inicial[0]+v[0]*escala, inicial[1]+v[1]*escala)))
  105.    
  106.         punto = closest( (0, l*sin(theta)), (l*cos(theta), 0) , s2p(pygame.mouse.get_pos()) )
  107.         v = getvel( punto[0], l*sin(theta)-punto[0]*tan(theta), theta, v0, l )
  108.         m = modulo(v)
  109.         color = (255,255,255)
  110.         escala = scale(m)
  111.         pygame.draw.line(screen, color, p2s(punto), p2s((punto[0]+v[0]*escala, punto[1]+v[1]*escala)), 3)
  112.         text_current = font.render('Velocidad en el punto seleccionado = %f con un ángulo %f'%(m,atan(v[1]/v[0])), True, (255,255,255), (0,0,0))
  113.         screen.blit(text_current, (200, 30))
  114.         pygame.draw.aaline(screen, (255,0,0), p2s((0, l*sin(theta))), p2s((l*cos(theta), 0)), 10)
  115.  
  116.         pygame.display.flip()