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 › Help with a game!
Page Index Toggle Pages: 1
Help with a game! (Read 707 times)
Help with a game!
Apr 19th, 2010, 3:47pm
 
Hi everyone! I'm making a game like Squares2, but simpler. Your mouse is a bear head that eats honey pots and avoids bees. Right now, I'm trying to change the bear head to a smiling face (the "happyBo" image) when the bear and the honey meet. But the image only flashes (too fast to see; I want to delay it) and it only works for a few vertical pots.

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

boolean hit;

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);
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");

//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(){
//Set clouds image as background.
background(clouds);
imageMode(CENTER);
ellipseMode(CENTER);
noFill();
noStroke();

//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.intersect(honeyX[i])){
    honeyX[i].caughtX();
    //Plays eat sound when pot and Bo intersect
    eat.play();
    eat.rewind();
    hit=true;
  }
  else{
    hit=false;
  }
}


//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.intersect(honeyY[i])){
    honeyY[i].caughtY();
    eat.play();
    eat.rewind();

    hit=true;
  }
  else {
    hit=false;
  }
}

//Display bear
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();
}

//Set up functions for bees to move vertically and display.
for(int i=0; i<beesY.length; i++){
  beesY[i].moveY2();
  beesY[i].display2();
}

}

class Bear{
float r;
float x,y;

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

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

void displayBear(){

  if(!hit){
    //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);
      }
      else {

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

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

//Set up honey class.
class Honey{
float x;
float y;
float speed;
float r;

//Initialize honey class.
Honey(){
  x=random(20,380);  //Start the honey pots between 20 & 380
  y=-110;            //Start the honey pots somewhere above screen
  speed=random(1,4); //Start the honey pots at random speed
  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);
    speed=random(1,4);
  }
}

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);
    speed=random(1,4);
  }
}

//If honey pot is caught
void caughtX(){
  speed=random(1,5);
  //Set location off screen
  x=0;
  y=random(30,370);

}

void caughtY(){
  speed=random(1,4);
  //Set location off screen
  x=random(30,370);
  y=0;
}
}

//Set up bee class.
class Bee{
float x;
float y;
float speed;
float r;

//Initialize bee class.
Bee(){
  x=random(20,380);  //Start the bees between 20 & 380
  y=-110;            //Start the bees somewhere above screen
  speed=random(1,4); //Start the bees at random speed
  r=30;
}

void display2(){
  //Draw bouding-box behind bees.
  ellipse(x,y,30,30);
  //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);
    speed=random(1,4);
  }
}

//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);
    speed=random(1,4);
  }
}
}
Re: Help with a game!
Reply #1 - Apr 19th, 2010, 10:35pm
 
I wish I receive an € each time I repeat this, I would be rich... Smiley

Basically, if you want to display something for some time in Processing, you have to set up a timer (someVar = millis()) and check (millis() - someVar) is below the wanted duration. While it is true, (the loop being provided by draw() calls, of course), display the happy face.
Build this in the Bear class, of course.
Re: Help with a game!
Reply #2 - Apr 20th, 2010, 8:01am
 
Thanks!
Page Index Toggle Pages: 1