2010-10-02 3 views
0

Je suis novice en programmation de jeux. J'essaie de développer un jeu de tir simple. Le terrain de jeu que j'essaie est circulaire. Le jeu a un tireur et 5 driods. Le tireur et les driods doivent se déplacer dans la zone circulaire uniquement. Voici l'extrait de code que j'ai essayé.Logique pour déplacer un élément dans la limite circulaire uniquement

J'ai essayé d'implémenter le jeu en utilisant le motif de conception d'état. Structure dans la logique où le tireur n'est pas autorisé à franchir la limite circulaire. S'il vous plaît aider à remplir la classe StateImpl qui a la logique de changer le jeu de tir positon

public class Board extends JPanel implements ActionListener { 
     private static final long serialVersionUID = -397810249729996307L; 
     private final Timer timer; 
     private final Shooter shooter; 

    public Board(Point boardDimensions) { 
     addKeyListener(new TAdapter()); 
     setFocusable(true); 
     setBackground(Color.WHITE); 
     setDoubleBuffered(true); 
     setSize(boardDimensions.x, boardDimensions.y); 
     shooter = new Shooter(new Point(200, 225), boardDimensions); 
     timer = new Timer(5, this); 
     timer.start(); 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 
     Graphics2D g2d = (Graphics2D) g; 
     // g2d.draw(new Ellipse2D.Double(20, 10,350,350)); 
     Ellipse2D.Double circle1 = new Ellipse2D.Double(20, 10, 350, 350); 
     g2d.draw(circle1); 
     g2d.drawImage(shooter.getImage(), shooter.getShooterPosition().x, 
       shooter.getShooterPosition().y, this); 

     g2d.setColor(Color.BLUE); 

     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 

    /** 
    * Called by the AWT just after the user informs the listened-to component 
    * that an action should occur. 
    */ 
    public void actionPerformed(ActionEvent e) { 
     repaint(); 
    } 

    private class TAdapter extends KeyAdapter { 

     @Override 
     public void keyPressed(KeyEvent e) { 

      // Method to enable use of keys for control of the craft; 
      int key = e.getKeyCode(); 

      if (key == KeyEvent.VK_LEFT) { 
       shooter.moveLeft(); 
      } 
      if (key == KeyEvent.VK_UP) { 
       shooter.moveForward(); 
      } 
      if (key == KeyEvent.VK_RIGHT) { 
       shooter.moveRight(); 
      } 
      if (key == KeyEvent.VK_DOWN) { 
       shooter.moveBackward(); 
      } 
     } 
    } 

    public void twait() { 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      System.out.println("main thread interrupted"); 
     } 
    } 
} 


public class Shooter { 

    private Point shooterPosition; 

    private final State state; 

    protected Image image; 

    public Shooter(Point shooterPosition, Point boardSize) { 
     super(); 
     this.shooterPosition = shooterPosition; 
     this.state = new StateImpl(this); 
     ImageIcon ii = new ImageIcon(this.getClass().getResource("bld2.png")); 
     image = ii.getImage(); 
    } 

    public Point getShooterPosition() { 
     return shooterPosition; 
    } 

    public void setShooterPosition(Point shooterPosition) { 
     this.shooterPosition = shooterPosition; 
    } 

    public void moveLeft() { 
     this.shooterPosition = state.moveLeft(); 
    } 

    public void moveRight() { 
     this.shooterPosition = state.moveright(); 
    } 

    public void moveForward() { 
     this.shooterPosition = state.moveForward(); 
    } 

    public void moveBackward() { 
     this.shooterPosition = state.moveBackward(); 
    } 

    public Image getImage() { 
     return image; 
    } 

    public void setImage(Image image) { 
     this.image = image; 
    } 

    @Override 
    public String toString() { 
     return "Shooter [shooterPosition=" + shooterPosition + ", state=" 
       + state + ", image=" + image + "]"; 
    } 

} 


public interface State { 

    public Point moveForward(); 

    public Point moveBackward(); 

    public Point moveLeft(); 

    public Point moveright(); 

    public void shoot(); 

} 

public class StateImpl implements State { 

    private final Shooter shooter; 
    private final int boardSize = 200; 

    public StateImpl(Shooter shooter) { 
     this.shooter = shooter; 
    } 

    public Point moveForward() { 
     Point currentShooterPosition = this.shooter.getShooterPosition(); 
     int newY = (currentShooterPosition.y + 1) > boardSize ? boardSize 
       : currentShooterPosition.y + 1; 
     return new Point(currentShooterPosition.x, newY); 
    } 

    public Point moveBackward() { 
     Point currentShooterPosition = this.shooter.getShooterPosition(); 
     return new Point(currentShooterPosition.x, currentShooterPosition.y - 1); 
    } 

    public Point moveLeft() { 
     Point currentShooterPosition = this.shooter.getShooterPosition(); 
     return new Point(currentShooterPosition.x - 1, currentShooterPosition.y); 
    } 

    public Point moveright() { 

     Point currentShooterPosition = this.shooter.getShooterPosition(); 
     int newX = (currentShooterPosition.x + 1) > boardSize ? boardSize 
       : currentShooterPosition.x + 1; 
     return new Point(newX, currentShooterPosition.y); 
    } 

    public void shoot() { 

    } 
} 

Répondre

1

Au lieu d'initialiser le cercle dans la méthode de peinture(), gardez-le (et surtout son centre) dans une variable de classe . Ensuite, à chaque fois que le tireur se déplace, mesurez la distance entre le centre du cercle et le nouvel emplacement du tireur. Si cette distance est plus grande que le rayon du cercle, bloquez le mouvement du tireur.

Questions connexes