variable increment
in
Programming Questions
•
2 years ago
xpos2 will not decrease no matter what I try.
- /*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, if it is a boss, you lose five life.
- You have 20 lives.
- Lasers must recharge so you can't use them for too long. To recharge, press another key.
- Hold space bar to shoot forward.
- */
- PImage bg;
- int bossHealth=888;
- int bg2=0;
- int death;
- int life=20;
- float playerYpos=200;
- int playerXpos=30;
- boolean menu=true;
- boolean instruct=false;
- int laserCounter=400;
- PFont font;
- float laserCharge=200;
- int enemiesLeft;
- int cannonCharge=4;
- int score;
- Enemy[]Enemies=new Enemy[165];
- void setup(){
- bg=loadImage("java final background.jpg");
- size(800,400);
- smooth();
- font=loadFont("Corbel-Bold-48.vlw");
- for (int i=0;i<3;i++){
- Enemies[i]=new Enemy(random(800,1000),random(60,height-60), 10, 1);
- }
- for (int i=3;i<9;i++){
- Enemies[i]=new Enemy(random(800,1000)+1200,random(60,height-60),20, 1);
- }
- for (int i=9;i<18;i++){
- Enemies[i]=new Enemy(random(800,1000)+2400,random(60,height-60), 30, 1);
- }
- for (int i=18;i<30;i++){
- Enemies[i]=new Enemy(random(800,1000)+3600,random(60,height-60), 42, 1);
- }
- for (int i=30;i<45;i++){
- Enemies[i]=new Enemy(random(800,1000)+4800,random(60,height-60), 60, 1);
- }
- for (int i=45;i<63;i++){
- Enemies[i]=new Enemy(random(800,1000)+6000,random(60,height-60), 31, 1);
- }
- for (int i=63;i<84;i++){
- Enemies[i]=new Enemy(random(800,1000)+7200,random(60,height-60),100, 1);
- }
- for (int i=84;i<108;i++){
- Enemies[i]=new Enemy(random(800,1000)+8400,random(60,height-60), 120, 1);
- }
- for (int i=108;i<135;i++){
- Enemies[i]=new Enemy(random(800,1000)+9600,random(60,height-60), 140, 1);
- }
- for (int i=135;i<165;i++){
- Enemies[i]=new Enemy(random(800,1000)+10800,random(60,height-60), 160, 1);
- }
- }
- Boss boss;
- void draw(){
- // println(death);
- background(255);
- fill(255,0,0);
- smooth();
- 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){
- background(255);
- 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(bg);
- meters();
- for (int i=0;i<165;i++){
- Enemies[i].display();
- Enemies[i].move();
- Enemies[i].death();
- boss=new Boss(11350);
- boss.display();
- player();
- }
- }
- if(life<=0){
- background(bg2);
- bg2+=5;
- textSize(80);
- fill(255,0,0);
- text("Game Over",80,150);
- text("Play Again",100,280);
- }
- if(bossHealth<=0&&life>0){
- text("You Win",80,150);
- text("Play Again",100,280);
- }
- }
- }
- class Enemy{
- float xpos;
- float ypos;
- float 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(255,0,94);
- 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(xpos-16<width&&laserCharge>0&&playerYpos>=ypos-16&&playerYpos<=ypos+16){
- health=constrain (health,-1,160);
- if(death<18){
- health-=1;
- }
- if(death>17&&death<63){
- health-=3;
- }
- if(death>62&&death<115){
- health-=4;
- }
- if(death>114){
- health-=5;
- }
- }
- }
- }
- }
- void death(){
- if(health==0){
- death+=1;
- }
- }
- void move(){
- if(health>0){
- xpos=xpos-speed;
- if(xpos+2<0){
- life-=1;
- score-=50;
- xpos=width;
- xpos+=speed;
- }
- }
- }
- }
- void player(){
- if(key!=' '){
- laserCharge+=0.05;
- }
- laserCharge=constrain (laserCharge,0,200);
- if(keyPressed){
- if(keyCode==UP){
- playerYpos-=0.02;
- }
- if(keyCode==DOWN){
- playerYpos+=0.02;
- }
- if(key==' '){
- if(laserCharge>1){
- fill(255,0,0);
- stroke(255,0,0);
- line(playerXpos,playerYpos,width,playerYpos);
- laserCharge-=0.03;
- }
- }
- }
- 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
- score=death*100;
- enemiesLeft=166-death;
- fill(0);
- text("LaserCharge:",170,350);
- rect(350,325,laserCharge,40);
- textSize(30);
- fill(255,0,0);
- text("Score:" + score,0,350);
- fill(0,255,255);
- text("Enemies Left:" +enemiesLeft ,550,350);
- }
- class Boss{
- int xpos2;
- Boss(int tempXpos2){
- xpos2=tempXpos2;
- }
- void display(){
- float ypos2=random(height);
- println(xpos2);
- xpos2=xpos2-1;
- fill(255);
- if(bossHealth>0){
- beginShape();
- vertex(xpos2-24,ypos2);
- vertex(xpos2+12,ypos2-24);
- vertex(xpos2+6,ypos2);
- vertex(xpos2+12,ypos2+24);
- endShape(CLOSE);
- fill(0);
- triangle(xpos2+4,ypos2-7,xpos2+4,ypos2+7,xpos2-17,ypos2);
- fill(0,255,255);
- ellipse(xpos2+4,ypos2+7,5,7);
- ellipse(xpos2+4,ypos2-7,5,7);
- ellipse(xpos2-17,ypos2,5,7);
- if(keyPressed){
- if(key==' '){
- if(xpos2-16<width&&laserCharge>0&&playerYpos>=ypos2-16&&playerYpos<=ypos2+16){
- bossHealth=constrain (bossHealth,-1,160);
- bossHealth-=5;
- }
- }
- }
- }
- }
- }
1