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

2019/2/27

Multiplayer

t3mp3st_0 t3mp3st_0

2019/2/27

#
I have to do a multiplayer game for the rocket game for school. It is not over the internet; it is on the keyboard for someone near you to play with. I cannot add another actor. I have already created a second rocket for the second player to play as, but how do i make it independent?
Andrew.2 Andrew.2

2019/2/27

#
I just made 2 almost identical classes-one for each player. if you really want to use only one i guess you can make your actor take an input and you change the controls based on it.
D_S D_S

2019/3/3

#
i made something like this and i had one actor controlled by the arrow keys and the second controlled by the asdw keys. something similar would easily enable multi-player off the same keyboard.
danpost danpost

2019/3/3

#
For rockets, you may want to use w and up for acceleration and a/d and left/right for turning. Only one class is really needed. Just use a boolean parameter to identify which one to create:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// with fields
String left, right, thrust;
 
// instead of
public Rocket() { ...
 
// use
public Rocket(boolean p2)
{
    if (p2)
    {
        left = "left";
        right = "right";
        thrust = "up";
    }
    else
    {
        left = "a";
        right = "d";
        thrust = "w";
    }
    ...
}
When creating a Rocket object (in World subclass):
1
2
Rocket player1 = new Rocket(false);
Rocket player2 = new Rocket(true);
In action code, you would use code similar to the following for checking key presses (back in Rocket class):
1
if (Greenfoot.isKeyDown(thrust))
You need to login to post a reply.