Examples
Platform game
This is an example of a platform games that can easily be created using PPhys2D. Use the arrows to control the pink ChocoBoyce and user W-A-D to control the blue ChocoBoyce. Your goal is to collect more cookies than your opponent. Code has been kept as simple as possible, explaining some small bugs.
Applet
Code
//Import all Phys2D libraries
import pphys2d.bodies.*;
import pphys2d.joints.*;
import pphys2d.shapes.*;
import pphys2d.phys2d.raw.collide.*;
import pphys2d.phys2d.raw.strategies.*;
import pphys2d.phys2d.raw.forcesource.*;
import pphys2d.phys2d.util.*;
import pphys2d.phys2d.raw.shapes.*;
import pphys2d.*;
import pphys2d.phys2d.raw.*;
import pphys2d.phys2d.math.*;
//Create a PPhys2D world
PPWorld world = new PPWorld();
//Create the font
PFont myFont;
//Create the two ChocoBoyces
ChocoBoyce choco1;
ChocoBoyce choco2;
//Create the two ChocoBoyces
BoyceCuit boycecuit;
//Levels objects : grounds ant steps
PPBox ground;
PPBox box1;
PPBox box2;
//Create an ArrayList to hold keys
ArrayList keyPressedList = new ArrayList();
//Create an Array to hold the images
PImage[] imagesLib = new PImage[2];
void setup () {
//Basic settings
frameRate(30);
size(256,256);
//World settings
world.setGravity(0, 400);
world.setEdges(this, new Color (26, 145, 38, 1));
world.setEdgesFriction(0);
//Load font
myFont = loadFont("AppleCasual-12.vlw");
//Load background images
imagesLib[0] = loadImage("background.png");
//Load first Chocoboyce images
PImage[] choco1Lib = { loadImage("choco_right_1.png"), loadImage("choco_right_2.png"), loadImage("choco_right_3.png"), loadImage("choco_right_4.png"), loadImage("choco_left_1.png"), loadImage("choco_left_2.png"), loadImage("choco_left_3.png"), loadImage("choco_left_4.png") };
choco1 = new ChocoBoyce (195, 180, choco1Lib, world);
//Load second Chocoboyce images
PImage[] choco2Lib = { loadImage("choco2_right_1.png"), loadImage("choco2_right_2.png"), loadImage("choco2_right_3.png"), loadImage("choco2_right_4.png"), loadImage("choco2_left_1.png"), loadImage("choco2_left_2.png"), loadImage("choco2_left_3.png"), loadImage("choco2_left_4.png") };
choco2 = new ChocoBoyce (225, 180, choco2Lib, world);
//Load Cookie image
PImage[] bocycuitLib = { loadImage("boycecuit.png") };
boycecuit = new BoyceCuit ((int)random(25,225), 10, bocycuitLib, world);
//Create ground object
ground = new PPBox(260, 20);
ground.setPosition(125, 250);
ground.setStaticBody(true);
ground.setFillColor(new Color(25,145,38));
ground.setStrokeColor(new Color(25,145,38));
ground.setFriction(1);
ground.setRestitution(0.7);
world.add(ground);
//Create first paltform object
box1 = new PPBox(85, 5);
box1.setPosition(125, 220);
box1.setStaticBody(true);
box1.setRotatable(false);
box1.setFillColor(new Color(0,0,0));
box1.setStrokeColor(new Color(0,0,0));
box1.setFriction(1);
box1.setRestitution(0.7);
world.add(box1);
//Create second paltform object
box2 = new PPBox(85, 5);
box2.setPosition(195, 180);
box2.setStaticBody(true);
box2.setRotatable(false);
box2.setFillColor(new Color(0,0,0));
box2.setStrokeColor(new Color(0,0,0));
box2.setFriction(1);
box2.setRestitution(0.7);
world.add(box2);
}
void draw () {
//Draw background
image(imagesLib[0], 0, 0);
//Draw PPhys World objects
world.draw(this);
//Advance each ChocoBoyce from one step
choco1.step();
choco2.step();
//Check Collision
if( choco1.checkEating(boycecuit) || choco2.checkEating(boycecuit)) {
boycecuit.ppbox.setPosition((int)random(25,225),80);
}
//Check keyboard interaction
checkKey();
//Draw Score
drawPoints();
}
void keyPressed() {
//Add presed key code to keyPressedList
if (!keyPressedList.contains(keyCode)) keyPressedList.add(keyCode);
}
void keyReleased() {
//Remove pressed key code from keyPressedList
keyPressedList.remove(keyPressedList.indexOf(keyCode));
}
void checkKey() {
for (int i=0; i < keyPressedList.size(); i++) {
String keycode = (keyPressedList.get(i)).toString() ;
if(keycode.equals("38"))choco1.jump(); //UP
if(keycode.equals("37"))choco1.goLeft(); //LEFT
if(keycode.equals("39"))choco1.goRight(); //RIGHT
if(keycode.equals("87"))choco2.jump(); //W
if(keycode.equals("65"))choco2.goLeft(); //A
if(keycode.equals("68"))choco2.goRight(); //D
}
}
void drawPoints () {
//Set matrix
pushMatrix();
translate(120, 250);
//Draw Text
fill(255,255,255);
textAlign(CENTER);
textFont(myFont);
text("Pink " + choco1.points + " | Blue " + choco2.points, 0, 0);
//Restore Matrix
popMatrix();
}
//A Class for the Cookie
class BoyceCuit {
PPCircle ppbox;
PImage[] images;
BoyceCuit(int _x, int _y, PImage[] _images, PPWorld _world) {
images = _images;
//Create the PPCircle reprenting the cookie
ppbox = new PPCircle(20);
ppbox.setPosition(_x, _y);
ppbox.setFriction(1);
ppbox.setDamping(1.5);
ppbox.setMass(0.02);
ppbox.setRestitution(0.7);
ppbox.attachImage( images [0] );
_world.add(ppbox);
}
}
//A class for the ChocoBoyce
class ChocoBoyce {
//Local variables
PPBox ppbox;
PImage[] images;
int imageIndex = 0;
int points = 0;
boolean isGoingLeft = false;
//Constructor
ChocoBoyce(int _x, int _y, PImage[] _images, PPWorld _world) {
images = _images;
//Create the PPBox reprenting the cookie
ppbox = new PPBox(20, 30);
ppbox.setPosition(_x, _y);
ppbox.setRotatable(false);
ppbox.setFriction(2);
ppbox.attachImage( images [0] );
_world.add(ppbox);
}
//This function appends every frame
void step() {
int goingLeftFactor = 0;
if(isGoingLeft)goingLeftFactor = 1;
imageIndex += abs(ppbox.getVelocity().getX()*0.02);
ppbox.attachImage( images [(int)(imageIndex%4 + goingLeftFactor*4)] );
}
//Going left
void goLeft() {
if(ppbox.getVelocityX() > -100) {
ppbox.addForce(-800000, 0);
isGoingLeft = true;
}
}
//Going right
void goRight() {
if(ppbox.getVelocityX() < 100) {
ppbox.addForce(800000, 0);
isGoingLeft = false;
}
}
//Jumping
void jump() {
if(ppbox.getTouching().size() > 0 && ppbox.getPosition().getX() > 15 && ppbox.getPosition().getX() < 241 && ppbox.getVelocity().getY() > -10) ppbox.addForce(new Vector2f(0, -1200000.0f));
}
//Check Collision with cookie
boolean checkEating(BoyceCuit boycecuit) {
if(ppbox.isTouchingBody(boycecuit.ppbox)) {
points++;
return true;
} else {
return false;
}
}
}
import pphys2d.bodies.*;
import pphys2d.joints.*;
import pphys2d.shapes.*;
import pphys2d.phys2d.raw.collide.*;
import pphys2d.phys2d.raw.strategies.*;
import pphys2d.phys2d.raw.forcesource.*;
import pphys2d.phys2d.util.*;
import pphys2d.phys2d.raw.shapes.*;
import pphys2d.*;
import pphys2d.phys2d.raw.*;
import pphys2d.phys2d.math.*;
//Create a PPhys2D world
PPWorld world = new PPWorld();
//Create the font
PFont myFont;
//Create the two ChocoBoyces
ChocoBoyce choco1;
ChocoBoyce choco2;
//Create the two ChocoBoyces
BoyceCuit boycecuit;
//Levels objects : grounds ant steps
PPBox ground;
PPBox box1;
PPBox box2;
//Create an ArrayList to hold keys
ArrayList keyPressedList = new ArrayList();
//Create an Array to hold the images
PImage[] imagesLib = new PImage[2];
void setup () {
//Basic settings
frameRate(30);
size(256,256);
//World settings
world.setGravity(0, 400);
world.setEdges(this, new Color (26, 145, 38, 1));
world.setEdgesFriction(0);
//Load font
myFont = loadFont("AppleCasual-12.vlw");
//Load background images
imagesLib[0] = loadImage("background.png");
//Load first Chocoboyce images
PImage[] choco1Lib = { loadImage("choco_right_1.png"), loadImage("choco_right_2.png"), loadImage("choco_right_3.png"), loadImage("choco_right_4.png"), loadImage("choco_left_1.png"), loadImage("choco_left_2.png"), loadImage("choco_left_3.png"), loadImage("choco_left_4.png") };
choco1 = new ChocoBoyce (195, 180, choco1Lib, world);
//Load second Chocoboyce images
PImage[] choco2Lib = { loadImage("choco2_right_1.png"), loadImage("choco2_right_2.png"), loadImage("choco2_right_3.png"), loadImage("choco2_right_4.png"), loadImage("choco2_left_1.png"), loadImage("choco2_left_2.png"), loadImage("choco2_left_3.png"), loadImage("choco2_left_4.png") };
choco2 = new ChocoBoyce (225, 180, choco2Lib, world);
//Load Cookie image
PImage[] bocycuitLib = { loadImage("boycecuit.png") };
boycecuit = new BoyceCuit ((int)random(25,225), 10, bocycuitLib, world);
//Create ground object
ground = new PPBox(260, 20);
ground.setPosition(125, 250);
ground.setStaticBody(true);
ground.setFillColor(new Color(25,145,38));
ground.setStrokeColor(new Color(25,145,38));
ground.setFriction(1);
ground.setRestitution(0.7);
world.add(ground);
//Create first paltform object
box1 = new PPBox(85, 5);
box1.setPosition(125, 220);
box1.setStaticBody(true);
box1.setRotatable(false);
box1.setFillColor(new Color(0,0,0));
box1.setStrokeColor(new Color(0,0,0));
box1.setFriction(1);
box1.setRestitution(0.7);
world.add(box1);
//Create second paltform object
box2 = new PPBox(85, 5);
box2.setPosition(195, 180);
box2.setStaticBody(true);
box2.setRotatable(false);
box2.setFillColor(new Color(0,0,0));
box2.setStrokeColor(new Color(0,0,0));
box2.setFriction(1);
box2.setRestitution(0.7);
world.add(box2);
}
void draw () {
//Draw background
image(imagesLib[0], 0, 0);
//Draw PPhys World objects
world.draw(this);
//Advance each ChocoBoyce from one step
choco1.step();
choco2.step();
//Check Collision
if( choco1.checkEating(boycecuit) || choco2.checkEating(boycecuit)) {
boycecuit.ppbox.setPosition((int)random(25,225),80);
}
//Check keyboard interaction
checkKey();
//Draw Score
drawPoints();
}
void keyPressed() {
//Add presed key code to keyPressedList
if (!keyPressedList.contains(keyCode)) keyPressedList.add(keyCode);
}
void keyReleased() {
//Remove pressed key code from keyPressedList
keyPressedList.remove(keyPressedList.indexOf(keyCode));
}
void checkKey() {
for (int i=0; i < keyPressedList.size(); i++) {
String keycode = (keyPressedList.get(i)).toString() ;
if(keycode.equals("38"))choco1.jump(); //UP
if(keycode.equals("37"))choco1.goLeft(); //LEFT
if(keycode.equals("39"))choco1.goRight(); //RIGHT
if(keycode.equals("87"))choco2.jump(); //W
if(keycode.equals("65"))choco2.goLeft(); //A
if(keycode.equals("68"))choco2.goRight(); //D
}
}
void drawPoints () {
//Set matrix
pushMatrix();
translate(120, 250);
//Draw Text
fill(255,255,255);
textAlign(CENTER);
textFont(myFont);
text("Pink " + choco1.points + " | Blue " + choco2.points, 0, 0);
//Restore Matrix
popMatrix();
}
//A Class for the Cookie
class BoyceCuit {
PPCircle ppbox;
PImage[] images;
BoyceCuit(int _x, int _y, PImage[] _images, PPWorld _world) {
images = _images;
//Create the PPCircle reprenting the cookie
ppbox = new PPCircle(20);
ppbox.setPosition(_x, _y);
ppbox.setFriction(1);
ppbox.setDamping(1.5);
ppbox.setMass(0.02);
ppbox.setRestitution(0.7);
ppbox.attachImage( images [0] );
_world.add(ppbox);
}
}
//A class for the ChocoBoyce
class ChocoBoyce {
//Local variables
PPBox ppbox;
PImage[] images;
int imageIndex = 0;
int points = 0;
boolean isGoingLeft = false;
//Constructor
ChocoBoyce(int _x, int _y, PImage[] _images, PPWorld _world) {
images = _images;
//Create the PPBox reprenting the cookie
ppbox = new PPBox(20, 30);
ppbox.setPosition(_x, _y);
ppbox.setRotatable(false);
ppbox.setFriction(2);
ppbox.attachImage( images [0] );
_world.add(ppbox);
}
//This function appends every frame
void step() {
int goingLeftFactor = 0;
if(isGoingLeft)goingLeftFactor = 1;
imageIndex += abs(ppbox.getVelocity().getX()*0.02);
ppbox.attachImage( images [(int)(imageIndex%4 + goingLeftFactor*4)] );
}
//Going left
void goLeft() {
if(ppbox.getVelocityX() > -100) {
ppbox.addForce(-800000, 0);
isGoingLeft = true;
}
}
//Going right
void goRight() {
if(ppbox.getVelocityX() < 100) {
ppbox.addForce(800000, 0);
isGoingLeft = false;
}
}
//Jumping
void jump() {
if(ppbox.getTouching().size() > 0 && ppbox.getPosition().getX() > 15 && ppbox.getPosition().getX() < 241 && ppbox.getVelocity().getY() > -10) ppbox.addForce(new Vector2f(0, -1200000.0f));
}
//Check Collision with cookie
boolean checkEating(BoyceCuit boycecuit) {
if(ppbox.isTouchingBody(boycecuit.ppbox)) {
points++;
return true;
} else {
return false;
}
}
}