Difference between revisions of "Move Draw Loop"

From CodeStuff
Jump to: navigation, search
(Created page with "There are many ways to do interactive programs. The Move/Draw system shown here is often used in video games. Two functions are set up, to do different parts of the game....")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
There are many ways to do interactive programs.  The Move/Draw system shown here is often used in video games.   
 
There are many ways to do interactive programs.  The Move/Draw system shown here is often used in video games.   
  
Two functions are set up, to do different parts of the game.
+
Two functions are set up, each to do a different part of the game.
  
 
The move function controls what is happening.  User input is read and any movement of objects occur in this function.
 
The move function controls what is happening.  User input is read and any movement of objects occur in this function.
  
During the draw function everything in the game is at a standstill.  The job of the draw function is to put an image of the current state of the game onscreen.  
+
During the draw function everything in the game is at a standstill.  The job of the draw function is to put an image of the current state of the game on the screen.  
  
 
The example below makes an etch-a-sketch.  The move function checks the keys 37,38,39,40 (the arrow keys) and changes the variables cx and cy.  The draw function just draws a circle at cx,cy.  The screen is never cleared so whenever the circle moves it will leave a trail.
 
The example below makes an etch-a-sketch.  The move function checks the keys 37,38,39,40 (the arrow keys) and changes the variables cx and cy.  The draw function just draws a circle at cx,cy.  The screen is never cleared so whenever the circle moves it will leave a trail.

Latest revision as of 01:15, 31 January 2012

There are many ways to do interactive programs. The Move/Draw system shown here is often used in video games.

Two functions are set up, each to do a different part of the game.

The move function controls what is happening. User input is read and any movement of objects occur in this function.

During the draw function everything in the game is at a standstill. The job of the draw function is to put an image of the current state of the game on the screen.

The example below makes an etch-a-sketch. The move function checks the keys 37,38,39,40 (the arrow keys) and changes the variables cx and cy. The draw function just draws a circle at cx,cy. The screen is never cleared so whenever the circle moves it will leave a trail.

The last line of the program is run(move,draw). The run function repeatedly calls move then draw thirty times a second.

You may give run an optional third parameter to adjust the speed run(move,draw,60) will make the move/draw calls 60 times a second.