Created
October 4, 2012 19:55
-
-
Save lizkhoo/3836001 to your computer and use it in GitHub Desktop.
duckiehunt_01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PFont PTSans; | |
color water = color(47, 108, 180); | |
PImage duck1, duck2, duck3; | |
float duck1x, duck2x, duck3x; | |
float duck1y, duck2y, duck3y; | |
float dspeed1, dspeed2, dspeed3; | |
boolean duckDraw1 = true, duckDraw2 = true, duckDraw3 = true; // decides whether to draw the duck | |
boolean duckHit1 = false, duckHit2 = false, duckHit3 = false; // tells me if the duck has been hit | |
float bulletX, bulletY, bspeedX, bspeedY, factor = 8; | |
boolean bulletState = false; //says whether bullet has been fired | |
boolean gameOver = false; //says whether game is started or ended | |
void setup() { | |
size(600, 450); | |
PTSans = loadFont ("PTSans-Bold-64.vlw"); | |
textAlign(CENTER); | |
duck1 = loadImage("rubberduck.png"); | |
duck2 = loadImage("rubberduck.png"); | |
duck3 = loadImage("rubberduck.png"); | |
duck1x = random(20, width-20); | |
duck2x = random(20, width-20); | |
duck3x = random(20, width-20); | |
duck1y = height*.2; | |
duck2y = height*.4; | |
duck3y = height*.6; | |
dspeed1 = 6; | |
dspeed2 = 5; | |
dspeed3 = 4; | |
} | |
void draw() { | |
//welcome message | |
background(water); | |
shotgun(constrain(mouseX, width*.3, width*.75), height*.85, width/2, height+10); | |
//open game play state | |
if (gameOver==false){ | |
if (millis()<3000) { | |
textFont(PTSans, 64); | |
text("Duckie Hunt", width/2, height/2); | |
textFont(PTSans, 18); | |
text("Click mouse to fire, click again to reload", width/2, height/2+40); | |
} | |
//enter ducks | |
if (duckDraw1==true && gameOver==false && millis()>3000) { | |
imageMode(CENTER); | |
image(duck1, duck1x, duck1y, 80, 80); | |
} | |
else { | |
} | |
if (duckDraw2 == true && gameOver==false && millis()>3000) { | |
imageMode(CENTER); | |
image(duck2, duck2x, duck2y, 80, 80); | |
} | |
else { | |
} | |
if (duckDraw3 == true && gameOver==false && millis()>3000) { | |
imageMode(CENTER); | |
image(duck3, duck3x, duck3y, 80, 80); | |
} | |
else { | |
} | |
duck1x = duck1x+dspeed1; | |
duck2x = duck2x+dspeed2; | |
duck3x = duck3x+dspeed3; | |
if (duck1x > width-20 || duck1x < 20) { | |
dspeed1 = -dspeed1; | |
} | |
if (duck2x > width-20 || duck2x < 20) { | |
dspeed2 = -dspeed2; | |
} | |
if (duck3x > width-20 || duck3x < 20) { | |
dspeed3 = -dspeed3; | |
} | |
//control bullet fired or not fired | |
if (bulletState == false) { | |
bulletX = constrain(mouseX, width*.3, width*.75); | |
bulletY = height*.85; | |
bspeedX = (mouseX-width/2)/factor; | |
bspeedY = (mouseX-width/2)/factor; | |
} | |
else { | |
bulletX+=bspeedX; | |
bulletY+=bspeedY; | |
} | |
bullet(bulletX, bulletY); | |
//control whether ducks appear or disappear | |
if (duck1x-40<bulletX && bulletX<duck1x+40 && duck1y-40<bulletY && bulletY<duck1y+40 && duckHit1==false) { | |
println("duck1x = " + duck1x); | |
duckHit1=true; | |
} | |
if (duckHit1 == true && duckDraw1 == true) { | |
duckDraw1 = !duckDraw1; | |
} | |
if (duck2x-40<bulletX && bulletX<duck2x+40 && duck2y-40<bulletY && bulletY<duck2y+40 && duckHit2 == false) { | |
println("duck2x = " + duck2x); | |
duckHit2=true; | |
} | |
if (duckHit2 == true && duckDraw2 == true) { | |
duckDraw2 = !duckDraw2; | |
} | |
if (duck3x-40<bulletX && bulletX<duck3x+40 && duck3y-40<bulletY && bulletY<duck3y+40 && duckHit3 == false) { | |
println("duck3x = " + duck3x); | |
duckHit3=true; | |
} | |
if (duckHit3 == true && duckDraw3 == true) { | |
duckDraw3 = !duckDraw3; | |
} | |
if (duckDraw1 == false && duckDraw2 == false && duckDraw3 == false ) { | |
gameOver=true; | |
} | |
//close game play if statement | |
} | |
//display end game message -- needs fixing | |
if (duckDraw1 == false && duckDraw2 == false && duckDraw3 == false ) { | |
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"); | |
} | |
//end draw | |
} | |
//restart game also needs fixing | |
void keyPressed(){ | |
if(gameOver==true){ | |
println("Key Pressed"); | |
gameOver=false; | |
duckHit1 = false; | |
duckHit2 = false; | |
duckHit3 = false; | |
duckDraw1 = true; | |
duckDraw2 = true; | |
duckDraw3 = true; | |
bulletState = false; | |
} | |
} | |
void mousePressed() { | |
println("bullet fired"); | |
bulletState = !bulletState; | |
} | |
void shotgun(float shaftLeftX, float shaftTopY, float shaftRightX, float shaftBottomY) { | |
fill(0); | |
strokeWeight(20); | |
line(shaftLeftX, shaftTopY, shaftRightX, shaftBottomY); | |
line(shaftLeftX+20, shaftTopY, shaftRightX+20, shaftBottomY); | |
} | |
void bullet(float bulletX, float bulletY) { | |
fill(0); | |
strokeWeight(1); | |
ellipse(bulletX, bulletY, 10, 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment