We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a game that is supposed to go to different screens when certain commands are issued. For instance, after winning "playstate2", I'm supposed to go to the homescreen so I could restart the game if I want to. However, the code sends me to a different screen and I cannot start another game. I don't understand why but I think it's an encapsulation problem but I'm not sure. Can anyone help?
//First declare objects used for this session
// Level 1 will have 2 blockers
// Level 2 will have 3 blockers w/ 1 going faster.
Blocker obstacleBlocktopdown;
Blocker obstacleBlockacross;
Blocker obstacleBlocklvl2_rocket;
Blocker obstacleBlocklvl2_uno;
Blocker obstacleBlocklvl2_dos;
Avatar playerAvatar;
Avatar playerAvatar2;
Seeker seekerStone1;
Seeker seekerStone2;
GoalState goal;
GoalState goal2;
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
boolean winstate = false;
boolean winstate2 = false;
boolean playstate = false;
boolean playstate2 = false;
boolean homescreen = true;
boolean tutorialscreen = false;
boolean losestate = false;
boolean losestate2 = false;
boolean pausestate = false;
boolean pausestate2 = false;
int wincount = 0;
int losscount = 0;
void setup(){
size(950, 950);
//////objects for playstate
obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 1.2);
obstacleBlockacross = new Blocker(1, height/2, 20, 20, 2, .4);
playerAvatar = new Avatar(11, 11, 18, 18);
seekerStone1 = new Seeker(1100, 600, 18, 18);
goal = new GoalState(790, 500);
//////objects for playstate2
playerAvatar2 = new Avatar (425, 20, 18, 18);
seekerStone2 = new Seeker(425, 930, 18, 18);
obstacleBlocklvl2_uno = new Blocker(10, 10, 20, 20, .7, 1.2);
obstacleBlocklvl2_dos = new Blocker(940, 940, 20, 20, .5, -1.3);
obstacleBlocklvl2_rocket = new Blocker ( 2, height/3, 15, 15, 1.7, 1.5);
goal2 = new GoalState(100, 20);
}
void draw(){
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////HOMESCREEN
if (homescreen == true){
pausestate = false;
background((mouseX+150)/4, (mouseY+150)/4, 0);
//image(titlecard, 200, 400, 500, 200);
stroke(0);
fill((mouseY)/5, 0, (mouseX)/5);
textSize(60);
text("Goal Seeker", 200, height/4);
stroke(0);
fill((mouseY -50)/3, 0, (mouseX-50)/3);
textSize(24);
text("Press Spacebar to begin", 200, height - (height/4));
textSize(14);
text("Press T for a tutorial", 775, height - (height/4));
if(key == ' ') {
homescreen = false;
playstate = true;
playstate2 = false;
}
if(key == 't') {
homescreen = false;
tutorialscreen = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////TUTORIAL SCREEN
if (tutorialscreen == true){
background(255);
stroke(0);
fill(0);
textSize(24);
text("How to play Goal Seeker", 30, 100);
textSize(20);
text("Components", 55, 150);
textSize(18);
text("Avatar - The Blue Ellipse", 70, 180);
text("Seeker - The Green Ellipse", 70, 198);
text("Blockers - The Red Ellipse", 70, 216);
text("Goal - The Grey Ellipse", 70, 234);
textSize(20);
text("GamePlay", 55, 270);
textSize(18);
text("*The Goal of the Game is to seek out the Grey Goal point on the screen.", 75, 300);
text("*You, the player, control the Avatar with the directional buttons.", 75, 320);
text("*When you move the Avatar, the Seeker moves as well but in a different way that you don't control.", 75, 340);
text("*So you must be aware of how you are moving the Avatar and how the Seeker moves in response to ", 75, 360);
text("your moves.", 75, 380);
text("*When the Seeker reaches the Goal, the game is won and a win is registered on the scoresheet.", 75, 400);
text("*If however, the Seeker OR the Avatar hits ANY of the Blockers, the game is lost", 75, 420);
text("and a loss is registered on the scoresheet.", 75, 440);
text("*If at anytime you need to stop the game, you can PAUSE the game by pressing 'P'.", 75, 470);
text("*Good Luck!", 100, 700);
text("Press Q to go back to Title Screen", 200, 800);
text("Or Press spacebar to start the Game!!", 200, 850);
if (key == 'q') {
tutorialscreen = false;
homescreen = true;
}
if (key == ' '){
tutorialscreen = false;
playstate = true;
playstate2 = false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LOSE STATE
if (losestate == true) {
background(0);
stroke(255);
fill(255);
textSize(56);
text("You Lost!!!", 200, height/2);
textSize(24);
text("Number of games won... " + wincount, 400, 600);
text("Number of games lost... " + losscount, 400, 675);
textSize(24);
text("Press spacebar to try again", 200, 750);
text("Or press Q to quit", 200, 900);
if (key == ' ') {
losestate = false;
playstate = true;
}
if (key == 'q') {
losestate = false;
homescreen = true;
}
}
//////////////////////////////////////////////////////////////////////////LOSE STATE 2 BEGINS
if (losestate2 == true) {
background(0);
stroke(255);
fill(255);
textSize(56);
text("You Lost!!!", 200, height/2);
textSize(24);
text("Number of games won... " + wincount, 400, 600);
text("Number of games lost... " + losscount, 400, 675);
textSize(24);
text("Press spacebar to try again", 200, 750);
text("Or press Q to quit", 200, 900);
if (key == ' ') {
losestate2 = false;
playstate2 = true;
}
if (key == 'q') {
losestate2 = false;
homescreen = true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PAUSE STATE
if (pausestate == true) {
background(0);
playerAvatar.display();
seekerStone1.display();
obstacleBlocktopdown.display();
obstacleBlockacross.display();
textSize(48);
text("PAUSE", 300, 300);
textSize(36);
text("Press R to resume Game", 100, 700);
text("Press Q to Quit and go back to Title Screen", 100, 800);
if (key == 'r' || key == 'R'){
pausestate = false;
playstate = true;
}
if (key == 'q' || key == 'Q'){
pausestate = false;
homescreen = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////ACTIVATES PLAY STATE 1
if (playstate == true) {
playerAvatar.constrainToScreen();
if (upPressed) {// playerAvatar moves up 1 pixel
playerAvatar.movesup();
}
if (downPressed) {//playerAvatar moves down 1 pixel
playerAvatar.movesdown();
}
if (leftPressed) {//playerAvatar moves to the left 1 pixel
playerAvatar.movesleft();
}
if (rightPressed) {//playerAvatar moves to the right 1 pixel
playerAvatar.movesright();
}
if (key == 'p' || key == 'P') {
playstate = false;
pausestate = true;
}
background(0);
goal.goalcheck();
obstacleBlocktopdown.run();
obstacleBlockacross.run();
strokeWeight(1);
stroke(255);
fill(255);
ellipse(goal.x, goal.y, 46, 46);
strokeWeight(1);
stroke(255);
fill(16, 49, 188);
textSize(14);
text("GOAL", goal.x-20, goal.y-6);
goal.display();
playerAvatar.display();
seekerStone1.synch2avatar();
}
///////////////////////////////////////////////////////////////////////////////////ACTIVATES PLAYSTATE 2
if (playstate2 == true) {
playerAvatar2.constrainToScreen();
if (upPressed) {// playerAvatar moves up 1 pixel
playerAvatar2.movesup2();
}
if (downPressed) {//playerAvatar moves down 1 pixel
playerAvatar2.movesdown2();
}
if (leftPressed) {//playerAvatar moves to the left 1 pixel
playerAvatar2.movesleft2();
}
if (rightPressed) {//playerAvatar moves to the right 1 pixel
playerAvatar2.movesright2();
}
if (key == 'p' || key == 'P') {
playstate2 = false;
pausestate2 = true;
}
background(0);
strokeWeight(1);
stroke(255);
fill(255);
ellipse(goal2.x, goal2.y, 46, 46);
strokeWeight(1);
stroke(255);
fill(16, 49, 188);
textSize(14);
text("GOAL", goal2.x-20, goal2.y-6);
goal2.display();
goal2.goalcheck2();
obstacleBlocklvl2_uno.run2();
obstacleBlocklvl2_dos.run2();
obstacleBlocklvl2_rocket.run2();
obstacleBlocklvl2_uno.bounce();
obstacleBlocklvl2_dos.bounce();
obstacleBlocklvl2_rocket.bounce();
playerAvatar2.display();
seekerStone2.synch2avatar2();
}
/////////////////////////////////////////////////////////PAUSE STATE 2
if (pausestate2 == true) {
background(0);
playerAvatar2.display();
seekerStone2.display();
obstacleBlocklvl2_uno.display();
obstacleBlocklvl2_dos.display();
obstacleBlocklvl2_rocket.display();
textSize(48);
text("PAUSE", 300, 300);
textSize(36);
text("Press E to resume Game", 100, 700);
text("Press Q to Quit and go back to Title Screen", 100, 800);
}
if (key == 'e' || key == 'E'){
pausestate2 = false;
playstate2 = true;
}
if (key == 'q' || key == 'Q'){
pausestate2 = false;
homescreen = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////CREATE WIN SCENARIO
if (winstate == true) {
background (0);
textSize(36);
text("You Win", 100, 100);
text("Seeker has reached Goal!", 100, 200);
textSize(24);
text("Number of games won... " + wincount, 100, 600);
text("Number of games lost... " + losscount, 100, 700);
textSize(14);
text("Press spacebar to go to the next level", 100, 800);
text("Or press 'q' to quit and go back to the homescreen", 100, 875);
if (key == ' ') {
winstate = false;
playstate2 = true;
playstate = false;
}
if (key == 'q' || key == 'Q') {
winstate = false;
homescreen = true;
}
}
//////////////////////////////////////////////////////////////WINSTATE 2
if (winstate2 == true) {
background (0);
textSize(36);
text("You Win", 100, 100);
text("Seeker has reached Goal!", 100, 200);
text("You have finished Goal Seeker!!", 100, 300);
textSize(24);
text("Number of games won... " + wincount, 100, 600);
text("Number of games lost... " + losscount, 100, 700);
textSize(14);
text("Press h to return to the homescreen", 100, 900);
if (key == 'h' || key == 'H') {
winstate2 = false;
homescreen = true;
}
}
}
//enacts the movement of desired object
void keyPressed (KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = true;
}
else if (keyCode == DOWN) {
downPressed = true;
}
else if (keyCode == LEFT) {
leftPressed = true;
}
else if (keyCode == RIGHT) {
rightPressed = true;
}
}
}
// stops movement once key(s) are released
void keyReleased(KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = false;
}
else if (keyCode == DOWN) {
downPressed = false;
}
else if (keyCode == LEFT) {
leftPressed = false;
}
else if (keyCode == RIGHT) {
rightPressed = false;
}
}
}
class Avatar {
float x;
float y;
float a;
float b;
static final color avatarColor = #537DF3, outline = 0;
//constructors
Avatar(){
}
Avatar (float _x, float _y, float _a, float _b) {
x = _x;
y = _y;
a = _a;
b = _b;
}
//Functions
void display () {
//avatarColor = (83, 125, 243);
strokeWeight(1);
stroke(outline);
fill(avatarColor);
ellipse(x, y, a, b);
}
void movesup(){// MAY NEED IF ELSE STATEMENTS
constrain(y, 10, height-10);
y = y - 2;
playerAvatar.display();
}
void movesdown() {
constrain(y, 10, height-10);
y = y + 2;
playerAvatar.display();
}
void movesleft() {
constrain(x, 10, width-10);
x = x - 2;
playerAvatar.display();
}
void movesright() {
constrain(x, 10, width-10);
x = x + 2;
playerAvatar.display();
}
void movesup2(){// MAY NEED IF ELSE STATEMENTS
constrain(y, 10, height-10);
y = y - 2;
playerAvatar2.display();
}
void movesdown2() {
constrain(y, 10, height-10);
y = y + 2;
playerAvatar2.display();
}
void movesleft2() {
constrain(x, 10, width-10);
x = x - 2;
playerAvatar2.display();
}
void movesright2() {
constrain(x, 10, width-10);
x = x + 2;
playerAvatar2.display();
}
void constrainToScreen() {
x = constrain(x, a/2.0, width - a/2.0);
y = constrain(y, b/2.0, height - b/2.0);
}
}
Answers
Code continued.
Last of the Code.
I haven't run the code but can see a large number of Boolean variables intended to track state and lots of if conditions testing against these. The problem with that is unless you're very careful multiple conditions can be true at the same time, leading to the unexpected results you describe.
If only one state should be available at any one time you would have to reset the value of all other states to false when you set another to true. That's really inefficient. Instead have a single int variable and change it's value and test against this in draw. You can use named constants to make the code more readable...