why won't player move???
in
Programming Questions
•
2 years ago
the player will not move. Also, how do I have the 'laser' kill enemies? Also, how do I monitor if all the enemies are dead to move the level up? Why do the enemies slow down when space is pressed too
int life=20;
int level=1;
float playerYpos=200;
int playerXpos=30;
boolean menu=true;
boolean instruct=false;
PFont font;
Enemy[]Enemies=new Enemy[level*3];
void setup(){
size(800,400);
smooth();
for (int i=0;i<Enemies.length;i++){
Enemies[i]=new Enemy(random(800,1000),random(height), level*10, 3);
}
}
void draw(){
background(255);
fill(255,0,0);
font=loadFont("Corbel-Bold-48.vlw");
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. Click a target and laser will shoot it, hold space bar to shoot forward, or press space for blast cannon. Blast cannons are extremely powerful but you only have four.";
text(s,50,20,750,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);
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(){
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);
}
void move(){
xpos=xpos-speed;
if(xpos+2<0){
life-=1;
}
}
}
void player(){
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);
if(keyPressed){
if(key==UP){
playerYpos-=2;
}
if(key==DOWN){
playerYpos+=2;
}
if(key==' '){
fill(255,0,0);
for(int i=playerXpos; i<=width; i+=3){
ellipse(i,playerYpos,3,3);
}
}
}
}
1