Skip to content

Instantly share code, notes, and snippets.

@cangevine
Created January 29, 2015 20:15
Show Gist options
  • Save cangevine/9c307cd467f805399c83 to your computer and use it in GitHub Desktop.
Save cangevine/9c307cd467f805399c83 to your computer and use it in GitHub Desktop.
abstract class Pokemon extends GameObject{
int currentHP;
int maxHP;
PImage leftImg, rightImg;
float yVel;
float GRAV = .5;
float xVel;
float xAcc;
void moveRight() {
direction = "right";
xVel = xVel + 9;
if (xVel <= 20) {
xAcc += 0.1;
} else {
xAcc = 0;
xVel = 20;
}
xVel = xVel + xAcc;
}
void moveLeft() {
direction = "left";
xVel = xVel - 9;
if (xVel >= -20) {
xAcc -= 0.1;
} else {
xAcc = 0;
xVel = -20;
}
xVel = xVel + xAcc;
}
void wrap() {
if (x > width - 50) {
x = -200;
} else if (x<-200) {
x = width - 50;
}
}
void jump() {
y -= 1;
if (y >= height - hei - 5) {
yVel = -10;
}
}
void updatePosition() {
if (y < height - hei) {
y = y + yVel;
yVel = yVel + GRAV;
} else {
yVel = 0;
y = height - hei;
}
x = x + xVel;
if (xVel > 0) {
xVel = xVel - 1;
} else if (xVel < 0) {
xVel = xVel + 1;
} else {
xVel = 0;
}
}
boolean isUnconscious() {
return currentHP <= 0;
}
}
class Pikachu extends Pokemon {
Pikachu(Game g) {
println("Pikachu initialized!");
wid = 250;
hei = 250;
imgSrc = loadImage("http://cdn.bulbagarden.net/upload/thumb/0/0d/025Pikachu.png/250px-025Pikachu.png");
}
void attack() {
println("ZAP!");
Bolt b = new Bolt(x, y, direction);
g.addProjectile(b);
}
}
class Charmander extends Pokemon {
Charmander(Game g) {
println("Charmander initialized!");
wid = 250;
hei = 250;
imgSrc = loadImage("http://cdn.bulbagarden.net/upload/thumb/7/73/004Charmander.png/250px-004Charmander.png");
}
void attack() {
println("fffffffire!");
Fireball f = new Fireball();
g.addProjectile(f);
}
}
class Game {
ArrayList<Projectile> projectiles;
Pikachu pik;
Charmander cha;
//ArrayList<Platform> platforms;
Game(){
pik = new Pikachu(this);
cha = new Charmander(this);
projectiles = new ArrayList<Projectile>();
}
void tick() {
updateAllProjectiles();
drawAllProjectiles();
updateAllPokemon();
drawAllPokemon();
}
void addProjectile(Projectile p) {
projectiles.add(p);
}
void updateAllProjectiles() {
for(int i = 0; i < projectiles.size(); i++){
Projectile p = projectiles.get(i);
p.update();
if(cha.hitTest(p) == true){
//p.currentHP--;
cha.jump();
projectiles.remove(i);
println("you have been hit!");
}
}
}
void drawAllProjectiles() {
for(int i = 0; i < projectiles.size(); i++){
Projectile p = projectiles.get(i);
p.drawSelf();
}
}
void updateAllPokemon() {
if (cha != null && pik != null) {
moveLR();
jump();
cha.wrap();
pik.wrap();
cha.updatePosition();
pik.updatePosition();
}
}
void drawAllPokemon() {
if (cha != null && pik != null) {
pik.drawSelf();
cha.drawSelf();
}
}
Pikachu getPikachu() {
return pik;
}
Charmander getCharmander() {
return cha;
}
}
abstract class GameObject {
float x, y;
int wid, hei;
PImage imgSrc;
String direction = "none";
int getWid(){
return wid;
}
int getHei(){
return hei;
}
float getX(){
return x;
}
float getY(){
return y;
}
boolean hitTest(GameObject otherObj){
//check X collision
if(this.getX()+float(this.getWid()) >= otherObj.getX()
&& this.getX() <= otherObj.getX() + float(otherObj.getWid())){
//check Y collision
if(this.getY()+float(this.getHei()) >= otherObj.getY()
&& this.getY() <= otherObj.getY() + float(otherObj.getHei())){
return true;
}
}
return false;
}
void drawSelf() {
pushMatrix();
if (direction =="left") {
scale(1.0, 1.0);
image(imgSrc, x, y);
} else if(direction == "right"){
scale(-1.0, 1.0);
image(imgSrc, -x - imgSrc.width, y);
} else {
scale(1.0, 1.0);
image(imgSrc, x, y);
}
popMatrix();
}
}
Game g;
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
boolean wPressed = false;
boolean aPressed = false;
boolean sPressed = false;
boolean dPressed = false;
//w,a,s,d,UP,LEFT,DOWN,RIGHT.
boolean keyz[] = new boolean [8];
void setup() {
g = new Game();
size(1080, 720);
}
void draw() {
background(255);
g.tick();
}
void keyPressed() {
if (key == 'w') keyz[0] = true;
if (key == 'a') keyz[1] = true;
if (key == 's') keyz[2] = true;
if (key == 'd') keyz[3] = true;
if (keyCode == UP) keyz[4] = true;
if (keyCode == LEFT) keyz[5] = true;
if (keyCode == DOWN) keyz[6] = true;
if (keyCode == RIGHT) keyz[7] = true;
}
void keyReleased() {
if (key == 'w') keyz[0] = false;
if (key == 'a') keyz[1] = false;
if (key == 's') keyz[2] = false;
if (key == 'd') keyz[3] = false;
if (keyCode == UP) keyz[4] = false;
if (keyCode == LEFT) keyz[5] = false;
if (keyCode == DOWN) keyz[6] = false;
if (keyCode == RIGHT) keyz[7] = false;
}
void moveLR() {
if (keyz[5] == true) {
g.getPikachu().moveLeft();
}
if (keyz[7] == true) {
g.getPikachu().moveRight();
}
if (keyz[6] == true) {
g.getPikachu().attack();
}
if (keyz[1] == true) {
g.getCharmander().moveLeft();
}
if (keyz[3] == true) {
g.getCharmander().moveRight();
}
if (keyz[2] == true) {
g.getCharmander().attack();
}
}
void jump() {
if (keyz[4] == true) {
g.getPikachu().jump();
}
if (keyz[0] == true) {
g.getCharmander().jump();
}
}
abstract class Projectile extends GameObject {
float vel;
float damage;
abstract void update();
}
class Fireball extends Projectile {
float GRAV;
color col;
Fireball() {
damage = random(5,25);
GRAV = .5;
col = color(255,0,0);
}
void update() {
x = x + vel;
if (vel > 0) {
vel = vel / 2;
}
}
void drawSelf() {
fill(col);
ellipse(x, y, 20, 20);
}
}
class Bolt extends Projectile{
color col;
Bolt(float startX, float startY, String startDir) {
x = startX;
y = startY + 125;
if (startDir.equals("left")) {
vel = -25.5;
} else {
vel = 25.5;
}
damage = random (10,30);
col = color(#ffff26);
println("whoosh");
}
void update() {
x = x + vel;
}
void drawSelf() {
fill(col);
ellipse(x, y, 20, 20);
}
}
class Wave extends Projectile{
color col;
Wave() {
damage = random (15,30);
col = color(#0000ff);
}
void update() {
}
void drawSelf() {
fill(col);
ellipse(x, y, 20, 20);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment