Advertisement
mechanizmos58

pygame_mouse_keyboard_rotating_cube.py

Dec 22nd, 2021
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.57 KB | None | 0 0
  1.  
  2. # Combine pygame and OpenGL to demonstrate a range
  3. #    of control and drawing options
  4.  
  5. # 23_12_21
  6.  
  7.  
  8. import pygame
  9. from pygame.locals import *
  10.  
  11. from OpenGL.GL import *
  12. from OpenGL.GLU import *
  13.  
  14. import sys
  15.  
  16.  
  17. print('\nLEFT, RIGHT, UP, DOWN: move cube in X or Y directions\n')
  18. print('CTRL-UP, CTRL-DOWN moves cube in Z direction\n')
  19. print('SPACE: toggle cube recede\n')
  20. print('CTRL-m: toggle cursor (two methods)\n')
  21. print('w: toggle wireframe\n')
  22. print('MOUSEWHEEL: move cube in Z direction (two methods)\n')
  23. print('Left mouse button double click: toggle rotation\n')
  24. print('Left mouse button: move cube around in X, Y  directions\n')
  25. print('ESC, RETURN, and middle mouse button: exit\n')
  26.  
  27. '''
  28.  NOTES:
  29.  
  30.  can't use screen.fill(col) on OpenGL "surfaces"
  31.  can't use pygame.display.update() on OpenGL
  32.  glVertex3f(*q])  ≍  glVertex3fv(q) where q is (x,y,z)
  33.  x = not x  ≍  x ^= 1
  34.  
  35.  COLLECT INFO:
  36.  
  37.    MOUSE = pygame.mouse.get_pressed()
  38.    KEYS = pygame.key.get_pressed()
  39.    MODS = pygame.key.get_mods()
  40.  
  41.  PYGAME EXIT TEMPLATE:
  42.  
  43.    for event in pygame.event.get():
  44.  
  45.        # the X button
  46.        if event.type == QUIT:
  47.          pygame.quit()
  48.          sys.exit() # OR running = False OR return
  49.  
  50.        if event.type == KEYDOWN:
  51.          if event.key == K_ESCAPE:
  52.              pygame.quit()
  53.              sys.exit() # OR running = False OR return
  54.  
  55.  FPS CONTROL:
  56.  
  57.    FPS = 30
  58.    FPSCLOCK = pygame.time.Clock()
  59.    ...
  60.    FPSCLOCK.tick(FPS)
  61.    OR...
  62.    pygame.time.Clock().tick(1000./15.)
  63.  
  64.    OR...
  65.    pygame.time.wait(15)
  66.  
  67.  
  68.  THE CUBE:
  69.  
  70.  
  71.         6 +--------+ 7
  72.          /|       /|
  73.         / |      / |
  74.        /  |     /  |
  75.     3 +--------+ 2 |
  76.       | 5 +----|---+ 4
  77.       |  /     |  /
  78.       | /      | /
  79.       |/       |/
  80.     0 +--------+ 1
  81.  
  82. '''
  83.  
  84. vertices = ((-1,-1,1), (1,-1,1), (1,1,1), (-1,1,1),
  85.             (1,-1,-1), (-1,-1,-1), (-1,1,-1), (1,1,-1))
  86.  
  87. faces = ((0,1,2,3), (1,4,7,2), (4,5,6,7),
  88.          (5,0,3,6), (3,2,7,6), (5,4,1,0))
  89.  
  90. colors = ((1,0,0), (0,0,1), (1,1,0), (1,0,1), (0,1,0), (0,1,1))
  91.  
  92. edges = ((0,1), (0,3), (0,5), (1,2), (1,4), (2,3),
  93.          (2,7), (3,6), (4,5), (4,7), (5,6), (6,7))
  94.  
  95.  
  96. def solid_cube():
  97.  
  98.     glBegin(GL_QUADS)
  99.     for face in range(6):
  100.         glColor3f(*colors[face])
  101.         for i in faces[face]:
  102.             glVertex3f(*vertices[i])
  103.     glEnd()
  104.  
  105.  
  106. def wireframe_cube():
  107.  
  108.     glLineWidth(4)
  109.     glBegin(GL_LINES)
  110.     for e in edges:
  111.         for v in e:
  112.             glVertex3f(*vertices[v])
  113.     glEnd()
  114.  
  115.  
  116. def make_chessboard():
  117.  
  118.     g.chessboard = glGenLists(1)
  119.     glNewList(g.chessboard, GL_COMPILE)
  120.  
  121.     xgridsize, zgridsize = 8, 8
  122.     sizex, sizez = 110, 110
  123.     y = -110
  124.     startz = -2
  125.  
  126.     glBegin(GL_QUADS)
  127.     for x in range(-xgridsize//2, xgridsize//2):
  128.         for z in range(startz, -(zgridsize-startz), -1):
  129.  
  130.             n = x+z & 1
  131.             glColor3f(n, n, n)
  132.  
  133.             glVertex3f(    x*sizex,  y,     z*sizez)
  134.             glVertex3f((x+1)*sizex,  y,     z*sizez)
  135.             glVertex3f((x+1)*sizex,  y, (z+1)*sizez)
  136.             glVertex3f(    x*sizex,  y, (z+1)*sizez)
  137.     glEnd()
  138.  
  139.     glEndList()
  140.  
  141.  
  142. def main():
  143.  
  144.     pygame.init()
  145.  
  146.     display = (1000, 800)
  147.  
  148.     screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
  149.  
  150.     pygame.display.set_caption('ESC or RETURN or middle button exits')
  151.  
  152.     glEnable(GL_BLEND)
  153.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  154.     glEnable(GL_LINE_SMOOTH)
  155.     glEnable(GL_POLYGON_SMOOTH)
  156.  
  157.     glEnable(GL_DEPTH_TEST)
  158.     glDepthFunc(GL_LESS)
  159.  
  160.     make_chessboard()
  161.  
  162.     angle = 1.
  163.     x = 0.
  164.     y = 0.
  165.     z = -6.
  166.     RECEDE_FLAG = 0
  167.     MOUSE_FLAG = 1
  168.     pygame.mouse.set_visible(MOUSE_FLAG)
  169.  
  170.     MOUSEWHEEL_METHOD = 'method1'
  171.     MODIFER_METHOD = 'method2'
  172.  
  173.     WIREFRAME_FLAG = 0
  174.     ROTATION_FLAG = 1
  175.  
  176.     mousex, mousey = pygame.mouse.get_pos()
  177.     TIMER_EVENT = USEREVENT
  178.     clicked = 0
  179.     TIMEOUT = 400 # milliseconds
  180.  
  181.     while True:
  182.  
  183.         for event in pygame.event.get():
  184.  
  185.             if event.type == QUIT:
  186.                 pygame.quit()
  187.                 return
  188.  
  189.             if event.type == KEYDOWN:
  190.                 if event.key in (K_ESCAPE, K_RETURN, K_q):
  191.                     pygame.quit()
  192.                     return
  193.  
  194.                 if event.key == K_w:
  195.                     WIREFRAME_FLAG ^= 1
  196.  
  197.                 if event.key == K_SPACE:
  198.                     RECEDE_FLAG ^= 1
  199.  
  200.                 if event.key == K_m:
  201.                     if MODIFER_METHOD == 'method1':
  202.                         mods = pygame.key.get_mods()
  203.                         if mods & KMOD_CTRL:
  204.                             MOUSE_FLAG ^= 1
  205.                             pygame.mouse.set_visible(MOUSE_FLAG)
  206.  
  207.                     elif MODIFER_METHOD == 'method2':
  208.                         if event.mod & KMOD_CTRL:
  209.                             MOUSE_FLAG ^= 1
  210.                             pygame.mouse.set_visible(MOUSE_FLAG)
  211.  
  212.  
  213.             if event.type == MOUSEBUTTONDOWN:
  214.                 if event.button == 1:
  215.                     if clicked:
  216.                         ROTATION_FLAG ^= 1
  217.                     else:
  218.                         clicked = 1
  219.                         pygame.time.set_timer(TIMER_EVENT, TIMEOUT)
  220.  
  221.             elif event.type == TIMER_EVENT:
  222.                 clicked = 0
  223.                 pygame.time.set_timer(TIMER_EVENT, 0)
  224.  
  225.  
  226.             if event.type == MOUSEBUTTONDOWN:
  227.  
  228.                 if event.button == 2:
  229.                     pygame.quit()
  230.                     return
  231.  
  232.                 if MOUSEWHEEL_METHOD == 'method1':
  233.                     # MOUSEWHEEL FORWARD
  234.                     if event.button == 4:
  235.                         z -= .5
  236.                     # MOUSEWHEEL BACKWARD
  237.                     if event.button == 5:
  238.                         z += .5
  239.  
  240.  
  241.             if MOUSEWHEEL_METHOD == 'method2':
  242.                 if event.type == MOUSEWHEEL:
  243.                     # event.y = +1: FORWARD
  244.                     # event.y = -1: BACKWARD
  245.                     z -= event.y / 2.
  246.  
  247.  
  248.         MOUSE = pygame.mouse.get_pressed()
  249.  
  250.         if MOUSE[0]:
  251.             x1, y1 = pygame.mouse.get_rel()
  252.             x += x1 / 100
  253.             y -= y1 / 100
  254.         else:
  255.             mousex, mousey = pygame.mouse.get_rel()
  256.  
  257.         KEYS = pygame.key.get_pressed()
  258.  
  259.         if KEYS[K_LEFT]:
  260.             x -= 0.05
  261.         if KEYS[K_RIGHT]:
  262.             x += 0.05
  263.  
  264.         MODS = pygame.key.get_mods()
  265.  
  266.         if KEYS[K_UP]:
  267.             if MODS & KMOD_CTRL:
  268.                 z -= .4
  269.             else:
  270.                 y += 0.05
  271.         if KEYS[K_DOWN]:
  272.             if MODS & KMOD_CTRL:
  273.                 z += .4
  274.             else:
  275.                 y -= 0.05
  276.  
  277.  
  278.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  279.         glClearColor(0.1, 0.1, 0.1, 1)
  280.  
  281.         glMatrixMode(GL_PROJECTION)
  282.         glLoadIdentity()
  283.         gluPerspective(60.0, display[0]/display[1], 1.0, 10000.0)
  284.  
  285.         glMatrixMode(GL_MODELVIEW)
  286.         glLoadIdentity()
  287.  
  288.         glCallList(g.chessboard)
  289.  
  290.         glTranslatef(x, y, z)
  291.  
  292.         if RECEDE_FLAG:
  293.             z -= 0.07
  294.  
  295.         glRotatef(angle, 1., 1., 1.)
  296.  
  297.         if ROTATION_FLAG:
  298.             angle += 1.
  299.  
  300.         if WIREFRAME_FLAG:
  301.             glColor3f(0.0, 0.0, 1.0)
  302.             wireframe_cube()
  303.         else:
  304.             solid_cube()
  305.             glColor3f(0.0, 0.0, 0.0)
  306.             wireframe_cube()
  307.  
  308.  
  309.         pygame.display.flip()
  310.  
  311.         # pygame.time.wait(15)
  312.         pygame.time.Clock().tick(1000./15.)
  313.  
  314.  
  315. class g: ...
  316.  
  317.  
  318. if __name__ == '__main__':
  319.     sys.exit(main())
  320.  
  321.  
  322.  
  323.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement