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

Comments for Hangman

Return to Hangman

PeachPeach

2014/5/14

how do you declare the method showLetter
PeachPeach

2014/5/14

sorry ignore my previous comment
PeachPeach

2014/5/14

how do you declare a color variable eg. black or blue
danpostdanpost

2014/5/14

@Peach, please start a new discussion if you have a question that does not pertain to a specific scenario.
PeachPeach

2014/5/14

i don't understand what this means "param alphaChar the letter to display on this new image" what is the parameter supposed to be?
danpostdanpost

2014/5/14

@Peach, you are talking about a parameter in the 'getLetterImage' method. '@param' means that the following word is the name of a parameter. 'alphaChar' is the name I gave the String parameter. 'the letter to display on this new image' tells you that the new image created within the method will contain the glyph of the letter contained in the 'alphaChar' string. The calling method, with the expressions for the arguments already evaluated, would look like this: getLetterImage("L", java.awt.Color.black); You were asking about declaring a color variable. You declare one just like you declare any other object. java.awt.Color color = null; // declare variable to hold a color color = java.awt.Color.blue; // assignment example one color = new java.awt.Color(255, 64, 40); // assignment example two If using 'import java.awt.Color;', the previous would be: Color color = null; color = Color.blue; color = new Color(255, 64, 40);
PeachPeach

2014/5/16

so I would need 26 getLetterImage methods for all 26 letters of the alphabet? eg. getLetterImage("A", java.awt.Color.black); getLetterImage("B", java.awt.Color.black); getLetterImage("C", java.awt.Color.black); etc thanks
PeachPeach

2014/5/16

when I write this, private GreenfootImage getLetterImage("A",java.awt.Color.black) a message saying "illegal start of type" keeps popping up. how do I fix it? also, in the method "return new GreenfootImage("A", 36, black, null);" what does the number 36 represent? Also,
danpostdanpost

2014/5/16

@Peach, it looks like you are missing a comma between "A" and java.awt.Color.black. And, no, you do not have to have 26 methods, one for each letter. The parameter 'alphaChar' can be any one of the alpha characters that is passed to the 'getLetterImage' method.. As far as the '36', refer to the following link: http://www.greenfoot.org/files/javadoc/greenfoot/GreenfootImage.html#GreenfootImage(java.lang.String, int, java.awt.Color, java.awt.Color)
danpostdanpost

2014/5/16

@Peach, maybe you are not missing a comma (the comma is so close to the 'j', it appears to be part of the 'j'). Please start a discussion on this issue showing how you are trying to use 'private GreenfootImage getLetterImage("A", java.awt.Color.black)'. Without context it is hard to figure out what you are trying to do.
PeachPeach

2014/5/19

I don't really understand this part of the code, could you please explain it? word = words.nextWord(); int baseX = getWidth()/2 - word.length() * 18 + 9; bg.setColor(Color.black); for (int i=0; i<word.length(); i++) { bg.fillRect(baseX+36*i, getHeight()-10, 24, 3); what is baseX?
danpostdanpost

2014/5/19

@Peach, the code you are showing gets a new random word and draws the underlines on the world background for each letter in the new word. 'baseX' is assigned the calculated x-coordinate for where the underline for the first letter in the new word is to go. Once that value is determined, the 'for' loop can draw all the underlines for all the letters in the new word in the proper positions so the word is centered along the base of the screen.
PeachPeach

2014/5/19

do you need to write "private int baseX;" under "public class WhiteBoard extends World"
danpostdanpost

2014/5/19

@Peach, what is 'WhiteBoard'? my world class is called 'Hangman'.
PeachPeach

2014/5/20

sorry typo. i meant to type "public class hangman extends World"
danpostdanpost

2014/5/20

@Peach, did I do so? if not, then no.
WatchbWatchb

2014/5/21

This is a very nice game! I was just wondering in your method "LetterPressed" what is the parameter? what is a string key? and what does this part of the code mean: letterClicked(letters[(int)key.charAt(0)-65]) In your next method, "private void letterClicked(Letter actor)" what is the parameter "letter actor"?
danpostdanpost

2014/5/21

@Watchb, in the middle section of the act method, you will see where I detect a keystroke, make it uppercase and call the 'letterClicked' method if the key is between "A" and "Z" inclusive. key.charAt(0) returns the ascii code value for the first (and only) character in the 'key' String object ('A' = 65 and 'Z' = 90); subtracting 65 from that gives a range of 0 to 25, which is the index of the Letter actor in the 'letters' array that contains the image of the 'key' letter. The 'letters' array contains the Letter actors that can be clicked on near the bottom of the screen. The Letters class can be found at the end of the Hangman class code.
KellyEmmerichKellyEmmerich

2014/5/22

Danpost, where I can see your code?
danpostdanpost

2014/5/22

@KellyEmmerich, click on the 'Run' button and click on one of the buttons in the top right.
WatchbWatchb

2014/5/23

Can I download this game onto greenfoot? I would like to take a look at your world classes and actor classes!
danpostdanpost

2014/5/23

@Watchb, just run the scenario and click on the buttons near the top-right of the window to look at the classes.
WatchbWatchb

2014/5/23

Danpost, what is the getGeneric actor method? i cannot find it in the greenfoot API
danpostdanpost

2014/5/23

@Watchb, the 'getGenericActor' method is within the Hangman class.
KellyEmmerichKellyEmmerich

2014/5/23

@Thank you very much :D
PeachPeach

2014/5/24

@Danpost, in this line of the code "addObject(letters[i] = new Letter(i), 24+38*(i%13),510+30*(i/13));" what does "24+38*(i%13),510+30*(i/13))" mean?
danpostdanpost

2014/5/24

@Peach, obviously it is a call to 'addObject', a method in the World class that uses the signature 'addObject(actor, int, int)'. The actor is a new Letter object that is being placed within the array of letters declared near the top of the class. This line is found within a 'for' loop that iterates 'i' through the numbers 0 to 25 (for each letter of the alphabet). Notice in the scenario how I split the 26 letters into two rows of 13 letters each. The first int (the x-coordinate of where to place the letter actor) is '24+38*(i%13)'. This says to start at x-coordinate 24 and add 'i' factors of 38 (the distance between each letter). Taking the modulus 13 of 'i' starts the factor back at zero again for the second row. The second int (the y-coordinate of where to place the letter actor) is '510+30*(i/13)'. This says to start at y-coordinate 510 and add a factor of 30 for the second row ('i/13' will return 0 until 'i' reaches 13; then it will return one for the rest of the loop).
PeachPeach

2014/5/24

@danpost, thank you for the explanation. If I wanted to have all the the 26 letters in 1 row, what would the code be like? do I just delete "(i%13)" and "(i/13)" from the code? so like this: addObject(letters[i] = new Letter(i), 24+38, 510+30; thank you!
danpostdanpost

2014/5/24

@Peach, if the world was wide enough to hold them, it would be 24+38*i, 510. The world width would have to be about 1000.
WatchbWatchb

2014/5/24

Danpost, is it possible to not use the getGenericActor method in the game? For example, is it possible to just create actor classes for each body part (head, right arm, right leg, body, etc)?
danpostdanpost

2014/5/24

@Watchb, absolutely.
WatchbWatchb

2014/5/24

danpost, could you please explain how you can do that? (I am quite new to java programming), thank you.
danpostdanpost

2014/5/24

Basically, you would just put the animation code for each within the 'addedToWorld' method of each class.
TotalBOSS01TotalBOSS01

2014/6/27

ill admit i failed to guess syzygy... i had never heard of that word
olenzolenz

2015/6/8

As my laptop does not run the Java plugin in a browser, I cannot access the scenario. Unfortunately, I am also unable to find the scenarios source code. Where can I find it?
danpostdanpost

2015/6/8

@olenz, the source for this scenario was not provided for download. It is, however, viewable while running the scenario. Did you try using a different browser (maybe IE)?
olenzolenz

2015/6/8

Nah, the trouble is that running a Java plugin on my laptop is disallowed administratively. So there is no difference between IE, Chrome or Firefox.
olenzolenz

2015/6/8

I managed to get it running, but I do not see how to get the source code, or even view it...
olenzolenz

2015/6/8

Oh, OK, I found it. Sorry.
A new version of this scenario was uploaded on 2020-10-28 19:49:18 UTC
A new version of this scenario was uploaded on 2020-10-28 19:52:15 UTC
A new version of this scenario was uploaded on 2020-10-28 19:52:38 UTC
A new version of this scenario was uploaded on 2020-10-28 19:57:06 UTC
A new version of this scenario was uploaded on 2020-10-28 20:01:38 UTC
Roshan123Roshan123

2021/10/7

Would u tell me what r the words omitted in Words class
Roshan123Roshan123

2021/10/7

And what is the difference between java. util. Random and Greenfoot. getRandomNumber()?
danpostdanpost

2021/10/7

The words omitted are the Hangman puzzle answers (not given for obvious reasons). Little difference between the two randoms. In fact, the method Greenfoot.getRandomNumber(int) uses java.util.Random in its implementation.
danpostdanpost

2021/10/7

As far as random, I used it here because I wanted to ensure a new random seed value each and every loading (greenfoot probably does it in a somewhat similar manner, but anyway ...).
Roshan123Roshan123

2021/10/7

I just asked the words to chea.... Whatever it may be, but the main thung is that how u got to know that Greenfoot.getRandomNubmer() is just a implementation of java.util.Random? Is their any api for all the methods of greenfoot(i mean how the methods are created and not the api which shows how to use it) If yes, plz share the link
danpostdanpost

2021/10/7

From source (comments are mine): ----------------- // importing class import java.util.Random; // creating Random object private static Random randomGenerator = new Random(); // using Random object public static int getRandomNumber(int limit) { return randomGenerator.nextInt(limit); }