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

2019/10/2

Choosing Characters

CodingCowgirl CodingCowgirl

2019/10/2

#
Hi, Guys! I decided to code my own game. It's simple. You play a witch or a wizard, who has to avoid other flying witches or wizards. Now my questions is: how do I code it that my player can choose if he or she wanna be a witch or a wizard. I thought about two yellow squares, where the player can click on. What's ya tip? (P.S.: I'd use the if-method.)
danpost danpost

2019/10/2

#
CodingCowgirl wrote...
how do I code it that my player can choose if he or she wanna be a witch or a wizard. I thought about two yellow squares, where the player can click on. What's ya tip? (P.S.: I'd use the if-method.)
There are so may ways that you can have the user make this choice. It is really up to you, as the designer of the game, to decide which way to go with this. Your suggested way would have the two squares act like buttons and you would have two if statements. In pseudo-code:
if (<< witch button clicked>>) { << witch player setup >> }
if (<< wizard button clicked >>) { << wizard player setup>> }
P.S. "if-method" is not a thing. You can call it an "if block" or an "if command structure". The individual parts are named as follows.
if (<< condition >>) // an "if statement"
{ << responsive code >> } // an "if block" or "if code block"
CodingCowgirl CodingCowgirl

2019/10/2

#
Ok. But do I have to code "Witch/Wizard" button first? The Player as well?
CodingCowgirl CodingCowgirl

2019/10/2

#
BTW, @danpost, do you know how I code that the "other flying objects" are created randomly. So, that they fly from the right side to the left side in random "places"?
danpost danpost

2019/10/2

#
CodingCowgirl wrote...
Ok. But do I have to code "Witch/Wizard" button first? The Player as well?
It is always best to keep your project, as you build it, in a "running" state ("Compiled -- no errors found" state). Buttons can be basic actors that you retain in fields. You really do not need to "code" a button as the world they are in can test for clicks on them and proceed accordingly. Something like this (in a world class)
private Actor button1, button2;
private Player player;

public void act()
{
    if (button1 != null && Greenfoot.mouseClicked(button1)) player = new Player("wizard");
    if (button2 != null && Greenfoot.mouseClicked(button2)) player = new Player("witch");
    // other codes
}
You could have a simple Actor subclass like this (it is the entire class):
public class SimpleActor extends greenfoot.Actor { }
and the world can then set up the buttons:
button1 = new SimpleActor();
button1.setImage(new GreenfootImage("Wizard", 48, Color.BLACK, Color.YELLOW));
addObject(button1, getWidth()/3, 300);
Notice how after each step (pretty much, each line, in order), the project will still run.
danpost danpost

2019/10/2

#
CodingCowgirl wrote...
BTW, @danpost, do you know how I code that the "other flying objects" are created randomly. So, that they fly from the right side to the left side in random "places"?
If they fly horizontally across the screen, it is rather simple. Just uses a few random values. One for when to spawn one and one to determine where to spawn (vetrically) along the right side of the window. This would be done in the world's act method.
CodingCowgirl CodingCowgirl

2019/10/2

#
Thanks for the code. But I thought about that we got two yellow squares besides each other and you click on the avatar you want. So, I have to connect Wrizard/Witch as a class with the Buttons in the world class, haven't I?
danpost danpost

2019/10/2

#
CodingCowgirl wrote...
Thanks for the code. But I thought about that we got two yellow squares besides each other and you click on the avatar you want.
In the button set up code given above, the image set to the button can be whatever -- not necessarily that which I gave it. Also, that set up code would be duplicated for the other button. Only differences would be the image given (maybe) and the x-coordinate at which it is placed into the world (use 2 times the value given for button1 -- 2*getWidth()/3
So, I have to connect Wrizard/Witch as a class with the Buttons in the world class, haven't I?
You only need one Player class -- not a Wizard and a Witch class (unless something more than just the image(s) will be different).
CodingCowgirl CodingCowgirl

2019/10/3

#
Ok.
You need to login to post a reply.