We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Game start screen problems.
Page Index Toggle Pages: 1
Game start screen problems. (Read 844 times)
Game start screen problems.
Apr 20th, 2010, 2:10pm
 
So, now I've got a different problem. I'm trying to make a start screen that runs the game (eventually on different levels) once you click the level button you want. Right now, it runs the game, but only as long as I'm inside the buttons' boundaries. Thoughts? (There are also objects in this project, but since there's a character limit, I can't post those.)

boolean levelEasy;
boolean levelHard;

import ddf.minim.*;
AudioPlayer song;
AudioPlayer eat;
Minim minim;

PFont font;
PImage startScreen;
PImage clouds;
PImage normalBo;
//Added image for Happy Bo!
PImage happyBo;
PImage honey;
PImage bee;

//Array for three bees to move horizontally.
Bee[] beesX= new Bee[3];
//Array for three bees to move vertically.
Bee[] beesY= new Bee[3];

//Array for three honey pots to move horizontally.
Honey[] honeyX= new Honey[2];
//Array for three honey pots to move vertically.
Honey[] honeyY= new Honey[2];

Bear bear;

void setup(){
 size(500,400);
  font=loadFont("ComicSansMS-Bold-48.vlw");
 //noCursor();
 minim = new Minim(this);
 //Load song
 eat=minim.loadFile("BLOOPF05.aiff");
 song = minim.loadFile("62 - First Festival of Stars.mp3");
 song.play();
 song.loop(5);

 smooth();
 //Load clouds image.
 clouds=loadImage("Game_bg.jpg");
 //Load bear image.
 normalBo=loadImage("Bear_icon_1.gif");
 happyBo=loadImage("Bear_icon_2.gif");
 //Load honey pot image.
 honey=loadImage("Honey_icon.gif");
 //Load bee image.
 bee=loadImage("Bee_icon - Copy.gif");
 //Load start screen image.
startScreen=loadImage("Game_bg.jpg");

 //Initialize horizontal bees.
 for(int i=0; i<beesX.length; i++){
   beesX[i]=new Bee();
 }

 //Initialize vertical bees.
 for(int i=0; i<beesY.length; i++){
   beesY[i]=new Bee();
 }

 //Initialize horizontal honey pots.
 for(int i=0; i<honeyX.length; i++){
   honeyX[i]=new Honey();
 }

 //Initialize vertical honey pots.
 for(int i=0; i<honeyY.length; i++){
   honeyY[i]=new Honey();
 }

 //Create bear
 bear= new Bear(40);
}

void draw(){
 startScreen();
 
 
 //Testing for button presses
 if(mousePressed && mouseX>=60 && mouseX<=200 && mouseY>=height/2+60 && mouseY<=370){
   levelEasy=true;
   mouseReleased();
  //Instead of drawing a red rect,
                                //Start game on Easy level (lower speeds)
 }
 
 if(mousePressed && mouseX>=290&& mouseX<=430 && mouseY>=height/2+60 && mouseY<=370){
   levelHard=true;
   mouseReleased(); //Instead of drawing a red rect,
                                //Start game on Hard level (higher speeds)
 }
 
}

void mouseReleased(){
 if(levelEasy=true && !levelHard){
   Easy();
 }
 else if(levelHard=true && !levelEasy){
   Hard();
 }
 else if(!levelHard && !levelEasy){
   startScreen();
 }
}

void startScreen(){
 background(startScreen);
 textFont(font,48);
 fill(0);
 textAlign(CENTER);
 text("[Insert title here]", width/2, height/2-110);
 textFont(font,29);
 text("Choose a level",width/2, height/2);
 fill(247,250,167);
 strokeWeight(2);
 stroke(233,237,117);
 rect(60, height/2+60, 140,60);
 rect(290,height/2+60,140,60);
 stroke(1,1,90);
 strokeWeight(2);
 fill(45,44,142);
 rect(70,height/2+67,120,45);
 rect(300,height/2+67,120,45);
 fill(242,248,255);
 rect(75,height/2+71,110,35);
 rect(305,height/2+71,110,35);
 fill(0);
 textFont(font,24);
 text("Easy",130,height/2+96);
 text("Hard",360,height/2+96);
}
Re: Game start screen problems.
Reply #1 - Apr 20th, 2010, 2:44pm
 
(I'm co-working with Whiskey)
Here's an altered version that (I think) runs as it should, but I want the game screen to display without having to hold down the e or h key for difficulty level.

[code]import ddf.minim.*;
//Variable for background music.
AudioPlayer song;
//Variable for sound effect.
AudioPlayer eat;
Minim minim;

//Font variable for words on Game Over screen.
PFont loseDisplay;
//Font variavle for replay instructions
PFont replay;
//Font variable for score.
PFont score;
//Variable for current score.
int currentScore;

//Variable for keyReleased.
boolean value=false;
//Variable for gameOver screen.
boolean gameOver;

//Image variable for clouds image.
PImage clouds;
//Image variable for Bo.
PImage normalBo;
//Image variable for hurt Bo.
PImage hurtBo;
//Image variable for honey pots
PImage honey;
//Image variable for bees.
PImage bee;

//Array for three bees to move horizontally.
Bee[] beesX= new Bee[3];
//Array for three bees to move vertically.
Bee[] beesY= new Bee[3];

//Array for three honey pots to move horizontally.
Honey[] honeyX= new Honey[2];
//Array for three honey pots to move vertically.
Honey[] honeyY= new Honey[2];

//Bear object.
Bear bear;

void setup(){
 size(500,400);
 minim = new Minim(this);
 //Load songs.
 eat=minim.loadFile("BLOOPF05.aiff");
 song = minim.loadFile("62 - First Festival of Stars.mp3");
 //Play "First Festival of Stars".
 song.play();
 //Loop song 5 times.
 song.loop(5);

 //Game is not over.
 gameOver=false;

 //Load font.
 score=loadFont("KristenITC-Regular-100.vlw");
 //Set variable currentScore at 0.
 currentScore=0;

 //Load clouds image.
 clouds=loadImage("Game_bg.jpg");
 //Load bear image.
 normalBo=loadImage("Bear_icon_1.gif");
 hurtBo=loadImage("Bear_icon_3.gif");
 //Load honey pot image.
 honey=loadImage("Honey_icon.gif");
 //Load bee image.
 bee=loadImage("Bee_icon - Copy.gif");

 smooth();
 //Hide cursor.
 noCursor();

 //Initialize horizontal bees.
 for(int i=0; i<beesX.length; i++){
   beesX[i]=new Bee();
 }

 //Initialize vertical bees.
 for(int i=0; i<beesY.length; i++){
   beesY[i]=new Bee();
 }

 //Initialize horizontal honey pots.
 for(int i=0; i<honeyX.length; i++){
   honeyX[i]=new Honey();
 }

 //Initialize vertical honey pots.
 for(int i=0; i<honeyY.length; i++){
   honeyY[i]=new Honey();
 }

 //Create bear
 bear= new Bear(40);
}

void draw(){
 background(0);
 fill(0);
 textFont(score);
 text("Press E for Easy Mode or H for Hard Mode!", width/2, height/2);
   //If game is over, go to lose function
   if(gameOver==true){
     lose();
   }
 //otherwise, run the game.
   else if((keyPressed==true) && (key=='e') || (keyPressed==true) && (key=='E') || (keyPressed==true) && (key=='h') || (keyPressed==true) && (key=='H')){
     runGame();
   }
}

void keyReleased(){
 if((keyPressed==true) && (key=='e') || (keyPressed==true) && (key=='E') || (keyPressed==true) && (key=='h') || (keyPressed==true) && (key=='H')){
   value=true;
 }
}

//Function to run game.
void runGame(){
 //Set clouds image as background.
 background(clouds);
 //Center images.
 imageMode(CENTER);
 //Center ellipses.
 ellipseMode(CENTER);
 noStroke();

 //Center text.
 textAlign(CENTER);
 fill(0,100);
 textFont(score);
 //Write current score in middle of screen.
 text(currentScore,width/2,height/2);

 noFill();

 //Set up functions for 3 honey pots
 //to move horizontally and display.
 for(int i=0; i<honeyX.length; i++){
   honeyX[i].moveX1();
   honeyX[i].display1();
   //If bear image intersects honey pots from array honeyX
   if(bear.intersect(honeyX[i])){
     honeyX[i].caughtX();
     //Play eat sound when honey pot and Bo intersect.
     eat.play();
     //Reset eat sound.
     eat.rewind();
   }
 }


 //Set up functions for 3 honey pots
 //to move vertically and display.
 for(int i=0; i<honeyY.length; i++){
   honeyY[i].moveY1();
   honeyY[i].display1();
   //If bear intersects honey pots from array honeyY
   if(bear.intersect(honeyY[i])){
     honeyY[i].caughtY();
     //Play eat sound when honey pot and Bo intersect.
     eat.play();
     //Reset eat sound.
     eat.rewind();
   }
 }

 //Display bear and set location.
 bear.setLocation(mouseX,mouseY);
 bear.displayBear();

 //Set up functions for bees to move horizontally and display.
 for(int i=0; i<beesX.length; i++){
   beesX[i].moveX2();
   beesX[i].display2();
   //If bear intersect bees
   if(bear.intersect(beesX[i])){
     //show hurtBo and end game.
     bear.displayHurt();
     gameOver=true;
   }
 }

 //Set up functions for bees to move vertically and display.
 for(int i=0; i<beesY.length; i++){
   beesY[i].moveY2();
   beesY[i].display2();
   //If bear intersects bees
   if(bear.intersect(beesY[i])){
     //Show hurtBo and end game.
     bear.displayHurt();
     gameOver=true;
   }
 }
}

//Function for game Over screen.
void lose(){
 background(0);
 //Load font for words on Game Over screen.
 loseDisplay=loadFont("KristenITC-Regular-48.vlw");
 fill(255);
 //Align loseDisplay font.
 textAlign(CENTER);
 textFont(loseDisplay);
 //Write 'Game Over'.
 text("Game Over", width/2,height/4);
 //Write 'Score: currentScore'.
 text("Score: " + currentScore, width/2, 300);
 //Display hurtBo image.
 image(hurtBo, width/2, height/2-20, hurtBo.width, hurtBo.height);
 //Stop song when game is over.
 song.close();
 minim.stop();
 super.stop();
}

I'll add the class code below.
Re: Game start screen problems.
Reply #2 - Apr 20th, 2010, 2:46pm
 
Code:
//Bear class
class Bear{
float r;
float x,y;

//Constructor
Bear(float tempR){
r=tempR;
x=0;
y=0;
}

void setLocation(float tempX, float tempY){
x=tempX;
y=tempY;
}

void displayBear(){
//Draw bounding-box for bear image.
ellipse(mouseX,mouseY+3,40,40);
//Draw bear icon to follow mouse.
image(normalBo,mouseX,mouseY, normalBo.width*.4, normalBo.height*.4);
}

void displayHurt(){
//Draw bounding-box for bear image.
ellipse(mouseX,mouseY+3,40,40);
//Draw happy bear icon to follow mouse.
image(hurtBo,mouseX,mouseY, hurtBo.width*.4, hurtBo.height*.4);
}

//Function to return true or false based on if bear intersects
//a honey pot
boolean intersect(Honey h){
//Calculate distance
float distance=dist(x,y,h.x,h.y);
//Compare distance to sum of radii
if(distance<r+h.r){
return true;
}
else {
return false;
}
}

boolean intersect(Bee b){
//Calculate distance
float distance=dist(x,y,b.x,b.y);
//Compare distance to sum of radii
if(distance<r+b.r){
return true;
}
else {
return false;
}
}
}


Code:
//Bee class.
class Bee{
float x;
float y;
float speed;
float r;

//Constructor
Bee(){
//Start the bees between 20 and 380.
x=random(20,380);
//Start the bees somewhere above screen.
y=-110;
//Start the bees at random speed
speed=random(1,4);
//Give bees' bounding-boxes a radius of 30.
r=25;
}

void display2(){
//Draw bouding-box behind bees.
ellipse(x,y,25,25);
//Draw bees.
image(bee,x,y,43,55);
}

//Move beesX across screen.
void moveX2(){
x=x+speed;
//If bees leaves screen, reset bee at x=0.
if(x>width){
x=0;
//When bee reappears, give a random y-coordinate and speed.
y=random(20,380);
if((keyPressed==true) && (key=='e') && (value==true)|| (keyPressed==true) && (key=='E') && (value==true)){
speed=random(1,4);
}
else if((keyPressed==true) && (key=='h') && (value==true) || (keyPressed==true) && (key=='H') && (value==true)){
speed=random(2,6);
}
}
}

//Move beesY down screen.
void moveY2(){
y=y+speed;
//If bee leaves screen, reset bee at y=0.
if(y>height){
y=0;
//When bee reappears, give a x-coordinate and speed.
x=random(20,480);
if((keyPressed==true) && (key=='e') && (value==true)|| (keyPressed==true) && (key=='E') && (value==true)){
speed=random(1,4);
}
else if((keyPressed==true) && (key=='h') && (value==true) || (keyPressed==true) && (key=='H') && (value==true)){
speed=random(2,6);
}
}
}
}




Code:
//Honey class.
class Honey{
float x;
float y;
float speed;
float r;

//Constructor
Honey(){
//Start the honey pots between 20 and 380.
x=random(20,380);
//Start the honey pots somewhere above screen.
y=-110;
//Start the honey pots at random speed.
speed=random(1,4);
//Give all honey pots' bounding boxes a radius of 30.
r=30;
}

//Move honeyX across screen.
void moveX1(){
x=x+speed;
//If honey pots leaves screen, reset honey pot at x=0.
if(x>width){
x=0;
//When a honey pot reappears
//give a random y-coordinate and speed.
y=random(20,380);
if((keyPressed==true) && (key=='e') && (value==true) || (keyPressed==true) && (key=='E') && (value==true)){
speed=random(1,4);
}else if((keyPressed==true) && (key=='h') && (value==true) || (keyPressed==true) && (key=='H') && (value==true)){
speed=random(2,6);
}
}
}

void display1(){
//Draw bounding-box behind honey pots.
ellipse(x,y,60,60);
//Draw honey pots.
image(honey,x,y, 60,60);
}

//Move honeyY down screen.
void moveY1(){
y=y+speed;
//If honey pot leaves screen, reset honey pot at y=0.
if(y>height){
y=0;
//When a honey pot reappears
//give a x-coordinate and speed.
x=random(20,480);
if((keyPressed==true) && (key=='e') && (value==true) || (keyPressed==true) && (key=='E') && (value==true)){
speed=random(1,4);
}else if((keyPressed==true) && (key=='h') && (value==true) || (keyPressed==true) && (key=='H') && (value==true)){
speed=random(2,6);
}
}
}

//If a horizontal honey pot is caught
void caughtX(){
//Give honey pot a random speed and y-coordinate.
speed=random(1,4);
x=0;
y=random(30,370);
//Increase score by 1 per honey pot.
currentScore++;
}

//If vertical honey pot is caught
void caughtY(){
//Give honey pot a random speed and x-coordinate
speed=random(1,4);
x=random(30,370);
y=0;
//Increase score by 1 per honey pot.
currentScore++;
}
}



I'm pretty sure the code's working, but, as I said before, I'd like the game to display and stay displayed after I release the key. (I'm open to the possibility of replacing keyPresses with mouseClicks.)
Thanks for your time!
Page Index Toggle Pages: 1