-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
authored and
unknown
committed
Feb 4, 2016
0 parents
commit cfc11ae
Showing
56 changed files
with
2,406 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#BlueJ class context | ||
comment0.params= | ||
comment0.target=Counter() | ||
comment0.text=\r\n\ Create\ a\ counter\ without\ text.\ \r\n | ||
comment1.params=prefix | ||
comment1.target=Counter(java.lang.String) | ||
comment1.text=\r\n\ Create\ a\ counter\ with\ prefix\ text,\ and\ 0\ value.\r\n | ||
comment2.params=prefix\ value | ||
comment2.target=Counter(java.lang.String,\ int) | ||
comment2.text=\r\n\ Create\ a\ counter\ with\ prefix\ text\ and\ value.\r\n | ||
comment3.params= | ||
comment3.target=void\ increment() | ||
comment3.text=\r\n\ Increment\ the\ counter\ by\ one.\r\n | ||
comment4.params= | ||
comment4.target=int\ getValue() | ||
comment4.text=\r\n\ Return\ the\ current\ counter\ value.\r\n | ||
comment5.params= | ||
comment5.target=void\ updateImage() | ||
comment5.text=\r\n\ Make\ the\ image\r\n | ||
numComments=6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
import java.awt.Graphics; | ||
import java.awt.image.BufferedImage; | ||
|
||
/** | ||
* Counter that displays an (optional) text and a number. | ||
* | ||
* @author Michael Kolling | ||
* @version 1.0 | ||
*/ | ||
public class Counter extends Actor | ||
{ | ||
public static final float FONT_SIZE = 18.0f; | ||
|
||
private int value; | ||
private String text; | ||
private int stringLength; | ||
private Font font; | ||
|
||
/** | ||
* Create a counter without text. | ||
*/ | ||
public Counter() | ||
{ | ||
this(""); | ||
} | ||
|
||
/** | ||
* Create a counter with prefix text, and 0 value. | ||
*/ | ||
public Counter(String prefix) | ||
{ | ||
this(prefix, 0); | ||
} | ||
|
||
/** | ||
* Create a counter with prefix text and value. | ||
*/ | ||
public Counter(String prefix, int value) | ||
{ | ||
this.value = value; | ||
text = prefix; | ||
stringLength = (text.length() + 4) * 10; | ||
|
||
GreenfootImage image = new GreenfootImage(stringLength, 22); | ||
setImage(image); | ||
font = image.getFont(); | ||
font = font.deriveFont(FONT_SIZE); | ||
updateImage(); | ||
} | ||
|
||
/** | ||
* Increment the counter by one. | ||
*/ | ||
public void increment() | ||
{ | ||
value++; | ||
updateImage(); | ||
} | ||
|
||
/** | ||
* Return the current counter value. | ||
*/ | ||
public int getValue() | ||
{ | ||
return value; | ||
} | ||
|
||
/** | ||
* Make the image | ||
*/ | ||
private void updateImage() | ||
{ | ||
GreenfootImage image = getImage(); | ||
image.clear(); | ||
image.setFont(font); | ||
image.setColor(Color.BLACK); | ||
image.drawString(text + value, 6, (int)FONT_SIZE); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#BlueJ class context | ||
comment0.params= | ||
comment0.target=Earth() | ||
comment0.text=\r\n\ Create\ a\ new\ world.\ \r\n | ||
comment1.params= | ||
comment1.target=void\ started() | ||
comment1.text=\r\n\ We\ detect\ the\ first\ to\ have\ a\ small\ delay\ for\ showing\ the\ start-screen.\r\n | ||
comment10.params= | ||
comment10.target=void\ clearWorld() | ||
comment10.text=\r\n\ Remove\ everything\ from\ the\ world.\r\n | ||
comment2.params=x\ y | ||
comment2.target=boolean\ isWater(int,\ int) | ||
comment2.text=\r\n\ Return\ true,\ if\ the\ specified\ coordinate\ shows\ water.\r\n\ (Water\ is\ defined\ as\ a\ predominantly\ blueish\ color.)\r\n | ||
comment3.params=map | ||
comment3.target=void\ jumpToMap(int) | ||
comment3.text=\r\n\ Jump\ to\ the\ given\ map\ number\ (1..n).\r\n | ||
comment4.params=mapNo | ||
comment4.target=void\ showMap(int) | ||
comment4.text=\r\n\ Set\ up\ the\ start\ scene.\r\n | ||
comment5.params= | ||
comment5.target=void\ act() | ||
comment5.text=\r\n\ The\ Earth's\ act\ method\ opens\ the\ ships'\ hatches\ when\ they\ are\ in\ position\ and\ \r\n\ starts\ the\ timer\ to\ check\ when\ the\ map\ run\ on\ this\ map\ should\ end.\r\n | ||
comment6.params=time | ||
comment6.target=void\ mapFinished(int) | ||
comment6.text=\r\n\ Game\ is\ over.\ Stop\ running,\ display\ score.\r\n | ||
comment7.params=ship | ||
comment7.target=void\ showAuthor(Ship) | ||
comment7.text=\r\n\ Write\ the\ author\ names\ on\ the\ background.\r\n | ||
comment8.params=time | ||
comment8.target=void\ displayScore(int) | ||
comment8.text=\r\n\ Display\ the\ score\ board\ for\ the\ result\ on\ this\ map.\r\n\ \r\n\ @param\ time\ How\ many\ act\ loops\ the\ score\ board\ should\ be\ shown\ for.\r\n | ||
comment9.params= | ||
comment9.target=void\ displayFinalScore() | ||
comment9.text=\r\n\ Display\ the\ score\ board\ with\ the\ final\ results.\r\n | ||
numComments=11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
|
||
/** | ||
* This is Earth. Or at least some remote, uninhabited part of Earth. Here, Greeps can | ||
* land and look for piles of tomatoes... | ||
* | ||
* @author Michael Kolling | ||
* @author Davin McCall | ||
* @version 2.0 | ||
*/ | ||
public class Earth extends World | ||
{ | ||
public static final boolean PLAY_SOUNDS = true; | ||
|
||
public static final int SCORE_DISPLAY_TIME = 240; | ||
|
||
private GreenfootImage map; | ||
private Ship ship1; | ||
private Ship ship2; | ||
private Timer timer; | ||
private int currentMap; | ||
private boolean firstStart; | ||
|
||
/* The first two 3-tuples are the ships: target (landing) y-coordinate, then initial x and y. */ | ||
/* Remaining 3-tuples: number of tomatoes, x co-ordinate, y co-ordinate */ | ||
private int[][][] mapData = { | ||
|
||
{ {240, 400, 0}, {360, 400, 599}, | ||
{14, 275, 43}, {14, 547, 541}, // map 1 | ||
{30, 114, 531}, {30, 663, 61} }, | ||
|
||
{ {305, 700, 0}, {305, 100, 0}, | ||
{60, 400, 300}, | ||
{7, 100, 500}, {7, 700, 500}, // map 2 | ||
{12, 100, 50}, {12, 700, 50} }, | ||
|
||
|
||
{ {480, 640, 0},{480, 160, 0}, | ||
{25, 40, 50}, {30, 50, 550}, // map 3 | ||
{25, 760, 50}, {30, 750, 549}, | ||
{50, 400, 220}, {50, 400, 440}} , | ||
|
||
{ {280, 310, 0}, {280, 490, 599}, | ||
{50, 385, 52}, {50, 404, 523} } // map 4 | ||
|
||
}; | ||
|
||
private int[][] scores; | ||
|
||
/** | ||
* Create a new world. | ||
*/ | ||
public Earth() | ||
{ | ||
super(800, 600, 1); | ||
currentMap = 0; | ||
firstStart = true; | ||
scores = new int[2][mapData.length]; // one score for each map | ||
setPaintOrder(ScoreBoard.class, Counter.class, Smoke.class, Ship.class, Greep.class, TomatoPile.class); | ||
} | ||
|
||
/** | ||
* We detect the first to have a small delay for showing the start-screen. | ||
*/ | ||
public void started() | ||
{ | ||
if (firstStart) { | ||
showMap(currentMap); | ||
firstStart = false; | ||
Greenfoot.delay(50); | ||
} | ||
} | ||
|
||
/** | ||
* Return true, if the specified coordinate shows water. | ||
* (Water is defined as a predominantly blueish color.) | ||
*/ | ||
public boolean isWater(int x, int y) | ||
{ | ||
Color col = map.getColorAt(x, y); | ||
return col.getBlue() > (col.getRed() * 2); | ||
} | ||
|
||
/** | ||
* Jump to the given map number (1..n). | ||
*/ | ||
public void jumpToMap(int map) | ||
{ | ||
clearWorld(); | ||
currentMap = map-1; | ||
showMap(currentMap); | ||
firstStart = false; | ||
} | ||
|
||
/** | ||
* Set up the start scene. | ||
*/ | ||
private void showMap(int mapNo) | ||
{ | ||
map = new GreenfootImage("map" + mapNo + ".jpg"); | ||
setBackground(map); | ||
Counter mapTitle = new Counter("Map ", mapNo+1); | ||
addObject(mapTitle, 60, 20); | ||
int[][] thisMap = mapData[mapNo]; | ||
for(int i = 2; i < thisMap.length; i++) { | ||
int[] data = thisMap[i]; | ||
addObject(new TomatoPile(data[0]), data[1], data[2]); | ||
} | ||
|
||
|
||
int [] shipData1 = thisMap[0]; | ||
int [] shipData2 = thisMap[1]; | ||
if (Greenfoot.getRandomNumber(2) == 0) { | ||
shipData2 = thisMap[0]; | ||
shipData1 = thisMap[1]; | ||
} | ||
|
||
// First ship | ||
ship1 = new Ship("spaceship-green.png", shipData1[0], 1); | ||
addObject(ship1, shipData1[1], shipData1[2]); | ||
|
||
// Second ship | ||
ship2 = new Ship("spaceship-purple.png", shipData2[0], 2); | ||
addObject(ship2, shipData2[1], shipData2[2]); | ||
|
||
// Timer starts when both ships have landed | ||
timer = null; | ||
} | ||
|
||
/** | ||
* The Earth's act method opens the ships' hatches when they are in position and | ||
* starts the timer to check when the map run on this map should end. | ||
*/ | ||
public void act() | ||
{ | ||
if (timer == null && ship1.inPosition() && ship2.inPosition()) { | ||
timer = new Timer(ship1, ship2); | ||
addObject(timer, 700, 570); | ||
|
||
ship1.openHatch(); | ||
showAuthor(ship1); | ||
ship2.openHatch(); | ||
showAuthor(ship2); | ||
} | ||
} | ||
|
||
/** | ||
* Game is over. Stop running, display score. | ||
*/ | ||
public void mapFinished(int time) | ||
{ | ||
displayScore(time); | ||
Greenfoot.delay(SCORE_DISPLAY_TIME); | ||
clearWorld(); | ||
currentMap++; | ||
if(currentMap < mapData.length) { | ||
showMap(currentMap); | ||
} | ||
else { | ||
displayFinalScore(); | ||
Greenfoot.stop(); | ||
} | ||
} | ||
|
||
/** | ||
* Write the author names on the background. | ||
*/ | ||
private void showAuthor(Ship ship) | ||
{ | ||
GreenfootImage im = getBackground(); | ||
Font font = im.getFont(); | ||
font = font.deriveFont(14f); | ||
im.setFont(font); | ||
im.drawString(ship.getGreepName(), ship.getX()-40, ship.getY()-36); | ||
} | ||
|
||
/** | ||
* Display the score board for the result on this map. | ||
* | ||
* @param time How many act loops the score board should be shown for. | ||
*/ | ||
private void displayScore(int time) | ||
{ | ||
int points1 = ship1.getTomatoCount(); | ||
int points2 = ship2.getTomatoCount(); | ||
scores[0][currentMap] = points1; | ||
scores[1][currentMap] = points2; | ||
String[] authors = new String[]{ship1.getGreepName(), ship2.getGreepName()}; | ||
ScoreBoard board = new ScoreBoard(authors, currentMap, scores); | ||
|
||
addObject(board, getWidth() / 2, getHeight() / 2); | ||
} | ||
|
||
/** | ||
* Display the score board with the final results. | ||
*/ | ||
private void displayFinalScore() | ||
{ | ||
clearWorld(); | ||
String[] authors = new String[]{ship1.getGreepName(), ship2.getGreepName()}; | ||
ScoreBoard board = new ScoreBoard(authors, scores); | ||
addObject(board, getWidth() / 2, getHeight() / 2); | ||
} | ||
|
||
/** | ||
* Remove everything from the world. | ||
*/ | ||
private void clearWorld() | ||
{ | ||
removeObjects(getObjects(null)); | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.