GUI

23-26-贪吃蛇
GamePanel.class
package snack;
//游戏面板
//1.定义数据2.画上去3.监听事件:键盘;事件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
public class GamePanel extends JPanel implements KeyListener,ActionListener{
 //定义蛇的数据结构
 int length;//蛇的长度
 int[] snakeX=new int[600];//蛇的x坐标
 int[] snakeY=new int[500];//蛇的Y坐标
 String direction;//头方向
 boolean isStart=false;//游戏当前状态,开始,停止
 int foodx;//食物的坐标
 int foody;
 Random random =new Random();
 boolean isFail = false;//游戏失败状态
 int score;//成绩
 //定时器,以毫秒为单位
 Timer timer=new Timer(100, this);//100毫秒执行一次
 //初始化方法
 public void init() {
  length=3;
  snakeX[0]=80;  snakeY[0]=80;//头坐标
  snakeX[1]=60;  snakeY[1]=80;//第一个身体坐标
  snakeX[2]=40;  snakeY[2]=80;//第二个身体坐标
  //食物随机分布在界面上
  foodx=20+20*random.nextInt(34);//边界
  foody=60+20*random.nextInt(24);//边界
  score=0;
  direction="right";//初始方向
 }
 //构造器
 public GamePanel() {
  // TODO Auto-generated constructor stub
  init();
  //获得焦点和键盘事件
  this.setFocusable(true);
  this.addKeyListener(this);
  timer.start();//游戏一开始定时器就启动
 }
 //绘制面板,画笔画所有游戏界面
 @Override
 protected void paintComponent(Graphics g) {
  // TODO Auto-generated method stub
  super.paintComponent(g);//清屏?
  this.setBackground(Color.WHITE);
  //绘制静态面板
  Data.headlineIcon.paintIcon(this,g,20,5);//绘画头部广告栏
  g.fillRect(20, 60, 680, 480);//默认游戏界面
  //画积分
  g.setColor(Color.BLACK);
  g.setFont(new Font("微软雅黑",Font.BOLD,10));
  g.drawString("长度: "+length, 650, 21);
  g.drawString("分数: "+score, 650, 34);
  //g.setColor(null);
  //画食物,先画会覆盖
    Data.eat.paintIcon(this, g, foodx, foody);
  //画小蛇
  if (direction.equals("right")) {
   Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
  }else if (direction.equals("left")) {
   Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
  }else if (direction.equals("up")) {
   Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
  }else if (direction.equals("down")) {
   Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
  }  
  for (int i = 1; i < length; i++) {
   Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);   
  }  
  //游戏状态
  if (isStart==false) {
   g.setColor(Color.white);
   g.setFont(new Font("微软雅黑",Font.BOLD,32));
   g.drawString("按下空格开始游戏!",240,300);
  }
  if (isFail) {
   g.setColor(Color.red);
   g.setFont(new Font("微软雅黑",Font.BOLD,32));
   g.drawString("失败,按下空格重新开始!",240,300);
  }
 }
 //键盘监听事件
 @Override
 public void keyPressed(KeyEvent e) {
  // TODO Auto-generated method stub  
  int KeyCode=e.getKeyCode();
  if (KeyCode==KeyEvent.VK_SPACE) {
   if (isFail) {
    isFail=false;//重新开始
    init();
   }else {
    isStart=!isStart;//取反
   }   
   repaint();
  }
  //小蛇移动
  if (KeyCode==KeyEvent.VK_UP) {
   direction="up";
  }else if (KeyCode==KeyEvent.VK_DOWN) {
   direction="down";
  }else if (KeyCode==KeyEvent.VK_LEFT) {
   direction="left";
  }else if (KeyCode==KeyEvent.VK_RIGHT) {
   direction="right";
  }
 }
 @Override
 public void keyReleased(KeyEvent var1) {
  // TODO Auto-generated method stub
 }
 @Override
 public void keyTyped(KeyEvent var1) {
  // TODO Auto-generated method stub 
 }
 //事件监听--通过固定的事件刷新,比如一秒10次
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if (isStart  && isFail==false) {//如果游戏是开始状态,让小蛇动起来
   //吃食物
   if (snakeX[0]==foodx && snakeY[0]==foody) {
    length++;
    //分数+10,长度+1
    score+=10;
    length+=1;
    //再次随机食物
    foodx=20+20*random.nextInt(34);//边界
    foody=60+20*random.nextInt(24);//边界
   }
   //右移   
   for (int i = length-1; i >0; i--) {//后一节移到前一节的位置
    snakeX[i]=snakeX[i-1];
    snakeY[i]=snakeY[i-1];
   }
   //走向
   if (direction.equals("up")) {
    snakeY[0]-=20;
    if (snakeY[0]<60) {
     snakeY[0]=520;
    }
   }else if (direction=="down") {
    snakeY[0]+=20;
    if (snakeY[0]>520) {
     snakeY[0]=60;
    }
   }else if (direction=="left") {
    snakeX[0]-=20;
    if (snakeX[0]<20) {
     snakeX[0]=680;
    }
   }else if (direction=="right") {snakeX[0]+=20;
   //边界判断
   if (snakeX[0]>680) {
    snakeX[0]=20;
    }
   }  
   //失败判断,撞到自己
   for (int i = 1; i < length; i++) {
    if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i]) {
     isFail=true;
    }
   }
   repaint();
//   timer.start();
  }  
 } 
}
StartGame.class
package snack;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class StartGame {
 public static void main(String[] arg) {
  JFrame jFrame=new JFrame();  
  jFrame.setBounds(10, 10, 720, 580);
  jFrame.setResizable(false);
  jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  jFrame.setVisible(true);
  jFrame.add(new GamePanel());
 }
}
Data.java
package snack;
//数据中心
import java.net.URL;
import javax.swing.ImageIcon;
public class Data {
 public static URL headlineURL =Data.class.getResource("headline.jpg");
 public static ImageIcon headlineIcon = new ImageIcon(headlineURL);
 public static URL bodyUrl =Data.class.getResource("body.jpg");
 public static ImageIcon body = new ImageIcon(bodyUrl);
 public static URL upUrl =Data.class.getResource("headup.jpg");
 public static ImageIcon up = new ImageIcon(upUrl);
 public static URL downUrl =Data.class.getResource("headdown.jpg");
 public static ImageIcon down = new ImageIcon(downUrl);
 public static URL leftUrl =Data.class.getResource("headleft.jpg");
 public static ImageIcon left = new ImageIcon(leftUrl);
 public static URL rightUrl =Data.class.getResource("headright.jpg");
 public static ImageIcon right = new ImageIcon(rightUrl);
 public static URL eatUrl =Data.class.getResource("eat.jpg");
 public static ImageIcon eat = new ImageIcon(eatUrl);
}

改进:积分等级改变刷新时间;
撞头,回头规避;
食物类型;有毒,长三节;
弹窗:登录
鼠标拖拽控制方向;
数据库:存盘
联机对战

27-总结

processon

网络编程

01-什么是计算机网络
02-网络通信的要素

IP与端口号,通信协议

https://blog.csdn.net/qq_27623337/article/details/80982931?ops_request_misc=&request_id=&biz_id=102&utm_term=%E7%BD%91%E7%BB%9C%E4%B8%83%E5%B1%82%E5%8D%8F%E8%AE%AE&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-80982931

太卡了,新开一节

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐