Lecture18 Java Game Programming I – An Example. Introduction We will look at the following We will look at the following Accelerated Mode GraphicsAccelerated.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
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.
COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
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.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Lecture19 Java Game Programming II – Example Continued.
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.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Lecture20 Java Game Programming III – Rate Control, Simple Animation and Display Mode.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory.
Guide to Programming with Python
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Introduction to TouchDevelop
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
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.
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
High Performance Java Swing Animation David Wallace Croft Presented to the Plano Java Users Group Plano, TX Copyright 2004 David Wallace Croft.
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.
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
Game Maker – Getting Started What is Game Maker?.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 Frisbee Physics Simulation Charles George Advisor: Brian Postow 03/05/05.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
CompSci Introduction to Jam’s Video Game Package.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Video Game Package Intro 1 Last Edited 1/10/04CPS4: Java for Video Games Introduction.
CompSci 44.1 Game Package Introduction to Jam’s Video Game Package.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
Graphical Output Basic Images.
MOM! Phineas and Ferb are … Aims:
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.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Game Loop Update & Draw.
Lecture 9 Announcements.
CS297 Graphics with Java and OpenGL
Update : about 8~16% are writes
Presentation transcript:

Lecture18 Java Game Programming I – An Example

Introduction We will look at the following We will look at the following Accelerated Mode GraphicsAccelerated Mode Graphics Simple game loopsSimple game loops Keyboard InputKeyboard Input Brute force collision detectionBrute force collision detection Acknowledgement: This is based on the space invaders tutorial written by Kewin Glass Acknowledgement: This is based on the space invaders tutorial written by Kewin Glass

Design The Game class: The Game class: This is the main gameThis is the main game Its going to have a main windowIts going to have a main window It also needs to respond to the player's key presses to move our player's ship around.It also needs to respond to the player's key presses to move our player's ship around. Most importantly it implements the game loopMost importantly it implements the game loop The Entity class: The Entity class: It represent things moving around in the main windowIt represent things moving around in the main window Subclasses:Subclasses: ShipEntity, AlienEntity and ShotEntity ShipEntity, AlienEntity and ShotEntity The images displayed: The images displayed: We call them SpritesWe call them Sprites We use the same sprite for multiple entities so that we don't waste memory.We use the same sprite for multiple entities so that we don't waste memory. We only load a sprite once.We only load a sprite once. Two classes:Two classes: Sprite and SpriteStore Sprite and SpriteStore

The Game class It will have main which starts everything It will have main which starts everything public static void main(String argv[]) { new Game(); } The Game class need to extend Canvas class since its one of the only components that supports using accelerated graphics. The Game class need to extend Canvas class since its one of the only components that supports using accelerated graphics. It will have a function gameLoop(), among other functions - we call gameLoop from the bottom of the constructor of Game. This just starts the game loop running around. It will have a function gameLoop(), among other functions - we call gameLoop from the bottom of the constructor of Game. This just starts the game loop running around.

Game Constructor - Creating the window  This code will go into the constructor of the Game class // create a frame to contain our game JFrame container = new JFrame("Space Invaders 101"); // get hold the content of the frame and set up the // resolution of the game JPanel panel = (JPanel) container.getContentPane(); panel.setPreferredSize(new Dimension(800,600)); panel.setLayout(null); // setup our canvas size and put it into the content of the frame setBounds(0,0,800,600);panel.add(this);

Game Constructor - Creating the window Since the canvas we're working with is going to be actively redrawn (i.e. accelerated graphics) we need to prevent Java AWT attempting to redraw our surface. Finally, we get the window to resolve its size, prevent the user resizing it and make it visible: Since the canvas we're working with is going to be actively redrawn (i.e. accelerated graphics) we need to prevent Java AWT attempting to redraw our surface. Finally, we get the window to resolve its size, prevent the user resizing it and make it visible: // Tell AWT not to bother repainting our canvas since we're // going to do that our self in accelerated mode setIgnoreRepaint(true); // finally make the window visible container.pack();container.setResizable(false);container.setVisible(true);

Game Constructor – Accelerated Graphics Canvas It is called BufferStrategy because its a strategy for managing buffers, or rather the swapping of buffers. It is called BufferStrategy because its a strategy for managing buffers, or rather the swapping of buffers. The only thing that needs to be specified is how many buffers to use to manage the screen, in this case we're going to use just 2. The only thing that needs to be specified is how many buffers to use to manage the screen, in this case we're going to use just 2. // create the buffering strategy which will allow AWT // to manage our accelerated graphics createBufferStrategy(2); strategy = getBufferStrategy();

The Game Loop The normal game loop runs something like this: The normal game loop runs something like this: Work out how long its been since we last loopedWork out how long its been since we last looped Process any input from the user/playerProcess any input from the user/player Move everything based on the time since last loopMove everything based on the time since last loop Draw everything on screenDraw everything on screen Swap the buffers over to make the new image visibleSwap the buffers over to make the new image visible Wait for a set periodWait for a set period

The Game Loop At this stage we're just interested in getting the screen swap and timing done (We will add more later): At this stage we're just interested in getting the screen swap and timing done (We will add more later): while (gameRunning) { // work out how long its been since the last update, this // will be used to calculate how far the entities should // move this loop long delta = System.currentTimeMillis() - lastLoopTime; lastLoopTime = System.currentTimeMillis(); // Get hold of a graphics context for the accelerated // surface and blank it out Graphics2D g = (Graphics2D) strategy.getDrawGraphics(); g.setColor(Color.black);g.fillRect(0,0,800,600); // finally, we've completed drawing so clear up the graphics // and flip the buffer over g.dispose();strategy.show(); // finally pause for a bit. Note: this should run us at about // 100 fps but on windows this might vary each loop try { Thread.sleep(10); } catch (Exception e) {} }

SpriteStore Class we only want to load any single sprite once. It would also be handy if the collection of sprites existing in one central location. We'll implement it as a singleton. we only want to load any single sprite once. It would also be handy if the collection of sprites existing in one central location. We'll implement it as a singleton.

SpriteStore Class public class SpriteStore { // The single instance of this class private static SpriteStore single = new SpriteStore(); public static SpriteStore get() { return single; } // The cached sprite map, from reference to sprite instance private HashMap sprites = new HashMap(); public Sprite getSprite(String ref) { …}}

SpriteStore Class – getSprite( String ref) method Things to do: Things to do: 1.if we've already got the sprite in the cache then just return the existing version if (sprites.get(ref) != null) { return (Sprite) sprites.get(ref); } 2.If we don’t have it then load it into the graphics memory so that we allow the image to be drawn without the CPU getting involved sourceImage = ImageIO.read(url); // create an accelerated image of the right size to store our sprite in GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage( sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK); // draw our source image into the accelerated image image.getGraphics().drawImage(sourceImage,0,0,null); 3.The other responsibility of our SpriteStore is to cache the images that have been loaded. // create a sprite, add it the cache then return it Sprite sprite = new Sprite(image); sprites.put(ref,sprite);

Sprite Class This is simply a wrapper class around BufferedImage class: public class Sprite { private BufferedImage image; public Sprite(BufferedImage image) { this.image = image; } public int getWidth() { return image.getWidth(null); } public int getHeight() { return image.getHeight(null); } public void draw(Graphics g,int x,int y) { g.drawImage(image,x,y,null);}}

Movement Movement in our case is going to be handled by our Entity class. Movement in our case is going to be handled by our Entity class. Each loop we'll ask the entities in the game to move themself. Each loop we'll ask the entities in the game to move themself. To facilitate this we add a list of entities to the main game class.To facilitate this we add a list of entities to the main game class. Each loop we'll cycle round this list asking each entity to move and then draw itself.Each loop we'll cycle round this list asking each entity to move and then draw itself.

Movement – In the gameLoop // cycle round asking each entity to move itself // delta indicates how long it'd been since we // looped round. for (int i=0;i<entities.size();i++) { Entity entity = (Entity) entities.get(i); entity.move(delta); } // cycle round drawing all the entities for (int i=0;i<entities.size();i++) { Entity entity = (Entity) entities.get(i); entity.draw(g); }

Entity Class It will have the following variables which indicates its state: // The current x location of this entity protected double x; // The current y location of this entity protected double y; // The sprite that represents this entity protected Sprite sprite; // The current speed of this entity horizontally (pixels/sec) protected double dx; // The current speed of this entity vertically (pixels/sec) protected double dy; // The rectangle used for this entity during collisions resolution private Rectangle me = new Rectangle(); // The rectangle used for other entities during collision resolution private Rectangle him = new Rectangle();

Entity Class It will have two methods move(), and draw(). We will add more methods later. It will have two methods move(), and draw(). We will add more methods later. The move() method looks like this: The move() method looks like this: public void move(long delta) { // update the location of the entity based on move speeds x += (delta * dx) / 1000; y += (delta * dy) / 1000; } Next we need to be able to draw an Entity onto our accelerated graphics context. draw() is implemented like this: Next we need to be able to draw an Entity onto our accelerated graphics context. draw() is implemented like this: public void draw(Graphics g) { sprite.draw(g,(int) x, (int) y); }

Entity Class The constructor: The constructor: // ref - The reference to the image to be displayed for this entity // x - The initial x location of this entity // y - The initial y location of this entity public Entity(String ref, int x, int y) { this.sprite = SpriteStore.get().getSprite(ref); this.x = x; this.y = y; }

Adding the entities to the game world we add a utility method to central Game class called initEntities(). we add a utility method to central Game class called initEntities(). This will initialise a set of entities at the game start.This will initialise a set of entities at the game start. The initialisation takes two steps.The initialisation takes two steps. The first is to create the player's ship and centre it at the bottom of our canvas. The first is to create the player's ship and centre it at the bottom of our canvas. The second step is to create all the aliens. We loop through creating a block of aliens. The second step is to create all the aliens. We loop through creating a block of aliens. In addition we count how many aliens we've created so we can track whether the player has won the game. In addition we count how many aliens we've created so we can track whether the player has won the game.

Adding the entities to the game world private void initEntities() { // create the player ship and place it roughly in the center of // the screen // the screen ship = new ShipEntity(this,"sprites/ship.gif",370,550); entities.add(ship); // create a block of aliens (5 rows, by 12 aliens, spaced evenly) alienCount = 0; for (int row=0;row<5;row++) { for (int x=0;x<12;x++) { for (int x=0;x<12;x++) { Entity alien = new AlienEntity(this, "sprites/alien.gif", 100+(x*50), (50)+row*30); entities.add(alien);alienCount++; }}}

Ship Entity We want to prevent the ship moving off the sides of the screen so we add this bit of code to the move() method in ShipEntity: We want to prevent the ship moving off the sides of the screen so we add this bit of code to the move() method in ShipEntity: public void move(long delta) { // if we're moving left and have reached the left hand side // of the screen, don't move if ((dx < 0) && (x < 10)) { return; } // if we're moving right and have reached the right hand side // of the screen, don't move if ((dx > 0) && (x > 750)) { return; } super.move(delta);}

Shot Entity It just wants to run up the screen until it either hits an alien (see Collision later on) or runs off the top of the screen, at which point we'd like to remove it from the entity list: It just wants to run up the screen until it either hits an alien (see Collision later on) or runs off the top of the screen, at which point we'd like to remove it from the entity list: public void move(long delta) { // proceed with normal move super.move(delta); // if we shot off the screen, remove ourselfs if (y < -100) { game.removeEntity(this);}}

Game Class - removeList public class Game extends Canvas { … // The list of entities that need to be removed // from the game this loop private ArrayList removeList = new ArrayList(); … public void removeEntity(Entity entity) { removeList.add(entity);}…}

Alien Entity As they move around we need to notice when they hit the side of the screen and start them moving in the opposite direction. As they move around we need to notice when they hit the side of the screen and start them moving in the opposite direction. In conjunction each time they change direction we'd like them all to move down a step. In conjunction each time they change direction we'd like them all to move down a step. Part of this will be covered in the movement routine and part in the game logic. Part of this will be covered in the movement routine and part in the game logic. Game logic is used in this case since we need to first detect that the aliens should change direction then change them all (rather than a localised change like the other entities)Game logic is used in this case since we need to first detect that the aliens should change direction then change them all (rather than a localised change like the other entities)

Alien Entity public void move(long delta) { // if we have reached the left hand side of the screen and // are moving left then request a logic update if ((dx < 0) && (x < 10)) { game.updateLogic();} // and vice vesa, if we have reached the right hand side of // the screen and are moving right, request a logic update if ((dx > 0) && (x > 750)) { game.updateLogic();} // proceed with normal move super.move(delta);}