howto: code with comments

March 4, 2006 at 3:07 pm | In gamemaker, howto, problems | Leave a Comment

Some students have been blogging a blow by blow description of every single thing they did to solve the problems.

This is a lot of work but also hard to read and follow because there is so much description in there.

A good alternative is to describe how the code works rather than what you did to create the code.

Here’s an example, using problem six:

A ball goes through a goal and the score goes up by one…the score does not go up if the ball misses the goal

goalMiss.jpggoal miss goalScore.jpggoal score
After you have made your demo click the Show Information button on your most important objects to obtain the code. You can copy and paste this into your blog like I have done below.

I think it’s best to intersperse comments about the code inside the code so that you explain the code as you go along. Focus on the main important parts of your code that explains how the problem was solved. Don’t write about every single thing because then the important stuff gets lost.

Use bold and italics to make it more readable. Unfortunately we don’t seem to have colour writing in wordpress.

Information about object: objBall

Step Event:
if expression y is less than one

set the score relative to 1
destroy the instance
create instance of object objBall at position (216,192)

Comment: If the ball goes through the goals, then do all of these things: increase the score by one, destroy the ball (so the score won’t keep going up) and create a new ball in the original position

Collision Event with object objWall:
bounce not precisely against solid objects

Comment: If the ball misses the goals then bounce it back into the playing arena

Keyboard Event for Key:
set speed to 5 and direction to 45+random(90)

Comment: random(90) will output a number between 0 and 89. then add this to 45 and you get a direction between 45 and 134. So sometimes the ball will go into the goals and sometimes it will miss. Just like real life.

Other Event: Outside Room:
jump to the start position
start moving in directions 000010000 with speed set to 0

Comment: If the ball leaves the room then jump back to start ready to kick for goal again.

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.