java简单贪吃蛇

学Java后第一个小游戏
效果图如下效果
有空做个双蛇版
package snake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SnakeV_1 {

public static void main(String[] args) {
new Snake();
}

}

class Snake extends JPanel{

private static final long serialVersionUID = 1L;

LinkedList snake = new LinkedList();

Point food;

public int WIDTH = 50;//格子数

public int HEIGHT =30;

public int CELLHEIGHT = 20;//格子高

public int CELLWIDTH = 20;

private boolean [][] background = new boolean[HEIGHT][WIDTH];

boolean isGameOver = false;
boolean runing = false;//运行

//蛇蛇当前方向方向
public final int UP_DIRECTION = 1;
public final int DOWN_DIRECTION = -1;
public final int LEFT_DIRECTION = 2;
public final int RIGHT_DIRECTION = -2;

//蛇当前方向,默认向右
private int currentDrection = -2;

JFrame frame = new JFrame("贪吃 1.0");

public Snake() {
super();
initBackground();
initSnake();
CreateFood();
CreateWall();
frame.add(this);
initFrame(WIDTH*CELLWIDTH+20, HEIGHT*CELLHEIGHT+70);
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
changeDirection(UP_DIRECTION);
break;
case KeyEvent.VK_DOWN:
changeDirection(DOWN_DIRECTION);
break;
case KeyEvent.VK_LEFT:
changeDirection(LEFT_DIRECTION);
break;
case KeyEvent.VK_RIGHT:
changeDirection(RIGHT_DIRECTION);
break;
case KeyEvent.VK_SPACE://暂停功能带实现
runing = false;
break;
case KeyEvent.VK_S://开始
runing = true;
break;
case KeyEvent.VK_R://开始
frame.dispose();
new Snake();
break;
default:
break;
}
}
});
new Thread() {
public void run() {
while(true) {
int time =300;
try {
Thread.sleep(time - 10 *snake.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
move();
isGameover();
if(isGameOver == true) {
runing = false;
}
}
}
}.start();
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.drawString("按s开始游戏,空格暂停 r从来",0,HEIGHT*CELLHEIGHT+20);

for(int rows = 0;rows < background.length; rows++) {
for (int cols = 0; cols < background[rows].length; cols++) {
//石头
if(background[rows][cols]) {
g.setColor(Color.GRAY);
}else {
g.setColor(Color.PINK);
}
g.fill3DRect(cols*CELLWIDTH, rows*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
}
}

g.setColor(Color.BLUE);//
for (int i = 1; i < snake.size(); i++) {
Point body = snake.get(i);
g.fill3DRect(body.x*CELLWIDTH, body.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
}

Point head = snake.getFirst();
g.setColor(Color.RED);
g.fill3DRect(head.x*CELLWIDTH, head.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);

g.setColor(Color.GREEN);
g.fill3DRect(food.x*CELLWIDTH, food.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
if(isGameOver == true) {
g.setColor(Color.RED);
g.drawString("GAMEOVER",WIDTH*CELLWIDTH/2,HEIGHT*CELLHEIGHT/2);
}
}

public void initFrame(int width,int height) {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取与系统相关的工具类对象
Dimension d = toolkit.getScreenSize();//分辨率
frame.setBackground(Color.blue);
frame.setBounds((d.width-width)/2, (d.height- height)/2, width, height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void initBackground() {//初始化地图
for(int rows = 0;rows < background.length; rows++) {
for (int cols = 0; cols < background[rows].length; cols++) {
if(rows == 0 || rows == HEIGHT-1) {
background[rows][cols] = true;
}
}
}
}

public void initSnake() {
int x = WIDTH/2;
int y = HEIGHT/2;
snake.addFirst(new Point(x-1,y));
snake.addFirst(new Point(x,y));
snake.addFirst(new Point(x+1,y));
}

public void CreateFood() {
Random random = new Random();
while(true) {
int x = random.nextInt(WIDTH-2)+1;
int y = random.nextInt(HEIGHT-2)+1;
if(background[y][x] != true) {
food = new Point(x,y);
break;
}
}
}

public void CreateWall() {
for (int i = 0; i < 10; i++) {
Random random = new Random();
while(true) {
int x = random.nextInt(WIDTH-2)+1;
int y = random.nextInt(HEIGHT-2)+1;
if(background[y][x] != true) {
background[y][x] = true;
break;
}
}
}
}

public void CleanWall() {
for(int rows = 1;rows < background.length-1; rows++) {
for (int cols = 0; cols < background[rows].length; cols++) {
//if(rows != 0 || rows != HEIGHT-1) {
background[rows][cols] = false;
//}
}
}
}

public boolean eatfood() {
Point head = snake.getFirst();
if(head.equals(food)) {
return true;
}
return false;
}

public void move() {
if(runing) {
Point head = snake.getFirst();
switch (currentDrection) {
case UP_DIRECTION:
snake.addFirst(new Point(head.x,head.y-1));
break ;
case DOWN_DIRECTION:
snake.addFirst(new Point(head.x,head.y+1));
break ;
case LEFT_DIRECTION:
if(head.x == 0) {
snake.addFirst(new Point(WIDTH-1,head.y));
}else {
snake.addFirst(new Point(head.x-1,head.y));
}
break ;
case RIGHT_DIRECTION:
if(head.x == WIDTH-1) {
snake.addFirst(new Point(0,head.y));
}else {
snake.addFirst(new Point(head.x+1,head.y));
}
break ;
default:
break;
}
if(eatfood()) {
CleanWall();
CreateFood();
CreateWall();
}else {
snake.removeLast();
}
repaint();
}
}

public void changeDirection(int newDirection) {
//判断是否相反 //改变方向
if(newDirection + currentDrection != 0) {
this.currentDrection = newDirection;
}
}

public void isGameover() {
//撞墙
Point head = snake.getFirst();
if(background[head.y][head.x] == true) {
isGameOver = true;
}
//咬自己
for (int i = 1; i < snake.size(); i++) {
Point body = snake.get(i);
if(head.equals(body)) {
isGameOver = true;
}
}
}

}

作者:weixin_44002250

相关推荐

在这里插入图片描述

javaScript——函数防抖

华为Java高级面试题:用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D…26Z

华为Java高级面试题:用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D…26Z

浅谈java单例设计模式

JavaEE-AT阿宝哥

《Oracle Java EE编程指南》04-01:Servlet基本概念