自己写的贪吃蛇 关于吃食物和食物坐标的问题

ljh849893248 2017-01-04 09:57:43
package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SnakeGameView extends JPanel {
//初始化
public static JFrame Frame = new JFrame("贪吃蛇");



static int Defen= 0;
static int Bushu= 0;


public static final int width=40;

public static final int high=30;

public static final int cellwidth=20;

public static final int cellhigh=20;

public static boolean[][] map = new boolean[high][width];//地图初始化

LinkedList<Point> snake = new LinkedList<Point>();//初始化集合

Point food;//食物的坐标保存在FOOD里

public static final int UP = 1;
public static final int DOWN = -1;
public static final int LEFT = 2;
public static final int RIGHT = -2;

int Nowdirector = -2 ;//默认向右走
public void move(){
Point head = snake.getFirst();
// snake.addFirst(new Point(head.x,head.y-1));
// snake.removeLast();
switch (Nowdirector) {
case UP:
snake.addFirst(new Point(head.x,head.y-1));
System.out.println("头:"+head.x+","+head.y+" 食:"+food.x+","+food.y);

break;
case DOWN:
snake.addFirst(new Point(head.x,head.y+1));
System.out.println("头:"+head.x+","+head.y+" 食:"+food.x+","+food.y);
break;
case RIGHT:
snake.addFirst(new Point(head.x+1,head.y));
System.out.println("头:"+head.x+","+head.y+" 食:"+food.x+","+food.y);

break;
case LEFT:
snake.addFirst(new Point(head.x-1,head.y));
System.out.println("头:"+head.x+","+head.y+" 食:"+food.x+","+food.y);

break;

default:
break;
}
if(eat()==1)
{
CreateFood();
Defen=Defen+10;
Frame.setTitle("当前分数: "+Defen);
}
else{
snake.removeLast();}
if(head.x==38 || head.x==1 || head.y==1 || head.y==18){
JOptionPane.showMessageDialog(null, "撞到墙了", "撞到墙了", JOptionPane.ERROR_MESSAGE);
System.exit(0);

}
}
public void Change(int Indirector){
if(Indirector+Nowdirector!=0)
this.Nowdirector=Indirector;//为了判断方向
Bushu++;
}

public int eat(){
Point head = snake.getFirst();
if(head.x==food.x && head.y == food.y)
{
return 1;
}
return 2;

}
public void initmap(){// 地图初始化--添加
for(int i=0;i<map.length;i++){
for (int j = 0; j < map[i].length; j++) {
if(i==0 || i==(high-1) || j==0 || j==(width-1)){
map[i][j]=true;
}
}
}
}


public void initsnake(){//初始化蛇
int x = width/2;
int y = high/2;
snake.addFirst(new Point(x-1,y));
snake.addFirst(new Point(x,y));
snake.addFirst(new Point(x+1, y));//初始化成功
}

public void CreateFood(){ //创建食物
Random random = new Random();
while (true){
int x = random.nextInt(width);
int y = random.nextInt(high);
if(map[y][x]){

}else{
food = new Point(x,y);
break;
}
}
}

@Override
public void paint(Graphics g) {
for(int i=0;i<map.length;i++){
for (int j = 0; j < map[i].length; j++) {
if(map[i][j]){
g.setColor(Color.GRAY);
//g.fill3DRect(x, y, i, height, raised)
}
else{
g.setColor(Color.WHITE);
}
g.fill3DRect(j*cellwidth, i*cellhigh, cellwidth, cellhigh, true);
}
}
Point head = snake.getFirst();
g.setColor(Color.RED);
g.fill3DRect(head.x*cellwidth, head.y*high, cellwidth, cellhigh, true);
g.setColor(Color.GREEN);
for (int i = 1; i < snake.size(); i++) {
Point body = snake.get(i);
g.fill3DRect(body.x*cellwidth, body.y*high, cellwidth, cellhigh, true);
}

g.setColor(Color.BLACK);
g.fill3DRect(food.x*cellwidth, food.y*cellhigh, cellwidth, cellhigh, true);
}

public static void main(String[] args) {


final JLabel labelBS = new JLabel("当前步数:"+Defen);
final JLabel labelFS = new JLabel("当前分数:"+Defen);
labelBS.setBounds(10,570, 100, 100);
labelFS.setBounds(300,570, 100, 100);
Frame.add(labelBS);
Frame.add(labelFS);

final SnakeGameView View = new SnakeGameView();
View.initmap();
View.initsnake();
View.CreateFood();
Frame.add(View);
Frame.setSize(width*cellwidth+20,high*cellhigh+80);
Frame.setVisible(true);
Frame.addKeyListener(new KeyAdapter() {

@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {

case KeyEvent.VK_UP:
View.Change(UP);
labelBS.setText("当前步数:"+Bushu);
break;
case KeyEvent.VK_DOWN:
View.Change(DOWN);
labelBS.setText("当前步数:"+Bushu);
break;
case KeyEvent.VK_LEFT:
View.Change(LEFT);
labelBS.setText("当前步数:"+Bushu);
break;
case KeyEvent.VK_RIGHT:
View.Change(RIGHT);
labelBS.setText("当前步数:"+Bushu);
break;
}
View.move();
View.repaint();
}

});/*


}

}


代码如上

吃食物的时候偶尔会变长偶尔不会

我试着输出了食物的坐标和蛇头的坐标

发现舌头在转弯的第一下坐标不会变

不知道是哪里出了问题

代码拿上来 麻烦各位大神帮忙看一下吧 拜托了
...全文
260 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bree06 2017-01-04
  • 打赏
  • 举报
回复
paint方法的152, 156行的head.y*high和body.y*high改为body.y*cellhigh和body.y*cellhigh. 另外撞墙的判断if(head.x==38 || head.x==1 || head.y==1 || head.y==18)也有问题, 改为if (head.x > width-2 || head.x < 1 || head.y < 1 || head.y > high-2) 另外CreateFood方法要判断坐标是否在蛇的身上. 也就是要遍历snake然后判断随机的坐标是否在snake上

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧