Month: December 2021

Computers 8: Monster Beach party

Monster Beach Party

We are going to make a photo-composite (a photo made up of other photos) that shows some monsters on a beach where people were having a party. There is a list of everything you need for the project at the bottom of this post.

Use Magic eraser,  magnetic lasso, Scale and transform, Copy and paste.

Remember, to open a picture you click file open and then go to where you saved the picture.

beach 1

nice beach

Use the magnetic lasso (or the magic eraser if it’s got a background that is the same color, then just use the marquee tool to put a box around it) to get just the thing you want to copy in to the beach.

us magnetic lasso.PNG

click Edit, Copy.

copy.PNG

Go to the beach picture tab and click Edit>Paste.

paste.PNG

If it is the wrong size (too big or small), click Edit>Transform>Scale

scale.PNG

Move the edges of the box until it’s the right size and then press Enter on the keyboard.

If there are any extra parts to the picture that don’t look right, use the background eraser to clean it up.

bckground eraser to clean up

Remember, every time you paste something, you make another layer. Check the layer you are working on, to make sure what you are doing. You can use the move tool to change layers quickly by clicking on the item you want to change. The move tool looks like a four way arrow, at the very top of the tool bar.

 

 

You need:

A deserted beach (no people, no manmade things on it)

2 monsters or more

Some scared people (at least two)

4 beach toys

Some food

Some drinks

Some beach sports equipment

A cameraperson

A source of music

Put them all together, so it looks natural. Make sure you can see the monsters and people just as you would if you were there. If we can’t see someone’s feet for example, make sure that there  is a reason why we can’t. Don’t put people floating in the air.

COMPUTERS 10: Greenfoot 4

If you need to download Greenfoot at home, you can go to the Greenfoot site:

HERE IS THE DOWNLOAD PAGE

It’s free and opensource! Choose the standalone one on Windows, if you are using Windows.

SDK.PNG

Capture

We are going to finish our improvements to the crab game in this unit. There is always more you can do, but we won’t improve it anymore. You are welcome to add stuff to it on your own after this though, and you will have a few suggestions on what you might do.

ADDING OBJECTS AUTOMATICALLY

The first thing we want to fix is that the crabs, kraken, and worms have to be placed manually in the game. Greenfoot should do that automatically, and we will change the source code so it does.

One thing that does happen every time the scenario is successfully compiled: the world itself is created. The world object, that you see on the screen (that sand-colored square area) is an instance of the CrabWorld class. World instances are different from actors, we have to create actors ourselves, but Greenfoot automatically creates one instance of our world class and displays that instance on screen.

Take a look at the source code for CrabWorld. You can see the usual import statement in the very first line. After that is the class header, and a comment.

crabworld source code

The next part is where we are very interested:

public CrabWorld()

{

super(560, 560, 1);

}

This part is the constructor of the class. While a constructor looks similar to a method there are some differences:

a constructor has no return type specified between the keyword “public” and the name;

the name of the constructor is always the same as the name of the class.

A constructor is a special type of method that is always automatically executed whenever a new instance of this class is executed. It then does what it wants to do to set up this new instance in to a starting state.

constructor

In this example the constructor builds the world to the size we want (560 cells by 560 cells) and a resolution of 1 pixel per cell. Because this constructor is executed every time a world is created, it can also create our actors automatically. Any code put in to the constructor, like to create an actor, will be executed alongside the world creation code.

Take a look at this:

public CrabWorld()

{

super (560, 560, 1);

addObject(new Crab(), 150, 100 );

}

This will put a new crab at the location x=150, y=100 in the world. That means 150 cells from the left edge of the world and 100 cells down from the top of the world. That means that the origin point (0,0) is the top left part of the world.

coordid

There are two new things going on here. The addObject method, and the new statement which is used to create a crab.

The addObject method is a method that belongs to the World class. You can find it by looking up the class documentation for the World class. Here is its signature:

void addObject(Actor object, int x , int y)

The signature tells us that:

the method doesn’t return a result (since it’s void return type)

the method’s name is addObject

The method requires 3 parameters, the object, x and y

The type of the object parameter is Actor, and the x and y both have to be int (integers).

We can use this to add new actors to the world, and since the method is part of World class and CrabWorld is a subclass of World it inherits the ability to do this too. We can just call the method to use it.

THE PILLARS OF CREATION

AddObject will method lets us add an actor object to the world, but we must have an object to add first.

The Java keyword new lets you create new objects of any existing classes. When the compiler sees the expression

new Crab()

it will make a new instance of class Crab. The expression to create new objects always has the keyword new at the start, then the name of the class we want to create, and finally a parameter list. We didn’t put in any parameters in the example above, that’s why the parentheses () are empty.

new keyword

Since we have created a new object, we have to do something with it. The easy way to do that is use it instead of the actor parameter of the addObject to add this object to our world:

addObject ( new Crab(), 150, 100);

The last two parameters tell where to put the crab on the x, y plane.

4.1 Do this: Add code to your CrabWorld constructor to make a crab automatically like you saw above.

Screenshot your code.

4.2 Do this: Add code to make 3 Krakens automatically too, you can choose a spot for each of them yourself.

Screenshot your code.

4.3 Do this: Add code to create 10 worms at places of your choosing in the world.

Screenshot your code.

4.4 Do this: Take all the code you just made that creates the objects into a separate method called populateWorld, in the CrabWorld class. You have  to declare the populateWorld yourself, it has no parameters and declares nothing. Then you have to call it from the constructor. Test it.

Screenshot your code.

4.5  Do this: Use random numbers for where the worms are going to appear. You will do that be replacing your coordinate values will calls to get some random numbers from the Greenfoot class.

Screenshot your code.

Now you have a version of the crab project that will bring all the actors in to the world whenever the scenario is compiled.

random filling code example

MOVING PICTURES

The next way to improve the game is to animate the movements of the crab. This is done with a very easy trick, we will just switch between two pictures of the crab quickly. The two pictures are called crab.png and crab2.png.

2 crabs

You can see that in one picture the crabs legs are out and the next they are in. When you switch between these two pictures quickly, it will look like the crab is walking. The way we are going to do this is by using two new concepts: Greenfoot images and variables.

GREENFOOT IMAGES

There is a class in Greenfoot called Greenfoot image and it helps in using and changing images. To make an image we construct a new GreenfootImage object, with the help of the keyword new, and putting the file name of the image file as a parameter for the constructor. Here is the code to access to the crab2.png image we would type:

new GreenfootImage(“crab2.png”)

Greenfoot can’t access the image unless it is saved in the images folder of the scenario.  Every Greenfoot actor has an image. If you don’t change anything, they get the image from their class. When you make an actor class, you choose an image, and every object created from that class will get a copy of that image when they are created. But, that doesn’t mean that all objects of the same have to always keep that same image. Any individual actor can have a new image if you use the correct method.

gREEN FOOT IMAGE

4.6 Do this: Go through the documentation of the Actor class and find the two methods that will let you change an actors image. Write them down, and include their parameters. What do they return?

Alright, we are going to use set image with a parameter of type GreenfootImage. First, we will create a GreenfootImage object from an image file and then we will use the actors setImage method.

setImage (new GreenfootImage(“crab2.png”));

This line gets two things done, 1: uses setImage which needs an image as a parameter

setImage (some image);

2: where the image is expected we write

new GreenfootImage(“crab2.png”)

which makes the image object from the file we named (crab2.png). When this line of code is executed (run) the inner part of the code (2) is run first. After that, the setImage method call is executed and that image object we just created is used as the parameter.

This way works, but if we are going to animate the way the crab walks, we need to do things differently.  We don’t want to have just one image set, we actually want to switch which image is used many many times. So first we will create the images and keep them (store them) and then we will use the stored images again and again to switch between the two pictures, instead of creating the images over and over. If we want to store two images in our crab object we need a variable.

INSTANCE VARIABLES (FIELDS)

When you are programming, parts of the program often need to hold (remember) some information, they do that with variables.

Java can support different types of variables. One of these is called a instance variable or field. You can call it either name. An instance variable is a piece of memory that belongs to the object (the instance of the class, that’s why it’s called an instance variable.) Anything that is placed in the variable will be remembered as long as the object exists and we can access (get at) that memory later.

An example of an instance variable is a monster’s hit points. In the Asteroids game, each asteroid had to be shot a number of times before it broke apart in to smaller pieces, and eventually got completely destroyed. The hit points for one asteroid don’t affect any of the other asteroids. That is, shooting one asteroid makes that asteroid weaker, it doesn’t change the other asteroids you haven’t shot yet.

instance

You declare an instance variable in a class by writing the keyword private followed by the type of the variable and the variable name:

private variable-type variable-name;

The type of variable is determined by what kind of information we want to store inside it. in this case, we want to store GreenfootImages in the variableso the type must be GreenfootImage. When you choose a variable you get a chance to name it, and the name should describe what the variable is used for.

You should always write instance declarations at the top of the class they are for, before the methods and constructors. This keeps things organized nicely, and it’s easier for you to find them later.

….

public class Crab extends Animal

{

private GreenfootImage image1;

private GreenfootImage image2;

public void act

….

4.7 Do this: Before you add the new code, make a new crab and place it in your world. Then right click on it and choose inspect. Write down the name of all the variables.

4.8 Do this: Why does crab have any variables, since we haven’t declared any in the Crab class? Where do these variables come from?

4.9 Do this: Add the variable declarations shown above to your crab class. Check to see that the class compiles.

Screenshot your code.

4.10 Do this: Now that the variables are entered, check the crab class again. Write down the variables and their values. What does null mean?

So you can see that declaring a GreenfootImage variable doesn’t give you a Greenfootimage object, it just gives you space to store an object. Since we declared two variables, we have two spaces to store objects. The space is shown by a white box.

two empty

The next step is to create the two image objects and store them into the variables. We saw already how to create the objects:

new GreenfootImage (“crab2.png”)

However, to store the object in the variable, we need a Java construct called an assignment.

Assignment

An assignment is like when your teacher puts you in a place in a seating plan. You are assigned a seat number. In programming, assignments let you store something into a variable. It uses an equal sign.

variable = expression;

The left side of the equal sign is the name of the variable we want to store into, and the right is the thing (value) we want to store.

Seat24 = BobSmith

Since the equal sign stands for assignment we can also call it the assignment symbol. You can read it as becomes, as in variable becomes expression or Seat24 becomes BobSmith.

In the Crab example, you would write:

image1 = new GreenfootImage(“crab.png”);

image2 = new GreenfootImage(“crab2.png”);

After those two lines of code are executed (run) we will have  created the two images that we want to store and stored them as the variables image1 and image2.  This will give 3 objects, the crab and two images. The Crabs variables will point to the images.

crab pointing

So now we have the variables and the images, where should we put the code that creates them and stores them in variables? We only want to do this when the crab is created, not every time we act. We need to put the code in the constructor.

reference.JPG

ACTOR CONSTRUCTORS

At the start of this unit, we used the world constructor to set up the world. We can also use the constructor of an actor class to set up (initialize) the actor. Whenever an actor is created the code in the constructor is executed (run). Check out the example below:

crab constructor

The rules that apply for the World Constructor also hold true for the Crab Constructor:

the signature of a constructor doesn’t include a return type;

the name of the constructor is the same as the name of the class;

the constructor is automatically executed when a crab object is created.

Because of that last rule, the constructor is automatically run (executed) makes sure that the image objects are made  automatically created and assigned when a crab is created.

The final line of the constructor has the first of the two images that were created set as the crab’s image.

SetImage(image1);

That shows you that you can now use the name of the variable to refer to the object that is stored in it.

4.11 Do this: Put this constructor in in your Crab class. When it compiles and you run it, you won’t see any change in how your crab works (yet).

4.12 Do this: Take a created crab and right click on it in the World again and inspect it. Take  note of all the variables and their values. Compare them to those in your notes. What has changed?

Switching between Images

Now we have a crab with two images that are available to be animated. We just have to put in the animation.

To make the image animate, we just have to have some code that will switch between the two pictures. So if we are showing image1 we want to switch to image2, and vice versa.

It will be something like you see below in pseudo code:

if (our current image is image1) then

use image2 now

else

use image 1 now

Pseudo code is a mix of normal English and Java. It’s good for expressing ideas so you get how to do something, but you can’ t actually program with it. It’s like an outline for what you want the code to do, but it’s not pure Java, so it won’t compile.

If else

An If else statement means do this if the if statement is true. But if the if statement is not true, do that instead (else). Think of it like this: If you are tired you should go to sleep, but if you are not tired, you should stay out of bed. That is in pseudo code:

If (tired) go to sleep

Else get up and do something.

So the image example says go check and see which picture we are using for the crab. Is it the first picture (image1)? If it is, use the second one (image2). If it is not (else) then you must be using picture 2 (image2) right now so switch to picture1 (image1).

CODY CODER SAYS: here is the real Java code:

if ( getImage() == image1 )

{

setImage(image2);

}

else

{

setImage(image1);

}

equal

This piece of code has a few new parts. One of them is that the method getImage can be used to receive the actor’s current image. Another is the operator == (two equal signs) can be used to compare two values and see if they are equal. If they are the result is true, if they are not, the result is false.  The last new part is the if else statement we talked about above.

PItfall of equals symbol

There are just a few more things to say about If/ Else: it has two blocks (pairs of curly brackets {statements} that surround some statements). Only one of the blocks will be executed each time the if else statement is executed. When the if statement is true, the code block underneath it is executed. When the if statement is false, then the if statement doesn’t get it’s code executed, but the else statements code block does get executed (run).

if  (condition)

{

statements that get done if the condition above is true;

}

else

{

statements that get done if the condition at the top is false.

}

if else main.JPG

4.13 Do this: Put the image switching code, under CODY CODER above in to your the act method of your crab class. Test it. If there is an error, fix it. This code should work. When you are testing it, use the act button, not the run button so you can see it easier.

Screenshot your code.

4.14 Do this: Now that we know it works take it out of the act method and make a new method called switchImage and put the code in there. Then call the method inside your act method.

Screenshot your code.

4.15 Test This: Try to call the switchImage method from the crab’s popup menu. Does it work?

It’s the final countdown (of worms)

The last part we will talk about with the crabs is making a winning condition. A winning condition for a game just means what do you need to do to win. In our case, we will add a feature to the crab so it will count the worms that we manage to eat. If our crab eats 8 worms, we want to say that we won the game. We will also play a short sound that lets us know that we won.

So we have to add a few more things to the crab code:

an instance variable that will store how many worms have been eaten

an assignment that sets (initializes) this worm counting variable to zero at the start of the game

code to increment (make it go up {by one in this case}) every time we eat a worm

code to check if we have eaten eight worms, and stops the game and plays the winning sound if we have eaten eight worms.

Let’s start by making the worms eaten counter variable.

We can define a new instance variable (because we want it tied to one crab that is created as the game starts) by adding the line

private int wormsEaten;

underneath the other two instance variable definitions.

Remember that we define a new instance variable by putting the word private at the beginning. The type int means it’s using integers, and wormsEaten is a great name because we immediately know what it is for.

The second thing to do is add this line to the end of our constructor:

wormsEaten=0;

That 0 (zero) sets the wormsEaten counter at zero every time the game starts. You don’t actually have to have this line in here, Java always initializes (starts off) int variables at zero automatically. But sometimes we will want the initial value (starting value) to be something that is not zero so writing our own initializing statement is always a good idea. For example, if you were using an int variable for hitpoints, zero would be a terrible starting value.

The final code challenge we have do surmount (beat) is to count worms that are eaten and stop at 8. The place that makes sense to put the code is right after we eat a worm, so go to the wormHunt method and add a bit of code that goes like this:

wormsEaten = wormsEaten + 1;

Remember that increment means to go up, so this bit of code says hey whatever number wormsEaten is right now, add 1 to it. So if wormsEaten was at 5 before we ate a worm, this code will make sure that it becomes 6 after we eat a worm.

The last part is to check if we have eaten 8 worms, and when we have (if we do), it will both play the winning sound and stop the execution (end the game).  Think about what you already know how to do, and where the best place for this code is.

4.16 Do this: Add the code we talk about above to your own game. Test it, and make sure that it works.

Screenshot your code.

Did you try it? Do you remember == ?

Alright, you can check your code against the correct code here:

Here is the constructor part we changed:

top part

And here are the changes to wormHunt:

last bit of code

4.17 Do this: As another test, open an object inspector for your crab object (choose inspect from the crab after you right click on it in the game world (pop up menu)), before you start to play. Leave that window open (you can move it around to a good spot) while you play and watch the wormsEaten change as you run around.  Here it is after 2 worms fell to feed my crabs hunger.

check the variable

Alright, that is all we are going to do with the crab game together, however there are still a lot of ways you could make it better.

For example you could:

use more different kinds of actors (like something that eats the Kraken);

not moving forward automatically all the time, but only going forward when the up arrow is pressed;

using different images for the background and actors;

making it a two player game with a different keyboard controlled class that is controlled by different keys;

making a new worm pop up whenever one is eaten or at random times;

a whole bunch more you can think of yourself (barriers, time limits, levels, moving worms….).

4.18 Do this: The crab’s image change very quickly as it runs around so it looks a bit weird. Can you think of a way that it would only change images every 2nd or 3rd act cycles? Try to do that with your code. Explain how it works.

(Hint below if needed)…

Hint: you could add a counter that gets incremented (by one) in the act method. When it reaches 2 or 3 the image changes and the counter is reset to zero.

What Programming Techniques did you Learn?

This time you were introduced to using constructors to initialize objects, how to use instance variables of fields, and assignment statements to store information, and how to get at that information later. You also learned how to use the new statement to get the program to create new objects and you used the full version of the if statement, if else.

what we learned less 4

Definitions list

addObject method
assignment statements
blocks
constructor
fields
GreenfootImage
If else
increment
instance declarations
instance variable
instances
new
operator
origin point
Pseudo code
variable
world class