java贪吃蛇源代码_java贪吃蛇源代码详解
1、本代码主要功能为实现贪吃蛇游戏,GUI界⾯做到尽量简洁和原游戏相仿。⽬前版本包含计分,统计最⾼分,长度⾃动缩短计时功能。
2、运⾏时请把.java⽂件放⼊default package 即可运⾏。
a)⽂件位置
b)进⼊游戏
c) 游戏进⾏中
JAVA实现代码
ALL RIGHTS RESERVED 3 import java.awt.*;
import java.awt.event.*;
importstaTIc java.lang.String.format; import java.uTIl.*; import java.uTIl.List; import javax.swing.*;
Publicclass Snakeextends JPanel implements Runnable { enum Dir {
up(0, -1), right(1, 0), down(0, 1), left(-1, 0); Dir(intx, inty) { this.x = x; this.y = y;
}
finalintx, y;
}
staTIcfinal Random rand = new Random(); staticfinalintWALL = -1;
staticfinalintMAX_ENERGY = 1500; volatilebooleangameOver = true;
贪吃蛇的编程代码Thread gameThread; intscore, hiScore; intnRows = 44; intnCols = 64;
Dir dir; intenergy;
int[][] grid;
List《Point>snake, treats;
Font smallFont;
public Snake() {
setPreferredSize(new Dimension(640, 440));
setBackground(Color.white);
setFont(new Font(“SansSerif”, Font.BOLD, 48));
setFocusable(true);
smallFont = getFont().deriveFont(Font.BOLD, 18);
initGrid();
addMouseListener(new MouseAdapter() {
@Override
publicvoid mousePressed(MouseEvent e) {
if (gameOver) {
startNewGame();
repaint();
}
}
});
addKeyListener(new KeyAdapter() {
@Override
publicvoid keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: if (dir != Dir.down) dir = Dir.up; break;case KeyEvent.VK_LEFT: if (dir !
= Dir.right) dir = Dir.left; break; case KeyEvent.VK_RIGHT: if (dir != Dir.left) dir = Dir.right; break; case KeyEvent.VK_DOWN: if (dir != Dir.up) dir = Dir.down; break; }
repaint();
}
});
}
void startNewGame() { gameOver = false;
stop();
initGrid();
treats = new LinkedList<>();
dir = Dir.left;
energy = MAX_ENERGY;
if (score>hiScore) hiScore = score; score = 0;
snake = new ArrayList<>(); for (intx = 0; x< 7; x++) snake.add(new Point(nCols / 2 + x, nRows / 2)); do addTreat(); while(treats.isEmpty());
(gameThread = new Thread(this)).start();
}
void stop() {
if (gameThread != null) {
Thread tmp = gameThread; gameThread = null; tmp.interrupt();
}
}
void initGrid() { grid = newint[nRows][nCols]; for (intr = 0;
r《nRows; r++) { for (intc = 0; c
if (c == 0 || c == nCols - 1 || r == 0 || r == nRows - 1) grid[r][c] = WALL;
}
}
}
@Override
publicvoid run() {
while (Thread.currentThread() == gameThread) { try { Thread.sleep(Math.max(75 - score, 25));
} catch (InterruptedException e) {
return;
} if (energyUsed() || hitsWall() || hitsSnake()) {
gameOver();
} else {
if (eatsTreat()) { score++; energy = MAX_ENERGY;
growSnake();
}
moveSnake();
addTreat();
}
repaint();
}
} boolean energyUsed() { energy -= 10;
returnenergy<= 0;
}
boolean hitsWall() {
Point head = (0);
intnextCol = head.x + dir.x;
intnextRow = head.y + dir.y;
returngrid[nextRow][nextCol] == WALL;
}
boolean hitsSnake() {
Point head = (0);
intnextCol = head.x + dir.x;
intnextRow = head.y + dir.y;
for (Point p : snake) if (p.x == nextCol&&p.y == nextRow) returntrue; returnfalse;
}
boolean eatsTreat() {
Point head = (0);
intnextCol = head.x + dir.x; intnextRow = head.y + dir.y; for (Point p : treats) if (p.x == nextCol&&p.y == nextRow) { ve(p);
}
returnfalse;
} void gameOver() { gameOver = true;
stop();
} void moveSnake() {
for (inti = snake.size() - 1; i> 0; i--) {
Point p1 = (i - 1);
Point p2 = (i); p2.x = p1.x;
p2.x = p1.x; p2.y = p1.y;
}
Point head = (0); head.x += dir.x; head.y += dir.y;
}
void growSnake() {
Point tail = (snake.size() - 1); intx = tail.x + dir.x; inty = tail.y + dir.y; snake.add(new Point(x, y));
} void addTreat() { if (treats.size() 《 3) {
if (Int(10) == 0) { // 1 in 10 if (Int(4) != 0) { // 3 in 4 intx, y; while (true) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论