本文介绍了我如何修复像这个i ++的死代码和一些重复的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到一个错误,上面写着我的i ++上的死代码,我将发表评论,我正试图制作一个蛇游戏,但我无法解决这个死代码也因为某种原因我得到了重复的食物我'经过这一遍过来随意运行,看看有什么效果



I keep getting an error that says dead code on my i++ that i will leave a comment on i am trying to make a snake game but i can't solve this dead code also i am getting duplicate food for some reason I've been through this over and over feel free to run this and see what works

package Snake;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Snake extends JFrame implements KeyListener, Runnable{
	
	private JPanel p = new JPanel();
	
	private final int WIDTH = 450;
	private final int HEIGHT = 450;
	
	//Snake Properties
	private JButton[] lb = new JButton[200];
	private int x = 500, y = 500;
	private int gu = 3;
	private int directionX = 1, directionY = 0, oldX, oldY;
	private final int speed = 50;
	
	//Snake Coordinates
	private int [] snake_X = new int [300];
	private int [] snake_Y = new int [300];
	
	//Snake Movements
	private boolean move_left = false;
	private boolean move_right = false;
	private boolean move_up = false;
	private boolean move_down = false;
	
	private Point[] Snake_Point = new Point[300];
	private boolean food = false;
	private int score = 0;
	private boolean game_over = false;
	
	//Start Thread
	Thread thread;
	Random r = new Random();
	
	public Snake(){
		super("Snake");
		setSize(WIDTH, HEIGHT);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		starting_Snake();
		
		p.setLayout(null);
		p.setBackground(Color.BLACK);
		
		
		
		add(p);
		show();
		
		initializeValues();
		addKeyListener(this);
		
		thread = new Thread(this);
		thread.start();
		
	}
	

	
	public void initializeValues(){
		gu = 3;
		snake_X[0] = 100;
		snake_Y[0] = 150;
		directionX = 10;
		directionY = 0;
		food = false;
		score = 0;
		move_left = false;
		move_right = true;
		move_up = true;
		move_down = true;
	}
	
	public void starting_Snake(){
		for(int i = 0; i < gu; i++){
			lb[i] = new JButton("lb" + i);
			lb[i].setEnabled(false);
			p.add(lb[i]);
			
			lb[i].setBounds(snake_X [i], snake_Y [i], 10, 10);
			snake_X[i+1] = snake_X[i] = snake_X[i] - 10;
			snake_Y[i+1] = snake_Y[i];
		}
		
	}
	
	public void reset(){
		initializeValues();
		p.removeAll();
		
		thread.stop();
		
		starting_Snake();
		thread = new Thread(this);
		thread.start();	
	}
	// Snake gets bigger
	public void growUp(){
		lb[gu] = new JButton();
		lb[gu].setEnabled(false);
		p.add(lb[gu]);
		
		int x = 10 + (10 * r.nextInt(44));
		int y = 10 + (10 * r.nextInt(44));
		
		snake_X[gu] = x;
		snake_Y[gu] = y;
		lb[gu].setBounds(x, y, 10, 10);
		gu++;
	}
	public void move_Forward(){
		for(int i = 0; i < gu; i++){
			Snake_Point[i] = lb[i].getLocation();
		}
		
		snake_X[0] += directionX;
		snake_Y[0] += directionY;
		lb[0].setBounds(snake_X[0], snake_Y[0], 10, 10);
		

		// dead code           ↓
		for(int i = 1; i < gu; i++){
			lb[i].setLocation(Snake_Point[i - 1]); 
			
			
			//logic for snake movements
			if(snake_X[0] == x){
				snake_X[0] = 10;
			}else if(snake_X[0] == 0){
			    snake_X[0] = x - 10;
		   }else if(snake_Y[0] == y){
			   snake_Y[0] = 10;
		   }else if(snake_Y[0] == 0){
			   snake_Y[0] = y - 10;
		   }
			//increase score when snake gets food
			if(snake_X[0] == snake_X[gu - 1] && snake_Y[0] == snake_Y[gu-1]){
				food = false;
				score += 5;
				System.out.println("Score: "+ score);
			
				//generates food when false
			}
			if(food == false){
				growUp();
				food = true;
			}else{
				lb[gu - 1].setBounds(snake_X[gu - 1], snake_Y[gu - 1] , 10, 10);
			}
			for (int i1 = 1; i1 < gu; i1++){
				
				if(Snake_Point[0] == Snake_Point[i1]){
					System.out.println("GAME OVER!!!:  "+ score);
				}
				try{
					thread.join();
				}catch(Exception e){
				
				}
			}
			break;
		}
		p.repaint();
		show();
	}
	
	@Override
	public void run() {
		// Starts game by moving snake
	
		while(!game_over){
			move_Forward();
			
			try{
				Thread.sleep(speed);
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
        @Override
		public void keyPressed( KeyEvent e) {
		// TODO Auto-generated method stub
		
        	//Moving Left
        	if(move_left == true && e.getKeyCode() == 37){
		    directionX = -10;
		    directionY = 0;
		    move_right = false;
		    move_up = true;
		    move_down = true;
		    System.out.println("MOVING LEFT");
		    
		    //Moving UP
		}	if(move_up == true && e.getKeyCode() == 38){
		    directionX = 0;
		    directionY = -10;
		    move_right = true;
		    move_left = true;
		    move_down = false;
		    System.out.println("MOVING UP");
		
		    //Moving RIGHT
		}	if(move_right == true && e.getKeyCode() == 39){
		    directionX = 10;
		    directionY = 0;
		    move_left = false;
		    move_up = true;
		    move_down = true;
		    System.out.println("MOVING RIGHT");
		
		    //Moving DOWN
		}	if(move_down == true && e.getKeyCode() == 40){
		    directionX = 0;
		    directionY = 10;
		    move_right = true;
		    move_up = false;
		    move_left = true;
		    System.out.println("MOVING DOWN");
	
		
		}
	}
	


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

	
		
	}





我尝试过:



重新排列并删除但我需要它我还搜索了重复的代码



What I have tried:

rearranging it and removing it but I need it I also searched for the duplicate code

推荐答案

引用:

我一直收到错误,说我的i ++上有死代码

I keep getting an error that says dead code on my i++



'死代码'的含义是这段代码永远不会是执行。


The meaning of 'dead code' is that this code will never be executed.

for(int i = 1; i < gu; i++){



编译时,'i ++'部分移动到循环结束,但是:


When compiled, the 'i++' part move to the end of loop, but:

for(int i = 1; i < gu; i++){
    ...
    break;
}



永远不会循环!

所以:死代码。


will never loop !
So: Dead Code.


这篇关于我如何修复像这个i ++的死代码和一些重复的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:42