Session 4 Starting the Air Raid Game

Similar documents
Session 1 Welcome to Greenfoot & Code Breaker Authored by Brian Cullen

Session 8 Finishing Touches to the Air Raid Game

Creating Java Programs with Greenfoot

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (

Improving the Crab more sophisticated programming

How does light energy travel? transparent transmit mediums media medium

Improving the crab: more sophisticated programming

Excel Basics: Working with Spreadsheets

CS 201 Advanced Object-Oriented Programming Lab 4 - Asteroids, Part 2 Due: February 24/25, 11:30 PM

About Technocamps. We go around schools and show you lots of interesting stuff! We also do things we call bootcamps during holidays!

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Hey there, I m (name) and today I m gonna talk to you about rate of change and slope.

Filter and PivotTables in Excel

#30: Graph theory May 25, 2009

SPRITES Moving Two At the Same Using Game State

Project 1 Balanced binary

Write for your audience

Lesson 2. Introducing Apps. In this lesson, you ll unlock the true power of your computer by learning to use apps!

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Table of Laplace Transforms

EEN118 LAB FOUR. h = v t ½ g t 2

Chapter 1 Operations With Numbers

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Decisions, Decisions. Testing, testing C H A P T E R 7

Swarm of Bees. See a preview animation of the effect to get an idea about what we are creating here:

EEN118 LAB FOUR. h = v t ½ g t 2

CSci 1113: Introduction to C/C++ Programming for Scientists and Engineers Homework 10 Spring 2018

CS103 Spring 2018 Mathematical Vocabulary

OrbBasic Lesson 1 Goto and Variables: Student Guide

INTERMEDIATE PROGRAMMING LESSON

Up in the air (PC/Mac)

[ the academy_of_code] Senior Beginners

LinkedList Implementation Mini-project intro

Wow Admin Panel. Version Probably the best WoW Private Servers trainer out there

Pong in Unity a basic Intro

Exercise 6 - Addressing a Message

Animation Workshop. Support Files. Advanced CODING CHALLENGE - ADVANCED

Changing How the Keyboard Works in Windows 7

OrbBasic 1: Student Guide

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Programming Project 1

class objects instances Fields Constructors Methods static

Variables and Data Representation

Using GitHub to Share with SparkFun a

Introduction to Programming

Lecture 3. Lecture

EEN118 LAB FOUR. h = v t ½ g t 2

Outlook is easier to use than you might think; it also does a lot more than. Fundamental Features: How Did You Ever Do without Outlook?

Creating Breakout - Part 2

ICS 61 Game Systems and Design Introduction to Scratch

ISY00245 Principles of Programming. Module 7

Express Yourself. Writing Your Own Classes

asteroids-day 3 Class Space

Programming Assignment #3 Thing Battle

CIT 590 Homework 10 Battleship

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Using Excel This is only a brief overview that highlights some of the useful points in a spreadsheet program.

How To Archive\Burn Your Documents to CD\DVD Using Roxio

WORD BASICS: MICROSOFT OFFICE 2010

DESIGN YOUR OWN BUSINESS CARDS

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

EEN118 LAB FOUR. h = v t - ½ g t 2

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

1.7 Limit of a Function

Stomp Rocket Lab Physics

Software Development

The Kurzweil K2000 & Galaxy Intro: Phase One: Phase Two:

Scratch Programming for Primary School Teachers Quiz Game

Document Imaging User Guide

The Problem, the Solution and the End FinalPart

Speed Up Windows by Disabling Startup Programs

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

OrbBasic LESSON 1 Goto and Variables Student Guide

Unit 9 Tech savvy? Tech support. 1 I have no idea why... Lesson A. A Unscramble the questions. Do you know which battery I should buy?

How to update Windows and Office offline

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

Programming in the Real World. Dr. Baldassano Yu s Elite Education

The Junior Woodchuck Manuel of Processing Programming for Android Devices

BEGINNER PHP Table of Contents

Divisibility Rules and Their Explanations

Claremont McKenna College Computer Science

Keep Track of Your Passwords Easily

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Introduction to Scratch

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Web Evaluation Report Guidelines

Peer-To-Peer Network!

The first program: Little Crab

MAFS Algebra 1. Quadratic Functions. Day 17 - Student Packet

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

4. Java Project Design, Input Methods

Section 2. Sending s

Introduction to Game Programming

x 2 + 3, r 4(x) = x2 1

Burning CDs in Windows XP

Tabbing Between Fields and Control Elements

Transcription:

Session 4 Starting the Air Raid Game Authored by Brian Cullen (bcullen@rossettschool.co.uk/@mrbcullen) (c) Copyright 2011 Computing At School. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-nc-sa/3.0/ for details.

Naming Conventions When writing code it is important to make it easy to understand and maintain. On most software projects there will be more than one person writing code so having a consistent way to name class, methods and variables makes it easier for them to work together. The Java Naming Conventions provide guidelines on how different names should be formatted and it is your task to find out what the rules are and provide an example for each of the following types of name. You can easily find this information on the web but in case you get stuck try the link provided. I have filled out the first of these as an example. Name Type Rule Example Class or Interface Should be in CamelCase. This is where AirRaidWorld the first letter of each new word is a capital and all spaces are removed. Method Variable Constants Name Conventions: http://www.oracle.com/technetwork/java/codeconventions-135099.html#367 Air Raid Game Today we are going to start a new game. In this game airplanes will fly across the top of the screen dropping supplies to enemy troops. You are in charge of a rocket launcher and it is your responsibility to destroy the crates before they hit the ground. To get started open the Air Raid scenario that you have been provided with. As you can see we already have two classes created for us. You should already be familiar with the purpose of the AirRaidWorld class from what we did before and we will use the StatusBoard class to display the players score but that is a little while off yet so we can ignore it for now. The two classes that have been created for us. The first step is to create classes for each type of object that we are going to have in the game. We will need at least the 4 classes listed below. It is important to realise that we only need a class for

each TYPE of object. This means that we only need to have one class for rockets even though we will be firing an awful lot of them. Object Plane Launcher Rocket Crate Description Represents the plan that flies across the top of the screen dropping the crates. Represents the rocket launcher that you control and the bottom of the screen. Represents the rockets you launch to try and shoot the crates and/or planes. Represents the crates that are dropped by the plane. So to get us started right click on the Actor class (on the right hand side) pick the New Subclass option and create a new subclass for each of these objects. You should notice that images have already been provided for each of them. Once you are finished your scenario should look something like that shown below although it won t do anything yet! Moving the Launcher The first job is to get the rocket launcher moving in response to the keys. This should be easier than the ship game because in this game we only want the launcher to move left and right along the bottom of the screen. To do this we must add the code to the act method of the Launcher class (to open the code double click on the class). 1. Write an if statement to check if the right arrow on the keyboard is being pressed exactly like the example below.

if ( Greenfoot.isKeyDown( right ) ) { // Add your code to do something // if the right arrow is pressed here. } 2. If the right arrow is being pressed we want to move the launcher 5 cells note that unless you turn an actor first the move method will move the actor towards the right of the screen. 3. Write an if statement to check if the left arrow on the keyboard is being pressed. 4. If the left arrow is being pressed we want to move the launcher 5 cells backwards (remember this should be a negative number). 5. Test that the code works and that you can move the launcher. Moving the Rocket Now we have got the launcher working we need to make sure that the Rocket class works. When we launch a rocket we want it to go straight up until it hits the top of the screen. Getting the rocket to move in a straight line should be simple for you by now. In the Rocket class act method write a single line of code telling it to move e.g. move(10);. When you test this code you will notice the problem. The rocket goes straight to the right edge of the screen rather than to the top. We should have known this because unless you turn an actor first it will always head towards the right hand side. So what we want to do is change the direction the rocket is pointing every time we make a new one. If you can remember we used a method like this to add actors to the ship game it was called a constructor. The constructor sets up things when we construct a new object of a class. To create a constructor for the Rocket class add the following code before the act method. public Rocket () { setrotation (270); } Our constructor simply does one thing which is turn the rocket 270 degrees clockwise which leaves it pointing at the top of the screen. If you test this code it should now fire the rocket at the top of the screen. Removing Rockets If, like me, you have fired a number of different rockets at the top of the screen you will have noticed that they get stuck there and don t disappear. That isn t really what we want to happen so let s think about how we can fix it. We already know we can get the world by calling getworld() and then use the removeobject method to remove something from the game. So far so good - but the question is how do we know when it has reached the top of the screen? Again we have done this before when we got the submarines to bounce off the sides of the screen can you remember what we checked to see if it was at the top of the screen?

In case you can t the answer is we checked if the y coordinate was less than or equal to zero. So we are going to do the same for the rocket and if it is we will remove it from the game! Remember the parameter for the removeobject method will be this because we want to remove the rocket we are controlling at the time. This code should go into the act method. int ycoord = gety(); if (ycoord <= 0) { World airworld = getworld(); airworld.removeobject(this); } Whenever you are writing a program it is really important to ensure that you always remove things that you no longer need. If you don t the computer will slow down as it runs out of memory and eventually crash! Anyway lets test this again and make sure it works. Firing from the Launcher Phew! We seem to have looked at a lot today but to finish off let s get the launcher firing the rockets. What we want to happen is that every time we press space a new rocket should be added to the game where the launcher is at that time. So let s give it a go 1. Write an if statement that checks if the space key is being pressed. 2. If the space key is pressed create a new variable called airworld and store the result of using the getworld() method into it. airworld.addobject(new Rocket(), getx(), gety()); 3. Use the addobject method as shown above to add the rocket to the game. This should look familiar from when we added the ships and submarines to the ship game. Notice how we use getx and gety to find the current position of the launcher and use that as the starting position for the rocket. 4. A sound called RocketTakeoff.mp3 has been provided. If you want to use it add the code to play it after the addobject method. 5. Test it it will work but not perfectly. As I have already said the last piece of code that you have created will not work perfectly. Can you explain what is going wrong and how you might try and fix it? Present your ideas to others in the class and see if you agree!