I am creating a game (using Processing 3.4) where I will need to spawn enemies in at random. I have created each different "type" of enemy in its own method in another class. In my main sketch I am trying to create a method that will randomly spawn one of the enemies periodically.
I am brand new to Processing, only started less then a week ago at Uni, but this was a little side project I wanted to complete to get a better understanding of Processing and Java.
Here is my current code;
From Main Sketch 1
void draw() {
background(225);
cursor(CROSS);
score(); //calls score
///MAKE A FUNCTION SO THAT ENEMIES SPAWN RANDOMLY
spawnEnemy();
player.playerChar(); //calls playerChar from Player
player.playerMovement(); //calls playerMovement from Player
}
From Main Sketch 2
void spawnEnemy() { //Create a method that when called spawns a random enemy.
j = j+1;
println(j);
if (j == 180) {
int enemyNumber = int(random(1, 6));
println(enemyNumber);
if (enemyNumber == 1) {
enemy.redBar(enemy.enemyX, enemy.enemyY, enemy.xDist, enemy.yDist);
}
if (enemyNumber == 2) {
enemy.blueBar(enemy.enemyX, enemy.enemyY, enemy.xDist, enemy.yDist);
}
if (enemyNumber == 3) {
enemy.redBigCircle(enemy.enemyX, enemy.enemyY, enemy.circleRad);
//circleCollision();
}
if (enemyNumber == 4) {
enemy.redBigCircle(enemy.enemyX, enemy.enemyY, enemy.circleRad);
//circleCollision();
}
if (enemyNumber == 5) {
enemy.redSmallCircle(enemy.enemyX, enemy.enemyY, enemy.circleRad);
//circleCollision();
}
if (enemyNumber == 5) {
enemy.blueSmallCircle(enemy.enemyX, enemy.enemyY, enemy.circleRad);
//circleCollision();
}
enemy.enemyMovement();
j=-1;
}
}
From Class Enemy
Player playerCheck;
class Enemy {
boolean redEnemy = false; // used to determine enemy colour
color enemyColour = color(#B9B9E8); // sets default colour to blue
float enemyX = width/2; // starting x coordinate //change to 0
float enemyY = -400; // starting y coordinate //change to 0
float xDist = width; // x distance for Bar
float yDist = 75; // y distance for Bar
float circleRad = 250; // radius for circles
void enemyMovement() {
enemyY = enemyY+5;
}
void redBar(float xPos, float yPos, float xDist, float yDist) {
redEnemy = true;
noStroke();
enemyColour = color(#E38585);
fill(enemyColour);
rect(xPos, yPos, xDist, yDist);
}
void blueBar(float xPos, float yPos, float xDist, float yDist) {
redEnemy = false;
noStroke();
enemyColour = color(#B9B9E8);
fill(enemyColour);
rect(xPos, yPos, xDist, yDist);
}
void redBigCircle(float xPos, float yPos, float rad) {
redEnemy = true;
noStroke();
enemyColour = color(#E38585);
fill(enemyColour);
ellipse(xPos, yPos, rad*2, rad*2);
}
void blueBigCircle(float xPos, float yPos, float rad) {
redEnemy = false;
noStroke();
enemyColour = color(#B9B9E8);
fill(enemyColour);
ellipse(xPos, yPos, rad*2, rad*2);
}
void redSmallCircle(float xPos, float yPos, float rad) {
redEnemy = true;
enemyColour = color(#E38585);
fill(enemyColour);
ellipse(width/2, height/2, rad, rad); //change xPos to 0
}
void blueSmallCircle(float xPos, float yPos, float rad) {
redEnemy = false;
enemyColour = color(#B9B9E8);
fill(enemyColour);
ellipse(xPos, yPos, rad, rad); //change xPos to 0
}
}
I have another Class for my player movement but basically its just an ellipse that is positioned to wherever the cursor is.
Aucun commentaire:
Enregistrer un commentaire