dots.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-Demo-3.0.1b1 » PyOpenGL-Demo » da » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL Demo 3.0.1b1 » PyOpenGL Demo » da » dots.py
#!/usr/bin/python

# This is statement is required by the build system to query build info
if __name__ == '__build__':
  raise Exception

import string
__version__ = string.split('$Revision: 1.1.1.1 $')[1]
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:28 $')[1:3], ' ')
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'

try:
  from numpy import *
  from numpy.random import *
except ImportError, err:
  try: 
    from Numeric import *
    from RandomArray import *
  except ImportError, err:
    print "This demo requires the numpy or Numeric extension, sorry"
    import sys
    sys.exit()
import string, sys
from OpenGL.GL import *
from OpenGL.GLUT import *

MY_LIST=1
NUMDOTS = 500
NUMDOTS2 = 600
x = random(NUMDOTS)*2-1
y = random(NUMDOTS)*2-1
MAX_AGE = 13
age = randint(0,MAX_AGE,(NUMDOTS,))
move_length = .005  # 1.0 = screen width
angle = 0      # in radians
delta_angle = .2  # in radians
move_x = move_length*cos(angle)
move_y = move_length*sin(angle)
halted = 0

def display(*args):
  global x, y, move_x, move_y, NUMDOTS, NUMDOTS2, MAX_AGE, age
  glClearColor(0.0, 0.0, 0.0, 0.0)
  glClear(GL_COLOR_BUFFER_BIT)
  glColor3f(1.0,1.0,0.0)
  x = x + move_x
  y = y + move_y
  age = age + 1
  which = greater(age, MAX_AGE)
  x = choose(which, (x, random(NUMDOTS)))
  y = choose(which, (y, random(NUMDOTS)))
  age = choose(which, (age, 0))
  x = choose(greater(x,1.0),(x,x-1.0))  # very cool - wraparound
  y = choose(greater(y,1.0),(y,y-1.0))
  x2 = random(NUMDOTS2)
  y2 = random(NUMDOTS2)
  v = concatenate((transpose(array([x,y])), transpose(array([x-.005,y+.005])), transpose(array([x+.005,y-.005])), transpose(array([x2,y2]))))
  glVertexPointerd(v)
  glEnableClientState(GL_VERTEX_ARRAY)
  glDrawArrays(GL_POINTS, 0, len(v))
  glDisableClientState(GL_VERTEX_ARRAY)
  glFlush()
  glutSwapBuffers()


def halt():
  pass

def keyboard(*args):
  sys.exit()

def mouse(button, state, x, y):
  global angle, delta_angle, move_x, move_y, move_length, halted
  if button == GLUT_LEFT_BUTTON:
    angle = angle + delta_angle
  elif button == GLUT_RIGHT_BUTTON:
    angle = angle - delta_angle
  elif button == GLUT_MIDDLE_BUTTON and state == GLUT_DOWN:
    if halted:
      glutIdleFunc(display)
      halted = 0
    else:
      glutIdleFunc(halt)
      halted = 1
  move_x = move_length * cos(angle)
  move_y = move_length * sin(angle)

def setup_viewport():
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()
  glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)

def reshape(w, h):
  glViewport(0, 0, w, h)
  setup_viewport()

def main():
  glutInit(sys.argv)
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
  glutInitWindowSize(300, 300)
  glutCreateWindow('Dots')
  setup_viewport()
  glutReshapeFunc(reshape)
  glutDisplayFunc(display)
  glutIdleFunc(display)
  glutMouseFunc(mouse)
  glutKeyboardFunc(keyboard)
  glutMainLoop()

print "Use the mouse buttons to control some of the dots."
print "Hit any key to quit."
main()



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.