Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ninja
# DMS423_HW02_Xiaoyu.py
#
# Homework 01 for DMS 423 Fall 14
# by Xiaoyu Tai(Sean)
# UBID: 50133396
#
# Fruit Ninja Simulator
#
# Credits:
# - Fruit Ninja, game of Halfbrick Studios, http://halfbrick.com/
from pyglet.gl import *
from pyglet.window import key
import time, random
window = pyglet.window.Window(1024, 640)
keys = key.KeyStateHandler()
window.push_handlers(keys)
startT = time.time()
startP = 0
triangle1 = pyglet.graphics.vertex_list(3, ('v2f', [-50,-32, 0,-32, 0,55]))
triangle2 = pyglet.graphics.vertex_list(3, ('v2f', [0,-32, 50,-32, 0,55]))
t, wait = 0, -1
data = []
mouse = [0,0]
for i in xrange(0, 100):
n = random.randint(0, 40)
x = random.randint(200, 824)
vx = random.randint(-200, 200)
vy = random.randint(850, 1000)
rotation = random.randint(-120, 120)
color = [random.uniform(0.1,1), random.uniform(0.1,1), random.uniform(0.1,1)]
data.append([n, x, -100, vx, vy, rotation, color, 1])
@window.event
def on_draw():
global n
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glLoadIdentity()
for i in xrange(0, 50):
draw_object(i, data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5], data[i][6])
def draw_object(i, delay, x, y, vx, vy, rotation, color):
global startT, triangle, mouse, data
dt = t - (startT + delay)
dx = vx * dt
dy = vy * dt - 400 * dt * dt
degree = rotation * dt
if(abs(mouse[0] - (x + dx)) < 50)and(abs(mouse[1] - (y + dy)) < 50):
data[i][7] = 0
if(y + dy > -200):
glPushMatrix()
glTranslatef(x + dx, y + dy, 0)
glRotatef(degree, 0, 0, 1)
glColor3f(color[0], color[1], color[2])
if data[i][7]:
triangle1.draw(GL_TRIANGLES)
triangle2.draw(GL_TRIANGLES)
else:
dxx = vx * 0.2 * dt
glPushMatrix()
glTranslatef(dxx, 0, 0)
glRotatef(0.5*degree, 0, 0, 1)
triangle1.draw(GL_TRIANGLES)
glPopMatrix()
glPushMatrix()
glTranslatef(-dxx, 0, 0)
glRotatef(-0.5*degree, 0, 0, 1)
triangle2.draw(GL_TRIANGLES)
glPopMatrix()
glPopMatrix()
def update(dt):
global keys, t, startT, startP, wait
if keys[key.SPACE]:
if wait == -1:
wait = time.time()
else:
startP = (time.time() - wait)
else:
if wait != -1:
startT += startP
wait = -1
t = time.time()
@window.event
def on_mouse_motion(x, y, dx, dy):
global mouse
mouse[0] = x
mouse[1] = y
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