So i have a rotating shape that when 1 is pressed it goes normal speed, 2 its pressed, but when you hit zero its suppose to stop right where it is, and I cant find a way to do it, and I cant use the idle function. I am told that I should stop the updating of the “angle” in the callback, which i update via angle +=1, but every way I try i end up reseting the angle to zero instead of stoping the update process heres my code
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
s = 4
zaxis = 1
seconds = 3600/36
angle = 0
def main ():
glutInit (sys.argv) # initial the openGL system
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) # use single buffer and RGB mode
glutInitWindowSize (500, 500) # window size
glutInitWindowPosition (0, 0) # window position
glutCreateWindow ("Polygon [Chan]") # create window with given caption
glutDisplayFunc (mydisplay) # bind the display callback
glutTimerFunc(seconds,rotate, 0)
glutKeyboardFunc(keypress)
myinit () # initialize states and attributes
glutMainLoop () # enter the event loop
def myinit ():
glClearColor (1.0, 1.0, 0.0, 1.0) # black background
glColor3f (1.0, 0.0, 0.0) # draw in white
glMatrixMode (GL_PROJECTION) # modify the projection matrix
glLoadIdentity () # start with no transformation
glOrtho (-1.5, 1.5, -1.5, 1.5, -1.0, 1.0) # setting the viewing volume (used in 2D or 3D)
# gluOrtho2D (-1.0, 1.0, -1.0, 1.0) # setting the viewing window (used in 2D)
def mydisplay ():
global angle
glLoadIdentity()
glRotatef(angle,0,0,zaxis)
drawPolygon(s)
def rotate(n):
global angle
global seconds
angle += 1
if angle > 360:
angle -= 360
glutPostRedisplay()
glutTimerFunc(seconds,rotate, 0)
def keypress(key, x, y):
global s
global seconds
global zaxis
global angle
if key == "0" :
if key == "1" :
seconds = 3600/36
if key == "2" :
seconds = 72
if key == "3" :
s = 3
if key == "4" :
s = 4
if key == "5" :
s = 5
if key == "6" :
s = 6
if key == "7" :
s = 7
if key == "8" :
s = 8
if key == "9" :
s = 9
if key == "9" :
s = 9
if key == "a" :
s = 10
if key == "b" :
s = 11
if key == "c" :
s = 12
if key == "d" :
s = 13
if key == "e" :
s = 14
if key == "f" :
s = 15
if key == "g" :
s = 16
if key == "h" :
s = 17
if key == "i" :
s = 18
if key == "j" :
s = 19
if key == "r" :
zaxis = -1
def drawPolygon (n):
glClear (GL_COLOR_BUFFER_BIT) # clear the window
vertices = []
for i in range (n):
angle = math.pi * 2 * i / n
vertices.append ((math.cos (angle), math.sin (angle)))
glBegin (GL_LINES) # define a set of line segments
for i in range (n):
for j in range (i):
glVertex2f (vertices [j][0], vertices [j][1])
glVertex2f (vertices [i][0], vertices [i][1])
glEnd () # end of definition
glFlush () # flush so the image appears
main () # run the program