简体   繁体   中英

How can I add LinkedList addObject to my game

I am trying to make a game with a GameEngine. I am following with this tutorial example , In that video he explain LinkedList very well and I understood the concept but I would like to use GameEngine.java (linked below) that I have. So Implement the code in my code but I cannot see the result of handler.addObject(new Box(200, 100)); in my game.

Here is the screenshot of the game screen.

在此处输入图片说明

The blue rectangle example is coming drawRectangle(100,100,32,32); from game.java

So my question is How can I see the handler.addObject result without change GameEngine.java

Here is game.java;


import java.awt.Canvas;



public class game extends GameEngine{


    private static Handler handler;

    public static void main(String args[]) {


        handler = new Handler();
        handler.addObject(new Box(200, 100));

        createGame(new game());

    }









    @Override
    public void update(double dt) {
        // TODO Auto-generated method stub

        handler.update(dt);
    }

    @Override
    public void paintComponent() {
        // TODO Auto-generated method stub

        changeBackgroundColor(red);
        clearBackground(500, 500);


        changeColor(blue);
        drawRectangle(100,100,32,32);


        handler.paintComponent(mGraphics);


    }


}

GameObject.java

import java.awt.Graphics;
import java.awt.Rectangle;

public abstract class GameObject extends GameEngine{

    protected int x, y;
    protected float velX = 0, velY = 0 ;


    public GameObject(int x , int y) {

        this.x = x;
        this.y = y;


    }

    public abstract void update(double dt);
    public abstract void paintComponent(Graphics g);
    public abstract Rectangle getBounds();



    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public float getVelX() {
        return velX;
    }

    public void setVelX(float velX) {
        this.velX = velX;
    }

    public float getVelY() {
        return velY;
    }

    public void setVelY(float velY) {
        this.velY = velY;
    }

    @Override
    public void paintComponent() {
        // TODO Auto-generated method stub

    }



}

Box.java

import java.awt.Graphics;
import java.awt.Rectangle;

public class Box extends GameObject{



    public Box(int x, int y) {
        super(x, y);

    }



    public Rectangle getBounds() {

        return null;
    }



    @Override
    public void update(double dt) {
        // TODO Auto-generated method stub

        x += velX;
        y += velY;

    }




    @Override
    public void paintComponent(Graphics g) {
        // TODO Auto-generated method stub

        changeColor(blue);
        drawRectangle(x,y,32,32);

    }
}

Handler.java


import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

    LinkedList<GameObject> object = new LinkedList<GameObject>();


    public void update(double dt) {

        for(int i = 0; i <object.size(); i++) {

            GameObject tempObject = object.get(i);

            tempObject.update(dt);

        }

    }

    public void paintComponent(Graphics g) {

        for(int i = 0; i <object.size(); i++) {

            GameObject tempObject = object.get(i);

            tempObject.paintComponent(g);

        }

    }

    public void addObject(GameObject tempObject) {
        object.add(tempObject);
    }

    public void removeObject(GameObject tempObject) {
        object.remove(tempObject);
    }


}

Here is the result. Should be 2 blue box one of from game.java the second from box.java

As far as I can see, you only created 1 box in your main method. This is because

public static void main(String args[]) {


    handler = new Handler();
    handler.addObject(new Box(200, 100));

    createGame(new game());

}

only has one handler.addObject() call. As a result, you are most likely going to end up with one box. As I have not written your code, I assume that this is indeed the case.

public static void main(String args[]) {


    handler = new Handler();
    handler.addObject(new Box(200, 100));
    handler.addObject(new Box(200, 200));

    createGame(new game());

}

Will likely give you two boxes, as you have now called handler.addObject(); twice.

If you're trying to learn OOP, and you already have a solid understanding of Java basics, you shouldn't be doing Graphics with java.awt, as it's highly deprecated. More modern game development tools are LWJGL 2-3, or OpenGL. If you aren't that well versed in OOP programming, acrobatics such as Linked Lists are really unnecessary to learn. I've found that simple text based projects such as this text adventure tutorial are more basic and can teach polymorphism and other concepts.

Also, you have far too much free space in your code. Don't use the enter bar too much when programming, or at least shrink it down to size on Stack Overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM