日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cmd上写的java简单代码_用cmd编辑一个超级简单的小游戏,求代码

發布時間:2024/4/20 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cmd上写的java简单代码_用cmd编辑一个超级简单的小游戏,求代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊?隱藏此樓查看此樓

貪吃蛇:

import java.awt.*;

import java.util.LinkedList;

import java.util.Scanner;

/**

* @author aachen0

* @date 2018/3/27 13:56

* IDE:IntelliJ IDEA

*/

public class SnakeGame {

static final int WIDTH = 40, HEIGHT = 10;

static char[][] map = new char[HEIGHT][WIDTH];

public static void main(String[] args) {

SnakeGame snakeGame = new SnakeGame();

snakeGame.initBackground();//初始化背景,放只蟲子

SnakeLine snakeLine = new SnakeLine();

snakeLine.initSnake();//初始化一條蛇

snakeGame.putSnakeInMap(snakeLine);

snakeGame.show();//顯示一下

//鍵盤移動蛇進行游戲

Scanner scanner = new Scanner(System.in);

int move;

while (true) {

System.out.println("輸入AWSD控制蛇的移動,Q退出游戲");

String choice = scanner.next();

switch (choice) {

case "a":

case "A":

move = 2;

break;

case "s":

case "S":

move = 1;

break;

case "w":

case "W":

move = 3;

break;

case "d":

case "D":

move = 0;

break;

case "q":

case "Q":

int points=snakeLine.snakePoints.size();

snakeGame.putGameOverInMap(points);

default:

System.out.println("輸入有誤,請重試");

continue;

}

if (snakeLine.move(move) == -1) {

snakeGame.putGameOverInMap(snakeLine.snakePoints.size());

snakeGame.show();

break;

}

snakeGame.putSnakeInMap(snakeLine);

snakeGame.show();

}

}

//用字符畫背景

private void initBackground() {

for (int i = 0; i < HEIGHT; i++) {//外圍控制行

for (int j = 0; j < WIDTH; j++) {//內循環控制各行的第幾個

this.map[i][j] = (j == 0 || (j == WIDTH - 1) || i == 0 || (i == HEIGHT - 1)) ? '*' : ' ';

}

}

}

//顯示背景

public void show() {

int height = map.length;

int width = map[0].length;

for (int i = 0; i < height; i++) {

for (int j = 0; j < width; j++) {

System.out.print(map[i][j]);

}

System.out.println();

}

}

//把加到地圖

void putSnakeInMap(SnakeLine snakeLine) {

Point p;

this.initBackground();

map[SnakeLine.food.y][SnakeLine.food.x] = SnakeLine.worm;

for (int i = 0; i < snakeLine.snakePoints.size(); i++) {

p = snakeLine.snakePoints.get(i);

if (p.y > 0 && p.y < HEIGHT - 1 && p.x > 0 && p.x < WIDTH - 1) {

map[p.y][p.x] = (i == 0) ? snakeLine.head : snakeLine.body;

} else {

putGameOverInMap(snakeLine.snakePoints.size());

}

}

}

void putGameOverInMap(int points) {

char[] gameOver = ("GameOver Score:"+(points-3)).toCharArray();

for (int i = 0; i < gameOver.length; i++) {

map[HEIGHT / 2 - 1][i + (WIDTH - gameOver.length) / 2] = gameOver[i];

}

show();

System.exit(1);

}

}

class SnakeLine {

static final int RIGHT = 0, DOWN = 1, LEFT = 2, UP = 3;

static final char head = 'O', body = 'o', worm = '~';//頭和身體表示

static Point food = new Point((int) (Math.random() * (SnakeGame.WIDTH - 2)) + 1, (int) (Math.random() * (SnakeGame.HEIGHT - 2)) + 1);

private void newFood() {

food = new Point((int) (Math.random() * (SnakeGame.WIDTH - 2)) + 1, (int) (Math.random() * (SnakeGame

.HEIGHT - 2)) + 1);

}

LinkedList snakePoints = new LinkedList<>();//蛇的身體內容

void initSnake() {

Point head = new Point(SnakeGame.WIDTH / 2, SnakeGame.HEIGHT / 2);

snakePoints.addFirst(head);//頭

snakePoints.addLast(new Point(head.x - 1, head.y));

snakePoints.addLast(new Point(head.x - 2, head.y));

}

int move(int orient) {

Point p = snakePoints.getFirst();

Point np = null;

switch (orient) {

case SnakeLine.RIGHT:

np = new Point(p.x + 1, p.y);

break;

case SnakeLine.LEFT:

np = new Point(p.x - 1, p.y);

break;

case SnakeLine.DOWN:

np = new Point(p.x, p.y + 1);

break;

case SnakeLine.UP:

np = new Point(p.x, p.y - 1);

break;

}

if (snakePoints.contains(np)) {

return -1;//咬到自己了

}

snakePoints.addFirst(np);

if (np.equals(food)) {//吃到食物了

newFood();

return 2;

}

snakePoints.removeLast();

return 1;

}

}

自己寫的

最后的txt改成cmd就行了,可以復制。

貪吃蛇

———————————————

版權聲明:本文為CSDN博主「aachen0」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/aachen0/java/article/details/79721547

總結

以上是生活随笔為你收集整理的cmd上写的java简单代码_用cmd编辑一个超级简单的小游戏,求代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。