score code

edited October 2015 in Library Questions

hi i'm really new at this, and i'm trying to make a game, and i want to know if someone can help me with the score, i want to keep score when the balls leave the screen in X i will really appreciate if someone can take a look...

import pbox2d.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.common.*; import org.jbox2d.dynamics.*;

PBox2D box2d;

ArrayList <Orange> oranges; ArrayList <Blue> blues; PImage blue; PImage orange; PImage cball; PImage oball; float x = 320; float y = 240; int score; int score1;

void setup() { size (640, 480); smooth();

score = 0; score1= 0;

blue= loadImage ("azul.png"); orange = loadImage ("naranja.png"); cball = loadImage ("cbola.png"); oball= loadImage ("obola.png");

box2d = new PBox2D(this); box2d.createWorld();

oranges = new ArrayList <Orange>(100); blues = new ArrayList <Blue>(100); }

void draw() {

background(#0E151F);

noStroke(); fill(255); ellipse(x, y, 5, 5); ellipseMode(CENTER);

noFill(); stroke(255); ellipse(586, 43, 50, 50); ellipseMode (CENTER);

imageMode(CENTER); image(blue, 50, 233.598);

imageMode(CENTER); image(orange, 590, 233.598);

fill (255); textSize(20); text(score, 581, 50);

box2d.step();

if (frameCount%100 == 0 ) {

Orange o = new Orange(width/2, -20); oranges.add(o);

}

if ( frameCount %50 == 0 && frameCount%100 !=0 ) {

Blue b = new Blue(width/2, -20); blues.add(b); }

for (int i = blues.size ()-1; i >= 0; i--) { Blue b = blues.get(i);

if (b.done()) { blues.remove(i); } }

for (Blue b : blues) { b.display(); }

for (int j = oranges.size ()-1; j >= 0; j --) { Orange o = oranges.get(j); if (o.done1()) { oranges.remove(j); } }

for (Orange o : oranges) { o.display(); } }

void keyPressed() {

if (keyCode == LEFT) { for (Blue b : blues) { if (b.movex()); } } if (keyCode == RIGHT) { for (Blue b : blues) { if (b.movex1()); } }

if (keyCode == RIGHT) { for (Orange o : oranges) { if (o.movex()); } } if (keyCode == LEFT) { for (Orange o : oranges) { if (o.movex1()); } } } void score() {

for (int j = oranges.size ()-1; j >= 0; j --) { Orange o = oranges.get(j); if (o.done1()); if (o.puntos2()) { score1 = score1 + 1 ; } }

for (int i = blues.size ()-1; i >= 0; i--) { Blue b = blues.get(i); if (b.done()); if (b.puntos()) { score = score +1 ; } } }

class Blue {

Body body; float r; color a; color c; float speed; float xspeed;Vec2 pos; Vec2 pos1;``

Blue (float x, float y) {

``makeBody(x, y);``

c = color (232, 245, 248); }

void makeBody (float x, float y) {

BodyDef bd= new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(box2d.coordPixelsToWorld(x, y)); body = box2d.createBody(bd);

CircleShape cs = new CircleShape();

``cs.m_radius = box2d.scalarPixelsToWorld(r);``
``FixtureDef fd = new FixtureDef();    ``
``fd.shape = cs;``

body.createFixture(fd); body.setLinearVelocity(new Vec2(0, 3)); }

void killBody() { box2d.destroyBody(body); }

boolean done() {

``Vec2 pos = box2d.getBodyPixelCoord(body);``

if (pos.y > height+r*2 || pos.x < -r|| pos.x > width+r ) { killBody(); return true; } return false; }

boolean movex () { Vec2 pos = box2d.getBodyPixelCoord(body); if ((pos.x > 295 && pos.x < 345 && pos.y > 215 && pos.y < 265)) { body.setLinearVelocity(new Vec2(-90, 0));

 `` textSize(40);``
  ``text("BIEN", 230, 400);``

``  return true;``

} return false; }

boolean movex1 () { Vec2 pos1 = box2d.getBodyPixelCoord(body); if ((pos1.x > 295 && pos1.x < 345 && pos1.y > 190 && pos1.y < 300)) { body.setLinearVelocity(new Vec2(90, 0));

  ``textSize(40);``
 `` text("BIEN", 230, 400);``

 `` return true;``

} return false; }

boolean puntos () { Vec2 pos2 = box2d.getBodyPixelCoord(body); if ((pos2.x > -12 && pos2.x < 112)) {

  ``return true;``
``}``

return false; }

void display() {

``Vec2 pos = box2d.getBodyPixelCoord(body); `` 


``pushMatrix();``

translate(pos.x, pos.y); noFill(); noStroke(); imageMode(CENTER); image(cball, 0, 0 ); popMatrix(); } }

``class Orange {v

Body body; float r; color a; color c;

Orange (float x, float y) {

``makeBody(x, y);``

a = color (254, 229, 76); }

boolean done1() {

``Vec2 pos1 = box2d.getBodyPixelCoord(body);``

if (pos1.y > height)/*+r*2 || pos.x < -r|| pos.x > width+r )*/ { box2d.destroyBody(body); return true; } return false; }

boolean movex() { Vec2 pos = box2d.getBodyPixelCoord(body); if ((pos.x > 295 && pos.x < 345 && pos.y > 215 && pos.y < 265)) { body.setLinearVelocity(new Vec2(90, 0)); fill(#f7931e); textSize(40); text("GANASTE", 230, 400);

 `` return true;``

} return false; } boolean movex1() { Vec2 pos = box2d.getBodyPixelCoord(body); if ((pos.x > 250 && pos.x < 390 && pos.y > 170 && pos.y < 280)) { body.setLinearVelocity(new Vec2(-90, 0)); fill(#f7931e); textSize(40); text("GANASTE", 230, 400);

 `` return true;``

} return false; } boolean puntos2 () { Vec2 pos3 = box2d.getBodyPixelCoord(body); if ((pos3.x > 652 && pos3.x < 528)) {

``  return true;``
``}``
``return false;``

}

void display() {

``Vec2 pos = box2d.getBodyPixelCoord(body); ``

pushMatrix(); translate(pos.x, pos.y);

fill(a); noStroke(); imageMode(CENTER); image(oball, 0, 0 ); popMatrix(); }

void makeBody (float x, float y) {

``CircleShape cs = new CircleShape();  ``      

cs.m_radius = box2d.scalarPixelsToWorld(r); FixtureDef fd = new FixtureDef();
fd.shape = cs;

fd.density = 1; fd.friction = 0.3; fd.restitution = 0.5;

``BodyDef bd= new BodyDef();``
``bd.type = BodyType.DYNAMIC;``
``bd.position.set(box2d.coordPixelsToWorld(x, y));``
``body = box2d.createBody(bd);``


``body.createFixture(fd);``
``body.setLinearVelocity(new Vec2(0, 3));``

} }

Tagged:

Answers

Sign In or Register to comment.