class Enemies
{
//set the enemies position
PVector enemiesPos = new PVector(0, 0);
//set and initialize the enemies speed
float enemiesSpeed = 0;
//set the enemies size
float enemiesSize;
//temp variable
float tempEnemiesSize;
//temp variable
float tempEnemiesSpeed;
//temp variable
float r1;
float g1;
float b1;
//Initialize all variables for enemies
Enemies(float enemiesPosX, float enemiesPosY, float tempEnemiesSpeed, float tempEnemiesSize)
{
enemiesSize = tempEnemiesSize;
enemiesPos.x = enemiesPosX;
enemiesPos.y = enemiesPosY;
enemiesSpeed = tempEnemiesSpeed;
}
//draw enemies
void drawEnemies()
{
//using user defined function to draw enemies(see bottom)
enemiesShape();
}
//function to define the movement of the enemies
void moveEnemies()
{
//set the horizontal movement
enemiesPos.x = enemiesPos.x + enemiesSpeed;
//set the vertical movement(simulate gravity)
enemiesPos.y = enemiesPos.y + speed;
//if touches the edges, bounce back
if (enemiesPos.x+enemiesSize>width || enemiesPos.x<0)
{
enemiesSpeed = enemiesSpeed*-1;
}
//constrain enemies inside the screen
enemiesPos.y = constrain(enemiesPos.y, 0, height-enemiesSize);
}
//detect the collision between enemies and the walls and the players.
void enemiesDetect()
{
//if entering right side wall, keep enemies outside the walls
if (enemiesPos.x>wall.x && enemiesPos.x<wall.x+widthWall && enemiesPos.y+enemiesSize>wall.y && enemiesPos.y<wall.y+20)
{
enemiesPos.y = wall.y-enemiesSize;
}
//if entering left side wall, keep enemies outside the walls
if (enemiesPos.x>walll.x && enemiesPos.x<widthWalll && enemiesPos.y+enemiesSize>walll.y && enemiesPos.y<walll.y+20)
{
enemiesPos.y = walll.y-enemiesSize;
}
//if player hit the enemies, then enemies' size reduce to 0 and stop shooting procedure
if (pos.x+shootWidth>enemiesPos.x && pos.x+shootWidth<enemiesPos.x+enemiesSize && pos.y+20>enemiesPos.y && pos.y+20+shootHeight<enemiesPos.y+enemiesSize && shoot == true)
{
shoot = false;
enemiesSize = 0;
}
//if enemies get to the right bottom corner, then respawn it at the top and increase its size and random its speed.
if (enemiesPos.y == height-enemiesSize && enemiesPos.x > width-enemiesSize-1)
{
int i = 0;
i = i+1;
r1 = random(0, 255);
g1 = random(0, 255);
b1 = random(0, 255);
enemiesPos.x=random(0);
enemiesPos.y =random(0);
enemiesSize = enemiesSize + i*10;
enemiesSpeed = random(1, 10);
}
}
void enemiesShape()
{
fill(r1, g1, b1);
rectMode(CORNER);
rect(enemiesPos.x, enemiesPos.y, enemiesSize, enemiesSize);
fill(0);
rect( enemiesPos.x+(enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(enemiesSize/5), enemiesSize/5, enemiesSize/5);
rect( enemiesPos.x+(3*enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(enemiesSize/5), enemiesSize/5, enemiesSize/5);
rect( enemiesPos.x+(enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(3*enemiesSize/5), -2*sin(frameCount)+enemiesSize/2, enemiesSize/5);
}
}//I did not put these into the class is because it will be needed in other class and in main code.
PVector pos=new PVector();//Position of the player
float speed;//speed of the player
float gravity=2;
boolean right;//movement triggers
boolean left;//
boolean up;//^
boolean down;//^
boolean dodge;//triggers dodge
boolean faceRight;//to represent when player is faacing right
boolean faceLeft;//to represent when player is faacing left
float shootSpeed;//set bullet speed
boolean shoot;//shoot bullet
float shootWidth;//bullet width
float shootHeight;//bullet height
float fadeSpeed = 0.5;
class Player
{
PVector feetPosL;//left foot position
PVector feetPosR;//right foot position
//initialize all the variables
public Player()
{
feetPosL=new PVector();
feetPosR=new PVector();
pos = new PVector(200, 200);
speed = 3;
right = false;
left = false;
up = false;
down = false;
dodge = false;
faceLeft = false;
faceRight = false;
shoot = false;
shootHeight = 10;
shootWidth = 50;
shootSpeed = 8;
gravity = 2;
}
//functions to draw the player
void playerShape()
{
if (faceRight == false && faceLeft == false)
{
fill(0);
noStroke();
//--------------Head parts------------------
rect(pos.x, pos.y, 20, 10);//the upper hair
rect(pos.x, pos.y+10, 10, 10);//the lower hair
fill(255, 187, 160);//change face color to light red
rect(pos.x+10, pos.y+10, 10, 10); //the face
//---------------Body parts-----------------
fill(155, 8, 0);//change body color to dark red
rect(pos.x+5, pos.y+20, 10, 20);//the body
fill(155, 103, 0);//change the pants color to dark brown
//---------------feet parts-----------------
rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
fill(0, 0, 0);//change the boots' color to black
rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
}
//in player is facing right, then draw the rightside of the body
if (faceRight == true)
{
fill(0);
noStroke();
//--------------Head parts------------------
rect(pos.x, pos.y, 20, 10);//the upper hair
rect(pos.x, pos.y+10, 10, 10);//the lower hair
fill(255, 187, 160);//change face color to light red
rect(pos.x+10, pos.y+10, 10, 10); //the face
//---------------Body parts-----------------
fill(155, 8, 0);//change body color to dark red
rect(pos.x+5, pos.y+20, 10, 20);//the body
fill(155, 103, 0);//change the pants color to dark brown
//---------------feet parts-----------------
rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
fill(0, 0, 0);//change the boots' color to black
rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
}
//in player is facing left, then draw the leftside of the body
if (faceLeft == true)
{
fill(0);
noStroke();
//--------------Head parts------------------
rect(pos.x, pos.y, 20, 10);//the upper hair
rect(pos.x+10, pos.y+10, 10, 10);//the lower hair
fill(255, 187, 160);//change face color to light red
rect(pos.x, pos.y+10, 10, 10); //the face
//---------------Body parts-----------------
fill(155, 8, 0);//change body color to dark red
rect(pos.x+5, pos.y+20, 10, 20);//the body
fill(155, 103, 0);//change the pants color to dark brown
//---------------feet parts-----------------
rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
fill(0, 0, 0);//change the boots' color to black
rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
}
}
//move the player
void playerMove()
{
//set the feet position
feetPosL = new PVector(pos.x, pos.y+40);
feetPosR = new PVector(pos.x, pos.y+40);
//go right and do animation using sine when d is pressed
if (right == true)
{
faceRight = true;
faceLeft = false;
pos.x = pos.x + speed;
feetPosL.x = 3*sin(frameCount/5)+pos.x;
feetPosR.x = 3*-sin(frameCount/5)+pos.x;
}
//go left and do animation using sine when a is pressed
if (left == true)
{
faceLeft = true;
faceRight = false;
pos.x = pos.x - speed;
feetPosL.x = 3*sin(frameCount/5)+pos.x;
feetPosR.x = 3*-sin(frameCount/5)+pos.x;
}
//go up and don't do animation when w is pressed
if (up == true)
{
speed = speed + 0.1;
speed = constrain(speed, 0, 10);
pos.y = pos.y-speed;
fill(255, 0, 0, 100);
ellipse(-sin(frameCount)+pos.x+10, pos.y+60, 10, 10);
fill(255, 0, 0, 50);
ellipse(sin(frameCount)+pos.x+10, pos.y+60, 20, 20);
fill(255, 100, 0);
triangle(feetPosL.x+7.5, feetPosL.y+20, feetPosL.x+12.5, feetPosL.y+20, 3*sin(frameCount)+feetPosL.x+10, feetPosL.y+50);
}
//constrain the player inside the screen
pos.x = constrain(pos.x, 0, width-20);
pos.y = constrain(pos.y, 0, height-80);
//if dodge, then teleport player ahead for 200 pixels/frame and then turn dodge to false so the player can stop.
if (dodge == true && right == true)
{
pos.x = pos.x + 200;
fill(255, 0, 0);
triangle(pos.x, pos.y+60, pos.x, pos.y, pos.x-200, pos.y+30);
dodge = false;
}
if (dodge == true && left == true)
{
pos.x = pos.x - 200;
fill(255, 0, 0);
triangle(pos.x, pos.y+60, pos.x, pos.y, pos.x+200, pos.y+30);
dodge = false;
}
//if shoot and face left, draw a rectangle at the left side of the player and make it height decline by 0.5 pixel per frame and its width increase.
if (shoot == true && faceLeft == true)
{
fill((random (0, 255)), (random (0, 255)), (random (0, 255)));
rect(pos.x+10, pos.y+20, shootWidth, shootHeight);
shootHeight = shootHeight - fadeSpeed;
shootWidth = shootWidth - shootSpeed;
shootHeight = constrain(shootHeight, 0, 10);
shootWidth = constrain(shootWidth, -1000, 1000);
}
//if shoot and face right, draw a rectangle at the right side of the player and make it height decline by 0.5 pixel per frame and its width increase.
if (shoot == true && faceRight == true)
{
fill((random (0, 255)), (random (0, 255)), (random (0, 255)));
rect(pos.x+10, pos.y+20, shootWidth, shootHeight);
shootHeight = shootHeight - fadeSpeed;
shootWidth = shootWidth + shootSpeed;
shootHeight = constrain(shootHeight, 0, 10);
shootWidth = constrain(shootWidth, -1000, 1000);
}
//simulate gravity on player
pos.y = pos.y + gravity;
}
}
//if mouseClicked, then shoot.
void mousePressed()
{
shoot = true;
}
//reset variables
void mouseReleased()
{
shoot = false;
shootWidth = 10;
shootHeight = 10;
}