The language used is called 'Processo' a weaker and simpler version of Processing used at my school to introduce Java syntax in a visual way. The overall purpose is similar to crossy road, but the cars are moving down the screen as the player avoids them. The player can only move horizontally, and the enemies only move vertically. Using if statements, once the enemy cars get below the screen that the user can see, the enemy car's y-variable is reset to a random y-coordinate between -1000 to -100 (above the screen).
At first the program runs fine, but immediately runs into a bug which causes the "Game Over" screen, despite the player not colliding with the enemies. I think the issue is with the collision conditions (the last three if statements in the gameScreen function).
size (300, 400);
double playerX = mouseX-20;
double playerY = 320;
double playerWidth = 40;
double playerHeight = 40;
double enemy1X = 10;
double enemy1Y = random(-1000, -100);
double enemy1Width = 80;
double enemy1Height = 80;
double enemy2X = 110;
double enemy2Y = random(-1000, -100);
double enemy2Width = 80;
double enemy2Height = 80;
double enemy3X = 210;
double enemy3Y = random(-1000, -100);
double enemy3Width = 80;
double enemy3Height = 80;
int gamePage = 0;
void player() {
noStroke();
fill(rgb(0, 255, 0));
rect(mouseX-20, 320, 40, 40);
}
void enemy1() {
fill(rgb(255, 0, 0));
rect(enemy1X, enemy1Y, enemy1Width, enemy1Height);
}
void enemy2() {
rect(enemy2X, enemy2Y, enemy2Width, enemy2Height);
}
void enemy3() {
rect(enemy3X, enemy3Y, enemy3Width, enemy3Height);
}
void initalScreen() {
background(rgb(0, 0, 0));
text("Click to start", width/2, height/2);
}
void gameScreen() {
background(rgb(255, 255, 255));
if(enemy1Y >= 400) {
enemy1Y = random(-1000, -100);
} else {
enemy1Y += 5;
}
if(enemy2Y >= 400) {
enemy2Y = random(-1000, -100);
} else {
enemy2Y += 5;
}
if(enemy3Y >= 400) {
enemy3Y = random(-1000, -100);
} else {
enemy3Y += 5;
}
//collision
if (playerX + playerWidth > enemy1X && playerX < enemy1X + enemy1Width && playerY + playerHeight > enemy1Y && playerY < enemy1Y + enemy1Height) {
gamePage = 2;
}
if (playerX + playerWidth > enemy2X && playerX < enemy2X + enemy2Width && playerY + playerHeight > enemy2Y && playerY < enemy2Y + enemy2Height) {
gamePage = 2;
}
if (playerX + playerWidth > enemy3X && playerX < enemy3X + enemy3Width && playerY + playerHeight > enemy3Y && playerY < enemy3Y + enemy3Height) {
gamePage = 2;
}
player();
enemy1();
enemy2();
enemy3();
}
void gameOverScreen() {
background(rgb(0, 0, 0));
textSize(30);
text("Game Over", width/2, height/2);
}
void startGame() {
gamePage = 1;
}
void mousePressed() {
if(gamePage == 0) {
startGame();
}
}
void draw() {
background(rgb(226, 225, 225));
if (gamePage == 0) {
initalScreen();
} else if (gamePage == 1) {
gameScreen();
} else if (gamePage == 2) {
gameOverScreen();
}
}
So the issue is
double playerX = mouseX-20;
is not being updated with the loop instead your actually just calling it once making the program think its still in the same place. You can fix this by moving the code above into the gameScreen() method and that will update its location the next time you move your player. So it should look something like this.
void gameScreen() {
background(rgb(255, 255, 255));
double playerX = mouseX-20; // Moved Variable
if(enemy1Y >= 400) {
enemy1Y = random(-1000, -100);
} else {
enemy1Y += 5;
}
if(enemy2Y >= 400) {
enemy2Y = random(-1000, -100);
} else {
enemy2Y += 5;
}
if(enemy3Y >= 400) {
enemy3Y = random(-1000, -100);
} else {
enemy3Y += 5;
}
//collision
if (playerX + playerWidth > enemy1X && playerX < enemy1X + enemy1Width && playerY + playerHeight > enemy1Y && playerY < enemy1Y + enemy1Height) {
gamePage = 2;
}
if (playerX + playerWidth > enemy2X && playerX < enemy2X + enemy2Width && playerY + playerHeight > enemy2Y && playerY < enemy2Y + enemy2Height) {
gamePage = 2;
}
if (playerX + playerWidth > enemy3X && playerX < enemy3X + enemy3Width && playerY + playerHeight > enemy3Y && playerY < enemy3Y + enemy3Height) {
gamePage = 2;
}
player();
enemy1();
enemy2();
enemy3();
}
Then Just remove the variable from the top so you don't have two of the same variable. Hope this helps!
Related
Im working on a project in processing, and I'm having some difficulty with this super annoying bug. All the code is pasted below, 4 class files.
Whenever the "Simulation speed" slider is moved up and then moved back down, the simulation will stop giving no errors. Same thing with the "Auto" button. I have no idea why this is occurring and I've gone through my code following the variables and I don't see anything wrong.
Help!
noisedemo file
int tableHeight = 20;
int tableWidth = 20;
float prismWidth = 10;
float tableTopx = 260;
float tableTopy = 200;
int k = 0;
int simulationSpeed;
int frameCounter;
Prism[][] prisms = new Prism[tableWidth][tableHeight];
//Declare the sliders
Slider simulationSpeedSlider = new Slider(10, 20, 100, 30, "Simulation Speed");
Slider noiseOctaveSlider = new Slider(10, 50, 100, 4, "Noise Ocatave");
Slider noiseScaleSlider = new Slider(10, 80, 100, 10, 10, "Noise Scale", true);
//Declare the buttons
Button autoScrollButton = new Button(400, 100, "Auto");
void setup(){
size(500, 500);
smooth();
pixelDensity(2);
frameRate(30);
noiseDetail((int)noiseOctaveSlider.sliderValue, noiseScaleSlider.sliderValue);
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
prisms[i][j] = new Prism(noise(i, j)*30+10, prismWidth);
prisms[i][j].setHeight(noise(i, j)*30+10);
}
}
}
float[] convertCoordinate(int xcoord, int ycoord, float xtop, float ytop, float pwidth){
float[] coord = new float[2];
coord[0] = (xtop + xcoord*cos(radians(40))*prismWidth - ycoord * cos(radians(40))*prismWidth)*0.95;
coord[1] = (ytop + ycoord*sin(radians(40))*prismWidth + xcoord * sin(radians(40))*prismWidth)*0.95;
return coord;
}
void updateHeight(){
for(int i = 0; i < tableWidth; i++){
for(int j = 0; j < tableHeight; j++){
prisms[i][j].drawPrism(convertCoordinate(i, j, tableTopx, tableTopy, prismWidth)[0], convertCoordinate(i, j, tableTopx, tableTopy, prismWidth)[1]);
}
}
}
void setHeight(){
for(int i = 0; i < tableWidth; i++){
for(int j = 0; j < tableHeight; j++){
prisms[i][j].setHeight(noise(i+k, j+k)*30+10);
}
}
}
void mouseClicked(){
if(autoScrollButton.isInRange()){
autoScrollButton.buttonActive = !autoScrollButton.buttonActive;
}
}
void draw(){
background(0, 150, 0);
noiseDetail((int)noiseScaleSlider.sliderValue, noiseScaleSlider.sliderValue);
simulationSpeedSlider.drawSlider();
simulationSpeed = (int)simulationSpeedSlider.sliderValue;
noiseOctaveSlider.drawSlider();
noiseScaleSlider.drawSlider();
autoScrollButton.drawButton();
if(frameCounter == simulationSpeed && autoScrollButton.buttonActive){
k++;
frameCounter = 0;
setHeight();
}
updateHeight();
frameCounter++;
}
button file
class Button{
boolean buttonActive = true;
float buttonX;
float buttonY;
float buttonWidth = 30;
float buttonHeight = 15;
String buttonLabel;
Button(float x, float y, String label){
buttonX = x;
buttonY = y;
buttonLabel = label;
}
void drawButton(){
if(!buttonActive){
fill(100);
rect(buttonX, buttonY, buttonWidth, buttonHeight, 5);
fill(255);
text(buttonLabel, buttonX + 4, buttonY + 10);
}else{
fill(200);
rect(buttonX, buttonY, buttonWidth, buttonHeight, 5);
fill(0);
text(buttonLabel, buttonX + 4, buttonY + 10);
}
}
boolean isInRange(){
if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){
return true;
}
else{
return false;
}
}
}
prism file
class Prism{
float pheight;
float pwidth;
Prism(float tempheight, float tempwidth){
pheight = tempheight;
pwidth = tempwidth;
}
Prism(){
pheight = 10;
pwidth = 5;
}
void drawPrism(float x, float y){
noStroke();
fill(175);
quad(x, y, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - pheight);
fill(100);
quad(x, y, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - pheight);
fill(225);
quad(x, y - pheight, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - 2*sin(radians(40))*pwidth - pheight, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight);
}
void setHeight(float tempheight){
pheight = tempheight;
}
}
slider file
class Slider{
float sliderLength;
float sliderX;
float sliderY;
float sliderWidth;
float handleWidth;
float sliderValue;
float sliderPos;
int sliderTotal;
boolean handleActive;
boolean isPrecise;
String sliderLabel;
//provide x and y position, length and the max value, the least amount of info
Slider(float x, float y, float slength, int total){
sliderX = x;
sliderY = y;
sliderLength = slength;
sliderWidth = 10;
handleWidth = sliderWidth*1.5;
sliderTotal = total;
sliderPos = sliderX + sliderWidth/2;
sliderLabel = "";
}
//provide x and y position, length and the max value AND a label
Slider(float x, float y, float slength, int total, String label){
sliderX = x;
sliderY = y;
sliderLength = slength;
sliderWidth = 10;
handleWidth = sliderWidth*1.5;
sliderTotal = total;
sliderPos = sliderX + sliderWidth/2;
sliderLabel = label;
}
//provide default values AND a label AND a width AND whether its precise
Slider(float x, float y, float slength, float swidth, int total, String label, boolean p){
sliderX = x;
sliderY = y;
sliderLength = slength;
sliderWidth = swidth;
handleWidth = sliderWidth*1.5;
sliderTotal = total;
sliderPos = sliderX + sliderWidth/2;
sliderLabel = label;
isPrecise = p;
}
void drawSlider(){
fill(255);
textSize(10);
text(sliderLabel, sliderX, sliderY - 5);
text(sliderValue, sliderX + sliderLength + 7, sliderY + sliderWidth/1.1);
fill(100);
rect(sliderX-1, sliderY, sliderLength+1, sliderWidth, 5);
fill(255);
ellipse(sliderPos, sliderY + (sliderWidth/2), handleWidth, handleWidth);
if(!isPrecise){
sliderValue = (int)((sliderPos - sliderX)/(sliderLength/sliderTotal));
}else if(isPrecise){
sliderValue = (sliderPos - sliderX)/(sliderLength/sliderTotal);
}
if(mouseX >= sliderX && mouseX <= sliderX+sliderLength + sliderX && mouseY >= sliderY - (handleWidth/2) && mouseY <= sliderY + (handleWidth/2) && mousePressed){
handleActive = true;
}
if(!mousePressed){
handleActive = false;
}
if(handleActive && sliderPos <= sliderX + sliderLength && sliderPos >= sliderX){
sliderPos = mouseX;
}
if(sliderPos > sliderX + sliderLength){
sliderPos = sliderX + sliderLength;
}
if(sliderPos < sliderX){
sliderPos = sliderX + 1;
}
}
}
Thank you! Sorry if the code is a little messy :)
In your main file, this block relies on the values of those two controls:
if(frameCounter == simulationSpeed && autoScrollButton.buttonActive){
k++;
frameCounter = 0;
setHeight();
}
When that gets skipped (because you changed simulationSpeed or buttonActive), your frameCounter gets out of sync with simulationSpeed so the block continues to get skipped.
I have a game currently which has a player and some obstacles, I want to navigate this player with some commands, but the problem is that the command get executed but the rectangle does not get updated so I have to do some weird while logic with break and a global variable, because otherwise the player would jump. Is there a way to make the functions only count once and maybe loop with for and while normally? this is my code so far:
int border = 20;
int sqsize = 96;
int i = 0;
ArrayList<player> players = new ArrayList<player>();
ArrayList<obstacle> obstacles = new ArrayList<obstacle>();
void setup() {
size(1000, 1000);
players.add(new player(9, 0));
obstacles.add(new obstacle(4, 0));
obstacles.add(new obstacle(4, 1));
obstacles.add(new obstacle(4, 2));
obstacles.add(new obstacle(4, 3));
obstacles.add(new obstacle(4, 4));
obstacles.add(new obstacle(4, 5));
}
void draw() {
background(#767C7C);
for (int l = 0; l < 10; l++) {
for (int w = 0; w < 10; w++) {
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
}
}
players.get(0).draw();
for ( int i = obstacles.size()-1; i>= 0; i--) {
obstacles.get(i).draw();
}
for ( int i = obstacles.size()-1; i>= 0; i--) {
obstacles.get(i).ask();
}
while (i < 5) {
players.get(0).links();
players.get(0).unten();
break;
}
i++;
}
class player {
int x, y;
player(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void right() {
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
}
void left() {
if (x > 20) {
x = x - sqsize;
}
}
void up() {
if (y > 20+sqsize) {
y = y + sqsize;
}
}
void down() {
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
}
void destroy() {
textSize(64);
text("Game Over", 500-2*sqsize, 500-sqsize/2);
noLoop();
}
int[] request() {
int[] pos = {x, y};
return pos;
}
}
class obstacle {
int x, y;
obstacle(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void rechts() {
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
}
void links() {
if (x > 20) {
x = x - sqsize;
}
}
void oben() {
if (y > 20+sqsize) {
y = y + sqsize;
}
}
void unten() {
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
}
void ask() {
for ( int i = players.size()-1; i>= 0; i--) {
int[] check = players.get(i).request();
if (/*dist(check[0]+sqsize, check[1]+sqsize, x, y) == 0 || dist(check[0]-sqsize, check[1]-sqsize, x, y) == 0 ||*/ dist(check[0], check[1], x, y) == 0) {
players.get(i).destroy();
}
}
}
}
Is there a way also to detect which side of the player has touched the obstacle.
thank you very much (I’m a newbie).
This is how I solved my problem (I used cases):
int border = 20;
int sqsize = 96;
int n = 0;
boolean f = true;
long lastTime = 0;
int count = 0;
int[] list = new int[100];
ArrayList<Player> Players = new ArrayList<Player>();
ArrayList<Obstacle> Obstacles = new ArrayList<Obstacle>();
void setup() {
size(1000, 1000);
Players.add(new Player(9, 0));
Obstacles.add(new Obstacle(4, 0));
Obstacles.add(new Obstacle(4, 1));
Obstacles.add(new Obstacle(4, 2));
Obstacles.add(new Obstacle(2, 3));
Obstacles.add(new Obstacle(4, 4));
Obstacles.add(new Obstacle(4, 5));
lastTime = millis();
}
void draw() {
background(#767C7C);
for (int l = 0; l < 10; l++) {
for (int w = 0; w < 10; w++) {
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
}
}
/*Players.get(0).left();
Players.get(0).down();*/
if (f) {
count = 0;
list = new int[50];
player();
f = false;
}
Players.get(0).draw();
for ( int i = Obstacles.size()-1; i>= 0; i--) {
Obstacles.get(i).draw();
}
for ( int i = Obstacles.size()-1; i>= 0; i--) {
Obstacles.get(i).ask();
}
}
class Player {
int x, y;
int posCase = 0;
Player(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
while (n < count) {
if ( millis() - lastTime > 600 ) {
posCase = list[n];
println(n);
if (posCase == 5) {
noLoop();
}
switch(posCase) {
case 1 :
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
break;
case 2 :
if (x > 20) {
x = x - sqsize;
}
break;
case 3 :
if (y > 20+sqsize) {
y = y + sqsize;
}
break;
case 4 :
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
break;
}
n++;
if ( n == count) {
println("yes");
f = true;
}
lastTime = millis();
}
break;
}
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void right() {
list[count] = 1;
count++;
/*f (x < 1000-2*sqsize) {
x = x + sqsize;
redraw();
}*/
}
void left() {
list[count] = 2;
count++;
/*if (x > 20) {
x = x - sqsize;
redraw();
}*/
}
void up() {
list[count] = 3;
count++;
/*if (y > 20+sqsize) {
y = y + sqsize;
redraw();
}*/
}
void down() {
list[count] = 4;
count++;
/*if (y < 1000-2*sqsize) {
y = y + sqsize;
redraw();*/
}
void ende() {
list[count] = 5;
count++;
}
void destroy() {
textSize(64);
text("Game Over", 500-2*sqsize, 500-sqsize/2);
noLoop();
}
int[] request() {
int[] pos = {x, y};
return pos;
}
}
class Obstacle {
int x, y;
int posCase = 0;
Obstacle(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
while (n < count) {
if ( millis() - lastTime > 600 ) {
posCase = list[n];
println(n);
if (posCase == 5) {
noLoop();
}
switch(posCase) {
case 1 :
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
break;
case 2 :
if (x > 20) {
x = x - sqsize;
}
break;
case 3 :
if (y > 20+sqsize) {
y = y + sqsize;
}
break;
case 4 :
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
break;
}
n++;
if ( n == count) {
println("yes");
f = true;
}
lastTime = millis();
}
break;
}
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void right() {
list[count] = 1;
count++;
/*f (x < 1000-2*sqsize) {
x = x + sqsize;
redraw();
}*/
}
void left() {
list[count] = 2;
count++;
/*if (x > 20) {
x = x - sqsize;
redraw();
}*/
}
void up() {
list[count] = 3;
count++;
/*if (y > 20+sqsize) {
y = y + sqsize;
redraw();
}*/
}
void down() {
list[count] = 4;
count++;
/*if (y < 1000-2*sqsize) {
y = y + sqsize;
redraw();*/
}
void ende() {
list[count] = 5;
count++;
}
void ask() {
for ( int i = Players.size()-1; i>= 0; i--) {
int[] check = Players.get(i).request();
if ( dist(check[0], check[1], x, y) == 0) {
Players.get(i).destroy();
}
}
}
}
I want to make a square player game but when trying to make the square move with functions it keeps on doing only one movement. i want it so that I can pass a series of statements to make the square move I also want to add obstacles later.
oben = up
unten = down
rechts = right
links = left
this is my code:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[0], ypos[0], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
I have made a few changes that will allow you to move the square using the WASD keys. The whole code can be simplified a lot by creating a two dimensional array to hold the state of the board.
I'll stick with the original implementation though and only add the pieces that are needed to get you going. The important bits are to add the keyPressed() method (see documentation) and replace line
rect(xpos[0], ypos[0], sqsize, sqsize);
with
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
Here is the above code including the needed changes:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
void keyPressed() {
switch(key) {
case 'a':
case 'A':
links();
break;
case 'd':
case 'D':
rechts();
break;
case 'w':
case 'W':
oben();
break;
case 's':
case 'S':
unten();
break;
}
}
so, I've spent a fair few hours on my Mario-like game but recently I've been stuck on the collisions after I changed to an object based collision system that seems like it should work but there is a bug somewhere that i cant find.
currently the box that represents the player just falls straight through the platform rather than sitting on the platform
You wont be able to run this snippet because I'm new to stack overflow and couldn't find anything closer to processing.
Here is the box class that has all my collision detection
//Box class -- THIS IS WHERE I NEED HELP -- everything else is just for context
//Box class
class Box {
private float xpos, ypos, sizeX, sizeY;
private float bottom = ypos + sizeY;
private float top = ypos;
private float left = xpos;
private float right = xpos + sizeX;
Box(float startX, float startY, float xSize, float ySize) {
boxes++;
xpos = startX;
ypos = startY;
sizeX = xSize;
sizeY = ySize;
}
void update() {
rect(xpos, ypos, sizeX, sizeY);
}
void collision() {
//If on box level
if (player.playerY + player.playerHeight >= top && player.playerY <= bottom) {
//Left Side
if ((player.playerX + player.playerWidth) <= left && (player.playerX + player.playerWidth) + speed >= left) {
player.playerX = left - player.playerWidth;
player.canMoveRight = false;
}
//Right Side
if (player.playerX >= right && player.playerX + speed <= right) {
player.playerX = right;
player.canMoveLeft = false;
}
}
//If in box Y coord
if (player.playerX + player.playerWidth >= left && player.playerX <= right) {
//top
if (player.playerY + player.playerHeight <= top && player.playerY + player.yVelocity + gravity >= top) {
player.Gravity = false;
player.playerY = top - player.playerHeight;
player.yVelocity = 0;
player.canMoveDown = false;
}
//bottom
if (player.playerY + player.yVelocity >= bottom && player.playerY + player.yVelocity + gravity <= bottom) {
player.canMoveUp = false;
player.yVelocity = 0;
player.playerY = bottom;
}
}
//onGround
if (player.playerY == ypos - player.playerHeight) {
player.onGround = true;
}
}
}
Control structure for multiple keypresses
//Control structure to enable multiple keypresses
//Control
float speed = 4;
float gravity = 1;
float jump = 10;
boolean isUp, isLeft, isDown, isRight;
void control() {
//Controls
if (isUp) {
if (player.canMoveUp) {
if (player.onGround) {
player.Gravity = true;
player.yVelocity = 0 + jump;
player.onGround = false;
}
}
}
if (isDown) {
if (player.canMoveDown) {
}
}
if (isRight) {
if (player.canMoveRight) {
player.playerX += speed;
}
}
if (isLeft) {
if (player.canMoveLeft) {
player.playerX -= speed;
}
}
if (player.onGround) {
player.Gravity = false;
player.yVelocity = 0;
}
}
void keyPressed() {
setMove(keyCode, true);
}
void keyReleased() {
setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {
switch (k) {
case UP:
return isUp = b;
case DOWN:
return isDown = b;
case LEFT:
return isLeft = b;
case RIGHT:
return isRight = b;
default:
return b;
}
}
Main tab, box/entity updates and player class
//Main tab
float boxes = 0;
//Calls classes
Box b = new Box(0, 700, 800, 100);
Box b1 = new Box(300, 675, 25, 25);
Player player = new Player(50, 400, 20, 20);
void setup() {
size(800, 800);
frameRate(60);
}
void draw() {
background(255, 255, 255);
boxesUpdate();
boxesCollision();
entityUpdate();
control();
println(player.playerX + ", " + player.playerY);
println(player.yVelocity + ", " + player.Gravity + ", " + player.onGround + ", " + player.canMoveDown);
}
//Box collision update method for when i add more boxes
void boxesCollision() {
player.Gravity = true;
player.canMoveUp = true;
player.canMoveDown = true;
player.canMoveLeft = true;
player.canMoveRight = true;
player.onGround = false;
b.collision();
b1.collision();
if (player.Gravity) {
player.playerY += player.yVelocity;
player.yVelocity += gravity;
}
}
//Along with another update method for the other parts of the boxes
void boxesUpdate() {
b.update();
b1.update();
}
//Entity update method to update players and for when i add NPCs and enemies
void entityUpdate() {
player.update();
}
//Player class
class Player {
private float playerX, playerY, playerWidth, playerHeight;
private float yVelocity = 0;
private float xVelocity = 0;
private boolean canMoveRight, canMoveLeft, canMoveUp, canMoveDown;
private boolean onGround, Gravity;
Player(float X, float Y, float xSize, float ySize) {
playerWidth = xSize;
playerHeight = ySize;
playerX = X;
playerY = Y;
}
void update() {
rect(playerX, playerY, playerWidth, playerHeight);
}
}
I am making a game in Eclipse Mars using the Processing library. I had made the game elsewhere and it ran fine. There were no errors after I copied and pasted the files from my flash drive to the folder in Eclipse. When I tried to run it, it said "The selection cannot be launched, and there are no recent launches." There were no recent launches because I had just gotten Eclipse. My code is as follows:
Main Class:
//package dogeball;
import processing.core.PApplet;
import processing.core.PImage;
import java.awt.Color;
import processing.core.PFont;
public class Dogeball extends PApplet {
Ball ball;
Furniture butterChair;
Furniture[] bricks;
PFont dogefont;
float py;
float score;
boolean game;
int checker;
boolean mode;
PImage img = loadImage("doge.jpg");
PImage img2 = loadImage("doge2.png");
public void setup() {
checker = 0;
size(300,250);
game = false;
mode = true;
ball = new Ball(this, 100, 225, 0, 0, 10, Color.DARK_GRAY );
butterChair = new Furniture(this, 130, 238, 40, 10, Color.YELLOW);
py = butterChair.w /2;
bricks = new Furniture[56];
dogefont = loadFont("ComicSansMS-48.vlw");
for(int rowNum = 0; rowNum<8; rowNum+= 1) {
for(int colNum = 0; colNum<7; colNum += 1){
bricks[7*rowNum + colNum] = new Furniture(this, 10+40*colNum, 10+15*rowNum, 40, 15, Color.red);
score = 0;
}
}
}
public void draw() {
if(game == false) {
background(img);
fill(0,255,255);
textSize(30);
textFont(dogefont);
text("DogeBall",33, 170);
fill(255,255,0);
textSize(20);
text("Press Space", 120,190);
fill(255,0,0);
text("Such BrickBreaker", 20, 20);
fill(0,0,255);
text("Much Atari", 190, 80);
fill(0,255,0);
text("How Breakout", 150, 230);
}
if(keyPressed == true) {
if (key == ' ') {
game = true;
}
}
if(game == true) {
if(keyPressed == true) {
if (key == 'm') {
mode = !mode;
}
}
//checker = 0;
background(img);
ball.appear();
ball.hover();
butterChair.appear();
if(mode == true) {
butterChair.x = mouseX-butterChair.w/2;
}
if(mode == false) {
if(keyPressed == true) {
if (key == CODED) {
if (keyCode == LEFT){
butterChair.x -= 3;
}
}
}
if(keyPressed == true) {
if (key == CODED) {
if (keyCode == RIGHT){
butterChair.x += 3;
}
}
}
}
if(butterChair.x <= 0) {
butterChair.x = 0;
}
if(butterChair.x >= width - butterChair.w) {
butterChair.x = width - butterChair.w;
}
textFont(dogefont);
fill(255,0,255);
text("Much Doge", 12, 160);
fill(255,0,0);
textSize(20);
text("M to toggle mouse mode.", 20,200);
fill(0);
textSize(10);
text("You might have to press twice", 10,220);
fill(0,0,255);
textSize(20);
text("Press S to Start", 150, 230);
if (keyPressed == true) {
if (key == 's' || key == 'S'){
ball.vy = 2;
ball.vx = 1;
}
}
/*if(mousePressed == true) {
ball.vx = 0;
ball.vy = 0;
}*/
for(int i = 0; i<56; i+= 1) {
bricks[i].appear();
}
}
detectCollision();
if(ball.y >= height) {
checker = 0;
if(checker ==0){
background(img);
ball.vx = 0;
ball.vy = 0;
textSize(30);
fill(255,0,0);
game = false;
text("Such Sorry", 130, 160);
fill(0,255,255);
text("Much Game Over", 20, 215);
fill(255,255,0);
text("So Losing", 10, 30);
textSize(20);
text("Press P to Play Again", 20, 245);
}
if(keyPressed == true) {
if(key == 'p') {
game = true;
ball.x = 100;
ball.y = 225;
checker = 1;
for(int rowNum = 0; rowNum<8; rowNum+= 1) {
for(int colNum = 0; colNum<7; colNum += 1){
bricks[7*rowNum + colNum] = new Furniture(this, 10+40*colNum, 10+15*rowNum, 40, 15, Color.red);
score = 0;
}
}
}
}
}
}
void detectCollision() {
if(keyPressed == true) {
if(key == '-')
{
for(int cCode = 0; cCode < 56; cCode += 1) {
Furniture b = bricks[cCode];
b.x = width * 2;
b.y = height * 2;
score = 56;
}
}}
if(ball.x >= butterChair.x &&
ball.x <= butterChair.x + butterChair.w &&
ball.y + ball.s /2 > butterChair.y) {
ball.vy *= -1;
}
for(int i = 0; i<bricks.length; i+= 1) {
Furniture b = bricks[i];
if(ball.x >= b.x && ball.x <= b.x+b.w && ball.y-ball.s/2 <= b.y) {
b.y = height * 2;
b.x = width * 2;
ball.vy *= -1;
score += 1;
}
if(score == 56){
background(img);
ball.vx = 0;
ball.vy = 0;
fill(255,0,0);
textSize(20);
text("Such Winning!", 20, 20);
textSize(40);
fill(0,255,0);
text("Much Congrats!",12 ,160);
textSize(20);
fill(255,0,255);
text("Press P to Play Again", 20, 245);
if(keyPressed == true) {
if(key == 'p') {
game = true;
ball.x = 100;
ball.y = 225;
checker = 1;
for(int rowNum = 0; rowNum<8; rowNum+= 1) {
for(int colNum = 0; colNum<7; colNum += 1){
bricks[7*rowNum + colNum] = new Furniture(this, 10+40*colNum, 10+15*rowNum, 40, 15, Color.red);
score = 0;
}
}
}
}
}
}
}
static public void main(String args[]) {
PApplet.main("Dogeball");
}
}
Ball Class:
//package dogeball;
import java.awt.Color;
import processing.core.PApplet;
public class Ball extends PApplet {
float x;
float y;
float vx;
float vy;
float s;
Color c;
PApplet p;
Ball(PApplet pApp, float xLocation, float yLocation, float xSpeed, float ySpeed, float size, Color shade){
x = xLocation;
y = yLocation;
vx = xSpeed;
vy = ySpeed;
s = size;
c = shade;
p = pApp;
}
void hover() {
x += vx;
y += vy;
if(x< 0 || x> p.width) {
vx *= -1;
}
if(y< 0) {
vy *= -1;
}
}
void appear() {
p.fill(c.getRGB() );
p.ellipse(x,y,s,s);
}
}
Paddle Class:
//package dogeball;
import java.awt.Color;
import processing.core.PApplet;
public class Furniture extends PApplet {
float x;
float y;
float w;
float h;
Color c;
PApplet p;
Furniture(PApplet PApp, float locationX, float locationY, float fWidth, float fHeight, Color shade) {
x = locationX;
y = locationY;
w = fWidth;
h = fHeight;
c = shade;
p = PApp;
}
void appear() {
p.fill(c.getRGB());
p.rect(x,y,w,h);
}
}
You have probably the wrong project selected on the projects tree or the run configurations are set to another project, since you haven't run it yet.
Either way, you have to right click your projects folder on the projects tree, then find Run As > Java Applet.
Another way to do it would be adding a main function, as you already did, and run is as a Java Application. Instead of using the current main function, you can try to use the code below to see it in present mode and see if it works:
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "Dogeball" });
}