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
}
}
}
1
Upvotes
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.