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.
IndexDiscussionExhibition › Simple Game
Page Index Toggle Pages: 1
Simple Game (Read 5606 times)
Simple Game
Oct 3rd, 2009, 1:18pm
 
I have decided that the first thing I would try in Process would be to make something similar to some of my old favorite Super Nintendo games  Gradius, Darius Twins, Super Nova etc...

http://www.openprocessing.org/visuals/?visualID=5136

Re: Simple Game
Reply #1 - Oct 4th, 2009, 4:21pm
 
- Added simple reset button
- removed mouse pointer
-stopped numbers from spiraling towards  Shocked infinity at an overwhelming rate
Re: Simple Game
Reply #2 - Oct 4th, 2009, 4:50pm
 
Hey,
Reset button does wonders!
I owe you a better explanation of the "separating timing from rendering" thing.  If you update at a rate which is directly related to millis(), you'll never have speed issues, and the game will run as smoothly as possible on the given platform.  Just break the frameRate limit with some absurd value like frameRate(999); in setup(), and then:

Quote:
int lastCheck=0;
void draw(){
 while ((millis()-lastCheck)>=10){
   lastCheck+=10;
   gameLogic(); // update all movement, etc.
 }
 gameRender(): // draw everything at its current position
}


Hope this makes sense.  The math involved in shifting all your object positions shouldn't take more than 10 milliseconds, but if it does, you'll have to increase 10 to some larger number.  So you're shifting at a given speed, 10 times if there are 100 millis between frames, 5 times for 50 millis between frames, etc.  Each frame, the objects render at their current position.  This way, even if your frameRate swings wildly, you have total control over the game's pace.

--Ben
Re: Simple Game
Reply #3 - Oct 4th, 2009, 9:34pm
 
Ok.I understand your suggestion. I just wan't to make sure that I follow the logic.

I'm setting FrameRate to a higher number so that the frame rate is being controlled by the timing of the program?

I am setting the math in a separate step then the render so that the computer makes  sure the math is done before rendering the drawing?

That way there is a cushion between the calculations  and the render so that a slower computer will pace the same speed as a faster one?

So my speed timings are going to change for everything because the way I am doing it now is based on a FrameRate that varies from computer to computer?


Re: Simple Game
Reply #4 - Oct 4th, 2009, 10:53pm
 
almost ...
right now, timing is off for a few reasons.  There's an intermittent slowdown due to garbage collection, about every two or three seconds.  And frameRate isn't really constant; it's just a measure of your current FPS.  When you set frameRate( n ); you're really setting the maximum.  If the computer isn't fast enough to meet that maximum, it'll vary somewhere below..and if someone starts a process in the background that uses CPU, framerate will swing wildly on any machine.

So, framerate is changing and can't be used for timing purposes.  If you're moving all your objects each frame, and the framerate is changing, your objects are lurching and stopping and so on.  The separation of game and render logic means that while you continue to render each frame (which is essential) you can update your movement/collision/whatever data as many times as needed between each frame.  Take the while() loop apart piece by piece: it's saying, if the "game time counter" has fallen behind the milliseconds this program's been running, by an amount >= 10, then update the game logic and add 10 to the counter."  This repeats until the counter is caught up to whatever that frame's millis() count is.

--Ben
Re: Simple Game
Reply #5 - Oct 4th, 2009, 11:22pm
 
So my code should be structured something like this.

class SimpleShip {

 float x_locOne=height/2;
 float y_locOne=200;
 int r1 = 9;
 float x_speed;
 float y_speed;
 float acceleration=.5;
 SimpleShip(){
 }
//-----------------------------------------------------------------MATH
 void calcship(){
   x_speed=constrain(x_speed,-2,2);
   y_speed=constrain(y_speed,-2,2);

   x_locOne=constrain(x_locOne,0+5,width-3);
   y_locOne=constrain(y_locOne,0+5,height-3);

   y_locOne+=y_speed;
   x_locOne+=x_speed;

 }

//-----------------------------------------------------------------RENDER

 void drawship(){
   fill(255,0,0);
   ellipseMode(RADIUS);
   ellipse(x_locOne,y_locOne,r1,r1);
   ellipseMode(CENTER);
   fill(255);
 }da

//--------------------------------------------------------KEYBOARD EVENTS

 void move(){
   if ((key=='w')||(key=='W')){
     y_speed-=acceleration;
   }

   if ((key=='s')||( key=='S')){
     y_speed+=acceleration;
   }

   if ((key=='a')||(key=='A')){
     x_speed-=acceleration;
   }

   if ((key=='d')||(key=='D')){
     x_speed+=acceleration;
   }
 }
}
Re: Simple Game
Reply #6 - Oct 5th, 2009, 9:08am
 
That much looks good, but an aside -- I'd recommend linking your keyPressed events to booleans if you want it to be possible to press multiple direction keys at once.  I.e. void keyPressed(){ if key==down, flag down boolean true;}, and void keyReleased(){ if key==down, flag down bool false; }  (outside the ship class) -- Ben
Re: Simple Game
Reply #7 - Oct 5th, 2009, 9:26pm
 
I added keyboard controls I didn't understand the last part that was suggested about them being outside of the ship class.Also I finished splitting the math and the rendering.
Tomorrow  I am going to clean up the code and try to adjust the game timing.
Re: Simple Game
Reply #8 - Oct 6th, 2009, 5:10pm
 
I set it up how you suggested and everything runs much smoother. Thank you thats a very important technique. Unfortunately it broke my rigged Pause button function located inside of the world class.I'll keep working at it. Any advice?

http://www.openprocessing.org/visuals/?visualID=4876

I have at least figured out what my problem is. When I pause the game its still counting the millis() the program has been running So..... When I unpause the game it runs the math check as if the game was never paused because the variable lastcheck has no been accumulating.
Re: Simple Game
Reply #9 - Oct 6th, 2009, 10:34pm
 
Heh, I've never seen noLoop() used as a pause function ;)
noLoop() means "don't loop draw."  So once you've called it ...draw never runs again.  (Unfortunately keyPresses, etc. depend on draw.)  Freeze.
I'd say just use a boolean -- in the draw loop, if (paused) write "paused" on the screen, otherwise do your current draw loop with the game and render logic and all that.
Oh, and P5 structure is a little different from pure Java: you never have to worry about public/static, etc., because P5 *is* a single class.  I have my doubts about putting setup() in a .java tab, by the way...but it does seem to work!
--Ben
Re: Simple Game
Reply #10 - Oct 8th, 2009, 10:01pm
 
Some times when I have the easiest problems I come up with the craziest solutions.

This is the last update I will do on this. I feel comfortable with working on something I little more difficult now that I have my first little App behind me and understand how to create classes. There are some more things that I'm sure that I will come back and touch up but this is pretty much it.

Thanks for you help Ben

final link
http://www.openprocessing.org/visuals/?visualID=5136
Re: Simple Game
Reply #11 - Nov 22nd, 2009, 5:11pm
 
Cool as. I like the sound.

Make a downloadable with levels and high scores and stuff.

I know people who would love it.

Namely my little bro
Re: Simple Game
Reply #12 - Nov 24th, 2009, 1:58pm
 
This is a good game, I could only get to 1758. What were your first steps in making it? A little bit like Space Invaders!
Re: Simple Game
Reply #13 - Dec 2nd, 2009, 2:35pm
 
It is good. The interface is cool. I beat that score got to around 13000. It would be cool to make the ships position in the window the same all the time but have it as a Side Scroller.

Josh
Re: Simple Game
Reply #14 - Dec 27th, 2009, 6:10am
 
Now with super awsome space missles!

http://www.openprocessing.org/visuals/?visualID=5136
Page Index Toggle Pages: 1