I want players to be able to press "r" after dying and be able to restart. I think I'm supposed to put my entire code into a reset method, but I am only a beginner, and I'm not quite there yet.
我希望玩家能够在死后按“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 } }}
Any help is greatly appreciated!
任何帮助是极大的赞赏!
2 个解决方案
#1
You can modify your code as follows:I've added comments to explain my modifications
您可以按如下方式修改代码:我添加了注释来解释我的修改
//this resets the position/size of the snake and clears the arraypublic 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 }}
#2
First of all, yes you are on the right track. The best way is to put the initilization code (setting all variables to their initial values) in a seperate method. Then when you press 'r' you simply call this method and it will 'reset' itself.
首先,是的,你是在正确的轨道上。最好的方法是将启动代码(将所有变量设置为初始值)放在一个单独的方法中。然后,当你按'r'时,你只需调用这个方法,它就会“重置”自己。
To make this happen you should do ALL variable initializations at the start. Currently you initialize b = new BodyPart(xCoor, yCoor, 10);
in tick()
. Since this only ever happens once (when the game started) this is better to put in your initilization method.
要实现这一点,您应该在开始时执行所有变量初始化。目前你初始化b = new BodyPart(xCoor,yCoor,10);在tick()中。因为这只发生过一次(游戏开始时),所以最好放入你的启动方法。
A rough proposal of an initilization method:
初始化方法的粗略提议:
public void initialize() { snake = new ArrayList<BodyPart>(); apples = new ArrayList<Apple>(); b = new BodyPart(xCoor, yCoor, 10); snake.add(b);}
This method initializes all variables that are 'resettable'. You can then call this method when the int key = e.getKeyCode();
is KeyCode.R
.
此方法初始化所有“可重置”的变量。然后,当int key = e.getKeyCode()时,可以调用此方法;是KeyCode.R。
#1
You can modify your code as follows:I've added comments to explain my modifications
您可以按如下方式修改代码:我添加了注释来解释我的修改
//this resets the position/size of the snake and clears the arraypublic 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 }}
#2
First of all, yes you are on the right track. The best way is to put the initilization code (setting all variables to their initial values) in a seperate method. Then when you press 'r' you simply call this method and it will 'reset' itself.
首先,是的,你是在正确的轨道上。最好的方法是将启动代码(将所有变量设置为初始值)放在一个单独的方法中。然后,当你按'r'时,你只需调用这个方法,它就会“重置”自己。
To make this happen you should do ALL variable initializations at the start. Currently you initialize b = new BodyPart(xCoor, yCoor, 10);
in tick()
. Since this only ever happens once (when the game started) this is better to put in your initilization method.
要实现这一点,您应该在开始时执行所有变量初始化。目前你初始化b = new BodyPart(xCoor,yCoor,10);在tick()中。因为这只发生过一次(游戏开始时),所以最好放入你的启动方法。
A rough proposal of an initilization method:
初始化方法的粗略提议:
public void initialize() { snake = new ArrayList<BodyPart>(); apples = new ArrayList<Apple>(); b = new BodyPart(xCoor, yCoor, 10); snake.add(b);}
This method initializes all variables that are 'resettable'. You can then call this method when the int key = e.getKeyCode();
is KeyCode.R
.
此方法初始化所有“可重置”的变量。然后,当int key = e.getKeyCode()时,可以调用此方法;是KeyCode.R。