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 › my first videogame
Page Index Toggle Pages: 1
my first videogame (Read 1691 times)
my first videogame
Dec 26th, 2009, 1:21pm
 
Hi

I've made mi first videogame with processing. The typical simple tennis game. I`ve set the frameRate to 1000, and I've used the keyPressed function to move the racket.

The problem is that I have to keep the key pressed for a while for the racket to start moving, and I have to increase the values of the racket coordenates by 8 pixels a time to make the racket move quick enought.

It seems the computer doesn't read the keywoard at the same rate than the framerate. Is there any way to set it?

This is the code:

Code:
int[] x=new int [3];
int[] y=new int [3];
int r=10;
int[] incx=new int [3];
int[] incy=new int [3];
int i;
int posx=350;
int posy =500;

void setup(){
x[0]=150;
x[1]=150;
x[2]=150;
y[0]=150;
y[1]=150;
y[2]=3;
incx[0]=1;
incx[1]=-1;
incx[2]=-1;
incy[0]=1;
incy[1]=1;
incy[2]=1;
size(700,700);
background(0);
stroke(255);
smooth();
frameRate(1000);
}
void draw() {
background(0);
for (i=0;i<=2;i++){
ellipse(x[i],y[i],r,r);
if ((x[i]>699)||(x[i]<1)){
incx[i]=incx[i]*(-1);

}
if (((y[i]>699)||(y[i]<1))||(((y[i]==posy)&&((posx-x[i])*(posx-x[i])<2500)))){
incy[i]=incy[i]*(-1);
}
x[i]=x[i]+incx[i];
y[i]=y[i]+incy[i];
}

rect(posx,posy,50,10);
}
void keyPressed(){
if (key == 'n'){
posx=posx-8;
}
if (key =='m'){
posx=posx+8;
}
}
Re: my first videogame
Reply #1 - Dec 26th, 2009, 1:34pm
 
Setting the framerate to 1000 FPS makes no sense. It's 60 FPS by default, and it's enough for smooth animations. 30 or even 25 would be OK.

Usually, you would better check if a key is pressed in draw(). It gives better results.

Code:
void draw() {
 if (keyPressed) {
   if (key == 'n') {
     // <-- do things here
   }
 }
}


Or use booleans to see if the keys are pressed or not. It's a better way to handle key events.

Code:
boolean keyN;

void keyPressed() {
 if (key == 'n') {
   keyN = true;
 }
}

void keyReleased() {
 if (key == 'n') {
   keyN = false;
 }
}

void draw() {
 if (keyN) {
   // do things here
 }
}
Re: my first videogame
Reply #2 - Dec 26th, 2009, 3:17pm
 
I've shanged the framerate to 60 and is really slow.

I`ve change the keypressed function for the if as you said and now the racket is quick enought, but I dont know why it is quicker changing its direction than starting to move when it is still. There is a little pause from when I push the key to when it start to move.

This is the code:

Code:
//ball position, radius and increments
int x;
int y;
int r=10;
int incx;
int incy;
//rackets positions
int posx=40;
int posy =150;
int pos2x=960;
int pos2y =150;

void setup(){
x=150;
y=150;
incx=1;
incy=1;
size(1000,300);
background(0);
stroke(255);
smooth();
frameRate(500);
}

void draw() {
//drawing the ball
background(0);
ellipse(x,y,r,r);
//bouncing whit horizontal walls
if ((y>299)||(y<1)){
incy=incy*(-1);
}
//goals
if ((x>960)||(x<20)){
background(80);
}
//bouncing with raquets or vertical walls
if (((x>999)||(x<1))||(((x==posx)&&((posy-y)*(posy-y)<2500)))||(((x==pos2x)&&((pos2y-y)*(pos2y-y)<2500)))){
incx=-(incx);
}
//moving the ball
x=x+incx;
y=y+incy;
//drawing the rackets
rect(posx,posy,10,50);
rect(pos2x,pos2y,10,50);
//moving the rackets
if (keyPressed){
if (key == 'a'){
posy=posy-1;
}
if (key =='z'){
posy=posy+1;
}
if (key == 'p'){
pos2y=pos2y-1;
}
if (key =='ñ'){
pos2y=pos2y+1;
}
}
}
Re: my first videogame
Reply #3 - Dec 26th, 2009, 4:00pm
 
Monitor the framerate with println(frameRate) and you'll see that it won't go further than 60FPS anyway.

If the animation is too slow, then increase the delta, i.e. posy += 5 instead of posy += 1.

If you want multiplayer control, you should better go with the second example (using mousePressed() and booleans).

Edit: of course you should understand keyPressed(), sorry...
Re: my first videogame
Reply #4 - Dec 27th, 2009, 1:55am
 
I,ve changed frameRate from 500 to 60 and is too slow, so it have framerate upper from 60, because it works like this.

I think Im not understanding you well. You tell to use mousepressed() but I have only one mouse and two players and I want to control with keyboard
Re: my first videogame
Reply #5 - Dec 27th, 2009, 2:57am
 
Hey atreides,

stay calm and think about it some more. The mention of "mousepressed()" was clearly a mistake, since there were no examples of mousepressed before that.

Now... you should understand this frameRate business. A normal PAL TV signal is only 25FPS (actually 50 "fields", which are half-frames, per second). 60FPS is twice the speed of an NTSC display. When you use "frameRate(1000)" you're asking Processing to execute your draw() method up to 1000 times per second, if it is able to. It is probably not going to finish what it is doing in less than 1 millisecond, so the actual frame rate is probably a lot less, maybe around 100FPS for something simple.

The problem you may be having is that your keyPressed() method is only being called each time a key is "pressed", and if you are holding down a key on your keyboard, the number and rate of "presses" is going to be determined by your key repeat speed (as managed by your operating system preferences at the least). When you hold down, say, "m" in the Processing editor, how quickly do the repeats come? Probably the first "m" appears right away, followed by a short delay then a succession of "m" characters at a fixed rate, and that rate is possibly not very fast. You will notice this same behaviour using the cursor keys - if you want to go up lots of lines, when you hold the up arrow, at first it moves only one line then there is a short pause before it keeps going up. The exact same thing is happening in your game.

So, the solution is not to count how many times the key is "pressed" (including key repeat pressings), but to remember whether the key was pressed down and since then not released. That is the idea in antiplastik's example with "boolean keyN". I suggest you use that, with more boolean variables to keep track of other keys you are interested in.

-spxl
Re: my first videogame
Reply #6 - Dec 27th, 2009, 7:04am
 
This gets asked about often enough...  Another benefit to using booleans is the ability to handle multiple keypresses.  Here's an example.  Notice that with this approach you also need to act on keyRelease...
Re: my first videogame
Reply #7 - Dec 27th, 2009, 4:12pm
 
Thank you,

Now I've understand. There was no problem with that of the mouse, I simply didnt understand and told it. I thank you both for your patient. Im new on this and some this and some things are hard for me to understand
Page Index Toggle Pages: 1