1 / 86

Lesson 5 – Putting It All Together

ALGORITMA PEMROGRAMAN 2 PROCESSING. Lesson 5 – Putting It All Together. Chapter. 10 – Algorithm 11 – Debugging 12 – Libraries. Chapter. 10 - Algorithm. Algorithm. In the real world, software projects usually involve many moving parts.

Télécharger la présentation

Lesson 5 – Putting It All Together

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ALGORITMA PEMROGRAMAN 2 PROCESSING Lesson 5 – Putting It All Together

  2. Chapter • 10 – Algorithm • 11 – Debugging • 12 – Libraries

  3. Chapter 10 - Algorithm

  4. Algorithm • In the real world, software projects usually involve many moving parts. • This chapter aims to demonstrate how a larger project is created out of many smaller “one feature” programs like the ones we are starting to feel comfortable making.

  5. Algorithm • You, the programmer, will start with an overall vision, but must learn how to break it down into individual parts to successfully execute that vision. • The focus will not be on good game design, but rather on good software design.

  6. Algorithm • How do you go from thought to code? • How do you implement your own algorithm to realize your ideas? • We will see how a larger project divides into four mini-projects and attack them one by one, ultimately bringing all parts together to execute the original idea.

  7. Algorithm • We will continue to emphasize object-oriented programming, and each one of these parts will be developed using a class. • Before we get to the idea and its parts, let’s review the concept of an algorithm which we’ll need for steps 2a and 2b.

  8. Algorithm

  9. Algorithm: Dance to the beat of your own drum • An algorithm is a procedure or formula for solving a problem. • In computer programming, an algorithm the sequence of steps required to perform a task. • Every single example we have created so far in this book involved an algorithm.

  10. Algorithm: Dance to the beat of your own drum • An Algorithm is not too far off from a recipe. • Preheat oven to 4000F. • Place four boneless chicken breasts in a banking dish. • Spread mustard evenly over chicken. • Bake at 4000F for 30 min.

  11. Algorithm: Dance to the beat of your own drum • The previous silde is a nice algorithm for cooking mustard chicken. • Clearly we are not going to write a Processing program to cook chicken. • Nevertheless, if we did, the above pseudocode might turn into the following code.

  12. Algorithm: Dance to the beat of your own drum • Implements of Algorithm :

  13. Algorithm: Dance to the beat of your own drum • An example that uses an algorithm to solve a math problem is more relevant to our pursuits. • Let’s describe an algorithm to evaluate the sum of a sequence of numbers 1 through N. • Where N is any given whole number greater than Zero. SUM(N) = 1 + 2 +3 + ... + N

  14. Algorithm: Dance to the beat of your own drum • Algorithm: • Set SUM = 0 and a counter I = 1 • Repeat the following steps while I is less than or equal to N. • Calculate SUM + I and save the result in SUM • Increase the value of I by 1. • The solution is now the number saved in SUM.

  15. Algorithm: Dance to the beat of your own drum • Translating the preceding algorithm into code, we have :

  16. Algorithm: Dance to the beat of your own drum • Output :

  17. 10.3 From Idea to Parts • To practice our development strategy, we will begin with the idea, a very simple game. • Before we can get anywhere, we should describe the game in paragraph form.

  18. 10.3 From Idea to Parts • Rain Game • The object of this game is to catch raindrops before they hit the ground. • Every so often (depending on the level of difficulty), a new drop falls from the top of the screen at a random horizontal location with a random vertical speed. • The player must catch the raindrops with the mouse with the goal of not letting any raindrops reach the bottom of the screen.

  19. 10.3 From Idea to Parts • How do we do this? • For one, we can start by thinking of the elements in the game: • Secondly, we should think about these elements behaviours. The raindrops The Catcher

  20. 10.3 From Idea to Parts • For example, we will need a timing mechanism so that the drops fall “every so often”. • We will also need to determine when a raindrop is “caught”. • Let’s organize these parts more formally.

  21. 10.3 From Idea to Parts • Develop a program with acircle controlled by the mouse. This circle will be the user-controlled “raind catcher” • Write a program to test if two circles intersect. This will be used to determine if the rain ctacher has caught a raindrop. • Write a timer program that executes a function every N seconds. • Write a program with circle falling from the top of the screen to the bottom. This will be the raindrops.

  22. 10.3 From Idea to Parts • Parts 1 through 3 are simple and each can be completed in one fell swoop. • However, with part 4, even though it represents one piece of the larger project, it is complex enough that we will need to complete this exact exercise by breaking it down into smaller steps and building it back up.

  23. 10.4 Part 1 : The Catcher • This is the simplest part to construct and requires little beyond what we learned in Chapter 3. • Having pseudocode that is only two lines long is a good sign, indicating that this step is small enought to handle and does not need to be made into even smaller parts.

  24. 10.4 Part 1 : The Catcher • Pseudocode : • Erase background • Draw an ellipse at the mouse location

  25. 10.4 Part 1 : The Catcher • Translating it into code is easy:

  26. 10.4 Part 1 : The Catcher • Translating it into code is easy:

  27. 10.4 Part 1 : The Catcher • This is good step, but we are not done. • As stated, our goal is to develop the rain ctacher program in an object-oriented manner. • When we take this code and incorporate it into the finl program, we will want to have it separated out into a class so that we can make a “catcher” object.

  28. 10.4 Part 1 : The Catcher • Our pseudocode would be revised to look like the following. • Setup • Initialize catcher Object • Draw • Erase Background • Set catcher location to mouse location • Display catcher

  29. 10.4 Part 1 : The Catcher • Example 10-1 shows the code rewritten assuming a Catcher object.

  30. 10.4 Part 1 : The Catcher • The Ctacher class itself is rather simple, with variables for location nd size, and two functions, one to set the location and one to display.

  31. 10.4 Part 1 : The Catcher

  32. 10.4 Part 1 : The Catcher

  33. 10.5 Part 2 : Intersection • Part 2 of our list requires us to determine when the catcher and a raindrop intersect. • Instersection functionality is what we want to focus on developing in this step. • We will start with a simple bouncing ball class (which we saw in Example 5-6) and work out how to determine when two bouncing circles intersect. • During the “integration” process, this intersect()function will be incorporated into the Catcther class to catch raindrops.

  34. 10.5 Part 2 : Intersection • Here is our algorithm for this intersection part. • Setup • Create two ball objects • Draw • Move balls • If ball #1 intersects ball #2, change color both balls to white. Otherwise, leave color gray. • Display balls.

  35. 10.5 Part 2 : Intersection • Certainly the hard work here is the intersection test, which we will get to in a moment. • First, here is what we need for a simple bouncing “Ball” class without an intersection test.

  36. 10.5 Part 2 : Intersection • Data • X and Y Location • Radius • Speed in X and Y directions

  37. 10.5 Part 2 : Intersection

  38. 10.5 Part 2 : Intersection • We are now ready to translate this into code.

  39. 10.5 Part 2 : Intersection • We are now ready to translate this into code.

  40. 10.5 Part 2 : Intersection • We are now ready to translate this into code.

  41. 10.5 Part 2 : Intersection • We are now ready to translate this into code.

  42. 10.5 Part 2 : Intersection • From here, it is prettty easy to create a sketch with two ball objects. • Ultimately, in the final sketch, we’ll need an array for many raindrops, but for now, two ball variables will be simpler.

  43. 10.5 Part 2 : Intersection • Example 10-2 Catcher (Cont.): Two ball objets

  44. 10.5 Part 2 : Intersection • Example 10-2 Catcher (Cont.): Two ball objets

  45. 10.5 Part 2 : Intersection • Example 10-2 Catcher (Cont.): Two ball objets

  46. 10.5 Part 2 : Intersection • Now that we have set up our system for having two circles moving around the screen, we need to develop an algorthm for determining if the circles intersect. • In Processing, we know we can calculate the distance between two points using the dist() function. • The diagram in Figure 10.3 shows how we can compare the distance between the circles and the sum of the radius to determine if the circles overlap.

  47. 10.5 Part 2 : Intersection

  48. 10.5 Part 2 : Intersection • OK, so assuming the following: • x1,y1 : coordinates of circle one • x2,y2 : coordinates of circle two • r1: radius of circle one • r2: radius of circle two If the distance between (x1,y1) and (x2,y2) is less than the sum of r1 and r2, circle one intersects circle two

  49. 10.5 Part 2 : Intersection • Our job now is to write a function that returns true or false based on the above statement.

  50. 10.5 Part 2 : Intersection • Putting it all together, we have the code in Example 10-3. (Example 10-3: Bouncing ball with intersection)

More Related