KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Create a Simple Game in Scratch
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Pong! “The oldest commercially available game in history” Resources created from the video tutorials provided by David Phillips on
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Microsoft® Small Basic Advanced Games Estimated time to complete this lesson: 1 hour.
This game is loosely based on the Whack-A- Mole arcade game.  Each game starts with 45 seconds of play.  Moles randomly pop out of holes on the landscape.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
1 Flash Actionscript Animation. 2 Introduction to Sprites We will now look at implementing Sprites in Flash. We should know enough after this to create.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Animation Most games have moving components, how do we make things move? –We will track the location of an object using an x,y coordinate –We will draw.
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Guide to Programming with Python
CSCI 101 Introduction to Software Development and Design.
Game Design Creating a game called PING Phase 3: Steps for building basic game.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
GREENFOOT CLUB RESOURCES Brian Cullen – Rossett School
Game Maker Day 2 Making a Maze Game.
How to make Space Invaders
BEANFEST TUTORIAL Please read through the instructions carefully! Press the space bar when you are ready to continue.
Learning Game Maker Studio:
We will be creating a spaceship that simulates real movements in space. The spaceship will fire a laser beam that can destroy targets. The spaceship will.
©Robomatter – Distribution or copying without permission is prohibited. 3B STEM Computer Science 1 ©Robomatter – Distribution or copying without permission.
Art 315 Lecture 5 Dr. J. Parker AB 606. Last time … We wrote our first program. We used a tool called GameMaker. The program we wrote causes a ball to.
Alice Pong Recreating Pong in Alice By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Programming games Show your version of Bo the dog. Start cannonball Preview: video, audio work session (cannonball) Homework: Cannonball with ball in a.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
©2009, Tom McKendree Biplanes ©2009, Tom McKendree.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Concurrent Programming and Threads Threads Blocking a User Interface.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Game Maker Galactic Mail Advanced Group: Complete Galactic Mail, then start developing an independent project.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Variables and Random Numbers Computer App Session 4.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Action Script 12 "The games of a people.
Game Project 1 Homage to Pong. Project Rules: The primary project is Pong, the design and development of which will be discussed in detail here. If you.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 12 "The games of.
Game Maker Evil Clutches.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
How to create a basic game in Scratch. The Scratch Stage The Scratch stage is 480 pixels wide and 360 pixels high x increasesx decreases.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Evil Clown – Quiz 1 Gamemaker Quiz 1. Set up your file Save all finished work to your desktop Final file should be called: “yourlastname_evilclown” Open.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
GAME:IT Junior Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a.
ICT/COMPUTING RULES Only use software allowed by the teacher
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 11 "The games of a people reveal.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
School of Computer Science Space School 2015 Programming a Lunar Lander Game.
Index Background Costumes Making object walk smoothly Controlling an object with the keyboard Control an object with the mouse Changing costume when hit.
Scratch Programming Cards
Scratch for Interactivity
Multimedia in Java Multimedia combines graphics, animation and sound
Barb Ericson Georgia Institute of Technology June 2007
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
Breakout in Greenfoot Barb Ericson
Events, Event Handlers, and Threads
Flappy bird Demo: Lesson 5 Flappy bird Demo:
Creating a Simple Game in Scratch
Presentation transcript:

KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement KeyListener, you must implement three methods: keyPressed, keyReleased and keyTyped all three methods receive a KeyEvent to determine which key was pressed you can pass the event the message getKeyChar( ) –in order for your Java program to watch for key presses, you also have to get your container (probably a JPanel) to watch the keyboard, this is done by saying panel.setFocusable(true); We will look at code that uses KeyListener to implement an “etch-a-sketch” program –you might also use KeyListener for a computer game where the user uses the keyboard to control movement in the game like Pong

Building a Key-controlled Game Let’s build a game where you use the keys to control an object on the screen (say an image) –We will use the keys ‘a’, ‘w’, ‘d’, ‘x’ to go left, up, right, down and ‘s’ to stop the motion –The game will require a Graphics JPanel which implements ActionListener for the Timer and KeyListener for keyboard interaction –In the constructor, instantiate the timer (t=new Timer(10, this); and start the timer (t.start( );) and add the keyboard listener (addKeyListener(this);) –In the main method, add a keyboard focus instruction panel.setFocusable(true); Now we need to implement actionPerformed, keyPressed, keyTyped, keyReleased and paintComponent

Continued For actionPerformed, move the object on the screen (x+=dx; y+=dy) and check to see if the object has hit a boundary, and then call repaint(); –In paintComponent, do super.paintComponent(g) and then redraw the object on the screen For keyPressed, do the following: –char c=e.getKeyChar( ); –if(e==‘a’) dx--; –else if(e==‘w’) dy--; –… we will leave keyTyped and keyReleased blank (that is, implement them as { })

Missile Defense Game The game for today is to finish implementing the Missile game –In this game, you have a missile launcher at the bottom of the screen –Press the space bar to fire a missile –Press left and right arrows to control the motion of the launcher – it only moves left or right, so we don’t have to worry about up and down Questions: should the missile launcher stop when it hits a boundary, wrap around or bounce off? Should missiles wrap around or disappear? Should the user be able to fire more than 1 missile at a time or wait until the previous missile goes off the screen?

How do You Move the Launcher? We will have variable x for the position of the launcher, (it never goes up and down so we do not need a y) We will use dx to indicate the rate of change of movement of the launcher –dx > 0 means it is moving to the right –dx < 0 means it is moving to the left –dx = 0 means it is stopped When a key is pressed, we will see if that key is a left arrow or right arrow and if so, adjust dx accordingly (not x, but dx)

How do You Launch a Missile? We will use the space bar to indicate “fire a missile” –The keyPressed method is invoked and we test to see if the key was the space bar, if so, fire the missile –If we have a single missile then firing a new missile will not work until the current missile is gone –To have a lot of missiles, we need an array each missile will need an x, y value, we will call them mx[i] and my[i] to indicate it is the ith missile if we want missiles to retain the momentum of the launcher (that is, allow the missile to move up toward the left or up toward the right), then each missile will need a mdx[i] which will be equal to the value of dx at the time the missile is launched (if the rocket launcher changes speed or direction, the missile will continue along its original path)

What do You Fire At? Of course the intention of this game is to have targets to shoot at –Recall the Ship program that had a space ship randomly changing course –You need to determine, after you move each missile, if the missile impacts the ship (recall the collision algorithm from the GraphicBalls game If yes, remove the missile from the screen (set my[i]=-1), add 1 to your score, and remove the ship from the screen (randomly place it somewhere else) Note that if the missile goes off the screen (at the top), you remove it by setting my[i]=-1 What else do we want in the game? –We could have the ship fire at us and if we get hit, we lose a life, give the player 3 lives –We could give your missile launcher a shield –We could try to let you shoot the alien missiles/bombs

Pong You have already done bouncing balls, so you know how to bounce a ball off of a wall –You can also bounce the ball off of a paddle You know how to move a paddle, it’s the same way you moved the missile launcher, except that the paddle goes up and down rather than left or right The game consists of two paddles (two players), one on each side –The game “field” has a border on top and bottom so that the ball can bounce of either border –There is no border on left or right, if the ball gets past the paddle, the other player gains a point –Display the score at the top of the screen –As the game goes on, you can increase the ball speed or decrease a player’s paddle size!