Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- from pygame.locals import *
- from OpenGL.GL import *
- from OpenGL.GLU import *
- import random
- from random import *
- x_pos = 100
- y_pos = 100
- box_width = 50
- box_height = 50
- blob_width = 20
- blob_height = 20
- game_width = 800
- game_height = 600
- going_right = False
- going_left = False
- going_up = False
- going_down = False
- score = 0
- blobs = []
- def init_game():
- pygame.display.init()
- pygame.display.set_mode((game_width, game_height), DOUBLEBUF | OPENGL)
- glClearColor(1.0, 1.0, 1.0, 1.0)
- def update():
- global x_pos
- global y_pos
- collisionDetection(x_pos, y_pos)
- if going_right:
- if x_pos < game_width - box_width:
- x_pos += 10
- if going_left:
- if x_pos > 0:
- x_pos -= 10
- if going_up:
- if y_pos < game_height - box_height:
- y_pos += 10
- if going_down:
- if y_pos > 0:
- y_pos -= 10
- def collisionDetection(x_pos, y_pos):
- global box_width
- global box_height
- for i in range(0,len(blobs)):
- blob_x, blob_y = blobs[i]
- 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)):
- 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)):
- print("hello")
- def generateBlob():
- blob_x = 200
- blob_y = 250
- blobs.append((blob_x, blob_y))
- glColor3f(1.0, 0.0, 0.0)
- glBegin(GL_POLYGON)
- glVertex2f(blob_x, blob_y)
- glVertex2f(blob_x + blob_width, blob_y)
- glVertex2f(blob_x + blob_width, blob_y + blob_height)
- glVertex2f(blob_x, blob_y + blob_height)
- glEnd()
- def drawSquare():
- glColor3f(0.2, 0.4, 0.0)
- glBegin(GL_POLYGON)
- glVertex2f(x_pos, y_pos)
- glVertex2f(x_pos + box_width, y_pos)
- glVertex2f(x_pos + box_width, y_pos + box_height)
- glVertex2f(x_pos, y_pos + box_height)
- glEnd()
- def display():
- glClear(GL_COLOR_BUFFER_BIT)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glViewport(0, 0, 800, 600)
- gluOrtho2D(0, 800, 0, 600)
- drawSquare()
- if score == 0:
- generateBlob()
- pygame.display.flip()
- def game_loop():
- global going_right
- global going_left
- global going_up
- global going_down
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- elif event.type == pygame.KEYDOWN:
- if event.key == K_ESCAPE:
- pygame.quit()
- quit()
- elif event.key == K_RIGHT:
- going_right = True
- elif event.key == K_LEFT:
- going_left = True
- elif event.key == K_UP:
- going_up = True
- elif event.key == K_DOWN:
- going_down = True
- elif event.type == pygame.KEYUP:
- if event.key == K_RIGHT:
- going_right = False
- if event.key == K_LEFT:
- going_left = False
- if event.key == K_UP:
- going_up = False
- if event.key == K_DOWN:
- going_down = False
- update()
- display()
- if __name__ == "__main__":
- init_game()
- while True:
- game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement