赞
踩
该项目具备贪吃蛇小游戏的正常功能:蛇吃到食物会变长,蛇头撞到边框或者尾部游戏结束。
(1)node类
package com.example.snake; import java.awt.*; import java.util.Objects; //节点类 保存坐标x,y public class Node { private int x; private int y; public Node() { } public Node(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void drawNode(Graphics g, int i) { //区分头和身体 if (0 == i) { g.fillOval(this.x, this.y, 20 - 1, 20 - 1); } else { g.fillRect(this.x, this.y, 20 - 1, 20 - 1); } } //是否吃到食物:下一步头结点的位置刚好和食物位置相等 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return x == node.x && y == node.y; } }
(2)SnakeFrame类
package com.example.snake; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //绘制窗体(位置、大小、背景色、标题)(维护蛇的主体) public class SnakeFrame09 extends Frame { private static final int Window_X=200; private static final int Window_Y=100; private static final int Window_W=600; private static final int Window_H=500; //上1;下3,;右0;左2 private static final int UP=1; private static final int DOWN=3; private static final int RIGHT=0; private static final int LEFT=2; //速度 private int speed =200; //程序还在继续吗 private boolean isRunning=true; //上1,下3;右0,左2 private int direction=0; //600/20小格子*20以20为单位的小格子,位置不要超过边界(-2) private Node food=new Node(randomNum(0,Window_W/20-1)*20,randomNum(2,Window_H/20-1)*20); private Node[] body={ //蛇的主体部分 new Node(140,100), new Node(120,100), new Node(100,100), }; //构造器初始化窗体 public SnakeFrame09(){ this.setLocation(Window_X,Window_Y); this.setSize(Window_W,Window_H); this.setBackground(Color.GREEN); this.setTitle("贪吃蛇大雨版"); //可见性 this.setVisible(true); //设置不能改变位置大小 this.setResizable(false); //注册窗口监听 this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { //退出虚拟机 System.exit(0); } }); //注册键盘监听 this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // System.out.println(e.getKeyCode()); //KeyEvent //上1,下3;右0,左2 //临时变量dir:有可能不改方向 int dir=1; switch (e.getKeyCode()){ case KeyEvent.VK_UP://向上走 dir=UP; break; case KeyEvent.VK_DOWN://向下走 dir=DOWN; break; case KeyEvent.VK_LEFT://向左走 dir=LEFT; break; case KeyEvent.VK_RIGHT://向右走 dir=RIGHT; break; } //根据合法性修改方向(不允许在同一条线上修改) if(direction%2 !=dir%2){ direction=dir;} } }); //手动一直重绘窗体 start(); } //画笔,绘制图形,线条,文本 @Override public void paint(Graphics g) { //绘制蛇的主体部分 g.setColor(Color.RED); for(int i=0;i<body.length;i++){ body[i].drawNode(g,i); } g.setColor(Color.blue); food.drawNode(g,-1); if(isRunning){ if(isEat()){ // System.out.println("吃到食物"); addBody(); }else { //修改坐标 move();} isOver(); }else { // System.out.println("game over"); g.setColor(Color.red); g.setFont(new Font(null,Font.ITALIC,100)); g.drawString("你没了哈哈哈哈",Window_W/2-280,Window_H/2); } } //判断游戏是否结束:碰到边界或者碰到身体 public void isOver(){ Node head=body[0]; //边界 if(head.getX()<20||head.getX()>=Window_W-20 ||head.getY()<=20 ||head.getY()>=Window_H-20){ isRunning=false; } //return true代表游戏结束,false代表未结束 //身体 for (int i=1;i<body.length;i++){ if(head.equals(body[i])){ isRunning=false; } } } //吃到食物,增加蛇的身体(将食物变为蛇的头部),食物消失,重新生成 public void addBody(){ Node[] temp=new Node[body.length+1]; temp[0]=food; System.arraycopy(body,0,temp,1,body.length); body=temp; food=null; productFood(); } //是否吃到食物 public boolean isEat(){ Node newHead=getNewHead(); if(newHead.equals(food)){ return true; } return false; } //随机产生食物 public void productFood(){ food=new Node(randomNum(0,Window_W/20-1)*20,randomNum(2,Window_H/20-1)*20); } //通过修改坐标达到移动效果 public void move(){ //向右移动,每次给一个新的头结点,去掉尾节点(加头去尾),手动重新绘制,改变坐标) Node[] temp=new Node[body.length];//创建新数组(空的) temp[0]=getNewHead();//通过方向确定新节点 //去掉尾部 System.arraycopy(body,0,temp,1,body.length-1); this.body=temp; } //根据方向确定新的头结点 public Node getNewHead(){ Node newNode=body[0]; switch (this.direction){ //上1,下3;右0,左2 case 0://右 newNode=new Node(newNode.getX()+20,newNode.getY()); break; case 2://左 newNode=new Node(newNode.getX()-20,newNode.getY()); break; case 1://上 newNode=new Node(newNode.getX(),newNode.getY()-20); break; case 3://下 newNode=new Node(newNode.getX(),newNode.getY()+20); break; } return newNode;//返回新节点 } public void start(){ while (isRunning){ super.repaint();//手动让窗体重新绘制,repaint方法会自动调用paint方法 try { Thread.sleep(speed);//暂停事件 } catch (InterruptedException e) { e.printStackTrace(); } } super.repaint(); } //双缓冲解决闪屏问题 Image offScreenImage =null; public void update(Graphics g){ if(offScreenImage==null){ offScreenImage=this.createImage(600,500); } Graphics gOffScreen=offScreenImage.getGraphics(); paint(gOffScreen); g.drawImage(offScreenImage,0,0,null); offScreenImage=null; } //生成指定区间随机整数 public static int randomNum(int min,int max){ return (int)(Math.random()*(max-min+1)+min); } public static void main(String[] args) { new SnakeFrame09(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。