Chapter 8 - Creating Images and Sound Bruce Chittenden.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Objectives Build and modify an organization chart.
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.
Introduction to Programming
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
FrontPage Express By John G. Summerville Ph.D.©, RN.
Chapter 10 - Additional Scenario Ideas Bruce Chittenden.
PowerPoint Practice Exercise
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
ABC’s of PowerPoint (Office 2007) Part 1: Basic Vocabulary Part 2: Cursors Part 3: Insert Your Text Part 4: Insert Your Pictures Part 5: Basic Tools &
Games and Simulations O-O Programming in Java The Walker School
GameMaker.  A lot of Different Definitions  Easier to Say What is NOT a Game  Movie is Not a Game  No Active Participation  Final Outcome is Fixed.
Chapter 5 - Making Music: An On-Screen Piano
Photoshop Backgrounds, Buttons, Banners & Animation In PowerPoint Presentations.
Java: Chapter 1 Computer Systems Computer Programming II Aug
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre.
Digital Images Chapter 8, Exploring the Digital Domain.
Java: Chapter 1 Computer Systems Computer Programming II.
Color Correct and Remove Keystoning A minimalist approach to photographing your art By Paul Marley.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Chapter 8 – Mouse Input, Images and Sound. Chapter 8 - Content In contrast to previous chapters, we will not build a complete scenario in this chapter.
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.
Chapter 7 – Collision Detection: Asteroids
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1. Press the New Layer Button 3. Double click names to re-name Ball & Shadow layers 2. Click to change to 12 fps Step 1.
Creating Greenfoot Backgrounds. GIMP: GIMP: is a software graphics editor is a software graphics editor is similar to Adobe Photoshop – but FREE. is similar.
Android Graphics Library. Color Android colors are represented with four numbers, one each for alpha, red, green, and blue (ARGB). Each component can.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Greenfoot Game Programming Club.
PowerPoint Practice Exercise 1 1.Save this file in your AV-TECH Folder as PowerPoint Practice Exercise 1. 2.Open this file in PowerPoint. 3.Edit each slide.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
Function Of Microsoft Words Tables. Where Table section is located Table section is located on top row with File, Edit, View, Insert, Format, Tools, Window.
Chapter 9 – Additional Scenarios. Marbles Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Introduction To Greenfoot
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Creating Scenarios In Greenfoot. Greenfoot Scenarios are made up of: Greenfoot Scenarios are made up of: A World class. A World class. Actor classes.
 UV mapping is a process of applying textures to an object by hand  Figure: Blender model textured through a UV map.
GAME:IT Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a simple.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Working with Tables A table is a grid of cells arranged in rows and columns. Tables can be customized and are useful for various tasks such as presenting.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Chapter 4 - Finishing the Crab Game
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 10: Creating Images and Sound
Formula Auditing, Data Validation, and Complex Problem Solving
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Create a Halloween Computer Game in Scratch
Chapter 5 – Making Music: An On-Screen Piano (Part 2 – Using Arrays)
Interlude 2 - The Greeps Competition
Chapter 3 – Improving the Crab Game
Chapter 5 Working with Color.
Chapter 5 Working with Color.
European Computer Driving Licence
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Breakout in Greenfoot Barb Ericson
Methods, Data and Data Types
Greenfoot November 8, 2009.
Creating a Simple Game in Scratch
Presentation transcript:

Chapter 8 - Creating Images and Sound Bruce Chittenden

8.1 Preparation In contrast to previous chapters, we will not build a complete scenario in this chapter but work through various smaller exercises that illustrate separate techniques that can then be incorporated into a wide variety of different scenarios.

Exercise 8.1 Click on Scenarios and Click New Enter a Name and Click Create

Exercise 8.1

Exercise 8.2 Right Click on World and Click New subclass

Exercise 8.2 Name the subclass MyWorld and select a Background

Exercise 8.2

Exercise x 300 x 1

Exercise 8.3

Exercise 8.4

Exercise 8.5 public class MyWorld extends World { /* * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world 400 x 300 cell size 1 pixel. super(400, 300, 1); addObject (new Turtle(), 200, 150); } Create a Turtle Object and add it to the center of the World

Exercise 8.5

Exercise 8.6

public class Turtle extends Actor { /* * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { int x = getX(); int y = getY(); setLocation (x+10, y); } Get our current location and add 10 to the x coordinate

Exercise 8.6

Exercise 8.7

Exercise 8.8 mousePressed The mouse button was pressed and held on an object (pressed and not released) mouseClicked The mouse was clicked on and object (pressed and released)

Exercise 8.9 public class Turtle extends Actor { /* * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked(null)) move(); } private void move() { int x = getX(); int y = getY(); setLocation (x+10, y); }

Exercise 8.9 Click anywhere in the World and the Turtle moves to the right

Exercise 8.10 public void act() { if (Greenfoot.mouseClicked(this)) move(); } private void move() { int x = getX(); int y = getY(); setLocation (x+10, y); }

Exercise 8.10 Must Click on the Turtle to make the Turtle move to the right

Exercise 8.11 public class MyWorld extends World { /* * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world 400 x 300 cell size 1 pixel. super(400, 300, 1); addObject (new Turtle(), Greenfoot.getRandomNumber(401), Greenfoot.getRandomNumber(301)); }

Exercise 8.11 Must Click on the individual Turtle to make the that Turtle move to the right

8.2 Working with Sound The Greenfoot class has a playSound method that we can use to play a sound file. To be playable, the sound file must be located in the sounds folder inside the scenario folder.

Exercise 8.12 /* * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked(this)) Greenfoot.playSound("Coyote.wav"); }

Exercise 8.12 Click on the Turtle to play a sound file

This section involves recording and editing sounds using a sound editing program. If you want to do the exercises in this section you can use any sound editing program or you can download an Open Source sound editor called Audacity from Sound Recording and Editing

8.4 Sound File Formats and File Sizes This section involves recording and editing sounds using a sound editing program. If you want to do the exercises in this section you can use any sound editing program or you can download an Open Source sound editor called Audacity from

8.5 Working with Images Managing images for actors and world backgrounds can be achieved in two different ways. We can use prepared images from files, or we can draw an image on the fly in our program.

8.6 Image Files and File Formats This section involves creating and editing images using an image editing program. If you want to do the exercises in this section then you can use Photoshop or download an Open Source image editor called gimp from

8.7 Drawing Images The color value is split into three components; the red, green, and blue component. Colors represented this way are usually referred to as RGB colors. This means that we can represent each pixel with color and transparency in four numbers that are: (R, G, B, A) The first three define Red, Green, and Blue components in that order and the last is the Alpha value. The values are in the range 0 to 255, where 0 indicates no color and 255 indicates full strength. An Alpha value of 0 is fully transparent (invisible), while 255 is opaque (solid).

Color Chart

Exercise 8.29 (255, 0, 0, 255) Red and Solid ( 0, 0, 255, 128) Blue and Half Transparent (255, 0, 255, 230) Magenta and Mostly Solid

Exercise 8.30 public ColorTest() { super(400, 400, 1); boolean black = true;

Exercise 8.30 (continued) for (int x = 0; x < 8; x++) { if (black) //alternate colors at the start of each column black = false; else black = true; for (int y = 0; y < 8; y++) { if (black) // alternate colors { ColorPatch blackSquare = new ColorPatch (0, 0, 0); addObject (blackSquare, x* , y* ); black = false; } else { ColorPatch redSquare = new ColorPatch (255, 0, 0); addObject (redSquare, x* , y* ); black = true; }

Exercise 8.30 (continued) * * Write a description of class ColorPatch here. * (your name) (a version number or a date) */ public class ColorPatch extends Actor { /* * Create a Checkers or Chess Board pattern. */ public ColorPatch (int r, int g, int b) { GreenfootImage img = new GreenfootImage (50, 50); img.setColor (new java.awt.Color(r, g, b)); img.fill(); setImage (img); }

Exercise 8.30

Exercise 8.31 public class ColorPatch extends Actor { /* * Create a Checkers or Chess Board pattern. */ public ColorPatch (int r, int g, int b, int a) { GreenfootImage img = new GreenfootImage (Greenfoot.getRandomNumber (100)+1, Greenfoot.getRandomNumber (100)+1); img.setColor (new java.awt.Color(r, g, b, a)); img.fill(); setImage (img); }

Exercise 8.31 (continued) public ColorTest() { super(400, 400, 1); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { int r = Greenfoot.getRandomNumber(256); int g = Greenfoot.getRandomNumber(256); int b = Greenfoot.getRandomNumber(256); int a = Greenfoot.getRandomNumber(256); ColorPatch square = new ColorPatch (r, g, b, a); addObject (square, x* , y* ); }

Exercise 8.31

Exercise 8.32 public ColorPatch (int r, int g, int b, int a) { int x = Greenfoot.getRandomNumber(100) + 1; int y = Greenfoot.getRandomNumber(100) + 1; GreenfootImage img = new GreenfootImage (x, y); for (int i=0; i<100; i++) { img.setColor (new java.awt.Color(r, g, b, a)); int xi = Greenfoot.getRandomNumber(x + 1); int yi = Greenfoot.getRandomNumber(y + 1); img.fillOval(xi, yi, 4, 4); setImage (img); }

Exercise 8.32

8.8 Combining Images Files and Dynamic Drawings Some of the most interesting visual effects are achieved when we combine static images from files with dynamic changes made by our program. We can for example, start with a static image file, and then paint onto it with different colors, or scale it up or down, or let it fade by changing its transparency.

Exercise 8.33 // Number of Squares in each row and column public static final int NUMBER = 12; // Size of the Square in pixels public static final int SIZE = 30; // Color and Transparency for the Darker Square private static final int BLACKR = 119; private static final int BLACKG = 136; private static final int BLACKB = 153; private static final int BLACKA = 100; // Color and Transparency for the Lighter square private static final int WHITER = 255; private static final int WHITEG = 255; private static final int WHITEB = 255; private static final int WHITEA = 255;

Exercise 8.33 public Board() { super(NUMBER*SIZE, NUMBER*SIZE, 1); boolean black = true; for (int x = 0; x < NUMBER; x++) { if (black) // alternate colors at the start of each column black = false; else black = true; for (int y = 0; y < NUMBER; y++) { if (black) // alternate colors { CreateSquare blackSquare = new CreateSquare (BLACKR, BLACKG, BLACKB, BLACKA); addObject (blackSquare, x*SIZE + SIZE/2, y*SIZE + SIZE/2); black = false; } else { CreateSquare whiteSquare = new CreateSquare (WHITER, WHITEG, WHITEB, WHITEA); addObject (whiteSquare, x*SIZE + SIZE/2, y*SIZE + SIZE/2); black = true; } This method is fully parameterized to generate any size board, with any number of squares, with any to color combinations

Exercise 8.33

Exercise 8.34 public class Box extends World { /* * Construct the box with a ball in it. */ public Box() { super(460, 320, 1); addObject ( new Ball(), getWidth() / 2, getHeight() / 2); }

Exercise 8.34 public class Ball extends Actor { private int deltaX; // x movement speed private int deltaY; // y movement speed private int count = 2; /* * Create a ball with random movement. */ public Ball() { deltaX = Greenfoot.getRandomNumber(11) - 5; deltaY = Greenfoot.getRandomNumber(11) - 5; } /* * Act. Move and produce smoke. */ public void act() { move(); }

Exercise 8.34 /* * Move the ball. Then check whether we've hit a wall. */ public void move() { setLocation (getX() + deltaX, getY() + deltaY); checkWalls(); } /* * Check whether we've hit one of the walls. Reverse direction if necessary. */ private void checkWalls() { if (getX() == 0 || getX() == getWorld().getWidth()-1) { deltaX = -deltaX; } if (getY() == 0 || getY() == getWorld().getHeight()-1) { deltaY = -deltaY; } If we are at a wall on the x-axis reverse direction

Exercise 8.34

Exercise 8.35 public class Smoke extends Actor { private GreenfootImage image; // the original image public Smoke() { image = getImage(); }

Exercise 8.35

Exercise 8.36 public class Smoke extends Actor { private GreenfootImage image; // the original image private int fade; // the rate of fading public Smoke() { image = getImage(); fade = Greenfoot.getRandomNumber(4) + 1; // 1 to 4 if (fade > 3) { fade = 2; // change 4 to 2, to have double probability for 2 } /* * In every step, get smaller until we disappear. */ public void act() { shrink(); }

Exercise 8.36 /* * Make the image of this actor a little smaller. If it gets very small, * delete the actor. */ private void shrink() { if(getImage().getWidth() < 10) { getWorld().removeObject(this); } else { GreenfootImage img = new GreenfootImage(image); img.scale ( getImage().getWidth()-fade, getImage().getHeight()-fade ); img.setTransparency ( getImage().getTransparency() - (fade*5) ); setImage (img); }

Exercise 8.36 Too much Smoke

Exercise 8.37 /* * Put out a puff of smoke (only on every second call). */ private void makeSmoke() { count--; if (count == 0) { getWorld().addObject ( new Smoke(), getX(), getY()); count = 2; } Add some delay for the Smoke

Exercise 8.37

Exercise 8.38 public Smoke() { image = getImage(); fade = Greenfoot.getRandomNumber(4) + 1; // 1 to 4 if (fade > 3) { fade = 2; // change 4 to 2, to have double probability for 2 } private void shrink() { if(getImage().getWidth() < 10) { getWorld().removeObject(this); } else { GreenfootImage img = new GreenfootImage(image); img.scale ( getImage().getWidth()-fade, getImage().getHeight()-fade ); img.setTransparency ( getImage().getTransparency() - (fade*5) ); setImage (img); } Fade is a Random Number 0, 1, 2, 2, 3

Exercise 8.38

8.9 Summary Being able to product sounds and images is a very valuable skill for producing good looking games, simulations, and other graphical applications. By combining images from a file with dynamic image operations, such as scaling and transparency changes, we can achieve attractive visual effects in our scenarios.

8.9 Concept Summary