Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from OpenGL.GL import *
  5. from OpenGL.GLU import *
  6. import random
  7. from random import *
  8.  
  9. x_pos = 100
  10. y_pos = 100
  11. box_width = 50
  12. box_height = 50
  13. blob_width = 20
  14. blob_height = 20
  15. game_width = 800
  16. game_height = 600
  17. going_right = False
  18. going_left = False
  19. going_up = False
  20. going_down = False
  21. score = 0
  22. blobs = []
  23.  
  24.  
  25. def init_game():
  26. pygame.display.init()
  27. pygame.display.set_mode((game_width, game_height), DOUBLEBUF | OPENGL)
  28. glClearColor(1.0, 1.0, 1.0, 1.0)
  29.  
  30.  
  31. def update():
  32. global x_pos
  33. global y_pos
  34. collisionDetection(x_pos, y_pos)
  35. if going_right:
  36. if x_pos < game_width - box_width:
  37. x_pos += 10
  38. if going_left:
  39. if x_pos > 0:
  40. x_pos -= 10
  41. if going_up:
  42. if y_pos < game_height - box_height:
  43. y_pos += 10
  44. if going_down:
  45. if y_pos > 0:
  46. y_pos -= 10
  47.  
  48.  
  49. def collisionDetection(x_pos, y_pos):
  50. global box_width
  51. global box_height
  52. for i in range(0,len(blobs)):
  53. blob_x, blob_y = blobs[i]
  54. if (((x_pos > blob_x and x_pos < blob_x + blob_width) or (x_pos + box_width) >= blob_x and x_pos <= blob_x + blob_width)):
  55. if (((y_pos > blob_y and y_pos < blob_y + blob_height) or (y_pos + box_height) >= blob_y and y_pos <= blob_y + blob_height)):
  56. print("hello")
  57.  
  58. def generateBlob():
  59. blob_x = 200
  60. blob_y = 250
  61. blobs.append((blob_x, blob_y))
  62. glColor3f(1.0, 0.0, 0.0)
  63. glBegin(GL_POLYGON)
  64. glVertex2f(blob_x, blob_y)
  65. glVertex2f(blob_x + blob_width, blob_y)
  66. glVertex2f(blob_x + blob_width, blob_y + blob_height)
  67. glVertex2f(blob_x, blob_y + blob_height)
  68. glEnd()
  69.  
  70.  
  71. def drawSquare():
  72. glColor3f(0.2, 0.4, 0.0)
  73. glBegin(GL_POLYGON)
  74. glVertex2f(x_pos, y_pos)
  75. glVertex2f(x_pos + box_width, y_pos)
  76. glVertex2f(x_pos + box_width, y_pos + box_height)
  77. glVertex2f(x_pos, y_pos + box_height)
  78. glEnd()
  79.  
  80.  
  81. def display():
  82. glClear(GL_COLOR_BUFFER_BIT)
  83. glMatrixMode(GL_PROJECTION)
  84. glLoadIdentity()
  85. glMatrixMode(GL_MODELVIEW)
  86. glLoadIdentity()
  87. glViewport(0, 0, 800, 600)
  88. gluOrtho2D(0, 800, 0, 600)
  89. drawSquare()
  90. if score == 0:
  91. generateBlob()
  92. pygame.display.flip()
  93.  
  94.  
  95. def game_loop():
  96. global going_right
  97. global going_left
  98. global going_up
  99. global going_down
  100. for event in pygame.event.get():
  101. if event.type == pygame.QUIT:
  102. pygame.quit()
  103. quit()
  104. elif event.type == pygame.KEYDOWN:
  105. if event.key == K_ESCAPE:
  106. pygame.quit()
  107. quit()
  108. elif event.key == K_RIGHT:
  109. going_right = True
  110. elif event.key == K_LEFT:
  111. going_left = True
  112. elif event.key == K_UP:
  113. going_up = True
  114. elif event.key == K_DOWN:
  115. going_down = True
  116. elif event.type == pygame.KEYUP:
  117. if event.key == K_RIGHT:
  118. going_right = False
  119. if event.key == K_LEFT:
  120. going_left = False
  121. if event.key == K_UP:
  122. going_up = False
  123. if event.key == K_DOWN:
  124. going_down = False
  125.  
  126. update()
  127. display()
  128.  
  129.  
  130. if __name__ == "__main__":
  131. init_game()
  132. while True:
  133. game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement