贪吃蛇java代码

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

@SuppressWarnings("serial")
public class MainClass extends JFrame {
 ControlSnake control;

 Toolkit kit;

 Dimension dimen;

 public static void main(String[] args) {
  new MainClass("my snake");
 }

 public MainClass(String s) {
  super(s);
  control = new ControlSnake();
  control.setFocusable(true);
  kit = Toolkit.getDefaultToolkit();
  dimen = kit.getScreenSize();

  add(control);
  setLayout(new BorderLayout());
  setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
  setSize(FWIDTH, FHEIGHT);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setResizable(false);
  setVisible(true);
 }

 public static final int FWIDTH = 315;

 public static final int FHEIGHT = 380;
}

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.Random;

@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
 Random rand;

 ArrayList<Point> list, listBody;

 String str, str1;

 static boolean key;

 int x, y, dx, dy, fx, fy, flag;

 int snakeBody;

 int speed;

 public ControlSnake() {
  snakeBody = 1;

  str = "上下左右方向键控制 P键暂停...";
  str1 = "现在的长度为:" + snakeBody;
  key = true;
  flag = 1;

  speed = 700;
  rand = new Random();
  list = new ArrayList<Point>();
  listBody = new ArrayList<Point>();

  x = 5;
  y = 5;
  list.add(new Point(x, y));
  listBody.add(list.get(0));

  dx = 10;
  dy = 0;

  fx = rand.nextInt(30) * 10 + 5;// 2
  fy = rand.nextInt(30) * 10 + 5;// 2

  setBackground(Color.WHITE);
  setSize(new Dimension(318, 380));

  final Timer time = new Timer(speed, this);
  time.start();

  addKeyListener(new KeyAdapter() {
   public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 37) {
     dx = -10;
     dy = 0;
    } else if (e.getKeyCode() == 38) {
     dx = 0;
     dy = -10;
    } else if (e.getKeyCode() == 39) {
     dx = 10;
     dy = 0;
    } else if (e.getKeyCode() == 40) {
     dx = 0;
     dy = 10;
    } else if (e.getKeyCode() == 80) {
     if (flag % 2 == 1) {
      time.stop();
     }
     if (flag % 2 == 0) {
      time.start();
     }
     flag++;
    }
   }
  });

 }

 public void paint(Graphics g) {
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, 400, 400);
  g.setColor(Color.DARK_GRAY);
  g.drawLine(3, 3, 305, 3);
  g.drawLine(3, 3, 3, 305);
  g.drawLine(305, 3, 305, 305);
  g.drawLine(3, 305, 305, 305);
  g.setColor(Color.PINK);

  for (int i = 0; i < listBody.size(); i++) {
   g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
  }
  g.fillRect(x, y, 9, 9);
  g.setColor(Color.ORANGE);
  g.fillRect(fx, fy, 9, 9);

  g.setColor(Color.DARK_GRAY);
  str1 = "现在的长度为:" + snakeBody;
  g.drawString(str, 10, 320);
  g.drawString(str1, 10, 335);
 }

 public void actionPerformed(ActionEvent e) {
  x += dx;
  y += dy;
  if (makeOut() == false) {
   JOptionPane.showMessageDialog(null, "重新开始......");

   speed = 700;

   snakeBody = 1;

   x = 5;
   y = 5;

   list.clear();
   list.add(new Point(x, y));
   listBody.clear();
   listBody.add(list.get(0));

   dx = 10;
   dy = 0;

  }
  addPoint(x, y);
  if (x == fx && y == fy) {
   speed = (int) (speed * 0.8);//速度增加参数
   if (speed < 200) {
    speed = 100;
   }
   fx = rand.nextInt(30) * 10 + 5;// 2
   fy = rand.nextInt(30) * 10 + 5;// 2
   snakeBody++;// 2
  } // 2
  repaint();
 }

 public void addPoint(int xx, int yy) {
  // 动态的记录最新发生的50步以内的移动过的坐标
  // 并画出最新的snakeBody
  if (list.size() < 100) {//蛇身长度最长为100
   list.add(new Point(xx, yy));
  } else {
   list.remove(0);
   list.add(new Point(xx, yy));
  }
  if (snakeBody == 1) {
   listBody.remove(0);
   listBody.add(0, list.get(list.size() - 1));
  } else {
   listBody.clear();
   if (list.size() < snakeBody) {
    for (int i = list.size() - 1; i > 0; i--) {
     listBody.add(list.get(i));
    }
   } else {
    for (int i = list.size() - 1; listBody.size() < snakeBody; i--) {
     listBody.add(list.get(i));
    }
   }
  }
 }

 public boolean makeOut() {
  if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
   return false;
  }
  for (int i = 0; i < listBody.size() - 1; i++) {
   for (int j = i + 1; j < listBody.size(); j++) {
    if (listBody.get(i).equals(listBody.get(j))) {
     return false;
    }
   }
  }
  return true;
 }
}
赞同
16
| 评论(2)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的贪吃Java代码,包含了基本的游戏逻辑和界面绘制。 ```java import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class SnakeGame extends JPanel implements Runnable, KeyListener { private static final long serialVersionUID = 1L; public static final int WIDTH = 500, HEIGHT = 500; private Thread thread; private boolean running = false; private SnakePart b; private ArrayList<SnakePart> snake; private Apple apple; private ArrayList<Apple> apples; private Random r; private int xCoor = 10, yCoor = 10; private int size = 5; private boolean right = true, left = false, up = false, down = false; private int ticks = 0; private Key key; public SnakeGame() { setFocusable(true); key = new Key(); addKeyListener(this); setPreferredSize(new Dimension(WIDTH, HEIGHT)); r = new Random(); snake = new ArrayList<SnakePart>(); apples = new ArrayList<Apple>(); start(); } public void start() { running = true; thread = new Thread(this); thread.start(); } public void stop() { running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } public void tick() { if (snake.size() == 0) { b = new SnakePart(xCoor, yCoor, 10); snake.add(b); } if (apples.size() == 0) { int xCoor = r.nextInt(49); int yCoor = r.nextInt(49); 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 < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) { stop(); } ticks++; if (ticks > 250000) { if (right) xCoor++; if (left) xCoor--; if (up) yCoor--; if (down) yCoor++; ticks = 0; b = new SnakePart(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(Color.BLACK); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.WHITE); 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 static void main(String[] args) { SnakeGame game = new SnakeGame(); JFrame frame = new JFrame(); frame.add(game); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Snake"); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } @Override public void run() { while (running) { tick(); repaint(); } } @Override 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) { } @Override public void keyTyped(KeyEvent arg0) { } public class Key { public Key() { } } public class SnakePart { private int xCoor, yCoor, width, height; public SnakePart(int xCoor, int yCoor, int tileSize) { this.xCoor = xCoor; this.yCoor = yCoor; width = tileSize; height = tileSize; } public void tick() { } public void draw(Graphics g) { g.setColor(Color.GREEN); g.fillRect(xCoor * width, yCoor * height, width, height); } public int getxCoor() { return xCoor; } public void setxCoor(int xCoor) { this.xCoor = xCoor; } public int getyCoor() { return yCoor; } public void setyCoor(int yCoor) { this.yCoor = yCoor; } } public class Apple { private int xCoor, yCoor, width, height; public Apple(int xCoor, int yCoor, int tileSize) { this.xCoor = xCoor; this.yCoor = yCoor; width = tileSize; height = tileSize; } public void tick() { } public void draw(Graphics g) { g.setColor(Color.RED); g.fillRect(xCoor * width, yCoor * height, width, height); } public int getxCoor() { return xCoor; } public void setxCoor(int xCoor) { this.xCoor = xCoor; } public int getyCoor() { return yCoor; } public void setyCoor(int yCoor) { this.yCoor = yCoor; } } } ``` 希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值