Help with Video Game Sketch
in
Programming Questions
•
2 years ago
So, I'm making a simulation of a video game that will work with mobile phones in the future. I'm having issues getting the sketch to restart when 'r' or 'R' is pressed and the sound effect on detonation seems to be delayed. Anyone have any ideas?
Here is an example of what it's doing/how it wont work.
Here is the code I'm working with:
- import ddf.minim.*;
- float xPos1, yPos1,xPos2, yPos2, ring1, ring2, missileX, missileY, defenderX, defenderY;
- boolean defenderDetonate,missileDetonate, explosion;
- int missileCount, defenderCount;
- PFont font_zigg;
- Minim minim;
- AudioSample boom;
- void setup()
- {
- //sound effects
- minim= new Minim(this);
- boom=minim.loadSample("explosion.wav", 2048);
- if ( boom == null ) println("Didn't get sound file!");
- reset();
- }
- void stop()
- {
- // always close Minim audio classes when you are done with them
- boom.close();
- minim.stop();
- super.stop();
- }
- void draw()
- {
- defenderX=mouseX;
- defenderY=mouseY;
- smooth();
- if (defenderCount>0)
- {
- fill(0,0,255);
- ellipse(defenderX, defenderY, 5,5);
- } //draw defender
- if (missileCount>0)
- {
- fill(255,140,0);
- ellipse(missileX, missileY, 5,5);
- }
- if (defenderDetonate==true && defenderCount>0)
- {
- drawDefenderExplosion();
- }
- if(missileDetonate==true && missileCount>0)
- {
- drawMissileExplosion();
- }
- if(missileCount==0)
- {
- stop();
- background(0);
- fill(255,255,255);
- text("Game Over: Defenders Win ", 350, 430);
- text("Press r to restart ", 350, 480);
- }
- if(defenderCount==0)
- {
- stop();
- background(0);
- fill(255,255,255);
- text("Game Over: Missiles Win ", 350, 430);
- text("Press r to restart ", 370, 480);
- }
- if(explosion==true) //reset if one explodes
- {
- background(0);
- explosion=false;
- missileDetonate=false;
- defenderDetonate=false;
- ring1=5;
- ring2=5;
- background(0);
- stroke(0,0,0);
- fill(255,0,0);
- text("Missiles: "+ missileCount, 300, 730);
- fill(0,0,255);
- text("Bases: " + defenderCount, 500, 730);
- }
- }
- void mousePressed()
- {
- boom.trigger();
- defenderDetonate=true;
- if (missileCount > 0)
- missileCount--;
- }
- void drawDefenderExplosion()
- {
- xPos2=defenderX;
- yPos2=defenderY;
- stroke(51,102,255);
- fill(0,0,0);
- ellipse(xPos2,yPos2, 5,5);
- ellipse(xPos2, yPos2, ring1, ring1);
- if (ring1>=20)
- {
- ellipse(xPos2, yPos2, ring2, ring2);
- if (ring2<20)
- {
- ring2++;
- }
- }
- if (ring1 < 40)
- {
- ring1++;
- }
- if (ring2==19)
- {
- explosion=true;
- }
- }
- void drawMissileExplosion()
- {
- xPos2=missileX;
- yPos2=missileY;
- stroke(255,102,51);
- fill(0,0,0);
- ellipse(xPos2,yPos2, 5,5);
- ellipse(xPos2, yPos2, ring1, ring1);
- if (ring1>=20)
- {
- ellipse(xPos2, yPos2, ring2, ring2);
- if (ring2<20)
- {
- ring2++;
- }
- }
- if (ring1 < 40)
- {
- ring1++;
- }
- if (ring2==19)
- {
- explosion=true;
- }
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- missileY-=3;
- }
- else if (keyCode == DOWN) {
- missileY+=3;
- }
- else if (keyCode == LEFT) {
- missileX-=3;
- }
- else if (keyCode == RIGHT) {
- missileX+=3;
- }
- }
- if (key=='d' || key=='D')
- {
- if (missileCount <7)
- {
- boom.trigger();
- missileDetonate=true;
- if (defenderCount > 0)
- defenderCount--;
- }
- }
- if (key=='r' || key=='R')
- {
- reset();
- }
- }
- void reset()
- {
- background(0);
- fill(0,0,255);
- size(1024,768);
- frameRate(30);
- defenderDetonate=false;
- missileDetonate=false;
- explosion=false;
- ring1=5;
- ring2=5;
- missileX=random(1024);
- missileY=random(668);
- missileCount=7;
- defenderCount=6;
- font_zigg = loadFont("Ziggurat-HTF-Black-32.vlw");
- textFont(font_zigg, 20);
- fill(255,255,255);
- text("Begin Missile Command", 350, 430);
- }
1