如何为此游戏添加重置选项?

时间:2015-05-05 19:43:33

标签: java eclipse

我希望玩家能够在死后按“r”并重新启动。我想我应该把我的整个代码放到一个重置方法中,但我只是一个初学者,我还没到那里。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 800, HEIGHT = 800;
    private Thread thread;
    private boolean running = false;

    private BodyPart b;
    private ArrayList<BodyPart> snake;

    private Apple apple;
    private ArrayList<Apple> apples;

    private Random r;

    private int xCoor = 10, yCoor = 10;
    private int size = 20;

    private boolean right = true, left = false, up = false, down = false;

    private int ticks = 0;

    private Key key;

    public Screen() {
        setFocusable(true);
        key = new Key();
        addKeyListener(key);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));

        r = new Random();

        snake = new ArrayList<BodyPart>();
        apples = new ArrayList<Apple>();

        start();
    }

    public void tick() {
        if(snake.size() == 0) {
            b = new BodyPart(xCoor, yCoor, 10);
            snake.add(b);
        }

        if(apples.size() == 0) {
            int xCoor = r.nextInt(80);
            int yCoor = r.nextInt(80);

            apple = new Apple(xCoor, yCoor, 10);
            apples.add(apple);
        }

        for(int i = 0; i < apples.size(); i++) {
            if(xCoor == apples.get(i).getxCoor() && yCoor ==          apples.get(i).getyCoor()) {
                size++;
                apples.remove(i);
                i--;
            }
        }

        for(int i = 0; i < snake.size(); i++) {
            if(xCoor == snake.get(i).getxCoor() && yCoor ==  snake.get(i).getyCoor()) {
                if(i != snake.size() - 1) {
                    stop();
                }
            }
        }

        if(xCoor < -1) xCoor = 80;
        if(xCoor > 80) xCoor = -1;
        if(yCoor < -1) yCoor = 80;
        if(yCoor > 80) yCoor = -1;

        ticks++;

        if(ticks > 250000) {
            if(right) xCoor++;
            if(left) xCoor--;
            if(up) yCoor--;
            if(down) yCoor++;

            ticks = 185000;

            b = new BodyPart(xCoor, yCoor, 10);
            snake.add(b);

            if(snake.size() > size) {
                snake.remove(0);
            }
        }
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, WIDTH, HEIGHT);

        g.setColor(new Color(10, 50, 0));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.BLACK);
        for(int i = 0; i < WIDTH / 10; i++) {
            g.drawLine(i * 10, 0, i * 10, HEIGHT);
        }

        for(int i = 0; i < HEIGHT / 10; i++) {
            g.drawLine(0, i * 10, WIDTH, i * 10);
        }

        for(int i = 0; i < snake.size(); i++) {
            snake.get(i).draw(g);
        }
        for(int i = 0; i < apples.size(); i++) {
            apples.get(i).draw(g);
        }

    }

    public void start() {
        running = true;
        thread = new Thread(this, "Game Loop");
        thread.start();
    }

    public void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while(running) {
            tick();
            repaint();
        }
    }

    private class Key implements KeyListener {


        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();

            if(key == KeyEvent.VK_RIGHT && !left) { 
                up = false;
                down = false;
                right = true;
            }

            if(key == KeyEvent.VK_LEFT && !right) { 
                up = false;
                down = false;
                left = true;
            }

            if(key == KeyEvent.VK_UP && !down) {
                left = false;
                right = false;
                up = true;
            }

            if(key == KeyEvent.VK_DOWN && !up) {
                left = false;
                right = false;
                down = true;
            }


        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

    }

}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

首先,是的,你是在正确的轨道上。最好的方法是将启动代码(将所有变量设置为初始值)放在一个单独的方法中。然后当你按下&#39; r&#39;你只需要调用此方法,它就会重置&#39;本身。

要实现这一点,您应该在开始时进行所有变量初始化。目前,您在b = new BodyPart(xCoor, yCoor, 10);中初始化tick()。因为这只发生过一次(游戏开始时),所以最好放入你的启动方法。

初始化方法的粗略提议:

public void initialize() {
    snake = new ArrayList<BodyPart>();
    apples = new ArrayList<Apple>();
    b = new BodyPart(xCoor, yCoor, 10);
    snake.add(b);
}

此方法初始化所有可重置的变量&#39;。然后,您可以在int key = e.getKeyCode();KeyCode.R时调用此方法。

答案 1 :(得分:0)

您可以按如下方式修改代码: 我添加了评论以解释我的修改

//this resets the position/size of the snake and clears the array
public void reset() {
    snake.clear();
    apples.clear();
    xCoor = 10;
    yCoor = 10;
    size = 20;
    running = true;
}


private class Key implements KeyListener {
    //reset when you are dead and the user presses r
    if (!running && (e.getKeyChar() == 'r' || e.getKeyChar() == 'R')){
        reset();
    }
}


public void tick() {
    while (running){

        //prev code

        for(int i = 0; i < snake.size(); i++) {
            if(xCoor == snake.get(i).getxCoor() && yCoor ==  snake.get(i).getyCoor()) {
                if(i != snake.size() - 1) {
                    //don't kill the process, just stop the game and wait for the user to press 'r'
                    //you may need to do additional stuff here
                    running = false;
                }
            }
        }

        //remaining code
    }
}