Java贪食蛇小游戏

Java实现贪食蛇小游戏

使用Java语言,结合链表(其实就是类),实现桌面小游戏贪食蛇。代码如下:

1.Snake.java

//定义贪食蛇节点
public class Snake {Snake next;int x;      //节点X坐标int y;      //节点Y坐标public Snake(int x,int y) {this.x = x;this.y = y;}
}//贪食蛇链表
class SnakeList{public Snake first;     //头节点public SnakeList() {// TODO Auto-generated constructor stubthis.first = null;}//添加头节点public void addFirstSnake(int x,int y){Snake s = new Snake(x, y);s.next = first;first = s;}public void add(int x,int y) {Snake node = new Snake(x, y);node.next = first;first = node;}//通过坐标寻找是否存在该节点public int findSnake(int x,int y){Snake current = first;while(current.x!=x||current.y!=y){if(current.next==null){return 0;}current = current.next;}return 1;}}

2.MyJpanel.java

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Currency;
import java.util.Map;import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;//主要任务在这,继承JPanel,使用KeyListener
public class MyJpanel extends JPanel implements KeyListener{int[][] map =  new int[22][22];     //定义地图坐标SnakeList snakeList;int appleX;             //贪食蛇的食物苹果 坐标int appleY;int direction = 2;      //当前蛇前进方向int score = 0;          //分数Timer time =  new Timer(500, new TimerListiner());      //建立时间点boolean isPause = false;public MyJpanel() {// TODO Auto-generated constructor stubdraWall();newGame();createApple();time.start();}//初始化贪食蛇public void newGame(){snakeList = new SnakeList();snakeList.addFirstSnake(10, 8);snakeList.addFirstSnake(10, 9);snakeList.addFirstSnake(10, 10);}//在地图四周画上墙壁public void draWall(){for (int i = 0; i < 22; i++) {map[0][i] = 1;map[21][i] = 1;map[i][0] = 1;map[i][21] = 1;}}//生产贪食蛇食物public void createApple(){int x,y;do{x = (int) (Math.random()*20)+1;y = (int) (Math.random()*20)+1;
//          Snake current = snakeList.findSnake(x, y);}while(snakeList.findSnake(x, y)==1);       //不可以在贪食蛇身体上生产appleX = x;appleY = y;}//控制贪食蛇移动,主要思想是链表添加头节点,删除尾节点,如果吃了苹果则不删除尾节点public void moving(){Snake current = snakeList.first;switch (direction) {case 0:snakeList.addFirstSnake(current.x-1, current.y);break;case 1:snakeList.addFirstSnake(current.x+1, current.y);break;case 2:snakeList.addFirstSnake(current.x, current.y+1);break;case 3:snakeList.add(current.x, current.y-1);break;}isOver();if (eating(current)==0) {Snake now = null;while (current.next!=null) {now = current;current = current.next;}now.next = null;current = null;}repaint();}//判断贪食蛇是否吃掉了苹果public int eating(Snake s){if(s.x==appleX&&s.y==appleY){score += 10;createApple();return 1;}return 0;}//判断游戏是否结束。撞墙了或者撞到自己了public void isOver(){Snake cur = snakeList.first;if (cur.x==0||cur.x==21||cur.y==0||cur.y==21||isSelf()==1) {JOptionPane.showMessageDialog(null, "游戏结束!");newGame();createApple();direction = 2;score = 0;}}//判断是否撞到自己public int  isSelf() {Snake cur = snakeList.first;Snake first = cur;cur = cur.next;while(cur!=null){if (first.x==cur.x&&first.y==cur.y) {return 1;}else{cur = cur.next;}}return 0;}//画图public void paintComponent(java.awt.Graphics g) {super.paintComponent(g);g.setColor(Color.RED);//画苹果g.fillOval(appleX*20, appleY*20, 20, 20);g.setColor(Color.gray);//画贪食蛇Snake current = snakeList.first;while (current!=null) {g.fillRoundRect(current.x*20, current.y*20, 20, 20, 12, 12);current = current.next;}g.drawString("得分:"+score, 340, 40);g.setColor(Color.black);for (int i = 0; i <22; i++) {for (int j = 0; j < 22; j++) {if(map[i][j]==1){g.fillRect(i*20, j*20, 20, 20);}}}};class TimerListiner implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {//          repaint();moving();//随着得分增加,提高行进速度,即减少刷新时间if(score<50){time.setDelay(500);}else if (score>=50&&score<100) {time.setDelay(400);}else if (score>=100&&score<200) {time.setDelay(300);}else if (score>=200&&score<300) {time.setDelay(200);}else if (score>=300) {time.setDelay(100);}if (isPause) {time.stop();}}}//按键上下左右的方法public  void down() {// TODO Auto-generated method stubif (direction==0||direction==1) {direction = 2;moving();}}public  void up() {// TODO Auto-generated method stubif (direction==0||direction==1) {direction = 3;moving();}}public  void left() {// TODO Auto-generated method stubif (direction==2||direction==3) {direction = 0;moving();}}public  void right() {// TODO Auto-generated method stubif (direction==2||direction==3) {direction = 1;moving();}}//添加按键事件@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubswitch (e.getKeyCode()) {case KeyEvent.VK_DOWN:down();break;case KeyEvent.VK_LEFT:left();break;case KeyEvent.VK_RIGHT:right();break;case KeyEvent.VK_UP:up();break;case KeyEvent.VK_SPACE:time.start();isPause = !isPause;}}@Overridepublic void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}}

3.SnakeGame.java

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;//定义类,继承JFrame
public class SnakeGame extends JFrame {public static void main(String[] args) {// TODO Auto-generated method stubSnakeGame snakeFrame = new SnakeGame();MyJpanel myJpanel = new MyJpanel();snakeFrame.addKeyListener(myJpanel);snakeFrame.add(myJpanel);JMenuBar jb = new JMenuBar();       //创建菜单栏,以下添加菜单项JMenu game = new JMenu("游戏");JMenu help = new JMenu("帮助");JMenuItem newGameItem = new JMenuItem("新游戏");JMenuItem stopItem = new JMenuItem("暂停");JMenuItem goonItem =  new JMenuItem("继续");JMenuItem exitItem = new JMenuItem("退出");JMenuItem about = new JMenuItem("关于");game.add(newGameItem);game.add(stopItem);game.add(goonItem);game.add(exitItem);help.add(about);jb.add(game);jb.add(help);snakeFrame.setJMenuBar(jb);snakeFrame.setSize(448, 492);snakeFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);snakeFrame.setVisible(true);snakeFrame.setResizable(false);//各菜单添加事件侦听newGameItem.addActionListener(e->{myJpanel.newGame();myJpanel.createApple();myJpanel.direction = 2;myJpanel.score = 0;});exitItem.addActionListener(e->{System.exit(0);});stopItem.addActionListener(e->{myJpanel.time.stop();});goonItem.addActionListener(e->{myJpanel.time.start();});about.addActionListener(e->{JOptionPane.showMessageDialog(null, "made by phil_chow");});}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部