Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import random
  2. from random import *
  3. from math import *
  4. import pygame
  5. from OpenGL.GL import *
  6. from OpenGL.GLU import *
  7. from pygame.locals import *
  8.  
  9. going_left = False
  10. going_right = False
  11. going_up = False
  12. going_down = False
  13. x_pos = 100
  14. y_pos = 100
  15. game_width = 800
  16. game_height = 600
  17.  
  18.  
  19. def init_game():
  20. pygame.display.init()
  21. pygame.display.set_mode((game_width, game_height), DOUBLEBUF | OPENGL)
  22. glClearColor(0.0, 1.0, 0.1, 0.0)
  23.  
  24.  
  25. def update():
  26. global x_pos
  27. global y_pos
  28. global going_left
  29. global going_right
  30. global going_down
  31. global going_up
  32. if going_left:
  33. x_pos -= 8
  34. if going_right:
  35. x_pos += 8
  36. if going_up:
  37. y_pos += 8
  38. if going_down:
  39. y_pos -= 8
  40.  
  41.  
  42.  
  43. def display():
  44. global y_pos
  45. glMatrixMode(GL_PROJECTION)
  46. glLoadIdentity()
  47. glMatrixMode(GL_MODELVIEW)
  48. glLoadIdentity()
  49. glViewport(0, 0, game_width, game_height)
  50. gluOrtho2D(0, game_width, 0, game_height)
  51. glColor3f(0.5, 1.0, 1.0)
  52. glBegin(GL_TRIANGLES)
  53. glVertex2f(x_pos, 100)
  54. glVertex2f(x_pos, 200)
  55. glVertex2f(x_pos + 300, y_pos)
  56. glEnd()
  57. glBegin(GL_TRIANGLES)
  58. glVertex2f(x_pos, 100)
  59. glVertex2f(x_pos, 200)
  60. glVertex2f(x_pos + 300, y_pos + 50)
  61. glEnd()
  62. pygame.display.flip()
  63.  
  64.  
  65. def game_loop():
  66. global going_left
  67. global going_right
  68. global going_down
  69. global going_up
  70. for event in pygame.event.get():
  71. if event.type == pygame.QUIT:
  72. pygame.quit()
  73. quit()
  74. elif event.type == pygame.KEYDOWN:
  75. if event.key == K_ESCAPE:
  76. pygame.quit()
  77. quit()
  78. elif event.key == K_q:
  79. glClearColor(random(), random(), random(), 1.0)
  80. elif event.key == K_RIGHT:
  81. going_right = True
  82. elif event.key == K_LEFT:
  83. going_left = True
  84. elif event.key == K_UP:
  85. going_up = True
  86. elif event.key == K_DOWN:
  87. going_down = True
  88. elif event.type == pygame.KEYUP:
  89. if event.key == K_LEFT:
  90. going_left = False
  91. if event.key == K_RIGHT:
  92. going_right = False
  93. if event.key == K_UP:
  94. going_up = False
  95. if event.key == K_DOWN:
  96. going_down = False
  97. elif event.type == pygame.MOUSEBUTTONDOWN:
  98. if event.button == 1:
  99. x_pos = pygame.mouse.get_pos()[0]
  100. y_pos = pygame.mouse.get_pos()[1]
  101.  
  102. update()
  103. display()
  104.  
  105.  
  106. if __name__ == "__main__":
  107. init_game()
  108. while True:
  109. game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement