简体   繁体   English

如何将LinkedList addObject添加到游戏中

[英]How can I add LinkedList addObject to my game

I am trying to make a game with a GameEngine. 我正在尝试用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. 我按照本教程示例进行操作 ,在该视频中,他很好地解释了LinkedList,并且我理解了这个概念,但是我想使用我拥有的GameEngine.java (在下面链接)。 So Implement the code in my code but I cannot see the result of handler.addObject(new Box(200, 100)); 所以在代码中实现代码,但看不到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); 蓝色矩形示例为drawRectangle(100,100,32,32); from game.java 来自game.java

So my question is How can I see the handler.addObject result without change GameEngine.java 所以我的问题是如何在不更改GameEngine.java的情况下查看handler.addObject结果

Here is game.java; 这是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 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 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 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 应该是2个蓝色框,来自game.java之一,第二个来自box.java

As far as I can see, you only created 1 box in your main method. 据我所知,您仅在main方法中创建了1个框。 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. 只有一个handler.addObject()调用。 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(); 就像您现在调用了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. 如果您想学习OOP,并且已经对Java基础有了扎实的了解,那么您不应该使用java.awt来做Graphics,因为它已经过时了。 More modern game development tools are LWJGL 2-3, or OpenGL. 更现代的游戏开发工具是LWJGL 2-3或OpenGL。 If you aren't that well versed in OOP programming, acrobatics such as Linked Lists are really unnecessary to learn. 如果您不熟悉OOP编程,则实际上不需要学习诸如链表之类的杂技。 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. 编程时,不要过多使用输入栏,或者至少在Stack Overflow上缩小输入栏的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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