admin 管理员组文章数量: 887021
2024年1月5日发(作者:美国国会中期选举)
JAVA实现代码ALL RIGHTS RESERVED 3 import .*;import .*;importstaTIc ; import .*;import ; import .*;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();setFont(new Font(“SansSerif”, , 48));setFocusable(true);smallFont = getFont().deriveFont(, 18);initGrid();addMouseListener(new MouseAdapter() {@Overridepublicvoid mousePressed(MouseEvent e) {if (gameOver) {startNewGame();repaint();}}});addKeyListener(new KeyAdapter() {@Overridepublicvoid keyPressed(KeyEvent e) {switch (Code()) {case _UP: if (dir != ) dir = ; break;case _LEFT: if (dir != ) dir = ; break;case _RIGHT: if (dir != ) dir = ; break;case _DOWN: if (dir != ) dir = ; break;}repaint();}});}void startNewGame() { gameOver = false;stop();initGrid();treats = new LinkedList<>();dir = ;
energy = MAX_ENERGY;if (score>hiScore) hiScore = score; score = 0;snake = new ArrayList<>(); for (intx = 0; x< 7; x++) (new Point(nCols / 2 + x, nRows / 2));do addTreat(); while(y());(gameThread = new Thread(this)).start();}void stop() {if (gameThread != null) {Thread tmp = gameThread; gameThread = null; upt();}}void initGrid() { grid = newint[nRows][nCols]; for (intr = 0;r《nRows; r++) { for (intc = 0; cif (c == 0 || c == nCols - 1 || r == 0 || r == nRows - 1) grid[r][c] = WALL;}}}@Overridepublicvoid run() {while (tThread() == gameThread) { try { ((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) {(p);}returnfalse;} void gameOver() { gameOver = true;stop();} void moveSnake() {for (inti = () - 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 = (() - 1); intx = tail.x + dir.x; inty = tail.y + dir.y; (new Point(x, y));} void addTreat() { if (() 《 3) {if (t(10) == 0) { // 1 in 10 if (t(4) != 0) { // 3 in 4 intx, y; while (true) {
x = t(nCols);y = t(nRows); if (grid[y][x] != 0) continue;Point p = new Point(x, y);if (ns(p) || ns(p)) continue;(p); break;}}elseif (() > 1) (0);}}}void drawGrid(Graphics2D g) { or(ray);for (intr = 0; r}}}void drawSnake(Graphics2D g) { or(); for (Point p : snake) ct(p.x * 10, p.y * 10, 10, 10);or(energy< 500 ? : );Point head = (0);ct(head.x * 10, head.y * 10, 10, 10);}void drawTreats(Graphics2D g) {or(); for (Point p : treats) ct(p.x * 10, p.y * 10, 10, 10);}void drawStartScreen(Graphics2D g) { or();t(getFont());ring(“Snake”, 240, 190);or(); t(smallFont);ring(“(click to start)”, 250, 240);}void drawScore(Graphics2D g) { inth = getHeight();t(smallFont);or(getForeground());String s = format(“hiscore %d score %d”, hiScore, score);ring(s, 30, h - 30);
ring(format(“energy %d”, energy), getWidth() - 150, h - 30);}@Overridepublicvoid paintComponent(Graphics gg) { omponent(gg);Graphics2D g = (Graphics2D) gg;deringHint(_ANTIALIASING, _ANTIALIAS_ON);drawGrid(g);if (gameOver) {drawStartScreen(g);} else {drawSnake(g);drawTreats(g);drawScore(g);}}publicstaticvoid main(String[] args) {Later(() -> {JFrame f = new JFrame();aultCloseOperation(_ON_CLOSE);le(“Snake”);izable(false);(new Snake(), );();ationRelativeTo(null);ible(true);});}}
版权声明:本文标题:java贪吃蛇源代码_java贪吃蛇源代码详解 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1704433958h459693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论