few things not working
in
Programming Questions
•
2 years ago
Thanks to all who have already helped me.I'm still pretty new at this so thanks.
Now there is an issue with the scoring, the levels(still can't get them to work), the laser timer(constrain doesn't work), and the number of enemies left per level.
Thanks again!
- /*William Wang
- creates a shooting game.
- Instructions:
- Press up and down arrows to move the ship up and down.
- The number of enemies is displayed in the bottom right corner.
- You laser will get stronger every level and so will the enemies.
- At level 10, there will be a boss.
- Every time an enemy goes off screen, you will lose a life.
- You have 20 lives.
- Lasers must recharge so you can't use them for too long.
- There are three ways to shoot.
- Hold space bar to shoot forward or press n for blast cannon.
- Blast cannons are extremely powerful but you only have four.
- */
- int score;
- int death;
- int life=20;
- int level=1;
- float playerYpos=200;
- int playerXpos=30;
- boolean menu=true;
- boolean instruct=false;
- int laserCounter=400;
- PFont font;
- float laserCharge=200;
- Enemy[]Enemies=new Enemy[level*3];
- void setup(){
- size(800,400);
- smooth();
- font=loadFont("Corbel-Bold-48.vlw");
- for (int i=0;i<Enemies.length;i++){
- Enemies[i]=new Enemy(random(800,1000),random(60,height-60), level*10, 3);
- }
- }
- void draw(){
- delay(100);
- println(laserCharge);
- background(255);
- fill(255,0,0);
- if(menu&&!instruct){
- textFont(font,70);
- text("Play Game",250,100);
- text("Instruct",290,300);
- if(mousePressed&&mouseX>250&&mouseX<580&&mouseY>30&&mouseY<100){
- menu=false;
- }
- if(mousePressed&&mouseX>290&&mouseX<540&&mouseY>230&&mouseY<300){
- instruct=true;
- }
- }
- if(menu&&instruct){
- textSize(27);
- String s="Press up and down arrows to move the ship up and down. The number of enemies is displayed in the bottom right corner. You laser will get stronger every level and so will the enemies. At level 10, there will be a boss. Every time an enemy goes off screen, you will lose a life. You have 20 lives. Lasers must recharge so you can't use them for too long. There are three ways to shoot. Hold space bar to shoot forward or press n for blast cannon. Blast cannons are extremely powerful but you only have four.";
- text(s,50,20,700,375);
- String back="Back";
- text(back,375,375);
- if(mousePressed&&mouseX>375&&mouseX<430&&mouseY<375&&mouseY>350){
- instruct=false;
- }
- }
- if(!menu&&!instruct){
- if(life>=0){
- background(255);
- meters();
- for (int i=0;i<Enemies.length;i++){
- Enemies[i].display();
- Enemies[i].move();
- player();
- }
- }
- }
- }
- class Enemy{
- float xpos;
- float ypos;
- int health;
- float speed;
- Enemy(float tempXpos, float tempYpos, int tempHealth, float tempSpeed){
- xpos= tempXpos;
- ypos= tempYpos;
- health= tempHealth;
- speed= tempSpeed;
- }
- void display(){
- if(health>0){
- stroke(196,20,20);
- fill(139,30,30);
- beginShape();
- vertex(xpos-16,ypos);
- vertex(xpos,ypos-8);
- vertex(xpos+8,ypos-16);
- vertex(xpos+4,ypos);
- vertex(xpos+8,ypos+16);
- vertex(xpos,ypos+8);
- endShape(CLOSE);
- }
- if(keyPressed){
- if(key==' '){
- if(laserCharge>0&&playerYpos>=ypos-16&&playerYpos<=ypos+16){
- health-=1;
- }
- }
- if(key=='n'){
- health-=100;
- }
- }
- }
- void score(){
- if(health==0){
- score+=100;
- death+=1;
- }
- if(death==level*3){
- level+=1;
- death=0;
- }
- }
- void move(){
- xpos=xpos-speed;
- if(xpos+2<0){
- life-=1;
- xpos=width;
- xpos+=speed;
- }
- }
- }
- void player(){
- constrain (laserCharge,0,200);
- laserCharge+=1;
- if(keyPressed){
- if(keyCode==UP){
- playerYpos-=2;
- }
- if(keyCode==DOWN){
- playerYpos+=2;
- }
- if(key==' '){
- if(laserCharge>0){
- fill(255,0,0);
- stroke(255,0,0);
- for(int i=playerXpos; i<=width; i+=8){
- ellipse(i,playerYpos,7,7);
- laserCharge-=0.02;
- }
- }
- }
- if(key=='n'){
- fill(random(255),random(255),random(255));
- for(int i=playerXpos; i<=width; i+=3){
- ellipse(i,playerYpos,50,800);
- }
- }
- }
- beginShape();
- fill(0,30,214);
- stroke(0,236,235);
- vertex(playerXpos+16,playerYpos);
- vertex(playerXpos,playerYpos+8);
- vertex(playerXpos-8,playerYpos+16);
- vertex(playerXpos-4,playerYpos);
- vertex(playerXpos-8,playerYpos-16);
- vertex(playerXpos,playerYpos-8);
- endShape(CLOSE);
- }
- void meters(){
- //laserCharge
- int enemiesLeft=level*3-death;
- fill(0,255,255);
- rect(100,350,laserCharge,40);
- textSize(30);
- text("Score:" + score,0,350);
- text("Enemies Left:" +enemiesLeft ,400,350);
- }
1