Presentation is loading. Please wait.

Presentation is loading. Please wait.

Games and Simulations O-O Programming in Java The Walker School

Similar presentations


Presentation on theme: "Games and Simulations O-O Programming in Java The Walker School"— Presentation transcript:

1 Games and Simulations O-O Programming in Java The Walker School
Newton’s Lab Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations

2 Space, The Final Frontier
You can create new bodies. What kinds of bodies are in our solar system? Why don’t we see a class for each type? The Walker School – Games and Simulations

3 Solar System The main types of bodies in our solar system contains planets, planetoids, moons, asteroids and comets. While each of these varies in size, mass, composition and velocity, they are also similar in many ways. Because each object does have a size, mass, and velocity, we can use abstraction in programming to create a Body Class that handles each of these items as variables. The Walker School – Games and Simulations

4 3 Basic Methods Right-click on Space world at the top?
What happens when you activate one of these methods? The Walker School – Games and Simulations

5 Public Methods Right – click on a body. What public methods does it have? The Walker School – Games and Simulations

6 Inspecting Planet Properties
Right click on a planet and inspect its properties. What is the mass? The Walker School – Games and Simulations

7 Space Public Methods What do the numbers mean in each case?
The Walker School – Games and Simulations

8 Helper Classes Also known as Abstract classes. They do not have constructors, so you can’t create instances of them. 2 Support Classes SmoothMover Vector The Walker School – Games and Simulations

9 Helper (or Support) Classes http://www. greenfoot
Some Helper Classes Counter Explosion Mover Plotter Rotator Slider Vector Wander The Walker School – Games and Simulations

10 Classes Inherited from SmoothMover
How are these 2 classes different? The Walker School – Games and Simulations What classes are inherited from Vector? Why is it not under Actor?

11 Overloading Can have the same name, as long as their parameters are different. This means that the methods (or constructors) have different signatures. The Walker School – Games and Simulations Has 2 constructers “Body” with different parameters.

12 Vectors dy dx Polar Representation = length and direction
The Walker School – Games and Simulations Polar Representation = length and direction Cartesian Representation = dx and dy

13 Vector Class Variables
Open SmoothMover and Vector. What methods do they have? The Walker School – Games and Simulations

14 Method Inheritance Open SmoothMover and Vector. Which methods can you call from the object menu? Which can’t you call? The Walker School – Games and Simulations

15 Default Constructors Does not have any parameters. Makes it easy to create bodies interactively without having to specify details. This is a default constructor. Is this an example of overloading? The Walker School – Games and Simulations

16 this. Only possible in constructors. Used to execute another constructor. What happens when you remove this. in front of the word mass? Does it compile? We’ll find out in Lab6 ! The Walker School – Games and Simulations

17 Constants Means the value never changes.
Means it’s shared between all actors of this class. These variables are constants. These variables do not change in a program. The Walker School – Games and Simulations

18 Add Movement The Walker School – Games and Simulations

19 Body Behavior Create multiple bodies. How do they behave?
The Walker School – Games and Simulations

20 Add Movement (Left) How would you make the body move left?
The Walker School – Games and Simulations

21 Solution: Left Movement
Change the + to a - The Walker School – Games and Simulations

22 Java Packages import java.awt.Color;
Programmers typically use packages to organize classes belonging to the same category or providing similar functionality. import java.awt.Color; Imported using dot notation. The Walker School – Games and Simulations

23 Importing Java Color Library
The Walker School – Games and Simulations

24 Default Color of Body Sets the RBG values Calls the Color class.
What is the legal range for colors? The Walker School – Games and Simulations

25 Adding Gravitational Force
The Walker School – Games and Simulations

26 Movement The Walker School – Games and Simulations

27 Apply Force To Do List Apply forces from other bodies: get all other bodies in space; for each of those bodies { apply gravity from that body to our own; } The Walker School – Games and Simulations

28 Apply Force – Part I Methods only intended to be called from within the class can be labeled private. When methods are intended to be called from outside the class, they should be labeled public. The Walker School – Games and Simulations

29 Greenfoot World Methods
Which methods give us access to objects within the world? The Walker School – Games and Simulations

30 Getting Objects Java.util.List getObjects(java.lang.Class cls)
Gives a list of all objects in the world of a particular class. getObjects(Body.class) Calls the objects in the world. getObjects(null) Keyword null is a special expression that means nothing, or no object. getWorld().getObjects(Body.class) Can be used from an actor to get all objects of class Body. The Walker School – Games and Simulations

31 Java.util.list What methods can be used to add items to a list?
What methods can be used to remove items to a list? What methods can be used to found out how many items are in a list? The Walker School – Games and Simulations

32 Lists are not Classes, but Interfaces
generic type – need to put the type of list in the brackets, such as String. The Walker School – Games and Simulations

33 Apply Force – Part II The Walker School – Games and Simulations

34 For (every) Loops for (ElementType variable : collection) {
Helps to step through the items in a collection, or list. for (ElementType variable : collection) { statements; } The Walker School – Games and Simulations

35 Apply Force – Part III Refers to the current object.
Use an if-statement so that gravity is applied to all objects (bodies) in the list except the current object. The Walker School – Games and Simulations

36 Applying Gravity The Walker School – Games and Simulations

37 Newton’s Law Newton's Law of Universal Gravitation states that every massive particle in the universe attracts every other massive particle with a force which is directly proportional to the product of their masses and inversely proportional to the square of the distance between them. The Walker School – Games and Simulations

38 Apply Gravity To Do List
Why do these numbers need to be doubles? Apply gravity from an object { get the location of the objects in space; calculate the distance between each of the objects; create a new vector using this data to determine direction; calculate the distance between the two bodies using the Pythagoras theorem; use Newton's formula to calculate the strength of the force calculate the acceleration; add the force; } The Walker School – Games and Simulations

39 Pythagorean Theorem The Walker School – Games and Simulations Look up the class Math in the Java API. How many parameters does the sqrt method have? (see answer on the next slide)

40 Square Root The Walker School – Games and Simulations Now, find the method that can be used to find the maximum of two integers. What is it called?

41 Acceleration Acceleration = Force / Mass
The Walker School – Games and Simulations

42 Appling Gravity Method
The Walker School – Games and Simulations

43 Trying it Out Try out the three initializing methods from Space object again. What do you observe? The Walker School – Games and Simulations

44 Experimenting with Gravity
Change the gravity constant at the top of the body class. What happens? Change it by ½, change it by doubling it; change it by 1, change it by a decimal. The Walker School – Games and Simulations

45 Experimenting with Mass & Movement
Experiment with changes to the mass and/or initial movement of the bodies defined in the Body class. What happens? size mass vector movement r g b x y The Walker School – Games and Simulations When experimenting it’s important to change one variable at a time.

46 Programming / Research Challenge
Create some new set-ups of stars and planets and see how they interact. Can you come up with a system that is stable? With what you have programmed, could you create a model of our solar system? Is our solar systems stable? Explain your reasoning. The Walker School – Games and Simulations

47 Obstacles, Gravity and Music
The Walker School – Games and Simulations

48 Improvements to Lab v. 3 Improvements new Obstacle class
modified Body class increased Gravity fixed obstacles more planets The Walker School – Games and Simulations

49 Add Obstacle Class Directions Right click on Actor Add new subClass
Name it “Obstacle” Choose the orange block from “Objects” The Walker School – Games and Simulations

50 Create Class Stub What are the basic methods we need to add to our stub? The Walker School – Games and Simulations

51 Create a Constructor What parameters do we want to pass into the constructor? The Walker School – Games and Simulations

52 Add Parameters to Constructor
The Walker School – Games and Simulations We want to know when a body hits the obstacle. When it does we want to play a sound.

53 Add Play Sound Method Use Audacity to create a sound that you want to play when the planet hits the obstacle. The Walker School – Games and Simulations What folder in the project, does this sound go?

54 Add an Act Method Stub The Walker School – Games and Simulations

55 Adding Obstacles What type of data structure is this?
The Walker School – Games and Simulations

56 Act To Do List public void act { call the intersecting object method from Actor use a conditional statement to determine if the object has collided with the obstacle if touched, set the appropriate image play sound if not touched, } The Walker School – Games and Simulations

57 Functioning Act Method
The Walker School – Games and Simulations

58 Increase the Gravity Constant
Gravity has been added to make the “bodies” move faster. What functionality has been added to slow them down? The Walker School – Games and Simulations

59 Slowing Fast Bodies The Walker School – Games and Simulations

60 Create Random Bodies Stub
What parameter(s) do we want to pass in order to create a number of bodies? Don’t forget to call this method in the act method. The Walker School – Games and Simulations

61 Random Bodies To Do List
write a method stub{ create a loop make a body of random size make a body of random mass (size * #?) give the body a random direction give the body a random speed give the body a random location (within the world) give the body a random color (r, g, b) create a new instance of the body (pass the parameters) } The Walker School – Games and Simulations

62 Create Random Bodies Method
The Walker School – Games and Simulations

63 Bouncing Bodies To Do List
write a method stub{ get the body’s location determine if the body is at the edge of the world cast the location to decimals (for movement) change the direction of movement, either in vertical or horizontal plane decelerate the body } The Walker School – Games and Simulations

64 Reflecting Bodies at World Edge
The Walker School – Games and Simulations Don’t forget to add the bounceAtEdge() method call in act() method.

65 Activities – Part I Change the number of bodies that are created by default in this scenario. Create a different arrangement of obstacles in your scenario. Use different sounds (sound files) for your obstacles. The Walker School – Games and Simulations

66 Changing the Number of Bodies
Under Space class change the number of random bodies created. The Walker School – Games and Simulations

67 Different Obstacle Arrangement
Creates a random location along they y-axis. The Walker School – Games and Simulations

68 Fixed Obstacle Arrangement
The Walker School – Games and Simulations

69 Different Sounds Changes the set of sounds from piano to trumpet sounds. These 1-minute notes were recorded by band members in Audacity then stored in the project’s sound folder. We added the prefix of “tp” so we didn’t have to change any of the names in the soundFiles [] list. The Walker School – Games and Simulations

70 Activities – Part II Use different images for your obstacles.
Make planets change color every time the bounce off the edge of the universe. Make planets change color every time they hit an obstacle. Make a different kind of obstacle that gets switched on and off by being hit. When on, it continuously blinks and produces a sound at fixed intervals. The Walker School – Games and Simulations

71 Change the Obstacle Image
Directions Right-click on Obstacle class Choose Set-Image Choose the new image from the Greenfoot image library. Make your own image in Photoshop and add it to the image folder for the project. The Walker School – Games and Simulations

72 Programming Challenge
Comets and asteroids move through the solar system. Create an image for one of these using Photoshop and add it to the world as an obstacle. To increase the difficulty of the challenge, have the object appear periodically at different speeds and if a ball hits this object, have it make a special sound. The Walker School – Games and Simulations

73 Activities – Part III Add some keyboard control. For example, pressing the right arrow key could add a small force to the right to all Body objects. Allow adding more planets. A mouse click into the universe while it is running should create a new planet at that location. The Walker School – Games and Simulations


Download ppt "Games and Simulations O-O Programming in Java The Walker School"

Similar presentations


Ads by Google