0

在我制作的游戏中,我有一个显示你分数的游戏结束屏幕,现在我想让你按空格键重新启动游戏,这样你就不需要关闭程序并再次打开它来再次玩它。问题不在于如何在按下空格键时让游戏重新启动,而是如何让游戏重新启动。我试过这段代码:

if(key == KeyEvent.VK_SPACE && papi.isAlive() != true){ 
            remove(papi);
            papi.setSize(600,600);
            add(papi);
            setVisible(true);
            repaint();
        } 

为什么它不起作用?我试图制作一个包含游戏代码的类的精确副本,然后添加它而不是相同的类,这样就可以了。不幸的是,我不能一直这样做,因为我需要它能够根据图层的需要重新启动多次,而不仅仅是一次。

这是启动游戏的代码:

package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JFrame implements KeyListener{
    private PAPITest papi;
    private JPanel panel;
    private boolean inMenu = true;
    private ImageIcon ii;
    private Image image;

    public Game(){
        addKeyListener(this);
        setFocusable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        requestFocusInWindow();
        requestFocus();
        ii = new ImageIcon("Ball Jump.png");
        image = ii.getImage();
        papi = new PAPITest();
        panel = new JPanel();
        add(panel);
        setSize(600,600);
        setVisible(true);
    }

    public void paint(Graphics g){
        if(inMenu == true){
            Graphics2D g2D = (Graphics2D) panel.getGraphics();
            g2D.drawImage(image, 0, 0, this);
            repaint();
        }
    }

    public void keyPressed(KeyEvent e){
        requestFocusInWindow();
        requestFocus();
        int key = e.getKeyCode();
        if(key == e.VK_SPACE && inMenu == true){
            inMenu = false;
            remove(panel);
            papi.setSize(600,600);
            papi.setAlive(true);
            add(papi);            
            setVisible(true); 
        }

        if (key == KeyEvent.VK_SPACE && papi.isAlive() != true){ 
            remove(papi);
            papi.setSize(600,600);
            add(papi);
            setVisible(true);
            repaint();
        }
    }

    public void keyReleased(KeyEvent e){

    }

    public void keyTyped(KeyEvent e){

    }

    public static void main(String[] args){
        new Game();
    }
}

这是游戏的代码: package Game;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.vecmath.*;

public class PAPITest extends Applet implements ActionListener,KeyListener{
    private TransformGroup objTrans,objTrans2, objTrans3, objTrans4, objTrans5, objTrans6, objTrans7;
    private Transform3D trans = new Transform3D();
    private BranchGroup objRoot = new BranchGroup();
    private BranchGroup objRoot2 = new BranchGroup();
    private BranchGroup objRoot3 = new BranchGroup();
    private BranchGroup objRoot4 = new BranchGroup();
    private BranchGroup objRoot5 = new BranchGroup();
    private BranchGroup objRoot6 = new BranchGroup();
    private BranchGroup objRoot7 = new BranchGroup();
    private SimpleUniverse u;
    private Canvas3D c;
    private BranchGroup scene, scene2, scene3, scene4, scene5, scene6, scene7;
    private Sphere sphere, sphere2, sphere3, sphere4, sphere5, sphere6, sphere7;
    private float height = 0.0f, sign = 1.0f, xloc = 0.0f;
    private float height2 = 0.0f, sign2 = -1.0f;
    private float rightX, right, rightDx, leftX, left, leftDx, rightX2, leftX2;
    private boolean isAlive = true;
    private JLabel scoreLabel;
    private int score = -400;
    private Timer timer;
    public int sumScore;


    public boolean isAlive(){
        return isAlive;
    }

    public void setAlive(boolean isAlive){
        this.isAlive = isAlive;
    }

    public BranchGroup createSceneGraph(){
        Color3f ambientColourRSphere = new Color3f(0.6f,0.0f,0.0f);
        Color3f emissiveColourRSphere = new Color3f(0.0f,0.0f,0.0f);
        Color3f diffuseColourRSphere = new Color3f(0.8f,0.0f,0.4f);
        Color3f specularColourRSphere = new Color3f(0.3f,0.0f,0.0f);
        float shininessRSphere = 20.0f;
        Appearance redSphereApp = new Appearance();
        redSphereApp.setMaterial(new Material(ambientColourRSphere,emissiveColourRSphere,
                             diffuseColourRSphere,specularColourRSphere,shininessRSphere));
        objRoot.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);
        sphere = new Sphere(0.15f,redSphereApp);
        objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans.setTransform(pos1);
        objTrans.addChild(sphere);
        objRoot.addChild(objTrans);
        BoundingSphere bounds = new BoundingSphere
                (new Point3d(0.0,0.0,0.0),100.0);
        Color3f light1Color = new Color3f(0.2f,0.2f,1.0f);
        Vector3f light1Direction = new Vector3f(+4.0f,-7.0f,-12.0f);
        DirectionalLight light1 = new DirectionalLight
                (light1Color,light1Direction);
        light1.setInfluencingBounds(bounds);
        objRoot.addChild(light1);
        Color3f ambientColor = new Color3f(1.0f,1.0f,1.0f);
        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        objRoot.addChild(ambientLightNode); 
        Color3f bgColor = new Color3f(0.0f, 0.2f, 1.0f);
        Background bg = new Background(bgColor);
        bg.setApplicationBounds(bounds);
        objRoot.addChild(bg);
        return objRoot;
    }

    public BranchGroup createSceneGraph2(){
        objRoot2.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans2 = new TransformGroup();
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot2.addChild(objTrans2);
        sphere2 = new Sphere(0.18f);
        objTrans2 = new TransformGroup();
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans2.setTransform(pos1);
        objTrans2.addChild(sphere2);
        objRoot2.addChild(objTrans2);
        return objRoot2;
    }

    public BranchGroup createSceneGraph3(){
        objRoot3.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans3 = new TransformGroup();
        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot3.addChild(objTrans3);
        sphere3 = new Sphere(0.18f);
        objTrans3 = new TransformGroup();
        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans3.setTransform(pos1);
        objTrans3.addChild(sphere3);
        objRoot3.addChild(objTrans3);
        return objRoot3;
    }

    public BranchGroup createSceneGraph4(){
        objRoot4.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans4 = new TransformGroup();
        objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot4.addChild(objTrans4);
        sphere4 = new Sphere(0.45f);
        objTrans4 = new TransformGroup();
        objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans4.setTransform(pos1);
        objTrans4.addChild(sphere4);
        objRoot4.addChild(objTrans4);
        return objRoot4;
    }

    public BranchGroup createSceneGraph5(){
        objRoot5.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans5 = new TransformGroup();
        objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot5.addChild(objTrans5);
        sphere5 = new Sphere(0.45f);
        objTrans5 = new TransformGroup();
        objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans5.setTransform(pos1);
        objTrans5.addChild(sphere5);
        objRoot5.addChild(objTrans5);
        return objRoot5;
    }

    public BranchGroup createSceneGraph6(){
        objRoot6.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans6 = new TransformGroup();
        objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot6.addChild(objTrans6);
        sphere6 = new Sphere(0.18f);
        objTrans6 = new TransformGroup();
        objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans6.setTransform(pos1);
        objTrans6.addChild(sphere6);
        objRoot6.addChild(objTrans6);
        return objRoot6;
    }

    public BranchGroup createSceneGraph7(){
        objRoot7.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans7 = new TransformGroup();
        objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot7.addChild(objTrans7);
        sphere7 = new Sphere(0.18f);
        objTrans7 = new TransformGroup();
        objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans7.setTransform(pos1);
        objTrans7.addChild(sphere7);
        objRoot7.addChild(objTrans7);
        return objRoot7;
    }

    public PAPITest(){
        setFocusable(true);
        setLayout(new BorderLayout());
        setVisible(true);
        setSize(505,525);
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        c = new Canvas3D(config);
        Color color = new Color(0.0f, 0.2f, 1.0f);
        setBackground(color);
        setSize(600,600);
        Font font = new Font("Helvatica",Font.BOLD,18);
        scoreLabel = new JLabel("Score: " + score);
        add("Center",c);
        c.addKeyListener(this);
        c.setSize(600,600);
        timer = new Timer(100,this);
        timer.start();
        scene = createSceneGraph();
        scene2 = createSceneGraph2();
        scene3 = createSceneGraph3();
        scene4 = createSceneGraph4();
        scene5 = createSceneGraph5();
        scene6 = createSceneGraph6();
        scene7 = createSceneGraph7();
        u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
        u.addBranchGraph(scene2);
        u.addBranchGraph(scene3);
        u.addBranchGraph(scene4);
        u.addBranchGraph(scene5);
        u.addBranchGraph(scene6);
        u.addBranchGraph(scene7);
        rightX = 2.5f;
        right = 4.0f;
        float randomSpawn = (float) Math.random() * -2.7f + -3.1f;
        float randomSpawn2 = (float) Math.random() * -5.7f + -15.1f;
        float randomSpawn3 = (float) Math.random() * 5.7f + 18.1f;
        leftX = randomSpawn;
        left = randomSpawn;
        rightX2 = randomSpawn3;
        leftX2 = randomSpawn2;
        rightDx = -.05f;
        leftDx = .05f;  
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyChar() == 'd'|e.getKeyChar() == 'D'|e.getKeyCode() == e.VK_RIGHT){
            xloc = xloc + .1f;
        }

        if(e.getKeyChar() == 'a'|e.getKeyChar() == 'A'|e.getKeyCode() == e.VK_LEFT){
            xloc = xloc - .1f;
        }
    }    

    public void keyReleased(KeyEvent e){

    }

    public void keyTyped(KeyEvent e){

    }

    public void actionPerformed(ActionEvent e){
        height += .1f * sign;
        if(Math.abs(height * 2) >= 1)
            sign = -1.0f * sign;
        height2 += .1f * sign2;
        if(Math.abs(height2 * 2) >= 1)
            sign2 = -1.0f * sign2;
        Vector3d vec;
        int thing = 1;
        int thing2 = 2;
        if(height < -.4f){
          vec = new Vector3d(1.0,.8,1.0);
        }else{
          vec = new Vector3d(1.0,1.0,1.0);
          thing = 2;
        }
        trans.setScale(vec);
        trans.setTranslation(new Vector3f(xloc,height - .15f,0.0f));
        objTrans.setTransform(trans);
        if(height < -.10f){
          vec = new Vector3d(1.0,1.0,1.0);
          thing2 = 1;
        }
        if(height < -.4f){
          trans.setScale(new Vector3d(1.0,1.0,1.0));
        }
        if(score >= 1000){
            rightDx = -.06f;
            leftDx = .06f; 
        }
        if(score >= 2000){
            rightDx = -.07f;
            leftDx = .07f; 
        }
        if(score >= 4000){
            rightDx = -.08f;
            leftDx = .08f; 
        }
        if(score >= 5000){
            rightDx = -.1f;
            leftDx = .1f; 
        }
        if(score >= 10000){
            rightDx = -.2f;
            leftDx = .2f; 
        }
        if(score >= 15000){
            rightDx = -.3f;
            leftDx = .3f; 
        }
        Vector3f vector = new Vector3f(rightX += rightDx,-.7f,0.0f);
        trans.setTranslation(vector);
        objTrans2.setTransform(trans);
        Vector3f vector2 = new Vector3f(leftX += leftDx,-.7f,0.0f);
        trans.setTranslation(vector2);
        objTrans3.setTransform(trans);
        Vector3f vector3 = new Vector3f(rightX2 += rightDx,-.4f,0.0f);
        trans.setTranslation(vector3);
        objTrans4.setTransform(trans);
        Vector3f vector4 = new Vector3f(leftX2 += leftDx,-.4f,0.0f);
        trans.setTranslation(vector4);
        objTrans5.setTransform(trans);
        Vector3f vector5 = new Vector3f(right += rightDx,-.7f,0.0f);
        trans.setTranslation(vector5);
        objTrans6.setTransform(trans);
        Vector3f vector6 = new Vector3f(left += leftDx,-.7f,0.0f);
        trans.setTranslation(vector6);
        objTrans7.setTransform(trans);
        float leftXDistance = vector2.x - xloc - .22f;
        float xDistance = vector.x - xloc + .22f;
        float yDistance = -.7f - height;
        float xDistance2 = vector3.x - xloc - .05f;
        float yDistance2 = -.4f - height + .22f;
        float xDistance3 = vector5.x - xloc + .22f;
        float leftXDistance2 = vector4.x - xloc + .05f;
        float leftXDistance3 = vector6.x - xloc - .22f;
        double distance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));
        double distance2 = Math.sqrt((xDistance2 * xDistance2) + (yDistance2 * yDistance2));
        double distance3 = Math.sqrt((xDistance3 * xDistance3) + (yDistance * yDistance));
        double leftDistance = Math.sqrt((leftXDistance * leftXDistance) + (yDistance * yDistance));
        double leftDistance2 = Math.sqrt((leftXDistance2 * leftXDistance2) + (yDistance2 * yDistance2));
        double leftDistance3 = Math.sqrt((leftXDistance3 * leftXDistance3) + (yDistance2 * yDistance2));
        if(distance < vec.x / 2 + .09f){
            if(thing == 2){
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
                rightX = randomSpawn;
                trans.setTranslation(vector);
                objTrans2.setTransform(trans);
            }
            if(thing == 1){
                score -= 100;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(distance3 < vec.x / 2 + .09f){
            if(thing == 2){
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
                right = randomSpawn;
                trans.setTranslation(vector5);
                objTrans6.setTransform(trans);
            }
            if(thing == 1){
                score -= 100;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(distance2 < vec.x / 2 + .09f){
            if(thing2 == 2){
                score += 200;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 4.1f + 20.9f;
                rightX2 = randomSpawn;
                trans.setTranslation(vector3);
                objTrans4.setTransform(trans);
            }
            if(thing2 == 1){
                score -= 200;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(leftDistance < vec.x / 2 + .09f){
            if(thing == 2){
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -1.5f + -2.7f;
                leftX = randomSpawn;
                trans.setTranslation(vector2);
                objTrans3.setTransform(trans);
            }
            if(thing == 1){
                score -= 100;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(leftDistance3 < vec.x / 2 + .09f){
            if(thing == 2){
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -1.5f + -2.7f;
                left = randomSpawn;
                trans.setTranslation(vector6);
                objTrans7.setTransform(trans);
            }
            if(thing == 1){
                score -= 100;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(leftDistance2 < vec.x / 2 + .09f){
            if(thing2 == 2){
                score += 200;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -3.1f + -17.9f;
                leftX2 = randomSpawn;
                trans.setTranslation(vector4);
                objTrans5.setTransform(trans);
            }
            if(thing2 == 1){
                score -= 200;
                objRoot.detach();
                isAlive = false;
            }
        }
        if(rightX < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            rightX = randomSpawn;
            trans.setTranslation(vector);
            objTrans2.setTransform(trans);
        }
        if(leftX > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            leftX = randomSpawn;
            trans.setTranslation(vector2);
            objTrans3.setTransform(trans);
        }
        if(right < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            right = randomSpawn;
            trans.setTranslation(vector);
            objTrans2.setTransform(trans);
        }
        if(left > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            left = randomSpawn;
            trans.setTranslation(vector2);
            objTrans3.setTransform(trans);
        }
        if(rightX2 < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            rightX2 = randomSpawn;
            trans.setTranslation(vector3);
            objTrans4.setTransform(trans);
        }
        if(leftX2 > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            leftX2 = randomSpawn;
            trans.setTranslation(vector4);
            objTrans5.setTransform(trans);
        }
        if(xloc > .8f){
            xloc = .8f;
        }
        if(xloc < -.8f){
            xloc = -.8f;
        }
        sumScore = score;
        if(!isAlive){
            setBackground(Color.black);
            remove(scoreLabel);
            remove(c);
            setSize(600,600);
            setVisible(true);
            Font font = new Font("Helvatica",Font.BOLD,24);
            if(sumScore < 0){
                sumScore = 0;
            }
            JLabel label = new JLabel("                             Your Score: " + sumScore);           
            label.setFont(font);
            add(label);
            setVisible(true);
            repaint();
        }
    }
}
4

1 回答 1

1

仍然很难说,但似乎这些是关键线:

    papi = new PAPITest();
    papig = new PAPIGame();

这两个似乎共同管理您的游戏状态,因此如果您重新创建它们,您应该重新开始您的游戏。例如:

if(key == KeyEvent.VK_SPACE && papi.isAlive() != true){ 
    remove(papi);
    papi = new PAPITest();
    papig = new PAPIGame();
    papi.setSize(600,600);
    add(papi);
    setVisible(true);
    repaint();
} 
于 2013-04-12T07:53:29.257 回答