public
Created

Cleaned up version of Duckie Hunt game

  • Download Gist
duckiehunt_02
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
 
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.