Skip to content

Instantly share code, notes, and snippets.

@MulverineX
Created September 28, 2017 18:41
Show Gist options
  • Save MulverineX/fb35157910f05a5407842eb94b8cf2aa to your computer and use it in GitHub Desktop.
Save MulverineX/fb35157910f05a5407842eb94b8cf2aa to your computer and use it in GitHub Desktop.
Asteroids In Processing
import ddf.minim.*;
import processing.sound.*;
AudioPlayer player;
SoundFile lazer;
Minim minim;//audio context
PShape asteroid1;
PShape asteroid2;
BufferedReader hscore;
int sCX=360;
int sCY=450;
int fireRandom;
int lazerBeam=0;
int timer=0;
int sT1;
int sT2;
int sT3;
int sT4;
int s1X=int(random(0,720));
int s1Y=0;
int s2X=int(random(0,720));
int s2Y=0;
int s3X=int(random(0,720));
int s3Y=0;
int s4X=int(random(0,720));
int s4Y=0;
int starTimer=0;
float a1RM=random(0.9,2);
float aTimer;
int asteroidX=int(random(20,700));
float a2RM=random(0.9,2);
float aTimer1=-100;
int tAT;
int asteroid2X=int(random(20,700));
int lives=5;
int sCS1X=551;
int sCS1Y=700;
int sCS2X=571;
int sCS2Y=700;
int sCS3X=591;
int sCS3Y=700;
int sCS4X=611;
int sCS4Y=700;
int sCS5X=631;
int sCS5Y=700;
int score=12;
int aTScore;
int active=0;
int tA1X=int(random(-10,10));
int tA1Y=int(random(-10,10));
int tA2X=int(random(-20,20));
int tA2Y=int(random(-20,20));
int tTGA=0;
int tTGAd=0;
void setup() {
size(720,900);
background(000);
minim = new Minim(this);
player = minim.loadFile("background.mp3", 2048);
player.play();
lazer = new SoundFile(this, "lazerA.mp3");
active=0;
hscore = createReader("score.txt");
}
void draw() {
background(000);
timer++;
if (starTimer<101) {
starTimer++;
} else {
starTimer=0;
s1X=int(random(0,720));
s1Y=0;
s2X=int(random(0,720));
s2Y=0;
s3X=int(random(0,720));
s3Y=0;
s4X=int(random(0,720));
s4Y=0;
sT1=0;
sT2=0;
sT3=0;
sT4=0;
}
if (starTimer>25) {
sT1++;
}
if (starTimer>50) {
sT2++;
}
if (starTimer>75) {
sT3++;
}
if (starTimer<25) {
sT4++;
}
s1Y=sT1*12;
s2Y=sT2*12;
s3Y=sT3*12;
s4Y=sT4*12;
fill(255);
if (starTimer>25) {
//println("Star 1");
ellipse(s1X,s1Y,10,10);
}
if (starTimer>50) {
//println("Star 2");
ellipse(s2X,s2Y,10,10);
}
if (starTimer>75) {
//println("Star 3");
ellipse(s3X,s3Y,10,10);
}
if (keyCode==ALT && keyPressed) {
active=0;
timer=0;
}
if (active==0) {
player.setGain(0);
if (tAT<101) {
tAT++;
} else {
tAT=0;
}
if (tAT > 1 && tAT < 100) {
shape(asteroid1,100+tA1X,120+tA1Y);
} else {
a1RM=random(0.9,2);
asteroid1 = createShape();
asteroid1.beginShape();
asteroid1.noStroke();
asteroid1.fill(#483B31);
asteroid1.vertex(0,0);
asteroid1.vertex(int(random(0,10))+10*a1RM,int(random(0,10))+5*a1RM);
asteroid1.vertex(int(random(0,10))+21*a1RM,int(random(0,10))+20*a1RM);
asteroid1.vertex(int(random(0,10))+20*a1RM,int(random(0,10))+40*a1RM);
asteroid1.vertex(int(random(0,10))+10*a1RM,int(random(0,10))+50*a1RM);
asteroid1.vertex(0,int(random(0,10))+55*a1RM);
asteroid1.vertex(int(random(0,10))-10*a1RM,50*a1RM);
asteroid1.vertex(int(random(0,10))-20*a1RM,40*a1RM);
asteroid1.vertex(int(random(0,10))-21*a1RM,20*a1RM);
asteroid1.vertex(int(random(0,10))-10*a1RM,5*a1RM);
asteroid1.endShape(CLOSE);
tA2X=int(random(-20,20));
tA2Y=int(random(-20,20));
}
if (tAT > 1 && tAT < 100) {
shape(asteroid2,615+tA2X,122+tA2Y);
} else {
a2RM=random(0.9,2);
asteroid2 = createShape();
asteroid2.beginShape();
asteroid2.noStroke();
asteroid2.fill(#483B31);
asteroid2.vertex(0,0);
asteroid2.vertex(int(random(0,10))+10*a2RM,int(random(0,10))+5*a2RM);
asteroid2.vertex(int(random(0,10))+21*a2RM,int(random(0,10))+20*a2RM);
asteroid2.vertex(int(random(0,10))+20*a2RM,int(random(0,10))+40*a2RM);
asteroid2.vertex(int(random(0,10))+10*a2RM,int(random(0,10))+50*a2RM);
asteroid2.vertex(0,int(random(0,10))+55*a2RM);
asteroid2.vertex(int(random(0,10))-10*a2RM,50*a2RM);
asteroid2.vertex(int(random(0,10))-20*a2RM,40*a2RM);
asteroid2.vertex(int(random(0,10))-21*a2RM,20*a2RM);
asteroid2.vertex(int(random(0,10))-10*a2RM,5*a2RM);
asteroid2.endShape(CLOSE);
tA2X=int(random(-20,20));
tA2Y=int(random(-20,20));
}
if (tTGAd==0) {
if (tTGA < 40) {
tTGA++;
} else {
tTGAd=1;
}
}
if (tTGAd==1) {
if (tTGA > 0) {
tTGA--;
} else {
tTGAd=0;
}
}
fill(22, 116+tTGA, 110);
textSize(115);
text("Asteroids", 100,200);
textSize(55);
if (mouseX > 210 && mouseX < 510 && mouseY > 231 && mouseY < 300) {
fill(31, 186+tTGA, 175);
textSize(58);
}
text("Play Game", 225,280);
fill(22, 116+tTGA, 110);
textSize(55);
if (mouseX > 210 && mouseX < 510 && mouseY > 306 && mouseY < 355) {
fill(31, 186+tTGA, 175);
textSize(58);
}
text("Help/Controls", 175,355);
fill(22, 116+tTGA, 110);
textSize(55);
if (mouseX > 210 && mouseX < 510 && mouseY > 375 && mouseY < 435) {
fill(31, 186+tTGA, 175);
textSize(58);
}
text("Scores", 265,425);
}
if (active==3) {
player.setGain(0);
if (tTGAd==0) {
if (tTGA < 40) {
tTGA++;
} else {
tTGAd=1;
}
}
if (tTGAd==1) {
if (tTGA > 0) {
tTGA--;
} else {
tTGAd=0;
}
}
fill(22, 116+tTGA, 110);
textSize(30);
text("Ship Controls:", 100,100);
textSize(25);
text(" W: Up", 100,130);
text(" A: Left", 100,160);
text(" D: Right", 100,190);
text(" S: Down", 100,220);
text(" Spacebar: Lazer/Shoot", 100,250);
textSize(30);
text("P: Pause", 100,290);
text("ALT: Reset", 100,330);
fill(22, 116+tTGA, 110);
textSize(55);
if (mouseX > 260 && mouseX < 590 && mouseY > 375 && mouseY < 435) {
fill(31, 186+tTGA, 175);
textSize(58);
}
text("Home Screen", 265,425);
}
if (active==4) {
player.setGain(0);
if (tTGAd==0) {
if (tTGA < 40) {
tTGA++;
} else {
tTGAd=1;
}
}
if (tTGAd==1) {
if (tTGA > 0) {
tTGA--;
} else {
tTGAd=0;
}
}
fill(22, 116+tTGA, 110);
textSize(45);
text("All Time HighScore", 225,280);
textSize(40);
text(aTScore+"points", 225,310);
}
if (active==1) {
player.setGain(-20);
fireRandom=int(random(-10,20));
timer++;
fill(255);
if (aTimer<900) {
aTimer++;
} else {
aTimer=0;
}
if (aTimer1<900) {
aTimer1++;
} else {
aTimer1=0;
}
if (key=='w' && keyPressed && sCY > 100) {
sCY=sCY-3;
}
if (key=='s' && keyPressed && sCY < 810) {
sCY=sCY+3;
}
if (key=='a' && keyPressed && sCX > 40) {
sCX=sCX-3;
}
if (key=='d' && keyPressed && sCX < 680) {
sCX=sCX+3;
}
if (key=='q' && keyPressed) {
sCX=sCX-2;
sCY=sCY-2;
}
if (key=='e' && keyPressed) {
sCX=sCX+2;
sCY=sCY-2;
}
if (key==' ' && keyPressed) {
lazerBeam=-900;
lazer.play();
if (sCX < asteroidX+30 && sCX > asteroidX-30) {
aTimer=5;
asteroidX=int(random(20,700));
score=score+5;
}
if (sCX < asteroid2X+30 && sCX > asteroid2X-30) {
aTimer1=5;
asteroid2X=int(random(20,700));
score=score+5;
}
} else {
lazerBeam=0;
}
noStroke();
fill(#05EAFC);
rect(sCX-5,sCY,10,10+lazerBeam);
fill(#343939);
rect(sCX-30,sCY-60,60,120);
triangle(sCX-30,sCY-60,sCX,sCY-100,sCX+30,sCY-60);
triangle(sCX-30,sCY+60,sCX-38,sCY+85,sCX-15,sCY+60);
triangle(sCX+30,sCY+60,sCX+38,sCY+85,sCX+15,sCY+60);
fill(#FF7C00);
triangle(sCX-15,sCY+60,sCX,sCY+100+fireRandom,sCX+15,sCY+60);
fill(#FCFC05);
triangle(sCX-8,sCY+60,sCX,sCY+70+fireRandom,sCX+8,sCY+60);
fill(#96FCFA);
ellipse(sCX,sCY,25,25);
if (aTimer>1 && aTimer<900) {
shape(asteroid1,asteroidX,aTimer);
} else {
a1RM=random(0.9,2);
asteroid1 = createShape();
asteroid1.beginShape();
asteroid1.noStroke();
asteroid1.fill(#483B31);
asteroid1.vertex(0,0);
asteroid1.vertex(int(random(0,10))+10*a1RM,int(random(0,10))+5*a1RM);
asteroid1.vertex(int(random(0,10))+21*a1RM,int(random(0,10))+20*a1RM);
asteroid1.vertex(int(random(0,10))+20*a1RM,int(random(0,10))+40*a1RM);
asteroid1.vertex(int(random(0,10))+10*a1RM,int(random(0,10))+50*a1RM);
asteroid1.vertex(0,int(random(0,10))+55*a1RM);
asteroid1.vertex(int(random(0,10))-10*a1RM,50*a1RM);
asteroid1.vertex(int(random(0,10))-20*a1RM,40*a1RM);
asteroid1.vertex(int(random(0,10))-21*a1RM,20*a1RM);
asteroid1.vertex(int(random(0,10))-10*a1RM,5*a1RM);
asteroid1.endShape(CLOSE);
asteroidX=int(random(20,700));
score=score-2;
}
if (sCX < asteroidX+50 && sCX > asteroidX-50 && sCY < aTimer+80 && sCY > aTimer-80) {
lives=lives-1;
aTimer=900;
}
if (aTimer1>1 && aTimer1<900) {
shape(asteroid2,asteroid2X,aTimer1);
} else {
a2RM=random(0.9,2);
asteroid2 = createShape();
asteroid2.beginShape();
asteroid2.noStroke();
asteroid2.fill(#483B31);
asteroid2.vertex(0,0);
asteroid2.vertex(int(random(0,10))+10*a2RM,int(random(0,10))+5*a2RM);
asteroid2.vertex(int(random(0,10))+21*a2RM,int(random(0,10))+20*a2RM);
asteroid2.vertex(int(random(0,10))+20*a2RM,int(random(0,10))+40*a2RM);
asteroid2.vertex(int(random(0,10))+10*a2RM,int(random(0,10))+50*a2RM);
asteroid2.vertex(0,int(random(0,10))+55*a2RM);
asteroid2.vertex(int(random(0,10))-10*a2RM,50*a2RM);
asteroid2.vertex(int(random(0,10))-20*a2RM,40*a2RM);
asteroid2.vertex(int(random(0,10))-21*a2RM,20*a2RM);
asteroid2.vertex(int(random(0,10))-10*a2RM,5*a2RM);
asteroid2.endShape(CLOSE);
asteroid2X=int(random(20,700));
if (aTimer1 > 0) {
score=score-2;
}
}
if (sCX < asteroid2X+50 && sCX > asteroid2X-50 && sCY < aTimer1+80 && sCY > aTimer1-80) {
lives=lives-1;
aTimer1=900;
}
if (lives >= 5) {
sCS1Y=700;
sCS2Y=700;
sCS3Y=700;
sCS4Y=700;
sCS5Y=700;
}
if (lives < 5) {
sCS5Y=1000;
}
if (lives < 4) {
sCS4Y=1000;
}
if (lives < 3) {
sCS3Y=1000;
}
if (lives < 2) {
sCS2Y=1000;
}
if (lives < 1) {
sCS1Y=1000;
active=2;
}
fill(#343939);
rect(sCS1X-5,sCS1Y-8,10,16);
triangle(sCS1X-5,sCS1Y-8,sCS1X,sCS1Y-12,sCS1X+5,sCS1Y-8);
triangle(sCS1X-5,sCS1Y+8,sCS1X-6,sCS1Y+10,sCS1X-3,sCS1Y+8);
triangle(sCS1X+5,sCS1Y+8,sCS1X+6,sCS1Y+10,sCS1X+3,sCS1Y+8);
fill(#FF7C00);
triangle(sCS1X-3,sCS1Y+8,sCS1X,sCS1Y+12+fireRandom,sCS1X+3,sCS1Y+8);
fill(#96FCFA);
ellipse(sCS1X,sCS1Y,4,4);
fill(#343939);
rect(sCS2X-5,sCS2Y-8,10,16);
triangle(sCS2X-5,sCS2Y-8,sCS2X,sCS2Y-12,sCS2X+5,sCS2Y-8);
triangle(sCS2X-5,sCS2Y+8,sCS2X-6,sCS2Y+10,sCS2X-3,sCS2Y+8);
triangle(sCS2X+5,sCS2Y+8,sCS2X+6,sCS2Y+10,sCS2X+3,sCS2Y+8);
fill(#FF7C00);
triangle(sCS2X-3,sCS2Y+8,sCS2X,sCS2Y+12+fireRandom,sCS2X+3,sCS2Y+8);
fill(#96FCFA);
ellipse(sCS2X,sCS2Y,4,4);
fill(#343939);
rect(sCS3X-5,sCS3Y-8,10,16);
triangle(sCS3X-5,sCS3Y-8,sCS3X,sCS3Y-12,sCS3X+5,sCS3Y-8);
triangle(sCS3X-5,sCS3Y+8,sCS3X-6,sCS3Y+10,sCS3X-3,sCS3Y+8);
triangle(sCS3X+5,sCS3Y+8,sCS3X+6,sCS3Y+10,sCS3X+3,sCS3Y+8);
fill(#FF7C00);
triangle(sCS3X-3,sCS3Y+8,sCS3X,sCS3Y+12+fireRandom,sCS3X+3,sCS3Y+8);
fill(#96FCFA);
ellipse(sCS3X,sCS3Y,4,4);
fill(#343939);
rect(sCS4X-5,sCS4Y-8,10,16);
triangle(sCS4X-5,sCS4Y-8,sCS4X,sCS4Y-12,sCS4X+5,sCS4Y-8);
triangle(sCS4X-5,sCS4Y+8,sCS4X-6,sCS4Y+10,sCS4X-3,sCS4Y+8);
triangle(sCS4X+5,sCS4Y+8,sCS4X+6,sCS4Y+10,sCS4X+3,sCS4Y+8);
fill(#FF7C00);
triangle(sCS4X-3,sCS4Y+8,sCS4X,sCS4Y+12+fireRandom,sCS4X+3,sCS4Y+8);
fill(#96FCFA);
ellipse(sCS4X,sCS4Y,4,4);
fill(#343939);
rect(sCS5X-5,sCS5Y-8,10,16);
triangle(sCS5X-5,sCS5Y-8,sCS5X,sCS5Y-12,sCS5X+5,sCS5Y-8);
triangle(sCS5X-5,sCS5Y+8,sCS5X-6,sCS5Y+10,sCS5X-3,sCS5Y+8);
triangle(sCS5X+5,sCS5Y+8,sCS5X+6,sCS5Y+10,sCS5X+3,sCS5Y+8);
fill(#FF7C00);
triangle(sCS5X-3,sCS5Y+8,sCS5X,sCS5Y+12+fireRandom,sCS5X+3,sCS5Y+8);
fill(#96FCFA);
ellipse(sCS5X,sCS5Y,4,4);
fill(#16746E);
textSize(35);
text("Lives", 550, 680);
text("Score: "+score, 500,820);
text("Time: "+timer, 500,870);
}
}
void mouseClicked() {
println(mouseX + ", " + mouseY);
if (active==0) {
if (mouseX > 210 && mouseX < 510 && mouseY > 231 && mouseY < 300) {
active=1;
timer=0;
aTimer=0;
aTimer1=0;
lives=5;
score=14;
}
if (mouseX > 210 && mouseX < 510 && mouseY > 306 && mouseY < 355) {
active=3;
}
if (mouseX > 210 && mouseX < 510 && mouseY > 375 && mouseY < 435) {
active=4;
}
}
if (active==3) {
if (mouseX > 260 && mouseX < 590 && mouseY > 375 && mouseY < 435) {
active=0;
}
}
}
void stop() {
player.close();
minim.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment