Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Stuff Learning by example: Responding to the mouse.

Similar presentations


Presentation on theme: "Advanced Stuff Learning by example: Responding to the mouse."— Presentation transcript:

1 Advanced Stuff Learning by example: Responding to the mouse

2 Learning by example Programmers often learn by studying examples and experimenting on them We will look at two examples of how to respond to the mouse – We will trying changing them – We will learn to program by examining someone else code – We will learn to respond to the mouse

3 Look at this scenario Open a web browesr to http://www.greenfoot.org/scenarios/5231 This is kminttech’s Astroids Mouse Control Try it on the website Click “Open in Greenfoot” to download the file Let’s look around

4 Asteroids

5 Getting start You should compile the code and make sure it works on your machine … Go head. Compile and try it.

6 Getting into the code Open the code for Space Oh my, that’s a lot of member variables. We’ll read the code to understand them

7 Getting into the code Let’s start with the constructor (how is the world made) It’s nicely organize. Setup is divided into methods with a clear name. Let’s dig in.

8 Getting into setUpMouse() Where do panel and scroll come from? Both are member variables scroll is a ScrollingListener defined in this class panel is a JPanel and has something to do with the World Google search “java Jpanel” and “java MouseWheelListener”

9 Getting into setUpMouse() This is setting up something called a “callback” or “listener” When the event, moving the scroll wheel happens, the mouseWheelMoved method is called on the ScrollListener This is how we listen for events The panel is like a window (or part of a window) in Java

10 Getting into setUpMouse() Next we create an empty image We set it as the cursor image and create something called mouseImage If we look at the mouse image class it doesn’t do anything But each time the world acts we move it to the mouse position Let’s try removing mouseImage to figure out what it does

11 Getting into setUpMouse() Comment out the addObject and the moving it code Compile and Run the program Doesn’t seem to do anything… How about setCursor??? Comment it out That one seems important Hides the cursor

12 Getting into setUpMouse() The rest of the constructor creates Objects The ship The asteroids The scoreboard At that is left in space is the code to rotate the ship. Let see it in action!!!

13 Getting into the act() Click in the whitespace beside setRotation(..) You should get a little stop sign When the program gets here it will stop and we can investigate

14 Getting into the act() Start the game Do not scroll the mousewheel and game goes on Scroll the wheel and everything stops!

15 Let’s look at how the ship works Nicely organized again! MouseMove moves the ship to the location of the mouse. Let’s try changing it!

16 MouseMove() Let’s make it so the ship points to the mouse and accelerates in that direction. Start with the pointing Then the moving

17 MouseMove() Ok we need to rotate the image… Either in an editor or when we load the image. Load the image and call rotate it, like we did on other days Then make bullet shoot straight

18 MouseMove() How let’s make it move toward the mouse We will need the Ship to inherit from SmoothMover (to be a smooth mover instead of Actor). When the mouse is far accelerate the ship, and near stop the movement

19 Lets look at painting Open a web browser to http://www.greenfoot.org/scenarios/2828 BuilderBoy2005 presents “Advanced Mouse Control” Try the program in the browser Download the program

20 Mouse Control

21 Getting start again You should compile the code and make sure it works on your machine … Go head. Compile and try it.

22 Getting into the world Open world Notice it looks like the previous project was built from this one. They have a lot in common. Here mouse image is used to create a circle where you will draw

23 Getting into the world Can we change the painting color? Try calling setColor on the mouseImage mouseImage.setColor( new Color(255,255,0) ); Where should we set it? Before drawing the oval What does the mouseWheel do in this program? Can we make it change colors based of if we press a number on the keyboard

24 Color your world Remember we used Greenfoot.isKeyDown Remember to use Control + space to get an autocomplete list Try the following code

25 Color your world Now try to add mouse control to your own games Can you make a shoot game with ducks? Popping bubbles? Navigating a maze without touching the slide?

26 Start your own game project Choose a game that you can work on for the rest of the week Over the rest of the week, we will learn new programming techniques and more about Greenfoot You will be given more time to work on your own idea for a game We will help your idea become reality.


Download ppt "Advanced Stuff Learning by example: Responding to the mouse."

Similar presentations


Ads by Google