This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alitzio
Alitzio wrote ...

2018/6/26

How to make scoreboard and game over page appear?

Alitzio Alitzio

2018/6/26

#
Hello, I'm attempting to make a game with one actor eating another actor while another actor is trying to kill the first actor. This is the Player One, who you would control. You are trying to eat the "Pokepuff". import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Peeko extends Actor { /** * Act - do whatever the Peeko wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { move(0); if (Greenfoot.isKeyDown("w")) {move(5);} if (Greenfoot.isKeyDown("s")) {move(-5);} if (Greenfoot.isKeyDown("d")) {turn(5);} if (Greenfoot.isKeyDown("a")) {turn(-5);} } public void eat() { Actor Pokepuff; Pokepuff = getOneObjectAtOffset(0,0, Pokepuff.class); if (Pokepuff !=null) { World world; world = getWorld(); world.removeObject(Pokepuff); PeekoWorld peekoworld = (PeekoWorld)world; Score score = peekoworld.getScore(); score.addScore(); } } } Here's the world: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A world where the lord lives. * * @author (Anders Sandberg and Harrison Li) * @version (insert version) */ public class PeekoWorld extends World { Score score = new Score(); public PeekoWorld() { super(1000, 1000, 1); prepare(); } public Score getScore() { return score; } public void prepare() { Peeko peeko = new Peeko(); addObject(peeko,151,732); Asteroid asteroid = new Asteroid(); addObject(asteroid,226,161); Asteroid asteroid2 = new Asteroid(); addObject(asteroid2,531,612); Asteroid asteroid3 = new Asteroid(); addObject(asteroid3,649,441); Asteroid asteroid4 = new Asteroid(); addObject(asteroid4,200,500); Asteroid asteroid5 = new Asteroid(); addObject(asteroid5,576,199); Asteroid asteroid6 = new Asteroid(); addObject(asteroid6,424,430); Pokepuff pokepuff = new Pokepuff(); addObject(pokepuff,766,81); Pokepuff pokepuff2 = new Pokepuff(); addObject(pokepuff2,610,98); Pokepuff pokepuff3 = new Pokepuff(); addObject(pokepuff3,502,149); Pokepuff pokepuff4 = new Pokepuff(); addObject(pokepuff4,488,244); Pokepuff pokepuff5 = new Pokepuff(); addObject(pokepuff5,753,320); Pokepuff pokepuff6 = new Pokepuff(); addObject(pokepuff6,819,539); Pokepuff pokepuff7 = new Pokepuff(); addObject(pokepuff7,574,663); Pokepuff pokepuff8 = new Pokepuff(); addObject(pokepuff8,734,708); Pokepuff pokepuff9 = new Pokepuff(); addObject(pokepuff9,407,654); Pokepuff pokepuff10 = new Pokepuff(); addObject(pokepuff10,287,476); Pokepuff pokepuff11 = new Pokepuff(); addObject(pokepuff11,155,366); Pokepuff pokepuff12 = new Pokepuff(); addObject(pokepuff12,81,183); Pokepuff pokepuff13 = new Pokepuff(); addObject(pokepuff13,105,95); Pokepuff pokepuff14 = new Pokepuff(); addObject(pokepuff14,306,157); Pokepuff pokepuff15 = new Pokepuff(); addObject(pokepuff15,260,334); Pokepuff pokepuff16 = new Pokepuff(); addObject(pokepuff16,411,56); Pokepuff pokepuff17 = new Pokepuff(); addObject(pokepuff17,296,51); Pokepuff pokepuff18 = new Pokepuff(); addObject(pokepuff18,34,811); Pokepuff pokepuff19 = new Pokepuff(); addObject(pokepuff19,53,721); Pokepuff pokepuff20 = new Pokepuff(); addObject(pokepuff20,191,812); Pokepuff pokepuff21 = new Pokepuff(); addObject(pokepuff21,309,826); Pokepuff pokepuff22 = new Pokepuff(); addObject(pokepuff22,238,687); Pokepuff pokepuff23 = new Pokepuff(); addObject(pokepuff23,92,632); Pokepuff pokepuff24 = new Pokepuff(); addObject(pokepuff24,86,491); } } Sorry for the long text.. Here's the ATTEMPTED scoreboard. import greenfoot.*; import java.awt.Color; public class Score extends Actor { int score = 0; public void act() { setImage(new GreenfootImage("Score : " + score, 30, Color.BLACK, Color.WHITE)); } public void addScore() { score ++; } } And finally, the game over code. import greenfoot.*; import java.awt.Color; /** * Write a description of class GameOver here. * * @author (your name) * @version (a version number or a date) */ public class GameOver extends Actor { /** * Act - do whatever the GameOver wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public GameOver() { setImage(new GreenfootImage("Game Over", 10, Color.WHITE, Color.BLACK)); } } When I run the program, the scoreboard will not show up, and when I am "eaten" the "Game Over" screen will not show up and the game will continue running, so I will need to reset after each round. Would be very helpful if I could get any help or advice with this. Thanks in advance. :D
danpost danpost

2018/6/26

#
First, you need to remove the following line from both the Score and GameOver classes:
1
import java.awt.Color;
You are not showing any code where you are eaten and a GameOver object is created.
You need to login to post a reply.