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 › How to collect objects in a minigame
Page Index Toggle Pages: 1
How to collect objects in a minigame? (Read 593 times)
How to collect objects in a minigame?
Dec 5th, 2009, 2:37am
 
Hi there!
I just started understanding processing a lil bit and im trying to write a minigame.

The minigame will be pretty simple. Its a character wich can be moved by keys. The player has to collect objects under timepressure.

Well my mainproblem is how to collect the objects and add the points at the counter. How can I define where the character is placed with keyPressed (is there a definition like mouseX, mouseY?!)

thats what i got at the moment.. Wink

float bg_ypos = -1800;
float characterx = 300;
float charactery = 450;
float character_geschwindigkeit=3;
float bg_geschwindigkeit=2;

float coiny001=-800;
float coiny002=-900;
float coiny003=-1000;

PImage bg_001_clouds;
PImage intro_lvl_001;
PImage plane001;
PImage coin002_bild;

PFont myriad;

void setup () {
 frameRate (0.25);
 
 size (800, 600);
   bg_001_clouds= loadImage ("bg_001_clouds.png");
   intro_lvl_001= loadImage ("intro_lvl_001.png");
   plane001= loadImage ("plane001.png");
   coin002_bild= loadImage ("coin002.png");
   
      myriad=loadFont ("MyriadPro-BoldIt-48.vlw");
      image(intro_lvl_001,0,0);
}

void draw () {
 
 smooth();
 frameRate (200);

 bg_ypos = bg_ypos + bg_geschwindigkeit;
   if (bg_ypos > 0) {
     bg_ypos = -1800;
     }
   
   coiny001 = coiny001 + bg_geschwindigkeit;
   coiny002 = coiny002 + bg_geschwindigkeit;
   coiny003 = coiny003 + bg_geschwindigkeit;
 
   image(bg_001_clouds,0,bg_ypos);
   image(plane001,characterx, charactery);
   
        image(coin002_bild,300,coiny001);
        image(coin002_bild,350,coiny002);
        image(coin002_bild,400,coiny003);
 
   if(keyPressed){
     if(key=='a'){
       characterx=characterx-character_geschwindigkeit;
       }
     if (key=='d'){
       characterx=characterx+character_geschwindigkeit;
       }
     if(key=='s'){
       charactery=charactery+character_geschwindigkeit;
       }
     if (key=='w'){
       charactery=charactery-character_geschwindigkeit;
       }
   }
}

thx a lot!
RaG
Re: How to collect objects in a minigame?
Reply #1 - Dec 5th, 2009, 3:07am
 
Some tips :
  • you could use PVector to store 2D positions. That would be much easier than storing x and y in different variables and it's a first step to OOP. For example :
    Code:
    PVector character = new PVector(200, 300);
    image(imgCharacter, character.x, character.y);
  • to check if your character has reached an object, you could use collision detection. For example, using the dist() method, you can calculate the distance between your character and a coin.
  • to check if your character has hit an coin, you may need to store all your coins in a unique array, so you can later iterate through this array :
    Code:
    // declare the array
    PVector[] coins = new PVector[3];

    // fill the array
    coins[0] = new PVector(100, 200);
    coins[1] = new PVector(200, 200);
    coins[2] = new PVector(300, 200);

    // iterate through the array
    for (int i = 0; i < coins.length; i++) {
     PVector coin = coins[i];
     if (dist(character.x, character.y, coin.x, coin.y) < 10) {
       // coin has been collected
     }
    }
Re: How to collect objects in a minigame?
Reply #2 - Dec 5th, 2009, 4:04am
 
yeah...merci beaucoup.. Smiley
gonna try it.. Smiley
Page Index Toggle Pages: 1