//
//
/* CLASSES FOR ALL THE CHARACTERS */
//
//
class Hero
{
//position
float mmX = 60;
float mmY = 145;
float hMom = 0;
float vMom = 5;
String dirState = "right";
String chillState = "right";
String jumpState = "none";
String shootState = "shoot";
float blinkMaker = random(10);
int blinkRate = 1;
// Declares all the images for the right and left sequences
int maxImagesR = 9; // Total # of images
int imageIndexR = 0; // Initial image to be displayed first
int maxImagesL = 9;
int imageIndexL = 0;
//
PImage[] imagesR = new PImage[maxImagesR];
PImage[] imagesL = new PImage[maxImagesL];
// Loads all the images for the right and left sequences
/*
0 - blink
1 - stanind
2-4 - Running
5 - jumping
6 - shooting ground
7 - shooting jump
8 - hurt
*/
void loadImages()
{
// Load all images running right
for (int i = 0; i < imagesR.length; i++) {
imagesR[i] = loadImage("mmR" + i + ".png");
}
// Load all images running left
for (int j = 0; j < imagesR.length; j++) {
imagesL[j] = loadImage("mmL" + j + ".png");
}
}
void runRight()
{
dirState = "right";
if (imageIndexR <= 4){
imageIndexR += 1;
mmX+=4;
}
if (Megaman.imageIndexR > 4) {
imageIndexR = 2;
}
}
void runLeft()
{
dirState = "left";
if (imageIndexL <= 4){
imageIndexL += 1;
mmX-=4;
}
if (imageIndexL > 4) {
imageIndexL = 2;
}
}
void chill()
{
dirState = "chill";
if (chillState == "right"){
imageIndexR = 0;
}
if (chillState == "left"){
imageIndexL = 0;
}
mmX -=0;
}
void update()
{
//println(dirState);
}
void jumping(){
if (jumpState == "jump")
{
mmY -= 6;
if(mmY <= 100) {
jumpState = "falling";
}
}
}
// void shooting(){
// if (shootState == "shoot")
//}
void falling(){
if(jumpState == "falling")
{
mmY +=5;
if(mmY >= 145){
jumpState = "none";
mmY = 145;
}
}
}
void blinkCheck()
{
blinkMaker = random(15);
int W = int(blinkMaker);
//println(W);
if (W == 10)
{
blinkRate = 0;
}
else {
blinkRate = 1;
}
}
void checkState()
{
if (dirState == "right"){
runRight();
chillState = "right";
}
else if (dirState == "left"){
runLeft();
chillState = "left";
}
else if (dirState == "chill"){
chill();
}
if (jumpState == "jump"){
jumping();
}
else if (jumpState == "falling"){
falling();
}
}
}
//
// BRUNO KRUSE
// Megaman Platform Engine
// - Take this code and make something awesome with it!
//
// Declares Megaman as our Hero
// The Hero class is actually used for all characters in the game (enemies too)
Hero Megaman;
PImage mmBackground;
void setup() {
size(300,200);
frameRate(12);
mmBackground = loadImage("background.png");
image(mmBackground, 0,0);
// Gatta remember to initialize him
Megaman = new Hero();
// Loads all of the images before the game starts so that
// loading images during run-time doesnt slow the game down
Megaman.loadImages();
}
void draw(){
// whatever
image(mmBackground, 0,0);
checkKeys();
Megaman.update();
Megaman.checkState();
Megaman.blinkCheck();
if (Megaman.dirState == "right"){
image(Megaman.imagesR[Megaman.imageIndexR], Megaman.mmX,Megaman.mmY);
}
else if (Megaman.dirState == "left")
{
image(Megaman.imagesL[Megaman.imageIndexL], Megaman.mmX,Megaman.mmY);
}
// all the code for standing still PLUS blinking!!!!
else if (Megaman.dirState == "chill")
{
// faces right if hes chillin' right
if (Megaman.chillState == "right"){
image(Megaman.imagesR[Megaman.blinkRate], Megaman.mmX,Megaman.mmY);
}
// faces left if hes chillin' left
if (Megaman.chillState == "left"){
image(Megaman.imagesL[Megaman.blinkRate], Megaman.mmX,Megaman.mmY);
}
}
if (Megaman.jumpState == "jump" || Megaman.jumpState == "falling")
{
if (Megaman.chillState == "right"){
image(mmBackground, 0,0);
image(Megaman.imagesR[5], Megaman.mmX,Megaman.mmY);
}
// faces left if hes chillin' left
if (Megaman.chillState == "left"){
image(mmBackground, 0,0);
image(Megaman.imagesL[5], Megaman.mmX,Megaman.mmY);
}
}
}
void checkKeys(){
if (keyPressed) {
if (keyCode == RIGHT) {
Megaman.dirState = "right";
}
if (keyCode == LEFT) {
Megaman.dirState ="left";
}
if (keyCode == UP && Megaman.jumpState == "none"){
Megaman.jumpState = "jump";
}
}
else {
Megaman.dirState = "chill";
}
}
A simple platform game engine. Use arrow keys to move and up to jump!