Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/05/2009, 17:27
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
Pygame: Pequeño graficador

Este es un pequeño graficador, que trabaja en una resolución fija, la verdad es que es un programita muy simple. Espero subir otro parecido pero para coordenadas polares. La función esta dentro del código, se llama f(x).
También pueden leer la funcion y utilizar la función eval. Claro no olviden validar antes de usar la función eval.

main.py
Código python:
Ver original
  1. #!/usr/bin/env python
  2. #coding: UTF-8
  3. import pygame
  4. from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_ESCAPE
  5.  
  6. blue = (0, 0, 255)
  7. black = (0, 0, 0)
  8.  
  9. def init():
  10.     pygame.init()
  11.  
  12.     size = width, height = 640, 480
  13.     screen = pygame.display.set_mode(size)
  14.     pygame.display.set_caption("Graficadora")
  15.  
  16.     return screen
  17.  
  18. def translate((x, y)):
  19.     return (x + 320, 240 - y)
  20. screen = init()
  21.  
  22. dx = 0.5
  23. x = 0
  24. def f(x):
  25.     return x**2 / 100
  26.        
  27. while 1:
  28.     for event in pygame.event.get():
  29.         if event.type == pygame.QUIT:
  30.             exit()
  31.    
  32.     keyboard = pygame.key.get_pressed()
  33.     if keyboard[K_ESCAPE]:
  34.         exit()
  35.    
  36.     try:
  37.         start_pos = (int(x), int(f(x)))
  38.         end_pos = (int(x+dx), int(f(x+dx)))
  39.         x += dx
  40.     except ZeroDivisionError:
  41.         break
  42.     if x > 640:
  43.         x = 0
  44.     pygame.draw.line(screen, blue, translate(start_pos), translate(end_pos))
  45.     pygame.display.flip()