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

2017/2/15

Multiplayer (two player) game, shooting in 8 directions

codica codica

2017/2/15

#
I want to make a multiplayer game with two bees that fly and want to shoot each other with some kind of bullet. They both should be able to move in 8 directions (N, S, E, W, NE, SE, NW, SW). Bullets should fly in the direction that bee was while shooting. One shooting the other adds 1 on it's score. How to make them shoot on the button pressed and how to make game to be playable in two players? Also, i have problem with this code i made, when a bee flies in directions which needs two button pressed (left-up, right-up, left-down or right-down) it sometimes flips the opposite of that side and continues flying like that.
public class bee extends Actor
{
    
    public void act() 
    {
        
        int speed = 6;
        if(Greenfoot.isKeyDown("up")){
            setLocation(getX(), getY() - speed);
            setRotation(270);
            
        }
        if(Greenfoot.isKeyDown("down")){
            setLocation(getX(), getY() + speed);
            setRotation(90);
           
        }
        if(Greenfoot.isKeyDown("left")){
            setLocation(getX() - speed, getY());
            setRotation(180);
            
           
            
        }
        if(Greenfoot.isKeyDown("right")){
            setLocation(getX() + speed, getY());
            setRotation(0);
            
        }
        
        
         if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("right")){
           
            setRotation(-45);
        }
        if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("left")){
          
            setRotation(45);
        }
        if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("left")){
          
            setRotation(135);
        }
        if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("right")){
          
            setRotation(225);
        }
    }  

}
danpost danpost

2017/2/16

#
I think you have the angles on lines 38 and 46 reversed. To make for 2 players, each controlling a bee, you will either need to create another class for a second bee that uses different keys for moving and shooting or, modify your current bee class so you can create two objects of the class that use a different set of keys. The latter is the preferred way, which will require String fields for the key assignments and a constructor or method to set the assigned keys to the bee. I see nothing in your code as yet for shooting or scoring.
codica codica

2017/2/17

#
Sometimes it turns like it should, and sometimes it don't. Got this part of multiplayer, and now how should i do the score part?
You need to login to post a reply.