Date: Thu, 01 May 2003 20:08:34 -0500 (CDT) From: Mark Jacobson To: 810-061-02@uni.edu Subject: hopToward method and web site... Please use the web site resources as you work on Frogger and study for the final exam. There will be a study guide for the final exam available next Monday afternoon or evening, in plenty of time for the Thursday morning 10 a.m. final exam. http://www.cns.uni.edu/~jacobson/061/lab6/frogger.htm You have this as a handout, but if you do not have it with you, or never got that handout, you can look at it on the web or print it out. I cannot imagine working on this or any programming assignment without the assignment specifications (the handout) constantly at my desk to read and reread and refer to as I work. http://www.cns.uni.edu/~jacobson/061 The frogger information is separated out on the web page, so please use it and let the TA helpers look at it too, and explain stuff to you when needed. Here is another excerpt from the Frogger lab assignment handout. ------- The frog clearly needs to be able to hop in each of the four directions in response to a user click. We suggest writing a single method hopToward that takes a Location parameter (e.g., point). Depending on which side of the frog the point is on, the frog should move in the appropriate direction. To keep the testing required to determine how the frog should jump simple, we suggest you divide the space around the frog as shown in the diagram. ^ ^ | | | Hop | | up | | | -------------- | | Hop | | Hop left | FROGGY | right | | | | -------------- | | | Hop | | down | | | | | Let us rename these 4 areas of the screen as A, B, C and D ^ ^ | | | C | | | | | -------------- | | | | A | FROGGY | B | | | | -------------- | | | | | D | | | | | A = hop left B = hop right C = hop up or hop forwards D = hop down or hop backwards -------------------------------------------------------------------- Did I reproduce the diagram from the handout pretty effectively here in the email note? It is ALWAYS worthwhile the take the time and effort to get out pen or pencil and notebook paper and draw out a diagram related to the problem, as you think about it. Did you know that even under the time contraints of a FINAL EXAM or any exam, it would even be worthwhile to ask for some scratch paper and do that drawing of a diagram and jotting down ideas? That is a fact that could prove useful in your future challenges in classes like CS II, but also in this CS I class on the final exam next Thursday, or as you work on your last program. It will only prove useful if you actually catch the Nike spirit and Just Do It as you work on this Frogger project or as you study for the final. ------------------------------------------------------------------ Okay, back to discussion of example above. 1. It would be wisest and simplest approach to have the code in hopToward() method check the x position of the Location the user clicked first..... if (x where user clicked mouse is < left edge of froggie) froggiePic.move( -HOP, 0 ); REGION A was the click else if (x where player of Frogger game clicked < right edge of froggie) froggiePic.move( HOP, 0 ); Player clicked in area B 2. Now, if we are using else if instead of just if, we are home free! If it is not in area A and it is not in are B, and its a click on the canvas, then it is either in area C (we need to hop forward or hop up), or area D (we need to hop backwards or hop down), or froggy area (inside its VisibleImage rectangle, and we don't want to hop at all... else if (y where player of Frogger game clicked < top edge of froggie) froggiePic.move( 0, -HOP ); Player clicked in area C else if (y where player clicked < top edge + height of froggie) froggiePic.move( 0, HOP ); Player clicked in area C I guess we could add an else clause, just to be cute, but there is no need. However, the following else clause could be added. :-) else froggiePic.move( 0, 0 ); Player clicked on froggie!!!!! Note that froggiePic.move( 0, 0 ); is NOT a very moving experience for our little froggie! Enough hints on how to work on the frogs hopping about in response to the players mouse clicks (or mouse presses or releases). Mark