Need help coding a little game (very unexperienced)
in
Programming Questions
•
9 months ago
Okay I want to learn Processing and Java in general and therefore pushed myself into coding a little game. I have a few problems, that I don't know a fix for and I would be very thankful if someone could help me. I'm very unexperienced though and therefore it would be nice if you could explain what you did and use the easiest way possible. Thanks for any help.
Code:
float x;
float y;
float a;
float b;
int px;
int score;
float addlimit;
int start;
float multi;
PFont arial;
float speedlimit;
String gamemode;
int status;
int crazy;
float rand;
void setup(){
size(600,600);
smooth();
x = random(10,590);
y = random(10,100);
a = 2;
b = 3;
px = 0;
multi = 1.25;
addlimit = 10;
score = 0;
arial = createFont("Arial",32);
textFont(arial,15);
speedlimit = 10;
start = 1;
crazy = 0;
status = 0;
}
void draw(){
if (start == 1){
background(0);
textFont(arial,15);
text("Commands: 'R' - Resets the game",100,100);
text("'S' - Pauses the game",189,115);
text("'P' - Unpauses the game",189,130);
text("'N' or '1' - Sets the gamemode to Normal",189,145);
text("'U' or '2' - Sets the gamemode to Ultra",189,160);
text("'X' or '3' - Sets the gamemode to Random",189,175);
text("'C' or '4' - Sets the gamemode to Crazy",189,190);
text("'H' or '5' - Sets the gamemode to Hardcore",189,205);
text("'M' - Returns to this menu",189,220);
textFont(arial,25);
text("Press 'P' or Leftclick to start the game",85,500);
start = 0;
noLoop();
}
else{
background(0);
if (status == 0){
gamemode = "Normal";
}
if (status == 1){
gamemode = "Ultra";
}
if (status == 2){
gamemode = "Random";
}
if (status == 3){
gamemode = "Crazy";
}
if (status == 4){
gamemode = "Hardcore";
}
textFont(arial,15);
text("Score: "+score,50,50);
text("Speed: "+str(abs(a))+"/"+str(abs(b)),50,75);
text("Gamemode: "+gamemode,50,100);
a = a*10;
a = round(a);
a = a/10;
b = b*10;
b = round(b);
b = b/10;
fill(125,125,255);
ellipse(x,y,10,10);
x = x + a;
y = y + b;
if (x > (width-5)){
a = a*(-1);
}
if (x < 5){
a = a*(-1);
}
if (y > (height-5)){
x = random(10,590);
y = random(10,100);
score = score -5;
}
if (y < 5){
b = b*(-1);
}
fill(255,125,125);
if (mouseX < width && mouseX > 0){
px = mouseX;
}
if (mouseX > width){
px = width;
}
if (mouseX < 0){
px = 0;
}
rand = round(random(750));
if (crazy == 1){
if (rand == 1){
a = a*(-1);
}
if (rand == 2){
b = b*(-1);
}
}
rectMode(CENTER);
rect(px,500,50,10);
if (x < (px + 25) && x > (px - 25) && y > 495 && y < 505){
b = b*(-1);
score = score + 1;
}
if (score == speedlimit){
a = a*multi;
b = b*multi;
speedlimit = speedlimit + addlimit;
}
if (score == 50){
textFont(arial,50);
text("YOU WIN!",height/2 -110,width/2-25);
textFont(arial,35);
text("Left click to continue in Free Mode",35,width -20);
noLoop();
}
if (score < 0){
score = 0;
}
}
}
void mousePressed() {
loop();
if (score == 50){
score = score + 1;
}
}
void keyPressed(){
if (key == 'r' || key == 'n' || key == '1'){
crazy = 0;
x = random(10,590);
y = random(10,100);
a = 2;
b = 3;
px = 0;
multi = 1.25;
addlimit = 10;
score = 0;
speedlimit = 10;
status = 0;
}
if (key == 'u' || key == '2'){
crazy = 0;
x = random(10,590);
y = random(10,100);
a = 2;
b = 3;
score = 0;
addlimit = 5;
multi = 1.25;
speedlimit = 5;
status = 1;
}
if (key == 's'){
noLoop();
}
if (key == 'p'){
loop();
}
if (key == 'x' || key == '3'){
crazy = 0;
score = 0;
speedlimit = random(20);
multi = (random(10,15)/10);
addlimit = random(20);
x = random(10,590);
y = random(10,100);
a = random(1,6);
b = random(1,6);
status = 2;
}
if (key == 'm'){
start = 1;
}
if (key == 'c' || key == '4'){
x = random(10,590);
y = random(10,100);
a = 2;
b = 3;
multi = 1.15;
addlimit = 10;
score = 0;
speedlimit = 10;
crazy = 1;
status = 3;
}
if (key == 'h' || key == '5'){
x = random(10,590);
y = random(10,100);
a = 2;
b = 3;
addlimit = 5;
multi = 1.35;
speedlimit = 5;
score = 0;
crazy = 1;
status = 4;
}
}
Description:
This program is as many of you probably know called the ball pad game. The ball is bouncing of the pad and all walls, but the one under the pad, that only moves on the x-axis. There is a score system involved that adds one if the ball hits the pad and substracts one if the ball hits the wall it doesn't bounce off of. I created a few gamemode to challenge myself and the player since the original game may get boring.
Problems:
1. If you play and you lose points the speed keeps being the same. I have not a clue how to fix that problem although I tried different stuff.
2. When the ball hits the side of the Pad there is a chance that the Ball keeps bouncing of the inside of the Pad without getting out. I know that you could just inverse the speed of the ball and it would bounce off, but that would result in the ball always bouncing back and not off, which is not wanted.
Thanks for any kind of help!
Sincerely SihingSin
1