欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单贪吃蛇小游戏

程序员文章站 2022-07-08 23:38:23
本文实例为大家分享了java实现贪吃蛇小游戏,供大家参考,具体内容如下有两个类,放在同一文件下下即可,背景图片,标题,蛇尾可自行更改import java.awt.*;import javax.swi...

本文实例为大家分享了java实现贪吃蛇小游戏,供大家参考,具体内容如下

有两个类,放在同一文件下下即可,背景图片,标题,蛇尾可自行更改

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
public class myjframe extends jframe{

 public myjframe() {
  
  int x= toolkit.getdefaulttoolkit().getscreensize().width;
  int y = toolkit.getdefaulttoolkit().getscreensize().height;
  setlocation((x-800)/2, (y-800)/2);
  setsize(800,800);
  setvisible(true);
  setdefaultcloseoperation(exit_on_close);
  settitle("贪吃蛇");
  
  setlayout(null);
  
  getlayeredpane().setlayout(null);
  jlabel background = new jlabel(new imageicon("c:\\users\\hp\\desktop\\javaa\\background3.jpg"));//注意此处路径
  jlabel title = new jlabel(new imageicon("c:\\users\\hp\\desktop\\javaa\\title1.jpg"));//注意此处路径
  background.setbounds(20,80,750,650);
  title.setbounds(0,0,800,75);
  getlayeredpane().add(background,new integer(integer.min_value));
  getlayeredpane().add(title,new integer(integer.min_value+1));
  
  ((jpanel)getcontentpane()).setopaque(false);
  
  mypanel mypanel = new mypanel();
  mypanel.setbounds(0,0,getwidth(),getheight());
  add(mypanel);
 
 }
 
 public static void main(string[] args) {
  new myjframe();
 }
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.random;

public class mypanel extends jpanel implements actionlistener{
 int lenght ;
 int score1;
 boolean jieshux = false;

 jlabel scorel = new jlabel("分数:"+score1);
 jlabel lenghl = new jlabel("长度:"+lenght);
 
 jlabel shibai = new jlabel("按下空格开始游戏!");
 jlabel jieshu = new jlabel("游戏失败!按空格重新开始!");
 
 string dir = "r";
 boolean isstart = false;
 int[][] snake = new int[300][2];
 
 random r = new random();
 int x,y;
 
 //初始化
 public void initialize() {
  snake[0][0]=70;
  snake[0][1]=80;
  snake[1][0]=45;
  snake[1][1]=80;
  snake[2][0]=20;
  snake[2][1]=80;
  isstart = true;
  jieshux = false;
  lenght = 3;
  score1 = 0;
  dir = "r";
  repaint();
 }
 
 public mypanel() {
  
  setlayout(null);
  scorel.setbounds(700, 20, 50, 30);
  lenghl.setbounds(700, 40, 50, 30);
  shibai.setbounds(300,400,200,60);
  shibai.setfont(new font("黑体",font.bold,20));
  jieshu.setfont(new font("黑体",font.bold,20));
  jieshu.setvisible(false);
  jieshu.setbounds(250,400,300,60);
  
  add(scorel);
  add(lenghl);
  add(shibai);
  add(jieshu);
  
  x = r.nextint(29)*25+20;
  y = r.nextint(25)*25+80;
  
  //this.setfocusable(true); //焦点获取
  setopaque(false);
  
  initialize();
  
  timer t = new timer(200,this);
  this.addkeylistener(new keyadapter() {
   public void keypressed(keyevent e) {
    if(e.getkeycode()==keyevent.vk_space && isstart==false) {
     isstart=true;
    }else if(e.getkeycode()==keyevent.vk_space && isstart==true &&jieshux == false){
     isstart = false;
    }else if(e.getkeycode()==keyevent.vk_w && isstart==true && dir!="d"&&jieshux == false) {
     dir="u";
    }else if(e.getkeycode()==keyevent.vk_a && isstart==true && dir!="r"&&jieshux == false) {
     dir="l";
    }else if(e.getkeycode()==keyevent.vk_d && isstart==true && dir!="l"&&jieshux == false) {
     dir="r";
    }else if(e.getkeycode()==keyevent.vk_s && isstart==true && dir!="u"&&jieshux == false) {
     dir="d";
    }else if(e.getkeycode()==keyevent.vk_space && jieshux==true) {
     initialize();
    }
   }
  });
  t.start();

 }
 
 @override
 public void actionperformed(actionevent e) {
  this.requestfocus(true);

   if(isstart&&dir.equals("r")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][0]>=745) {
      //snake[i][0]=20;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][0]=snake[1][0]+25;
   } else if(isstart&&dir.equals("d")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][1]>=705) {
      //snake[i][1]=80;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][1]=snake[1][1]+25;
   }else if(isstart&&dir.equals("u")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][1]<=80) {
      //snake[i][1]=705;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][1]=snake[1][1]-25;
   }else if(isstart&&dir.equals("l")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][0]<=20) {
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][0]=snake[1][0]-25;
   }

   if(snake[0][1]==y&&snake[0][0]==x) {
    x = r.nextint(29)*25+20;
    y = r.nextint(25)*25+80;
    
    score1+=10;
    lenght++;
    snake[lenght-1][1]=snake[lenght-2][1];
    snake[lenght-1][0]=snake[lenght-2][0];

    
    add(scorel);
    add(lenghl);
   }
   
   if(isstart==false) {
    shibai.setvisible(true);
   }else {
    shibai.setvisible(false);
    if(jieshux == false) {
     jieshu.setvisible(false);
    }else {
     jieshu.setvisible(true);
    }
   }
   
   for(int i = 1;i<lenght;i++) {
    if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1]) {
     jieshux=true;
    }
   }
   
   if(isstart&&!jieshux)repaint();
 }
 
 public void paint(graphics g) {
  super.paint(g);
  //边界为(20,80),(770,730)
  scorel.settext("分数:"+score1);
  lenghl.settext("长度:"+lenght);
  
  g.drawline(18, 78, 18, 732);
  g.drawline(18, 78, 772, 78);
  g.drawline(18, 732, 772, 732);
  g.drawline(772, 78, 772, 732);
  //竖线
  for(int i=20;i<=770;i+=25) {
   g.drawline(i,80,i,730);
   //g.drawline(0,i,600,i);
  }
  
  //横线
  for(int i=80;i<=750;i+=25) {
   //g.drawline(i,50,i,780);
   g.drawline(20,i,770,i);
  }
  
  for(int i = 0;i<lenght;i++) {
   if(i>0) {
    g.drawimage(new imageicon("c:\\users\\hp\\desktop\\javaa\\body.png").getimage(), snake[i][0],snake[i][1],this);//注意此处路径
   }else {
    g.filloval(snake[i][0], snake[i][1], 25, 25);
   }
  }
  
  g.setcolor(color.red);
  g.filloval(x, y, 25, 25);
  
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: java 贪吃蛇