Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1109873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:18:23+00:00 2026-05-17T02:18:23+00:00

I’m newbie to Game programming. I’m trying to develop a simple shooting game. The

  • 0

I’m newbie to Game programming. I’m trying to develop a simple shooting game. The game ground which I’m trying is a circular one. The game has one shooter and 5 droids. The shooter and the droids have to move within the circular area only. Following is the code snippet i tried.

I tried implementing the game using the State design pattern.
Got struct in the logic where the Shooter is not allowed to cross the circular boundary.
Please help in completing the StateImpl class which has the logic of changing the shooter
position.

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() {

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:18:23+00:00Added an answer on May 17, 2026 at 2:18 am

    Instead of initializing the circle in the paint() method, keep it (and more importantly its centre) in a class variable. Then, whenever the shooter moves, measure the distance between the centre of the circle and the shooter’s new location. If that distance is larger than the radius of the circle, block the shooter’s movement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.