Hi again, hope everyone is doing well…
im stuck on trying to get my player to shoot bullets at the alien.
main:
my background is an everlasting background which will always carry on!
PImage background;
int x=0; //global variable background location
int y=0;
Player player1;
Alien Alien1;
bullet bullet1;
void setup(){
//size(1000,800);
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
player1 = new Player();
Alien1 = new Alien();
bullet1 = new bullet();
}
void draw ()
{
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
player1.display();
Alien1.display();
Alien1.move();
}
void keyPressed() {
if (key == CODED)
{
if (keyCode == UP && player1.y>=0) { //restrict to screen edge
player1.y -=15;
} else if (keyCode == DOWN && player1.y<=360) {
player1.y +=15;
} else if (keyCode == LEFT && player1.x>=0) {
player1.x -=15;
} else if (keyCode == RIGHT && player1.x<=800) {
player1.x +=15;
}
}
if (key==' ')
{
bullet1.x=player1.x+75;
bullet1.y=player1.y;
bullet1=new bullet();
// bullet2=new bullet(bulletX, bulletY);
}
}
Player class:
class Player {
int x,y;
float SpeedX,SpeedY;
Player() {
this.x = width/2 - 350;
this.y = height/2;
}
void display() {
// fill(0,0,200);
// rect(this.x,this.y,20,10); //draw top box
fill(255,0,0); //draw rocket
rect(this.x,this.y+10,50,20);
triangle(this.x+50,this.y+10,this.x+50,this.y+30,this.x+60,this.y+20);
}
/*
boolean crash()
{
color detectedColour;
for (int i=y; i<=y+3-0; i++)
{
detectedColour=get(x+10,i);
if (detectedColour==color(255))
{
return true;
}
}
return false;
}
*/
}
bullet class:
class bullet
{
int x;
int y;
bullet()
{
this.x=width/2 - 350;
this.y=height/2;
}
void render()
{
fill(0,255,255);
rect(x-5,y,10,20);
fill(255);
rect(x-5,y-10,10,10);
//fill(255);
//ellipse(x,y,20,20);
//fill(0);
//ellipse(x,y,5,5);
}
void move()
{
y=y-10;
}
void update()
{
render();
move();
}
boolean crash()
{
color detectedColour;
for (int i=y; i<=y+3-0; i++)
{
detectedColour=get(x+30,i);
if (detectedColour==color(255))
{
return true;
}
}
return false;
}
}//end of class