Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DMS423_HW04_Xiaoyu
# DMS423_HW04_Xiaoyu.py
#
# Homework 04 for DMS 423 Fall 14
# by Xiaoyu Tai(Sean)
# UBID: 50133396
#
# Sprite-style video game
# Free Pac-Man
#
# --------
# Pac-Man Ghost & Pac-Man icons Copyright:
# License: Free for commercial use (Include link to authors website)
# By: Vicent Pla, https://www.iconfinder.com/megavyto98
#
# Some codes are modified from texturedSquare.py
# --------
#
# Control: A,D,W,S / right,left,up,down / H,J,K,L
# To do: AVOID Ghost(s), eat as much dots as you can!
#
# Once you touch a ghost, the game is over.
# You may press "SPACE" key to restart.
#
# A new ghost will pop up in random location every 5 seconds.
# The eldest ghost has a chance to disappear.
from pyglet.gl import *
from pyglet.window import key
import math, time, random
window = pyglet.window.Window(512, 512)
keys = key.KeyStateHandler()
window.push_handlers(keys)
background = pyglet.graphics.vertex_list(4, ('v2f', [0,0, 512,0, 0,512, 512,512]), ('t2f', [0,0, 1,0, 0,1, 1,1]))
background_tex_1 = pyglet.image.load("bg.png").get_texture()
background_tex_2 = pyglet.image.load("bgo.png").get_texture()
class Avatar:
def __init__(self, width, height, xpos, ypos, texturefile):
self.xpos = xpos
self.ypos = ypos
self.angle = 0
self.size = 1
self.texture = pyglet.image.load(texturefile).get_texture()
x = width/2.0
y = height/2.0
self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-x,-y, x,-y, -x,y, x,y]), ('t2f', [0,0, 1,0, 0,1, 1,1]))
def draw(self):
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture.id)
glColor4f(1,1,1,1)
glPushMatrix()
glTranslatef(self.xpos, self.ypos, 0)
glRotatef(self.angle, 0, 0, 1)
glScalef(self.size, self.size, self.size)
self.vlist.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
glDisable(GL_BLEND)
def reset():
global last, count, notover, dots, ghost, strat, pac, info
last = 0
count = 0
notover = 1
dots = []
ghost = []
strat = time.time()
pac = Avatar(32, 32, 482, 30, "pac.png")
info = pyglet.text.Label("You have 0/100 dot.", x=10, y=490)
ghost.append(Avatar(32, 32, 30, 482, "gho.png"))
for x in xrange(1,101):
dots.append(Avatar(8, 8, 32*random.randint(1,15), 32*random.randint(1,15), "dots.png"))
@window.event
def on_draw():
global background_tex_1, background_tex_2, info
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
if notover:
draw_background(background_tex_1)
else:
draw_background(background_tex_2)
if notover:
for x in dots:
x.draw()
for x in ghost:
x.draw()
pac.draw()
info.draw()
def draw_background(texture):
global background
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glPushMatrix()
glColor3f(1, 1, 1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture.id)
background.draw(GL_TRIANGLE_STRIP)
glDisable(GL_TEXTURE_2D)
glPopMatrix()
def update(dt):
global ghost, keys, pac, notover
if notover:
contral(pac)
loop(pac)
for x in ghost:
loop(x)
chase(x, pac)
newghost()
check()
elif keys[key.SPACE]:
reset()
def contral(avatar):
global keys
if keys[key.A] or keys[key.LEFT] or keys[key.H]:
avatar.xpos -= 5
avatar.angle = 180
elif keys[key.D] or keys[key.RIGHT] or keys[key.L]:
avatar.xpos += 5
avatar.angle = 0
elif keys[key.W] or keys[key.UP] or keys[key.K]:
avatar.ypos += 5
avatar.angle = 90
elif keys[key.S] or keys[key.DOWN] or keys[key.J]:
avatar.ypos -= 5
avatar.angle = -90
def loop(avatar):
if avatar.xpos > 512:
avatar.xpos = 0
if avatar.xpos < 0:
avatar.xpos = 512
if avatar.ypos > 512:
avatar.ypos = 0
if avatar.ypos < 0:
avatar.ypos = 512
def chase(a, b):
dx = b.xpos - a.xpos
dy = b.ypos - a.ypos
r = float(2 / ((dx * dx) + (dy * dy)))
r = 2 * math.sqrt(r)
a.xpos += dx * r
a.ypos += dy * r
def newghost():
global strat, last
if int(time.time() - strat) - last == 5:
ghost.append(Avatar(32, 32, random.randrange(512), random.randrange(512), "gho.png"))
last = int(time.time() - strat)
if random.randrange(10) > 3:
ghost.pop(0)
def check():
global ghost, pac, notover, info, count, dots
for x in ghost:
dx = abs(x.xpos - pac.xpos)
dy = abs(x.ypos - pac.ypos)
if dx < 16 and dy < 16:
notover = 0
for x in dots:
dx = abs(x.xpos - pac.xpos)
dy = abs(x.ypos - pac.ypos)
if dx < 16 and dy < 16:
dots.remove(x)
count += 1
if count > 1:
info.text = "You have %d/100 dots." % (count)
else:
info.text = "You have %d/100 dot." % (count)
reset()
pyglet.clock.schedule_interval(update, 1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment