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

2015/1/5

Need help on coding

1
2
lionkitten lionkitten

2015/1/5

#
In my game, I have an actor that is controlled by the player. This actor is responsible in eating other actors of a different kind. The actors that are getting eaten pops up randomly and when it gets eaten a question will pop up. The player must answer it. So my game is like a quiz-eating game where the player has to eat up the questions in order to win. What advice would you give me to start this up? Thank you!!
danpost danpost

2015/1/5

#
lionkitten wrote...
In my game, I have an actor that is controlled by the player. This actor is responsible in eating other actors of a different kind. The actors that are getting eaten pops up randomly and when it gets eaten a question will pop up. The player must answer it. So my game is like a quiz-eating game where the player has to eat up the questions in order to win. What advice would you give me to start this up? Thank you!!
Sounds like you got everything and you only need to press 'Run'. Or, you have nothing and need to start by creating classes for the objects of the game (the world and each different type actor). Then, if you run into specific problems, post the code you tried with any error messages, and ask for help.
lionkitten lionkitten

2015/1/7

#
Hi, thanks for the reply. I have started doing my project and I have gotten into a problem. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; import java.awt.Color; /** * Write a description of class FilmWorldPLAY here. * * @author (your name) * @version (a version number or a date) */ public class FilmWorldPLAY extends World { private ScoreBoard score = new ScoreBoard(); /** * Constructor for objects of class FilmWorldPLAY. * */ public FilmWorldPLAY() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 500, 1); addObject (new Player(), 105, 210); addObject (new Flag(), 143, 61); addObject (new Flag(), 342, 122); addObject (new Flag(), 98, 375); addObject (new Flag(), 497, 441); } public void populateWorld () { addObject( score , 100, 100); int x,y ; int limit = Greenfoot.getRandomNumber (30)+1; for(int count = 0 ; count < limit; count ++) { y = Greenfoot.getRandomNumber (getHeight()); x = Greenfoot.getRandomNumber (getWidth()); addObject (new Flag(), x,y); } } } This is my code and I want to have random flags in my FilmWorld to pop up at random times but it seems that it's not working. I tried to set new objects in the world as you can see but it doesn't make a difference, it only works when I right click and press "void populateWorld()" then everything, including the scoreboard, pops up. I also want to adjust the score boards position in the world but I'm kind of lost in how to do that.
danpost danpost

2015/1/7

#
The 'populateWorld' method will not execute unless it is called to execute. You can do this from the constructor (the 'public FilmWorldPLAY()' block) by adding a line calling the method. With the placement of the score, you can change where it is placed by changing the values in the line where it is added into the world (the first line of your 'populateWorld' method).
lionkitten lionkitten

2015/1/7

#
Wait, I don't get it. I have the method populateWorld(), can you look at my FilmWorldPLAY code please? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; import java.awt.Color; /** * Write a description of class FilmWorldPLAY here. * * @author (your name) * @version (a version number or a date) */ public class FilmWorldPLAY extends World { private ScoreBoard score = new ScoreBoard(); /** * Constructor for objects of class FilmWorldPLAY. * */ public FilmWorldPLAY() { super(600, 500, 1); addObject (new Player(), 105, 210); addObject (new Flower(), 143, 61); addObject (new Flower(), 342, 122); addObject (new Flower(), 98, 375); addObject (new Flower(), 497, 441); addObject (new Flower(), 220, 426); addObject (new Flower(), 494, 91); addObject (new Bee(), 437, 68); addObject (new Bee(), 561, 251); addObject (new Bee(), 561, 251); addObject (new ScoreBoard(), 387, 319); } public void populateWorld () { addObject( score , 100, 100); int x,y ; int limit = Greenfoot.getRandomNumber (15)+1; for(int count = 0 ; count < limit; count ++) { y = Greenfoot.getRandomNumber (getHeight()); x = Greenfoot.getRandomNumber (getWidth()); addObject (new Flower(), x,y); } } } This is the whole code I have right now...
danpost danpost

2015/1/7

#
I do not deny the existence of the method. I would not have mentioned it if I thought it was not there. Just having the method in the class is not enough to have it executed. When you right-click on the world and select 'void populateWorld()', you are telling it to execute. The system cannot just pick and choose when a method will run on its own, it must be told when to execute it. If you want it to execute during world construction, the method must be called from the constructor of the world (from the 'public FilmWorldPLAY()' block). The constructor current already has a bunch of calls to the 'addObject' method. The constructors of each 'new' object created in those lines are also executed. It would be quite a feat to have the 'addObject' method being executed without being called; however, it does exist in the World class.
lionkitten lionkitten

2015/1/7

#
Okay, I fixed that. Thanks for the help! Now I have another problem, I'm trying to make an array list. Since my game is like a quiz, I am putting this coding into the actor (Which is the flower) that holds the questions. When the player (The butterfly) makes contact with the flower, one of the questions from this array list will pop up and the player will answer it. Here is the code: questions = new ArrayList<Question>(); String questionText = "What is a documentary film?"; ArrayList<Answer> answers = new ArrayList<Answer>(); answers.add(new Answer("Non-fiction movie that in some way documents or captures reality.", false)); answers.add(new Answer("A movie that reveals an unusual, interesting or unknown angle.", false)); answers.add(new Answer("A movie directors feel a particular story or viewpoint is not being covered by mainstream media", false)); answers.add(new Answer("All of the above", true)); Collections.shuffle(answers); Question question = new Question(questionText, answers); String explanation = "The answer ALL OF THE ABOVE would be correct in this case."; explanation += "A documentary film is a non-fiction movie that documents and capture reality."; explanation += "It reveals unknown, interesting and unusual angles that a director may feel that particular point of view"; explanation += "must be covered by the media"; question.setExplanation(explanation); questions.add(question); I have an error. It says that: questions = new ArrayList<Question>(); has an error. The error is: <identifier> expected. What does that mean??
danpost danpost

2015/1/7

#
It means you did not supply what type of variable 'questions' is -- what type of data it is to hold ( 'List<Question>' )
lionkitten lionkitten

2015/1/7

#
So am I suppose to write List<questions> List<answer before the array like that...? But it says that I need a ; between List and <answer>
danpost danpost

2015/1/7

#
lionkitten wrote...
So am I suppose to write List<questions> List<answer before the array like that...? But it says that I need a ; between List and <answer>
Not like you have it here. Here, you have (almost) two different types one after another. The compiler expects each to be followed by a variable name. You gave this as the first few lines of code:
1
2
3
4
questions = new ArrayList<Question>();
         
String questionText = "What is a documentary film?";
ArrayList<Answer> answers = new ArrayList<Answer>();
The second and third lines give a type before the variable name -- 'questionText' is of type String and 'answers' is of type ArrayList<Answer>. The compiler wants to know what type of variable 'questions' is. You can tell it either ArrayList<Question> or just List<Question>; but, you must tell it something.
lionkitten lionkitten

2015/1/8

#
ok thanks~! How do I stop my countdown timer when it hits zero? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.Calendar; /** * Displays a countdown timer in SS:T format, where SS = seconds, and T = tenths of a second. */ public class CountdownTimer extends Actor { private int width = 60; // Dimensions of the display private int height = 20; private int prevSecond = -1; private int counter = 20; /** * The constructor creates the actor's image. */ public CountdownTimer() { GreenfootImage image = new GreenfootImage(width, height); image.setColor(Color.WHITE); image.fill(); image.setColor(Color.BLACK); image.drawRect(0, 0 ,width - 1, height - 1); setImage(image); updateDisplay(counter + ".0"); } /** * The clock is tick'd on every act() call, but the display changes * only when the second's increments. */ public void act() { if (counter > 0) { updateDisplay(tick()); } } /** * Increment the display string for the clock. */ public String tick() { // Compose new display string Calendar calendar = Calendar.getInstance(); int sec = calendar.get(Calendar.SECOND); int tsec = calendar.get(Calendar.MILLISECOND) / 100; if (counter == 0) { return("0.0"); } if (sec != prevSecond ) { prevSecond = sec; counter = counter - 1; } String now = (counter < 10 ? "0" + counter : counter) + "." + tsec; return(now); } /** * Update the clock with the new time string. */ public void updateDisplay(String newTime) { // x and y relative to the image. baseline of leftmost character. int x = 5; int y = 15; // Repaint the clock display GreenfootImage image = getImage(); image.setColor(Color.WHITE); image.fillRect(1, 1, width-2, height-2); // "erase" the display image.setColor(Color.BLACK); image.drawString(newTime, x, y); setImage(image); } /** * Returns the current value of the clock. */ public int getCounter() { return counter; } } and also, how do I add 5 points to my score board when a butterfly eats a flower?? import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Graphics; /** * Displays and increments a value. Could be used to keep score. * Very similar to the SimpleCounter class, except that the image for this class includes a * box and a border. * Note: * - an actor can compose its own image, on the fly, or as we say, programatically. * - the constructor creates the image when a ScoreBoard object is created. * - updateDisplay() edits the image to reflect the new score. * * @author Mark Hayes * @version 1.0 */ public class ScoreBoard extends Actor { private int score = 0; private final int WIDTH = 80; // width of the score board private final int HEIGHT = 20; // height of the score board /** * The constructor composes the image for the ScoreBoard. */ public ScoreBoard() { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(Color.WHITE); image.fill(); image.setColor(Color.BLACK); image.drawRect(0, 0 , WIDTH - 1, HEIGHT - 1); setImage(image); updateDisplay(); } public void add(int value) { score = score + value; updateDisplay(); } public void reset() { score = 0; updateDisplay(); } public int getScore() { return score; } /** * Update the score display. */ public void updateDisplay() { // x and y relative to the image. baseline of leftmost character. int x = 5; int y = 15; // "Repaint" the score display GreenfootImage image = getImage(); image.setColor(Color.WHITE); image.fillRect(1, 1, WIDTH-2, HEIGHT-2); // "erase" the display image.setColor(Color.BLACK); image.drawString("Score: " + score, x, y); setImage(image); } }
danpost danpost

2015/1/8

#
The CountdownTimer class appears to already be programmed to stop when the counter reaches zero. Ok. So that is your ScoreBoard class, which could help in the long run; but, for now, you need to post the Butterfly class, which has the code that has the butterfly eating a flower
lionkitten lionkitten

2015/1/8

#
The countdown timer doesn't stop though... bbutterfly class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Play { String array1 = {{"What is a documentary film?", "Non-fiction movie that in some way 'documents' or captures reality."}, {"How many types of documentary films are there?", "Six"}}; String array2 = {{"What do documentary films have that’s unique to it’s genre?", "It explores a certain aspect of anything"}, {"What is a documentaries goal?", "To change and improve societies and let them be aware of any problems that the documentary proposes"}}; String array3 = {{"Which genre is documentary films mostly on?", "Non-fiction"}, {"What signifies a horror film?", "Unsettling imagery and sound"}}; String array4 = {{"What signifies film sound?", "The orchestra, the SFX and human sounds"}, {"Who is the head of the sound departments?", "Sound Designer"}}; /** * Act - do whatever the PopUpMessage wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { { JFrame frame = new JFrame("InputDialog Example #1"); int i = Greenfoot.getRandomNumber(2); String answer = ""; while(!answer.equals(array1)) { answer = JOptionPane.showInputDialog(frame, array1); if(answer.equals(array1)) { System.out.println("Correct! Great job!"); } else { System.out.println("Wrong! Try Again!"); } } Greenfoot.stop(); } { JFrame frame = new JFrame("InputDialog Example #1"); int i = Greenfoot.getRandomNumber(2); String answer = ""; while(!answer.equals(array2)) { answer = JOptionPane.showInputDialog(frame, array2); if(answer.equals(array2)) { System.out.println("Correct! Great job!"); } else { System.out.println("Wrong! Try Again!"); } } Greenfoot.stop(); } { JFrame frame = new JFrame("InputDialog Example #1"); int i = Greenfoot.getRandomNumber(2); String answer = ""; while(!answer.equals(array3)) { answer = JOptionPane.showInputDialog(frame, array3); if(answer.equals(array3)) { System.out.println("Correct! Great job!"); } else { System.out.println("Wrong! Try Again!"); } } Greenfoot.stop(); } { JFrame frame = new JFrame("InputDialog Example #1"); int i = Greenfoot.getRandomNumber(2); String answer = ""; while(!answer.equals(array4)) { answer = JOptionPane.showInputDialog(frame, array4); if(answer.equals(array4)) { System.out.println("Correct! Great job!"); } else { System.out.println("Wrong! Try Again!"); } } Greenfoot.stop(); } { move(); turnAtEdge(); lookForFlower(); checkKeyPress(); } } public void turnAtEdge() { if ( atWorldEdge() ) { turn(17); } } public void move() { } public void lookForFlower() { if ( canSee(Flower.class) ) { //FilmWorldPLAY fw = (FilmWorldPLAY) getWorld(); eat(Flower.class); //Greenfoot.playSound("slurp.wav"); //fw.addObject(question, 100,200); } } public void checkKeyPress() { int speed = 3; if(Greenfoot.isKeyDown("up")) setLocation(getX(), getY() - speed); if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY() + speed); if(Greenfoot.isKeyDown("left")) setLocation(getX() - speed, getY()); if(Greenfoot.isKeyDown("right")) setLocation(getX() + speed, getY()); } } this is what i got
danpost danpost

2015/1/8

#
Provided you only have one ScoreCounter object in the world, you can add the following line of code within the 'if' block of the 'lookForFlower' method:
1
((Counter)getWorld().getObjects(ScoreBoard.class).get(0)).add(5);
Another way is to add a getter method in your world class for the scoreboard:
1
2
3
4
public ScoreBoard getScoreBoard()
{
    return score;
}
and add this line within the 'if' block of the 'lookForFlower' method of the Butterfly class:
1
((FilmWorldPLAY)getWorld()).getScoreBoard().add(5);
lionkitten lionkitten

2015/1/8

#
so i use this code you gave me: public ScoreBoard getScoreBoard() { return score; } and this one: ((FilmWorldPLAY)getWorld()).getScoreBoard().add(5); With this second one, it says that it cannot find the symbol - method getScoreBoard()
There are more replies on the next page.
1
2