Skip to content

Commit

Permalink
second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
boxysean committed Nov 2, 2011
1 parent 9c8b3f4 commit 47ed482
Show file tree
Hide file tree
Showing 5 changed files with 465 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Bullet.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
int BULLET_SIZE = 15;
int BULLET_SPEED = 1;

class Bullet {
float x;
float y;
float speed;
int playerId;

Bullet(float x, float y, float speed, int playerId) {
this.x = x;
this.y = y;
this.speed = speed;
this.playerId = playerId;
}

void draw() {
noStroke();
fill(game.players[playerId].colour);
rect(x-(BULLET_SIZE/2), y-(BULLET_SIZE/2), BULLET_SIZE, BULLET_SIZE);
}

void move() {
x += speed;
}
}

144 changes: 144 additions & 0 deletions KinectTracker.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
class KinectTracker {



// Size of kinect image
int kw = 640;
int kh = 480;
int threshold = 745;

// Raw location
PVector loc1;
PVector loc2;

// Interpolated location
PVector lerpedLoc;

// Depth data
int[] depth;


PImage display;

KinectTracker() {
kinect.start();
kinect.enableDepth(true);

// We could skip processing the grayscale image for efficiency
// but this example is just demonstrating everything
kinect.processDepthImage(true);

display = createImage(kw,kh,PConstants.RGB);

loc1 = new PVector(0,0);
loc2 = new PVector(kw/2,0);
lerpedLoc = new PVector(0,0);
}

void track() {

// Get the raw depth as array of integers
depth = kinect.getRawDepth();

// Being overly cautious here
//if (depth == null) return;

float sumX = 0;
float sumY = 0;
float count = 0;

for(int x = 0; x < kw/2; x++) {
for(int y = 0; y < kh; y++) {
// Mirroring the image
int offset = kw-x-1+y*kw;
// Grabbing the raw depth
int rawDepth = depth[offset];

// Testing against threshold
if (rawDepth < threshold) {
sumX += x;
sumY += y;
count++;
}
}
}
// As long as we found something
if (count != 0) {
loc1 = new PVector(sumX/count,sumY/count);
}
sumX=sumY=count=0;
for(int x = kw/2; x < kw; x++) {
for(int y = 0; y < kh; y++) {
// Mirroring the image
int offset = kw-x-1+y*kw;
// Grabbing the raw depth
int rawDepth = depth[offset];

// Testing against threshold
if (rawDepth < threshold) {
sumX += x;
sumY += y;
count++;
}
}
}
// As long as we found something
if (count != 0) {
loc2 = new PVector(sumX/count,sumY/count);
}
}


PVector getPos1() {
return loc1;
}

PVector getPos2() {
return loc2;
}

void display() {
PImage img = kinect.getDepthImage();

// Being overly cautious here
if (depth == null || img == null) return;

// Going to rewrite the depth image to show which pixels are in threshold
// A lot of this is redundant, but this is just for demonstration purposes
display.loadPixels();
for(int x = 0; x < kw; x++) {
for(int y = 0; y < kh; y++) {
// mirroring image
int offset = kw-x-1+y*kw;
// Raw depth
int rawDepth = depth[offset];

int pix = x+y*display.width;
if (rawDepth < threshold) {
// A red color instead
display.pixels[pix] = color(150,50,50);
}
else {
display.pixels[pix] = img.pixels[offset];
}
}
}
display.updatePixels();

// Draw the image
image(display,0,0);
}

void quit() {
kinect.quit();
}

int getThreshold() {
return threshold;
}

void setThreshold(int t) {
threshold = t;
}
}

93 changes: 93 additions & 0 deletions PewPewDestroy.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
int HP_BAR_WIDTH = 50;
int HP_MAX = 10;
int BAR_BUFFER = 10;
int PADDLE_WIDTH = 20;
int PADDLE_HEIGHT = 60;
int PADDLE_SPEED = 5;
int SHOOT_INTERVAL = 1000; // ms

class Game {
Player players[] = new Player[2];

int nextShootTime;

void setup() {
players[0] = new Player(0, "A", #FF0000, true);
players[1] = new Player(1, "B", #0000FF, false);

players[0].y = height/2;
players[1].y = height/2;

nextShootTime = millis() + SHOOT_INTERVAL;
}

void draw() {
// background(#000000);

// if it's time, shoot a new object from player 1/2
if (millis() >= nextShootTime) {
nextShootTime += SHOOT_INTERVAL;
for (int i = 0; i < 2; i++) {
players[i].shootBullet();
}
}

// draw player 1 block
// draw player 2 block
// draw player 1 HP
// draw player 2 HP
for (int i = 0; i < 2; i++) {
players[i].drawPaddle();
players[i].drawHP();

ArrayList destroy = new ArrayList();

for (int j = 0; j < players[i].bullets.size(); j++) {
Bullet bullet = (Bullet) players[i].bullets.get(j);

// increment bullets
bullet.move();

if ((bullet.speed > 0 && bullet.x >= width - HP_BAR_WIDTH) || (bullet.speed < 0 && bullet.x <= HP_BAR_WIDTH)) {
// it hits the HP bar
players[1-i].hit();
destroy.add(bullet);
} else {
// draw bullets
bullet.draw();
}
}

for (int j = 0; j < destroy.size(); j++) {
Bullet bullet = (Bullet) destroy.get(j);
players[i].bullets.remove(bullet);
}
}

// time to generate powerup
}

void keyPressed() {
switch (key) {
case 'q':
case 'Q':
players[0].up();
break;

case 'a':
case 'A':
players[0].down();
break;

case 'o':
case 'O':
players[1].up();
break;

case 'l':
case 'L':
players[1].down();
break;
}
}
}
121 changes: 121 additions & 0 deletions Player.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
class Player {
int id;
String name;
int colour;
int hp = HP_MAX;
LinkedList bullets = new LinkedList();
boolean sideLeft;
float y;

Player(int id, String name, int colour, boolean sideLeft) {
this.id = id;
this.name = name;
this.colour = colour;
this.sideLeft = sideLeft;
}

void drawPaddle() {
noStroke();
fill(colour);

if (sideLeft) {
rect(HP_BAR_WIDTH + BAR_BUFFER, y - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT);
} else {
rect(width - HP_BAR_WIDTH - PADDLE_WIDTH - BAR_BUFFER, y - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT);
}
}

void drawHP() {
noStroke();
fill(#00FF00);

float y = map(hp, 0, HP_MAX, height, 0);

if (sideLeft) {
rect(0, y, HP_BAR_WIDTH, height);
} else {
rect(width - HP_BAR_WIDTH, y, width, height);
}
}

void shootBullet() {
int x = 0;
int speed = BULLET_SPEED;

if (sideLeft) {
x = HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH;
} else {
x = width - (HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH);
speed *= -1;
}

Bullet bullet = new Bullet(x, y, speed, id);

bullets.add(bullet);
}

void up() {
y = max(y-PADDLE_SPEED, 0);
}

void down() {
y = min(y+PADDLE_SPEED, height);
}

void moveTo(float y) {
// update y
this.y = y;

// collision detect bullets, destroy if collided
Iterator ii = game.players[1-id].bullets.descendingIterator();

ArrayList destroy = new ArrayList();

while (ii.hasNext()) {
Bullet bullet = (Bullet) ii.next();

float bx[] = new float[2];
float by[] = new float[2];
float px[] = new float[2];
float py[] = new float[2];

bx[0] = bullet.x - (BULLET_SIZE / 2.0f);
by[0] = bullet.y - (BULLET_SIZE / 2.0f);
bx[1] = bullet.x + (BULLET_SIZE / 2.0f);
by[1] = bullet.y + (BULLET_SIZE / 2.0f);

if (sideLeft) {
px[0] = HP_BAR_WIDTH + BAR_BUFFER;
px[1] = HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH;
} else {
px[0] = width - (HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH);
px[1] = width - (HP_BAR_WIDTH + BAR_BUFFER);
}

py[0] = y - (PADDLE_HEIGHT / 2.0);
py[1] = y + (PADDLE_HEIGHT / 2.0);

// bx[0], by[0]
// bx[0], by[1]
// bx[1], by[0]
// bx[1], by[1]

// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 2; j++) {
// if ((px[0] <= bx[i] && bx[i] <= px[1]) && (py[0] <= by[j] && by[j] <= py[1])) {
// // collides, destroy bullet
// destroy.add(bullet);
// }
// }
// }
}

// for (int i = 0; i < destroy.size(); i++) {
// bullets.remove(destroy.get(i));
// }
}

void hit() {
hp--;
}
}
Loading

0 comments on commit 47ed482

Please sign in to comment.