贪吃蛇是一款经典的小游戏,是许多人童年的回忆。本文将介绍如何用电脑自己做一个贪吃蛇App。
开发工具和语言
首先要选择一款适合自己的开发工具和编程语言。目前比较流行的开发工具有Android Studio、Xcode等,编程语言则可以选择Java、Kotlin、Swift等。
界面设计
贪吃蛇App的界面应该是简单明了的,包含开始界面、游戏界面、结束界面等。开始界面应该有开始游戏按钮和游戏规则介绍,游戏界面应该有蛇体、食物、得分等元素,结束界面应该有得分、重新开始和返回主界面等选项。
游戏逻辑
蛇的移动方向应该由玩家控制,当蛇头碰到游戏界面的边界或者蛇尾时,游戏结束。当蛇头吃到食物时,蛇的长度增加一节,游戏得分也会增加。食物应该随机生成,并且不能与蛇体重叠。
代码实现
以下是用Java语言实现的贪吃蛇App代码:
1. 定义Snake类
public class Snake {
private LinkedList
public Snake() {
body = new LinkedList
body.add(new Node(5, 5));//蛇的初始位置
}
public LinkedList
return body;
}
public boolean eatFood(Node food) {//判断蛇是否吃到了食物
Node head = body.getFirst();
if (head.getX() == food.getX() && head.getY() == food.getY()) {
body.addFirst(food);//蛇的头部添加一节身体
return true;
}
return false;
}
public void move(int direction) {//蛇的移动
Node head = body.getFirst();
int x = head.getX();
int y = head.getY();
switch (direction) {
case MainActivity.UP:
y--;
break;
case MainActivity.DOWN:
y++;
break;
case MainActivity.LEFT:
x--;
break;
case MainActivity.RIGHT:
x++;
break;
}
Node newHead = new Node(x, y);
body.addFirst(newHead);//加入新的头部
body.removeLast();//移除尾部
}
public boolean isDead() {//判断蛇是否死亡
Node head = body.getFirst();
if (head.getX() < 0 || head.getX() >= MainActivity.COL_NUM) {//超出界限
return true;
}
if (head.getY() < 0 || head.getY() >= MainActivity.ROW_NUM) {//超出界限
return true;
}
for (int i = 1; i < body.size(); i++) {//与身体相撞
if (head.getX() == body.get(i).getX() && head.getY() == body.get(i).getY()) {
return true;
}
}
return false;
}
}
2. 定义Node类
public class Node {
private int x;
private int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
3. 定义Food类
public class Food {
private Node food;
public Food() {
food = new Node(0, 0);//初始化位置
}
public Node genNewFood(LinkedList
boolean isOverlap = true;//判断是否与蛇体重叠
while (isOverlap) {
int x = (int) (Math.random() * MainActivity.COL_NUM);
int y = (int) (Math.random() * MainActivity.ROW_NUM);
Node newFood = new Node(x, y);
isOverlap = false;
for (int i = 0; i < body.size(); i++) {
if (newFood.getX() == body.get(i).getX() && newFood.getY() == body.get(i).getY()) {
isOverlap = true;
break;
}
}
if (!isOverlap) {
food = newFood;
}
}
return food;
}
}
4. 定义MainActivity类
public class MainActivity extends AppCompatActivity {
public static final int COL_NUM = 15;//游戏界面的列数
public static final int ROW_NUM = 20;//游戏界面的行数
public static final int UP = 0;//方向,用常量表示方便修改
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
private Snake snake;
private Food food;
private boolean isRunning = true;//控制游戏是否运行
private int direction = RIGHT;//初始方向为向右
private int score = 0;//得分
private Handler handler = new Handler();//用于UI更新
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
snake.move(direction);//蛇移动
if (snake.eatFood(food.genNewFood(snake.getBody()))) {//吃到了食物
score++;
}
if (snake.isDead()) {//蛇死亡
isRunning = false;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("游戏结束");
builder.setMessage("得分:" + score);
builder.setPositiveButton("重新开始", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartGame();
}
});
builder.setNegativeButton("返回主界面", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setCancelable(false);
builder.show();
return;
}
handler.postDelayed(this, 500);//每500毫秒刷新界面
drawGameView();
}
}
};
private GameView gameView;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gameView = (GameView) findViewById(R.id.game_view);
startButton = (Button) findViewById(R.id.start_button);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startGame();
}
});
snake = new Snake();
food = new Food();
}
private void startGame() {//开始游戏
startButton.setVisibility(View.GONE);//隐藏开始按钮
score = 0;//分数清零
isRunning = true;//游戏开始
direction = RIGHT;//方向重置为向右
handler.postDelayed(runnable, 500);
}
private void restartGame() {//重新开始游戏
startButton.setVisibility(View.VISIBLE);//显示开始按钮
snake = new Snake();//重新创建蛇和食物实例
food = new Food();
gameView.invalidate();//界面重绘
}
private void drawGameView() {//绘制游戏界面
gameView.invalidate();//界面重绘
}
public int getScore() {
return score;
}
public Snake getSnake() {
return snake;
}
public Food getFood() {
return food;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
5. 定义GameView类
public class GameView extends View {
private MainActivity context;
private Paint paint;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = (MainActivity) context;
paint = new Paint();//绘图工具
paint.setAntiAlias(true);//设置抗锯齿
}
@Override
protected void onDraw(Canvas canvas) {//重写绘制方法
super.onDraw(canvas);
int cellWidth = getWidth() / MainActivity.COL_NUM;//列宽
int cellHeight = getHeight() / MainActivity.ROW_NUM;//行高
drawSnake(cellWidth, cellHeight, canvas);//绘制蛇
drawFood(cellWidth, cellHeight, canvas);//绘制食物
drawScore(canvas);//绘制分数
}
private void drawSnake(int cellWidth, int cellHeight, Canvas canvas) {//绘制蛇
Snake snake = context.getSnake();
LinkedList
paint.setColor(Color.GREEN);
for (int i = 0; i < body.size(); i++) {
int x = body.get(i).getX() * cellWidth;
int y = body.get(i).getY() * cellHeight;
canvas.drawRect(x, y, x + cellWidth, y + cellHeight, paint);
}
}
private void drawFood(int cellWidth, int cellHeight, Canvas canvas) {//绘制食物
Node food = context.getFood().getFood();
paint.setColor(Color.RED);
int x = food.getX() * cellWidth;
int y = food.getY() * cellHeight;
canvas.drawRect(x, y, x + cellWidth, y + cellHeight, paint);
}
private void drawScore(Canvas canvas) {//绘制分数
paint.setColor(Color.BLACK);
paint.setTextSize(30);
int score = context.getScore();
String scoreText = "得分:" + score;
canvas.drawText(scoreText, 10, 30, paint);
}
}
以上就是用Java语言实现的贪吃蛇App的代码,当然你也可以用其他语言和工具完成这个小项目。希望这篇文章能够对有志于开发自己的应用的初学者们有所帮助。