Skip to content

@lizkhoo /duckiehunt_02
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Cleaned up version of Duckie Hunt game
Duck[] myDuck = new Duck[3];
Shotgun theGun;
Bullet theBullet;
PImage rubberduck;
color water = color(47, 108, 180);
PFont PTSans;
boolean gameOver = false;
void setup() {
size(600, 400);
PTSans = loadFont ("PTSans-Bold-64.vlw");
textAlign(CENTER);
float startY = 50;
float spacingY = 100;
for (int i=0; i< myDuck.length; i++) {
myDuck[i] = new Duck(random(0, width), startY + (spacingY * i));
rubberduck = loadImage("rubberduck.png");
}
//initiate bullet object once
theBullet = new Bullet();
}
void draw() {
background(water);
//draw shotgun
theGun = new Shotgun(constrain(mouseX, width*.3, width*.75));
theGun.drawGun();
//open game play state
if (gameOver==false) {
if (millis() < 3000) { //welcome message
textFont(PTSans, 64);
text("Duckie Hunt", width/2, height/2);
textFont(PTSans, 18);
text("Click mouse to fire", width/2, height/2+40);
}
//if shoot==true, draw & increment bullet position
if (theBullet.shoot == true) {
theBullet.drawBullet();
theBullet.shootBullet();
}
else {
}
//move ducks back and forth after 3 seconds
if (millis() > 3000) {
println("millis = " +millis());
/*for (int t=0; t<10; t++) { // adding more ducks at 2 sec intervals? need to still figure this out
if (millis() == 5000+2000*t) {
println("millis = " +millis());
myDuck = (Duck[]) append(myDuck, new Duck(random(0, width), random(0, theGun.shaftTopY)) );
}*/
}
for (int i=0; i<myDuck.length; i++) {
myDuck[i].moveDucks();
myDuck[i].reverseDucks(rubberduck);
myDuck[i].drawDucks(rubberduck);
if (myDuck[i].duckx-30 < theBullet.bulletX
&& myDuck[i].duckx+30 > theBullet.bulletX
&& myDuck[i].ducky-30 < theBullet.bulletY
&& myDuck[i].ducky+30 > theBullet.bulletY) {
myDuck[i].duckKilled = true;
}
if (myDuck[0].duckKilled==true
&& myDuck[1].duckKilled==true
&& myDuck[2].duckKilled==true) {
gameOver=true;
}
} //close for loop
} //close millis if
} //close gameover false if
//display end game message
if (gameOver==true) {
textFont(PTSans, 28);
text("Good Job, Hot Shot!", width/2, height/2-30);
text("Hit any key to restart", width/2, height/2+30);
println("Game over");
}
}//close draw loop
void mousePressed() {
theBullet.shoot=true;
theBullet.calculateTrajectory(mouseX, height*.85);
}
void keyPressed() {
for (int i=0; i<myDuck.length; i++) {
myDuck[i].duckKilled = false;
}
gameOver = false;
theBullet.shoot=false;
theBullet.calculateTrajectory(mouseX, height*.85);
}
/////////////////
class Bullet {
float bulletX, bulletY, speedX, speedY, factor, bulletX2, bulletY2;
boolean shoot;
void calculateTrajectory(float myX, float myY) {
bulletX = myX;
bulletY = myY;
factor = 10;
speedX = (myX-width/2)/factor;
speedY = (height-myY)/factor;
boolean shoot = false;
drawBullet();
}
void shootBullet() {
if (shoot==true){
bulletX += speedX;
bulletY -=speedY;
}
}
void drawBullet() {
fill(0);
strokeWeight(1);
ellipse(bulletX, bulletY, 10, 10);
println(shoot);
}
}
/////////////
/////////////////////////
class Duck {
float duckx, ducky, speedx, duckSize;
PImage rubberduck;
boolean duckKilled;
Duck(float x, float y) {
duckx=x;
ducky=y;
speedx=random(3, 7);
duckSize=40;
duckKilled = false;
}
void moveDucks() {
duckx+=speedx;
}
void drawDucks(PImage rubberduck) {
if (duckKilled==false) {
imageMode(CENTER);
//flip duck image when at edge of screen
pushMatrix();
translate(duckx, ducky);//move origin to scale at (duckx, ducky)
if (speedx < 0) { //if speed is negative, then flip ducks
scale(-1, 1);
}
image(rubberduck, 0, 0, 80, 80);
popMatrix();
}
}
void reverseDucks(PImage rubberduck) {
if (duckx > width || duckx < 0) {
speedx = -speedx;
}
}
}
///////////////
class Shotgun {
float shaftTopX, shaftTopY, shaftBottomX, shaftBottomY;
Shotgun(float x){
shaftTopX = x;
shaftTopY = height*.85;
shaftBottomX = width/2;
shaftBottomY = height+10;
}
float shotgunPos() {
return shaftTopX;
}
void drawGun() {
fill(0);
strokeWeight(20);
line(shaftTopX, shaftTopY, shaftBottomX, shaftBottomY);
line(shaftTopX+20, shaftTopY, shaftBottomX+20, shaftBottomY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.