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

2020/4/15

NEED HELP WITH SCHOOL PROJECT

macaroon72 macaroon72

2020/4/15

#
Hi guys! I am in need of some help with a school project for my Information CS class. I am working on a duck typing lab and can not seem to figure out how to do some of the starting features of the program. Below is my code. I need to know how to add the "livesleft" variable to the screen at the coordinate (250, 50). I was thinking about using "getWorld().addObject" or a print Statement, but can not seem to work around either of those ideas. Additionally, I need to ask the program if the person playing the game his the correct key (myKey), but I looked through my notes and can not find how to do so. There will probably be more questions to come(; . Thank you all in advance!!! PS - I am working in Greenfoot using Java Compiler! import greenfoot.*; public class Duck extends SmoothMover { private int score = 0; private int livesleft = 10; double velocity = 0.0; //Leave this myKey alone. Read it to see if you can understand //how it generates a letter randomly, it is similar to an //(int)(Math.random() * __ ) + ___ but this time I wanted a char. private String myKey = "" + (char)(Math.random() * 26 + 'A'); public Duck() { //This is called a Constructor. //The constructor is called the moment a Duck is built. //Leave this section alone, it draws the Duck's letter/key on the Duck GreenfootImage img = getImage(); img.setFont(new Font("VERDANA", true, false, 20)); img.drawString(myKey, 50, 80 ); } public void act() { setLocation(getExactX(), getExactY()+velocity); velocity+=0.1; if(isTouching(Gator.class)) { livesleft--; System.out.print(livesleft); ??????HELP??????? Greenfoot.playSound("quack.mp3"); getWorld().removeObject(this); if(livesleft==0) { Greenfoot.stop(); } else if(myKey==????HELP????) { } } } }
danpost danpost

2020/4/15

#
Maybe you should try using getWorld().showText(String, int, int) which would put the given string to the world at the given coordinates.
macaroon72 macaroon72

2020/4/15

#
I tried that, and when it compiled, it stated... Incompatible Types: Int cannot be converted to java.lang.string Any other ideas, and do you know the answer to the other question above which was... I need to ask the program if the person playing the game is typing the correct key (Math.random variable - myKey)? Thanks!
Super_Hippo Super_Hippo

2020/4/15

#
macaroon72 wrote...
Incompatible Types: Int cannot be converted to java.lang.string
1
getWorld().showText(""+livesleft, 50, 50);
macaroon72 macaroon72

2020/4/15

#
Actually, I worked it around and figured it out thanks to you! I still need help though, for my second question... I need to ask the program if the person playing the game is typing the correct key (Math.random variable - myKey)? Thanks!
macaroon72 macaroon72

2020/4/15

#
Super_Hippo wrote...
macaroon72 wrote...
Incompatible Types: Int cannot be converted to java.lang.string
1
getWorld().showText(""+livesleft, 50, 50);
Thank you this worked when I tried it. But my second question is still unknown! Thanks!
danpost danpost

2020/4/15

#
There are two methods you can use to get keyboard input from the user -- both in the Greenfoot class. Refer to API documentation.
RcCookie RcCookie

2020/4/15

#
If you want to actually ask the user for input you may use this:
1
2
3
else if(myKey.equals(Greenfoot.ask(„What is the key?“))){
    //your code
}
In any case though you don’t want to use myKey== but myKey.equals because == Checks if on both sides there is the same instance given while equals just checks the value of the given instance.
RcCookie RcCookie

2020/4/15

#
danpost wrote...
There are two methods you can use to get keyboard input from the user -- both in the Greenfoot class. Refer to API documentation.
Yeah, you could use Greenfoot.getKey() to get a String
macaroon72 macaroon72

2020/4/15

#
Okay, thank you both so very much! I will post here letting you know if it works! Wish me luck!
You need to login to post a reply.