目录
描述:
实现步骤:
效果图
源码下载:地址链接:https://pan.baidu.com/s/1JQESGI4GaZb3p5EqikrUhQ 提取码:gzlp
//创建窗口
JFrame jf=new JFrame();
jf.setTitle("贪吃蛇"); //标题
jf.setBounds(400,0,900,700); //窗口大小位置
jf.setResizable(false); //是否可拉大
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭方式
SankePanel panel=new SankePanel(); //创建画布
jf.add(panel);
jf.setVisible(true); //是否可显示
//蛇的数据结构
int[] sankex= new int[850];
int[] sankey= new int[850];
int len=4;
String direction = "R"; //向右R 左L 下D 上U
//画蛇
//画蛇头
if(direction.equals("R")){
g.drawImage(headright.getImage(),sankex[0],sankey[0],25,25, this);
//headright.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("L")){
g.drawImage(headleft.getImage(),sankex[0],sankey[0],25,25, this);
//headleft.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("U")){
g.drawImage(headup.getImage(),sankex[0],sankey[0],25,25, this);
//headup.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("D")){
g.drawImage(headdowm.getImage(),sankex[0],sankey[0],25,25, this);
//headdowm.paintIcon(this, g, sankex[0], sankey[0]);
}
//画蛇身
for(int i=1;i
g.drawImage(heng.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]);
}else if(direction.equals("D")||direction.equals("U")){
g.drawImage(shu.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]
}
}
//画蛇尾
if(sankex[len-1]>=tailx&&sankey[len-1]==taily){
g.drawImage(tailright.getImage(),sankex[len-1],sankey[len-1],25,25, this);
//tailright.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]
//tailleft.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]==tailx&&sankey[len-1]
//tailup.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]==tailx&&sankey[len-1]>taily){
g.drawImage(taildown.getImage(),sankex[len-1],sankey[len-1],25,25, this);
//taildown.paintIcon(this, g, sankex[len], sankey[len]);
}
//移动尾部
tailx=sankex[len-1];
taily=sankey[len-1];
//移动身体
for (int i = len; i > 0; i--) {
sankex[i]=sankex[i-1];
sankey[i]=sankey[i-1];
}
//移动头部
if(direction.equals("R")){
//横坐标+25
sankex[0]=sankex[0]+25;
if(sankex[0]>850){
isFaild=true;
//sankex[0]=25;
}
}else if(direction.equals("L")){
//横坐标-25
sankex[0]=sankex[0]-25;
if(sankex[0]<25){
isFaild=true;
//sankex[0]=850;
}
}else if(direction.equals("U")){
//纵坐标-25
sankey[0]=sankey[0]-25;
if(sankey[0]<100){
isFaild=true;
//sankey[0]=625;
}
}else if(direction.equals("D")){
//纵坐标+25
sankey[0]=sankey[0]+25;
if(sankey[0]>625){
isFaild=true;
//sankey[0]=100;
}
}
实现转向
//按键控制
int keyCode = e.getKeyCode();
if (keyCode==KeyEvent.VK_SPACE) {
if (isFaild) {
initSanke();
}else {
isSarted=!isSarted;
}
}else if(keyCode==KeyEvent.VK_UP&& !direction.equals("D")){
direction="U";
}else if(keyCode==KeyEvent.VK_DOWN&& !direction.equals("U")){
direction="D";
}else if(keyCode==KeyEvent.VK_LEFT&& !direction.equals("R")){
direction="L";
}else if(keyCode==KeyEvent.VK_RIGHT&& !direction.equals("L")){
direction="R";}
食物
//food数据结构
Random random=new Random();
int foodx=random.nextInt(34)*25+25;
int foody=random.nextInt(22)*25+100;
//画食物
g.drawImage(food.getImage(),foodx,foody,25,25, this);
//吃食物
if(sankex[0]==foodx &&sankey[0]==foody){
score++;
len++;
foodx=random.nextInt(34)*25+25;
foody=random.nextInt(22)*25+100;
//避免食物在蛇身上
for(int j=1;j<2;j++){
for(int i=1;i
foodx=25;
foody=100;
}
}
}
提示开始结束
//游戏是否开始
boolean isSarted = false;
//游戏失败
boolean isFaild = false;
//画提示
if (!isSarted) {
g.setColor(Color.white);
g.setFont(new Font("arial",Font.BOLD,30));
g.drawString("press Spce to Start/pause", 300, 300);
}
if(isFaild){
g.setColor(Color.white);
g.setFont(new Font("arial",Font.BOLD,30));
g.drawString("Game Over! press Spce to Start", 300, 300);
}
if (keyCode==KeyEvent.VK_SPACE) {
if (isFaild) {
initSanke();
}else {
isSarted=!isSarted;
}
优化蛇转向
//画蛇头
if(direction.equals("R")){
g.drawImage(headright.getImage(),sankex[0],sankey[0],25,25, this);
//headright.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("L")){
g.drawImage(headleft.getImage(),sankex[0],sankey[0],25,25, this);
//headleft.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("U")){
g.drawImage(headup.getImage(),sankex[0],sankey[0],25,25, this);
//headup.paintIcon(this, g, sankex[0], sankey[0]);
}else if(direction.equals("D")){
g.drawImage(headdowm.getImage(),sankex[0],sankey[0],25,25, this);
//headdowm.paintIcon(this, g, sankex[0], sankey[0]);
}
//画蛇身
for(int i=1;i
g.drawImage(heng.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]);
}else if(direction.equals("D")||direction.equals("U")){
g.drawImage(shu.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]);
* sankey[i]==sankey[0]&&sankex[i]!=sankex[0]
}*/
//蛇身转向
if(sankex[i]==sankex[i-1]&&sankey[i]==sankey[i+1]&&sankey[i]
}else if (sankex[i]==sankex[i-1]&&sankey[i]==sankey[i+1]&&sankey[i]>sankey[i-1]&&sankex[i+1]
}else if (sankex[i]==sankex[i-1]&&sankey[i]==sankey[i+1]&&sankey[i]
g.drawImage(upright.getImage(),sankex[i],sankey[i],25,25, this);//左下转
}else if (sankex[i]==sankex[i-1]&&sankey[i]==sankey[i+1]&&sankey[i]>sankey[i-1]&&sankex[i+1]>sankex[i]) {
g.drawImage(downright.getImage(),sankex[i],sankey[i],25,25, this);//左上转
}else if (sankex[i]==sankex[i+1]&&sankey[i]==sankey[i-1]&&sankey[i]
g.drawImage(upright.getImage(),sankex[i],sankey[i],25,25, this);//上右转
}else if (sankex[i]==sankex[i+1]&&sankey[i]==sankey[i-1]&&sankey[i]
}else if (sankex[i]==sankex[i+1]&&sankey[i]==sankey[i-1]&&sankey[i]>sankey[i+1]&&sankex[i-1]>sankex[i]) {
g.drawImage(downright.getImage(),sankex[i],sankey[i],25,25, this);//下右转
}else if (sankex[i]==sankex[i+1]&&sankey[i]==sankey[i-1]&&sankey[i]>sankey[i+1]&&sankex[i-1]
}
else {
if(sankey[i]==sankey[i-1]){
g.drawImage(heng.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]);
}else{
g.drawImage(shu.getImage(),sankex[i],sankey[i],25,25, this);
//heng.paintIcon(this, g, sankex[i], sankey[i]);
}
}
}
//画蛇尾
if (sankex[len-1]
}else if (sankex[len-1]>sankex[len-2]&&sankey[len-1]==sankey[len-2]) {
g.drawImage(tailleft.getImage(),sankex[len-1],sankey[len-1],25,25, this);
}
else if (sankex[len-1]==sankex[len-2]&&sankey[len-1]>sankey[len-2]) {
g.drawImage(tailup.getImage(),sankex[len-1],sankey[len-1],25,25, this);
}
else if (sankex[len-1]==sankex[len-2]&&sankey[len-1]
}
else {
if(sankex[len-1]>=tailx&&sankey[len-1]==taily){
g.drawImage(tailright.getImage(),sankex[len-1],sankey[len-1],25,25, this);
//tailright.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]
//tailleft.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]==tailx&&sankey[len-1]
//tailup.paintIcon(this, g, sankex[len], sankey[len]);
}else if(sankex[len-1]==tailx&&sankey[len-1]>taily){
g.drawImage(taildown.getImage(),sankex[len-1],sankey[len-1],25,25, this);
//taildown.paintIcon(this, g, sankex[len], sankey[len]);
}
}
添加计分
//统计分数
int score=0;
//等级
String grade="新手";
//分数和长度统计,等级
g.setColor(Color.white);
g.setFont(new Font("arial",Font.BOLD,18));
g.drawString("Score:"+score, 750, 40);
g.drawString("len:"+len, 750, 75);
if (score<=20) {
grade="新手";
}else if (score>20&&score<=50) {
grade="入门";
}else if (score>50&&score<=80) {
grade="优秀";
}else if (score>80&&score<=100) {
grade="大师";
}else if (score>100&&score<=150) {
grade="精英";
}else if (score>150&&score<=200) {
grade="大神";
}
else {
grade="超神";
}
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,18));
g.drawString("等级:"+grade, 80, 40);
}
添加幸运蛋
//幸运蛋
Random r=new Random();
int egex=r.nextInt(32)*25+50;
int egey=r.nextInt(20)*25+125;
//画幸运蛋
if (score%10==0&&score!=0) {
g.setColor(Color.RED);
g.fillOval(egex, egey, 50, 50);
}
//吃幸运蛋
if(sankex[0]==egex &&(sankey[0]==egey||sankey[0]==egey+25||sankey[0]==egey-25)||
sankey[0]==egey &&(sankex[0]==egex+25||sankex[0]==egex-25) ){
score+=5;
len+=5;
egex=r.nextInt(32)*25+50;
egey=r.nextInt(20)*25+125;
//避免食物在蛇身上
for(int j=1;j<2;j++){
for(int i=1;i
egex=r.nextInt(32)*25+50;
egey=r.nextInt(20)*25+125;
}
}
}