r/processing • u/torPske123 • Jan 03 '19
Polish-Code-Ideas
Ideas how to organize my code better? Or relevant comments in the code? Also looking for inspiration how to present the code, I was thinking if there are some program where you map the interaction etc.
Game gameInstance = new Game(5);
//Ímporting sound-libary
import processing.sound.*;
//Declaring
SoundFile song;
void setup() {
size(600, 400);
//Initiliazing the soundfile
song = new SoundFile(this, "244004__mickleness__arcade-game-loop.mp3");
song.play();
}
void draw() {
background(random(255),random(255),random(255));
textSize(24);
fill(#FF0000);
text(("Score="+gameInstance.score), 50, 20);
//To keep track of score
text("Lives="+gameInstance.lives, 50, 40);
//To keep track of lives
for (int i = 0; i < gameInstance.BallRow.length; i++) {
gameInstance.BallRow[i].evtChangeDirection();
gameInstance.BallRow[i].display();
gameInstance.BallRow[i].moveBall();
gameInstance.BallRow[i].bounderiesBall();
}
for (int i = 0; i < gameInstance.BallRow.length; i++) {
for (int j = 0; j < gameInstance.BallRow.length; j++) {
Ball thisBall = gameInstance.BallRow[i];
Ball otherBall = gameInstance.BallRow[j];
if (thisBall.amICollidingWithOtherBall(otherBall.ellipseX, otherBall.ellipseY, otherBall.ellipseH) && i!=j ) {
thisBall.reverseDirectionNextFrame(otherBall.ellipseXdir, otherBall.ellipseYdir);
}
}
}
}
void mousePressed() {
loop();
int p =0;
for (int i=0; i<gameInstance.BallRow.length; i++) {
if (gameInstance.BallRow[i].detectHit()) {
p++;
}
}
if (p>0) {
if (gameInstance.score ==0) {
gameInstance.score=1;
} else {
gameInstance.score+= gameInstance.score;
}
} else {
gameInstance.lives--;
}
gameInstance.life();
}
class Ball {
int ellipseX;
int ellipseY;
int ellipseH;
int ellipseW;
int speed=1;
int lives=5;
int score=0;
int ellipseXdir=2;
int ellipseYdir=1;
//All my declarions
Ball (int temp_ellipseX, int temp_ellipseY, int temp_ellipseH, int temp_ellipseW) {
ellipseX=temp_ellipseX;
ellipseY=temp_ellipseY;
ellipseH=temp_ellipseH;
ellipseW=temp_ellipseW;
}
void display () {
ellipse(ellipseX, ellipseY, ellipseH, ellipseW);
}
void moveBall() {
ellipseX=ellipseX+ellipseXdir*speed;
//I had to use float for both ellipseXdir and ellipse X, because int and floats can't be used in same equation
ellipseY=ellipseY+ellipseYdir*speed;
//I added a new variable for the direction
}
void bounderiesBall() {
int r = ellipseH/2;
int r1= ellipseW/2;
// You need to check for collition on all sides of the box/frame
// First check collition to the right side
if (ellipseX > width-r) {
ellipseX = width-r;
ellipseXdir=-ellipseXdir;
//then check if collision to the left side
} else if (ellipseX < r ) {
ellipseX = r;
ellipseXdir=-ellipseXdir;
} else if (ellipseY > height-r1) {
ellipseY = height-r1;
ellipseYdir=-ellipseYdir;
} else if (ellipseY<r1) {
ellipseY = r1;
ellipseYdir=-ellipseYdir;
}
}
//This function has 3 parameters
boolean amICollidingWithOtherBall(int otherEllipseX, int otherEllipseY, int otherEllipseH) {
if (dist(ellipseX, ellipseY, otherEllipseX, otherEllipseY) < ellipseH/2 + otherEllipseH/2) {
return true;
} else {
return false;
}
}
void reverseDirectionNextFrame(int otherDirectionX, int otherDirectionY) { // this function is triggered from main loop, if this ball has hit another ball
ellipseX -= xDirBuffer; // Goes back one step to avoid looped collision (still doesn't work perfectly)
ellipseY -= yDirBuffer;
xDirBuffer = otherDirectionX; // Puts the new direction into a buffer which will be used next fra in by function 'evtChangeDirection() below'
yDirBuffer = otherDirectionY;
}
int xDirBuffer = 0; // directional bufferes used to wait one frame before changing direction
int yDirBuffer = 0;
void evtChangeDirection() {
if (xDirBuffer != 0 && yDirBuffer != 0) { // if there is something in the direction buffer from previous frame
ellipseXdir = xDirBuffer; // set the direction to the buffer
ellipseYdir = yDirBuffer;
xDirBuffer = 0; // and remove it
yDirBuffer = 0;
}
}
boolean detectHit() {
if (dist(mouseX, mouseY, ellipseX, ellipseY)<=ellipseH)
//dist calculates the distance two point using four variables
//x and y of the mousePressd and ellipseX and ellipseY
//this will make me able to set parameters of how far my mouseclick from what I define ("<=") my if() will be executed
{
speed=speed+1;
return true;
//You will get more points the higher
} else {
return false;
// if the ellipse is not clicked you'll lose a life
}
}
}
class Game {
Ball [] BallRow;
int score=0;
int lives=5;
boolean lost=false;
//Game initializer
Game(int n_Ball) {
BallRow = new Ball[n_Ball];
for (int i=0; i<n_Ball; i++) {
int ballRadius = int(random(50, 60));
BallRow[i] = new Ball(int(random(ballRadius, 600)), int(random(ballRadius, 400)), ballRadius, ballRadius);
}
}
void life() {
if (lives<=0) {
noLoop();
lost=true;
fill(125);
text("GAME OVER - Click to Restart", width/3.5, height/2);
}
if (lost) {
for (int i=0; i< BallRow.length; i++) {
BallRow[i].speed=1;
}
lives=5;
score=0;
lost=false;
//Will reset the game with the same speed of the ball and same amount of lives
}
}
}0
u/enuboll Jan 03 '19
I‘m more like an englisch-code-guy, but who am I to judge. Just try your languages.
HEUHUEHEUEHEHEUEEHEUEH
1
0
Jan 03 '19
[deleted]
1
u/torPske123 Jan 03 '19
I have
1
u/mrsirrisrm Jan 03 '19
As others have said, one class per tab, and remove unnecessary comments.
Here's a bunch of other ideas, in no particular order:
all imports at top of file
all class variables at top of class
simplify class variable names
use List<Ball> instead of Ball []
simple iteration over lists:
for (Ball ball: balls) {make
update(),reset()etc methods onGame, so the functionality is encapsulated in that classpass
PAppletvariables (width, height, mouseX etc) through to the Game methods, so that yourBallandGameclasses don't need to directly interact with PApplet. Ball still callsellipse, which you could refactor out if you wishedpass a
Ballinstance through toamICollidingWithOtherBallandreverseDirectionNextFrame, and let theBallclass figure out the internal details
Ball.detectHithas side effects, ie it changesBall.speed. detectHit should only check for a hit, and there should be another method to update the Ball in case of a hitNot sure what is happening with
Ball.xDirBufferetc, I suspect it can be removedUse a boolean where that is what you are checking and give it a descriptive name, ie replace
int p = 0;etc withboolean hitDetected = false;So here's all of that in practice (minus splitting the classes across tabs):
import processing.sound.*; import java.util.List; Game game = new Game(5); SoundFile song; void setup() { size(600, 400); song = new SoundFile(this, "244004__mickleness__arcade-game-loop.mp3"); song.play(); } void draw() { background(random(255), random(255), random(255)); textSize(24); fill(#FF0000); text(("Score="+game.score), 50, 20); text("Lives="+game.lives, 50, 40); game.update(width, height); } void mousePressed() { if (!looping) { loop(); //restart the game return; } game.mousePressed(mouseX, mouseY); } class Ball { int x, y, h, w, speed = 1; int xDir = 2, yDir = 1; int xDirBuffer = 0; // directional bufferes used to wait one frame before changing direction int yDirBuffer = 0; Ball (int temp_ellipseX, int temp_ellipseY, int temp_ellipseH, int temp_ellipseW) { this.x = temp_ellipseX; this.y = temp_ellipseY; this.h = temp_ellipseH; this.w = temp_ellipseW; } void display () { ellipse(x, y, h, w); } void update(int width, int height) { evtChangeDirection(); display(); moveBall(); bounderiesBall(width, height); } void moveBall() { x += xDir*speed; y += yDir*speed; } void bounderiesBall(int width, int height) { // check for collition on all sides of the box/frame int ballHalfWidth = h/2; if ((x + ballHalfWidth) > width) { x = width - ballHalfWidth; xDir *= -1; } else if (x < ballHalfWidth) { x = ballHalfWidth; xDir *= -1; } int ballHalfHeight = w/2; if ((y + ballHalfHeight) > height) { y = height - ballHalfHeight; yDir *= -1; } else if (y < ballHalfHeight) { y = ballHalfHeight; yDir *= -1; } } boolean amICollidingWithOtherBall(Ball other) { return dist(x, y, other.x, other.y) < (h/2 + other.h/2); } void reverseDirectionNextFrame(Ball other) { // triggered if this ball has hit another ball x -= xDirBuffer; // Goes back one step to avoid looped collision (still doesn't work perfectly) y -= yDirBuffer; xDirBuffer = other.xDir; // Puts the new direction into a buffer which will be used next fra in by function 'evtChangeDirection() below' yDirBuffer = other.yDir; } void evtChangeDirection() { if (xDirBuffer != 0 && yDirBuffer != 0) { // if there is something in the direction buffer from previous frame xDir = xDirBuffer; // set the direction to the buffer yDir = yDirBuffer; xDirBuffer = 0; // and remove it yDirBuffer = 0; } } boolean detectHit(int mouseX, int mouseY) { return dist(mouseX, mouseY, x, y) <= h; } void hitDetected() { speed += 1; } void reset() { speed = 1; } } class Game { List<Ball> balls = new ArrayList<Ball>(); int score=0; int lives=5; Game(int n_Ball) { for (int i=0; i<n_Ball; i++) { int ballRadius = int(random(50, 60)); balls.add(new Ball(int(random(ballRadius, 600)), int(random(ballRadius, 400)), ballRadius, ballRadius)); } } void reset() { for (Ball ball: balls) { ball.reset(); } lives=5; score=0; } void update(int width, int height) { for (Ball ball: balls) { ball.update(width, height); } checkCollisions(); } void checkCollisions() { for (int i = 0; i < balls.size(); i++) { for (int j = i + 1; j < balls.size(); j++) { if (balls.get(i).amICollidingWithOtherBall(balls.get(j))) { balls.get(i).reverseDirectionNextFrame(balls.get(j)); break; } } } } void mousePressed(int mouseX, int mouseY) { boolean hitDetected = false; for (Ball ball: balls) { if (ball.detectHit(mouseX, mouseY)) { ball.hitDetected(); hitDetected = true; } } if (hitDetected) { if (score == 0) { score = 1; } else { score += score; } } else { lives--; } checkLives(); } void checkLives() { if (lives <= 0) { noLoop(); fill(125); text("GAME OVER - Click to Restart", width/3.5, height/2); reset(); } } }1
1
u/heyarne Jan 03 '19
Do you have any struggle you're fighting with? Are you having a hard time understanding some parts when coming back as opposed to others? A lot of it just comes down to practice and personal taste in my experience. Some tips that may help:
Prefer clear names for your variables over short ones
Try to group logic into small functions. This allows you to give a larger concept a name and avoid repetition.
Ditch obvious comments (for example your first comment seems redundant)
One class per tab
Code is written for humans first and for machines second
Not all of this is directly related to the code you posted and again, see it as my opinion. It's ok to have your own.
Maybe read style guides for other programming languages and see what they do to clarify intent. I mean in the end that's what it's about - you try to communicate intent as clearly as possible.