下面是自己在学习java时捣鼓出来的,只实现了业务逻辑,没有考虑美观。欢迎大家帮忙看看,大家相互学习,相互探讨!
业务分析:
一、找对象:
块:组成贪吃蛇的组成元素和每次随机生成的块。Cell
贪吃蛇:可以考虑用数组表示,不单独设置一个类
主窗体对象:Tcs
二、分析类:
Cell:x y width height image
up();dpwn();left();right();
Tcs:
各种图片变量,
c:存下次出现的块
she:存贪吃蛇主体的变量数组。
三、抽类
1、写Cell类
2、开始写Tcs类
3、加载各种静态图片
4、绘制主窗体
5、编写生成块的方法nextOne()方法
6、将块绘制到主页面去。
7、贪吃蛇初始化为2块,并绘制到页面上
8、编写贪吃蛇移动方法move();
9、添加键盘事件,控制贪吃蛇方向
10、编写bang()方法,确定2个块之间是否碰撞
11、编写jia()方法,实现当贪吃蛇碰到自由块时,长度增加1个
12、分别编写gameOver()方法,键盘事件,paintOver()方法,实现游戏结束、开始、暂停
public class Cell {
int x;
int y;
BufferedImage image;
int width;
int height;
public Cell(int x,int y,BufferedImage image) {
this.x=x;
this.y=y;
this.image=image;
width=image.getWidth();
height=image.getHeight();
}
//向上
public void up(){
y-=30;
}
//向下
public void down(){
y+=30;
}
//向左
public void left(){
x-=30;
}
//向右
public void right(){
x+=30;
}
}
public class Tcs extends JPanel{
//定义窗体的宽和高
public static final int WIDTH=800;
public static final int HEIGHT=850;
public static final int LEFT=0;
public static final int RIGHT=1;
public static final int UP=3;
public static final int DOWN=4;
public static final int RUN=0;
public static final int START=1;
public static final int PASUE=3;
public static final int OVER=4;
//定义一些变量
public static BufferedImage I;
public static BufferedImage J;
public static BufferedImage L;
public static BufferedImage O;
public static BufferedImage S;
public static BufferedImage T;
public static BufferedImage Z;
public static BufferedImage shetou;
public static BufferedImage shetou1;
public static BufferedImage shetou2;
public static BufferedImage shetou3;
public static BufferedImage back;
public static BufferedImage start;
public static BufferedImage gameover;
//定义一些用到的变量
public Cell c=nextOne();//自由块
public Cell[] she={new Cell(480,450,shetou),new Cell(510,450,L)};//初始化蛇
public int fangxiang;//定义方向的
public int zhuangtai=START;//默认是等待开始状态
public int sudu=600;
public int lv=1;
//静态加载图片
static {
try {
I = ImageIO.read(Tcs.class.getResource("I.png"));
J = ImageIO.read(Tcs.class.getResource("J.png"));
L = ImageIO.read(Tcs.class.getResource("L.png"));
O = ImageIO.read(Tcs.class.getResource("O.png"));
S = ImageIO.read(Tcs.class.getResource("S.png"));
T = ImageIO.read(Tcs.class.getResource("T.png"));
Z = ImageIO.read(Tcs.class.getResource("Z.png"));
back = ImageIO.read(Tcs.class.getResource("back.png"));
start = ImageIO.read(Tcs.class.getResource("sater.png"));
gameover = ImageIO.read(Tcs.class.getResource("gameover.png"));
shetou = ImageIO.read(Tcs.class.getResource("shetou.jpg"));
shetou1 = ImageIO.read(Tcs.class.getResource("shetou1.jpg"));
shetou2 = ImageIO.read(Tcs.class.getResource("shetou2.jpg"));
shetou3 = ImageIO.read(Tcs.class.getResource("shetou3.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
//主程序
public static void main(String[] args) {
JFrame jf = new JFrame("杨程的贪吃蛇");
Tcs ck = new Tcs();
jf.add(ck);
jf.setSize(WIDTH, HEIGHT);
jf.setAlwaysOnTop(true);// 总在最上
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默认关闭方式
jf.setLocationRelativeTo(null);
jf.setVisible(true); // 尽快调用paint()方法
ck.start();
}
//开始
private void start() {
KeyListener l = new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
if(zhuangtai==RUN){
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
if(fangxiang!=LEFT){
fangxiang=RIGHT;
}
break;
case KeyEvent.VK_LEFT:
if(fangxiang!=RIGHT){
fangxiang=LEFT;
}
break;
case KeyEvent.VK_UP:
if(fangxiang!=DOWN){
fangxiang=UP;
}
break;
case KeyEvent.VK_DOWN:
if(fangxiang!=UP){
fangxiang=DOWN;
}
break;
}
}
switch (e.getKeyCode()) {
case KeyEvent.VK_S:
if(zhuangtai==START||zhuangtai==PASUE){
zhuangtai=RUN;
}else if(zhuangtai==OVER){
c=nextOne();
she=new Cell[]{new Cell(480,450,shetou),new Cell(510,450,L)};
sudu=500;
lv=1;
zhuangtai=RUN;
}
break;
case KeyEvent.VK_P:
zhuangtai=PASUE;
break;
}
}
};
this.addKeyListener(l);
this.setFocusable(true);
// request 请求 Focus 焦点
this.requestFocus();
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
//贪吃蛇入场:编写一个绘制贪吃蛇的方法,但不用在这里调用
//贪吃蛇移动
if(zhuangtai==RUN){
move();
//如果蛇头与自由块碰撞,则蛇身加一
jia();
gameOver();
lv();
}
repaint();
}
},10,sudu);
}
//如果碰到自由块时,增加长度,并新建自由块
protected void jia() {
if(bang(c,she[0])){
she=Arrays.copyOf(she, she.length+1);
she[she.length-1]=new Cell(she[she.length-2].x,she[she.length-2].y,c.image);
c=nextOne();
}
}
//贪吃蛇的移动方法
protected void move() {
for(int i=she.length-1;i>0;i--){
she[i].x=she[i-1].x;
she[i].y=she[i-1].y;
}
switch (fangxiang) {
case LEFT:
she[0].left();
she[0].image=shetou;
break;
case RIGHT:
she[0].right();
she[0].image=shetou2;
break;
case UP:
she[0].up();
she[0].image=shetou1;
break;
case DOWN:
she[0].down();
she[0].image=shetou3;
break;
}
}
//绘制
public void paint(Graphics g) {
g.drawImage(back, 0, 0, null);
g.drawImage(c.image, c.x, c.y, null);//绘制自由块
paintTcs(g);
paintScore(g);
paintOver(g);
}
//绘制长度等
public void paintScore(Graphics g){
g.setFont(new Font("微软雅黑", Font.BOLD, 15));
g.drawString("长度:" + she.length, 10, 25);
g.drawString("LV:" + lv, 10, 45);
}
//绘制贪吃蛇
public void paintTcs(Graphics g){
for(int i=0;i<she.length;i++){
g.drawImage(she[i].image, she[i].x,she[i].y, null);
}
}
//绘制状态图片
public void paintOver(Graphics g){
if(zhuangtai==START){
g.drawImage(start, 0, 0, null);
}else if(zhuangtai==OVER){
g.drawImage(gameover, 0, 0, null);
}else if(zhuangtai==PASUE){
g.drawImage(start, 0, 0, null);
}
}
//工厂方法,生成出现块
public Cell nextOne(){
Random rd=new Random();
int a=rd.nextInt(7);
BufferedImage image=Tcs.I;
switch (a) {
case 0:
image=Tcs.I;
break;
case 1:
image=Tcs.J;
break;
case 2:
image=Tcs.L;
break;
case 3:
image=Tcs.O;
break;
case 4:
image=Tcs.S;
break;
case 5:
image=Tcs.T;
break;
case 6:
image=Tcs.Z;
break;
}
c=new Cell(rd.nextInt(Tcs.WIDTH-60),rd.nextInt(Tcs.HEIGHT-60),image);
return c;
}
public boolean bang(Cell f1, Cell f) {
int x1 = f1.x + f1.width / 2;
int y1 = f1.y + f1.height / 2;
int x2 = f.x + f.width / 2;
int y2 = f.y + f.height / 2;
return Math.abs(x1 - x2) < f1.width / 2 + f.width / 2
&& Math.abs(y1 - y2) < f1.height / 2 + f.height / 2;
}
public void gameOver(){
boolean flag=she[0].x<0||she[0].x>Tcs.WIDTH-she[0].width||she[0].y<0||she[0].y>Tcs.HEIGHT-she[0].height;
if(flag){
zhuangtai=OVER;
}
for(int i=1;i<she.length;i++){
if(bang(she[0],she[i])){
zhuangtai=OVER;
}
}
}
public void lv(){
if(she.length%10==0&&she.length<=110){
lv=she.length/10+1;
sudu=600-she.length*5;
}else{
lv=she.length/10+1;
sudu=50;
}
}
}