Last active
May 7, 2016 00:36
-
-
Save sabotai/57678e4b3c15c1f1d9eef09d8c689af1 to your computer and use it in GitHub Desktop.
Enemy Class Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Enemy someEnemy; //declare a new object of the class "Enemy" | |
Enemy[] manyEnemies; | |
void setup(){ | |
size(1280,600); | |
// someEnemy = new Enemy(); //initialize our new enemy object | |
manyEnemies = new Enemy[20]; | |
for (int i = 0; i < manyEnemies.length; i++){ | |
manyEnemies[i] = new Enemy(); | |
} | |
} | |
void draw(){ | |
background(100,30,159); //clear the background | |
//someEnemy.display(); //make someEnemy display for our player | |
for (int i = 0; i < manyEnemies.length; i++){ //use a for loop to do these things for each of the manyEnemies | |
manyEnemies[i].display(); //make this currently looped enemy display | |
manyEnemies[i].move(); //make this currently looped enemy move | |
} | |
} | |
void mousePressed(){ | |
//if the player clicked the mouse, check if they clicked close enough to someEnemy | |
for (int i = 0; i < manyEnemies.length; i++){ | |
if (dist(manyEnemies[i].xPos, manyEnemies[i].yPos, mouseX, mouseY) < manyEnemies[i].size/2){ //the player not only presses the mouse, but is also close enough | |
//someEnemy.takeDmg(); //if they clicked someEnemy, make someEnemy take damage >:O | |
manyEnemies[i].takeDmg(); //if they click one of the manyEnemies, make that one take damage | |
} | |
} | |
} | |
//this was on a separate tab | |
class Enemy { //make a new class called "Enemy" (note the capital E) that can be used to create new Enemy objects | |
//declare all the variables you need that an "Enemy" might need | |
int healthAmt; | |
int xPos; | |
int yPos; | |
int size; | |
PImage enemyImage; | |
int speed; | |
String name; | |
Enemy(){ //once the object is initialized, run the following lines to initialize this particular one | |
healthAmt = int(random(100,300)); //give them a random health between 100 and 300 | |
xPos = int(random(width)); //and a random position along the x axis (while still on screen) | |
yPos = int(random(height)); //and a random position along the y axis (while still on screen) | |
size = healthAmt; //make the size equivalent to the healthAmt assigned above | |
int i = int(random(100)); //i is just a temporary variable to get a random assortment of Enemy types | |
if (i < 10){ //if that random value is less than 10, make the Enemy Beyonce | |
enemyImage = loadImage("beyonce.png"); //load the beyonce image | |
name = "Beyonce"; //assign this Enemy object with the name Beyonce | |
} else if (i < 20){ //if i is NOT less than 10, but is less than 20... | |
enemyImage = loadImage("jayz.png"); | |
name = "Jay-Z"; | |
} else if (i < 90){ //if i is NOT less than 10 and NOT less than 20, but is less than 90 | |
enemyImage = loadImage("goomba.png"); | |
name = "Goooooommmmbbbbaaa"; | |
} else { //if none of the above are true, Wario will be the default | |
enemyImage = loadImage("wario.png"); | |
name = "Wario"; | |
} | |
speed = int(random(-5,5)); | |
} | |
void display(){ //if the programmer uses (EnemyObjectName).display(), display that enemy | |
ellipse(xPos,yPos,size/2,size/2); | |
imageMode(CENTER); | |
image(enemyImage, xPos, yPos, size, size); | |
} | |
void move(){ //if the programmer uses (EnemyObjectName).move(), move that enemy | |
xPos += speed; | |
} | |
void takeDmg(){ //if the programmer uses (EnemyObjectName).takeDmg(), decrease that Enemy's health | |
healthAmt -= 5; | |
size = healthAmt; //make sure the size corresponds to the new health | |
println("You hit " + name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment