user163505
user163505

Reputation: 499

Questions about Creating Rectangles

first off I am very sorry if I am asking this in the wrong place or doing something wrong by asking this. Please inform me if I need to correct anything. I am making a dodge ball type game where you move a square with a mouse and try to dodge enemies. The enemies will be squares themselves, and they will come from random sides of the screen. I want to know how I could:

A. Make the program create squares on it's own, increasing the amount the higher the player's score.

B. Make the location of the square's appearance a random place on the edges of the screen.

Here's my code. This is the part where I draw the rectangles:

public void paint(Graphics g){

        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);

    }

    public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);

    }

Here's the entire code for the GamePanel class:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

    //Global variables

    //Double buffering
    private Image dbImage;
    private Graphics dbg;


    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 500;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);

    //Game variable
    private Thread game;
    private volatile boolean running = false;
    public boolean mouseClicked = false;

    //Character variables
    int playerX = 150, playerY = 150;

    public class Mouse extends MouseAdapter{

        public void mousePressed(MouseEvent e){

            mouseClicked = true;

        }

        public void mouseReleased(MouseEvent e){

            mouseClicked = false;

        }

        public void mouseMoved(MouseEvent e){

            mouseClicked = false;

            repaint();
            playerX = e.getX()-25;
            playerY = e.getY()-25;
            if(playerX <= 50){
                playerX = 50;
            }
            else if(playerX >= 400){
                playerX = 400;
            }

            if(playerY <= 25){
                playerY = 25;
            }
            else if(playerY >= 400){
                 playerY = 400;
            }

            repaint();

        }
    }



    public GamePanel(){

        addMouseMotionListener(new Mouse());
        setPreferredSize(gameDim);
        setBackground(Color.BLUE);
        setFocusable(true);
        requestFocus(true);

    }

    public void run(){

        while(running){



        }

    }

    public void addNotify(){

        super.addNotify();
        startGame();

    }

    private void startGame(){

        if(game == null || !running){

            game = new Thread(this);
            game.start();
            running = true;

        }

    }

    public void stopGame(){

        if(running){

            running = false;

        }

        //Paint method


    }

    public void paint(Graphics g){

        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);

    }

    public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);

    }

    private void log(String s){

        System.out.println(s);

    }

}

Thank you for your time.

Upvotes: 0

Views: 136

Answers (1)

thomas
thomas

Reputation: 176

Well, I suppose it could be a question on how to accomplish a task in programming; so I'll give it a shot.

I won't give any code, rather I will offer an explanation on how to achieve what you are wanting to do. So the coding you'll have to research or figure out.

So GamePanel is your primary class in your java class.

Start by making a Rectangle class that is capable of painting itself. You will also want to include a "move" method to allow the rectangle (or enemy) to move itself (preferably in relation to type and speed/velocity). Inside of its constructor you can set its initial X, Y positions to a random value. To have it randomly start at the top (assuming top = 0, bottom = screen size y, and left = 0 and right = screen size x); you have four options: to randomly select a position at the top, set y = 0 and x = random int; to randomly select a position on the bottom set y = screen size - rectangle height, and x = random int; for left you'll set x = 0 and y = random int; and for right set x = screen size - rectangle width.

Note: This rectangle class could also be used for your player rectangle.

Next, you will need a datastructure inside of your GamePanel to contain all of the rectangles (enemies). If you aren't familiar with data structures look up how to do lists in java.

Also in GamePanel, set a int variable to what you want the max number of enemies to be. You can change this as the player's score increases. You will need another variable to keep track of the number of enemies you currently have, if you use the aforementioned list it will probably a length method you can use to get the number of current enemies in memory.

In your main loop, check to see how many enemies you have and if the number is lower, then create one enemy (essentially, you will create one enemy each loop if you don't have enough; this may be too slow or too fast depending on what you want); but the essential idea is (in psuedocode):

public void run(){

    while(running){

       if(current_enemies < max_enemies) {
           enemy = new Enemy()
           enemy_list.append(enemy)
       }

    }

}

You will have to check for when the enemies run off the screen and remove them from the list, otherwise once they all run off they will still exist but no new enemies will be created.

May not be the answer you were looking for, but I hope this helps.

Upvotes: 1

Related Questions