#LD24 WARMUP GAME
import pygame
from pygame.locals import *
import sys
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL import *
import time
import random
from ctypes import util
try:
from OpenGL.platform import win32
except AttributeError:
pass
pygame.mixer.pre_init(44100,-16,2, 512)
pygame.init()
pygame.mixer.init()
shootsfx = pygame.mixer.Sound("shoot.wav")
explosionsfx = pygame.mixer.Sound("explosion.wav")
SCREEN_W = 800
SCREEN_H = 400
DISPLAY = (SCREEN_W,SCREEN_H)
sFlags = pygame.OPENGL | pygame.DOUBLEBUF
screen = pygame.display.set_mode(DISPLAY,sFlags)
glClearColor(0, 0, 0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, SCREEN_W, SCREEN_H, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
score = 0
def kill():
global rects, bullets, time_passed, explosions, score
print "SCORE:%d" % score
score = 0
rects = []
bullets = []
explosions = []
plyr.reset()
time_passed = 0
class Player:
def __init__(self):
self.x = 0
self.y = 200
self.w = 10
self.h = 10
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
self.overheat = 0
self.shotdelay = 150
self.oldtime = 0
self.oktime = 0
def update(self ):
for index in rects:
if index.rect.colliderect(self.rect):
kill()
self.overheat -= 0.3
if self.overheat <= 0:
self.overheat = 0
if self.x <= 0:
self.x = 0
if self.x >= 800-self.w:
self.x = 800-self.w
if self.y <= 0:
self.y = 0
if self.y >= 380-self.h:
self.y = 380-self.h
def render(self):
glPushMatrix()
glTranslatef(self.x,self.y,0)
glBegin(GL_QUADS)
glColor3f(0.8,0.8,0.8)
glVertex2f(0,0)
glVertex2f(self.w,0)
glVertex2f(self.w,self.h)
glVertex2f(0,self.h)
glEnd()
glPopMatrix()
glPushMatrix()
glTranslatef(0,380,0)
glBegin(GL_QUADS)
glColor3f(0.7,0.7,0.7)
glVertex2f(0,0)
glVertex2f(800,0)
glVertex2f(800,800)
glVertex2f(0,800)
glEnd()
glPopMatrix()
glPushMatrix()
glTranslatef(0,380,0)
glBegin(GL_QUADS)
glColor3f(0.9,0,0)
glVertex2f(0,0)
glVertex2f(self.overheat*8,0)
glVertex2f(self.overheat*8,20)
glVertex2f(0,20)
glEnd()
glPopMatrix()
glPushMatrix()
glTranslatef(0,380,0)
glBegin(GL_QUADS)
glColor3f(0.9,0.9,0)
glVertex2f(640,0)
glVertex2f(650,0)
glVertex2f(650,20)
glVertex2f(640,20)
glEnd()
glPopMatrix()
def move(self,dir):
if dir == 1: self.x -= 3
elif dir == 2:self.y -= 3
elif dir == 3: self.x += 3
elif dir == 4: self.y += 3
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
def shoot(self):
if pygame.time.get_ticks() >= self.oktime and self.overheat <= 80:
self.overheat += 8
shootsfx.play()
bullets.append(Bullet())
self.oldtime = pygame.time.get_ticks()
self.oktime = self.oldtime + self.shotdelay
def reset(self):
self.x = 0
self.y = 200
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
self.overheat = 0
rects = []
bullets = []
explosions = []
plyr = Player()
time_passed = 0
class Bullet:
def __init__(self):
self.x = plyr.x + plyr.w
self.y = plyr.y + (plyr.h/2)
self.w = 2
self.h = 2
self.dx = 10
self.dy = random.choice([-0.5,-0.4,-0.3,-0.2,-0.1,0.1,0.2,0.3,0.4,0.5])
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
self.dead = False
def update(self):
self.x += self.dx
self.y += self.dy
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
if self.x >= 800+self.w:
self.dead = True
if self.dead == True:
for index in bullets:
if index == self:
bullets.remove(index)
def render(self):
glPushMatrix()
glTranslatef(self.x,self.y,0)
glBegin(GL_QUADS)
glColor3f(1,0,1)
glVertex2f(0,0)
glVertex2f(self.w,0)
glVertex2f(self.w,self.h)
glVertex2f(0,self.h)
glEnd()
glPopMatrix()
class Particle:
def __init__(self,x,y):
self.x = x
self.initx = x
self.y = y
self.w = random.choice([1,2,3])
self.h = random.choice([1,2,3])
self.r = random.choice([0.7,0.8,0.9])
self.dy = random.choice([-1,-2,-3,1])
self.dx = random.choice([-1,1])
self.speedy = random.choice([0.1,0.2,0.3])
self.speedx = 0
self.color = random.choice(["red","orange","yellow"])
def render(self):
glPushMatrix()
glTranslatef(self.x,self.y,0)
glBegin(GL_QUADS)
if self.color == "red":
glColor3f(random.choice([0.7,0.8,0.9]),0,0)
if self.color == "yellow":
glColor3f(random.choice([0.7,0.8,0.9]),random.choice([0.7,0.8,0.9]),0)
if self.color == "orange":
glColor3f(random.choice([0.7,0.8,0.9]),random.choice([0.4,0.5,0.6]),0)
glVertex2f(0,0)
glVertex2f(self.w,0)
glVertex2f(self.w,self.h)
glVertex2f(0,self.h)
glEnd()
glPopMatrix()
def update(self):
self.y += self.dy
self.dy += self.speedy
self.x += self.dx
if self.x < self.initx:
self.dx += .01
elif self.x > self.initx:
self.dx -= .01
class Explosion:
def __init__(self,x,y):
self.x = x
self.y = y
self.particles = []
for x in xrange(random.randrange(10,20)):
self.particles.append(Particle(self.x+random.choice([-4,-5,-6,-3,-2,-1,1,2,3,4,5,6]),self.y+random.choice([-4,-5,-6,-3,-2,-1,1,2,3,4,5,6])))
self.deadcount = 0
def update(self):
self.deadcount = 0
for index in self.particles:
index.update()
if index.y >= 380:
self.deadcount += 1
if self.deadcount >= len(self.particles):
for index in explosions:
if index == self:
explosions.remove(index)
def render(self):
for index in self.particles:
index.render()
class Rect:
def __init__(self):
self.x = 800
self.y = random.randrange(0,350)
self.w = random.choice([16,24,32])
self.h = random.choice([8,16,24,32])
self.speed = random.choice([4,5,6,7,8,9,10])
self.r = random.randrange(40,255)
self.g = random.randrange(40,255)
self.b = random.randrange(40,255)
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
self.dead = False
def update(self):
self.x -= self.speed
if self.x <= 0-self.w:
self.dead = True
for index in bullets:
if index.rect.colliderect(self.rect):
explosions.append(Explosion(self.x,self.y))
explosionsfx.play()
self.dead = True
index.dead = True
break
if self.dead == True:
for index in rects:
if index == self:
rects.remove(index)
self.rect = pygame.Rect((self.x,self.y),(self.w,self.h))
def render(self):
glPushMatrix()
glTranslatef(self.x,self.y,0)
glBegin(GL_QUADS)
glColor3f(float(self.r)/255,float(self.g)/255,float(self.b)/255)
glVertex2f(0,0)
glVertex2f(self.w,0)
glVertex2f(self.w,self.h)
glVertex2f(0,self.h)
glEnd()
glPopMatrix()
clock = pygame.time.Clock()
pygame.time.set_timer(USEREVENT+1,16)
def eventHandler():
global time_passed, explosions, score
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == USEREVENT+1:
time_passed += 1
if not(len(rects) >= time_passed/50):
rects.append(Rect())
for index in rects:
index.update()
for index in bullets:
index.update()
for index in explosions:
index.update()
plyr.update()
score += 1
ki = pygame.key.get_pressed()
if ki[K_a]: plyr.move(1)
if ki[K_w]: plyr.move(2)
if ki[K_d]: plyr.move(3)
if ki[K_s]: plyr.move(4)
if ki[K_k]: plyr.shoot()
def draw():
glClearColor(0.1,0.1,0.1,0.1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
plyr.render()
for index in rects:
index.render()
for index in bullets:
index.render()
for index in explosions:
index.render()
pygame.display.flip()
def main():
while True:
eventHandler()
draw()
clock.tick(60)
main()