screen blanks out
in
Programming Questions
•
2 years ago
After a while, the screen blanks out. Why?
- /*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 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;
- int enemiesLeft;
- int score;
- Enemy[]Enemies=new Enemy[165];
- void setup(){
- 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), 30, 2);
- }
- for (int i=30;i<45;i++){
- Enemies[i]=new Enemy(random(800,1000)+4800,random(60,height-60), 30, 2);
- }
- for (int i=45;i<63;i++){
- Enemies[i]=new Enemy(random(800,1000)+6000,random(60,height-60), 10, 2);
- }
- for (int i=63;i<84;i++){
- Enemies[i]=new Enemy(random(800,1000)+7200,random(60,height-60),20, 3);
- }
- for (int i=84;i<108;i++){
- Enemies[i]=new Enemy(random(800,1000)+8400,random(60,height-60), 30, 3);
- }
- for (int i=108;i<135;i++){
- Enemies[i]=new Enemy(random(800,1000)+9600,random(60,height-60), 30, 3);
- }
- for (int i=135;i<165;i++){
- Enemies[i]=new Enemy(random(800,1000)+10800,random(60,height-60), 30, 4);
- }
- }
- void draw(){
- println(level);
- 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<165;i++){
- Enemies[i].display();
- Enemies[i].move();
- Enemies[i].death();
- player();
- }
- }
- }
- }
- 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(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+2*death/4;
- }
- }
- if(key=='n'){
- health-=100;
- }
- }
- }
- void death(){
- 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(){
- if(key!=' '){
- laserCharge+=0.5;
- }
- laserCharge=constrain (laserCharge,0,200);
- if(keyPressed){
- if(keyCode==UP){
- playerYpos-=2;
- }
- if(keyCode==DOWN){
- playerYpos+=2;
- }
- if(key==' '){
- if(laserCharge>1){
- fill(255,0,0);
- stroke(255,0,0);
- for(int i=playerXpos; i<=width; i+=8){
- ellipse(i,playerYpos,7,7);
- }
- laserCharge-=0.3;
- }
- }
- 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
- score=death*100;
- 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