Processing Platformer

Video:

 

Code:

Main Code:

import java.awt.Rectangle;
platforms platforms;
player player;

void setup(){
size(800,800);
smooth();
frameRate(60);
noStroke();
player = new player();
platforms = new platforms();
}

boolean[] keys = new boolean[256];
void keyPressed() {
keys[keyCode]=true;
};
void keyReleased() {
keys[keyCode]=false;
};

void draw(){
background (150,150, 255);
fill(255,240,0);
ellipse(650,99,29,29);
player.display();
player.playerInput();
player.playerUpdate();
platforms.display();
platforms.collision();

}

Platforms:

class platforms {

Rectangle ground;
Rectangle plat1;
Rectangle plat2;
Rectangle plat3;
Rectangle plat4;
Rectangle plat5;
Rectangle plat6;

platforms() {
ground = new Rectangle(0,750,900,50);
plat1 = new Rectangle(-13,700,217,200);
plat2 = new Rectangle(317,600,165,30);
plat3 = new Rectangle(600,500,200,30);
plat4 = new Rectangle(440,340,115,30);
plat5 = new Rectangle(219,249,90,30);
plat6 = new Rectangle(379,159,89,30);
}

void display(){
fill(80,34,0);
rect(-13,700,230,200);

fill(30,190,0);
rect(0,750,900,50);

fill(143,61,0);
rect(349,600,152,20);

fill(189,80,0);
rect(630,500,200,20);

fill(222,104,0,150);
rect(472,340,91,14);

fill(252,119,0,100);
rect(253,246,70,7);

fill(255,163,65,85);
rect(394,159,89,2);

}

void collision(){
if (ground.contains(player.x, player.y+player.size)){
player.fallingGround = false;
} else {
player.fallingGround = true;
}

if (plat1.contains(player.x, player.y+player.size)){
player.fallingPlat = false;
} else {
player.fallingPlat = true;
}

if (plat2.contains(player.x, player.y+player.size)){
player.fallingPlat2 = false;
} else {
player.fallingPlat2 = true;
}

if (plat3.contains(player.x, player.y+player.size)){
player.fallingPlat3 = false;
} else {
player.fallingPlat3 = true;
}

if (plat4.contains(player.x, player.y+player.size)){
player.fallingPlat4 = false;
} else {
player.fallingPlat4 = true;
}

if (plat5.contains(player.x, player.y+player.size)){
player.fallingPlat5 = false;
} else {
player.fallingPlat5 = true;
}

if (plat6.contains(player.x, player.y+player.size)){
player.fallingPlat6 = false;
} else {
player.fallingPlat6 = true;
}

if (!player.fallingGround) {
player.y = 725 – player.size/2;
}

if (!player.fallingPlat) {
player.y = 675 – player.size/2;
}

if (!player.fallingPlat2) {
player.y = 575 – player.size/2;
}

if (!player.fallingPlat3) {
player.y = 475 – player.size/2;
}

if (!player.fallingPlat4) {
player.y = 315 – player.size/2;
}

if (!player.fallingPlat5) {
player.y = 221 – player.size/2;
}

if (!player.fallingPlat6) {
player.y = 134 – player.size/2;
}

}
}

Player:

class player {
PImage image;
int size = 50;
float x = 650;
float y = 700;
float rx = x;
float ry = y;
float xv = 0;
float yv = 0;
float xspeed = 7;
float gravity = 0;
boolean dead = false;
boolean fallingGround = true;
boolean fallingPlat = true;
boolean fallingPlat2 = true;
boolean fallingPlat3 = true;
boolean fallingPlat4 = true;
boolean fallingPlat5 = true;
boolean fallingPlat6 = true;

player() {
x = 650;
size = 50;
y = 700;
xspeed = 7;
image = loadImage(“player.png”);
}

void playerInput() {
xv=0;
yv=0;

if (dead==false) {
if (keys[UP] && fallingGround==false) {
gravity-=19;
}

if (keys[UP] && fallingPlat==false) {
gravity-=19;
}
if (keys[UP] && fallingPlat2==false) {
gravity-=19;
}
if (keys[UP] && fallingPlat3==false) {
gravity-=19;
}
if (keys[UP] && fallingPlat4==false) {
gravity-=19;
}
if (keys[UP] && fallingPlat5==false) {
gravity-=19;
}
if (keys[UP] && fallingPlat6==false) {
gravity-=19;
}

if (keys[LEFT]) {
xv=-xspeed;
}
if (x <= 0){
x = 0;
}
if (keys[RIGHT]) {
xv=xspeed;
}
if (x >= 750){
x = 750;
}
if (keys[BACKSPACE]){
x = 650;
y = 700;
}
}

yv+=gravity;
gravity+=0.4;

if (gravity >= 25){
gravity = 25;
}else{
gravity+=0.4;
}

fallingGround = true;
fallingPlat = true;
fallingPlat2 = true;
fallingPlat3 = true;
fallingPlat4 = true;
fallingPlat5 = true;
fallingPlat6 = true;
}

void playerUpdate() {
x+=xv;
y+=yv;

}

void display(){
image(image,x,y);
}
}

Advertisements
This entry was posted in Best Work, Non-Timebased and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s