package com.company.snake;
import javax.swing.*;
import java.awt.*;
public class startGame {
public static void main(String[] args) {
JFrame jFrame = new JFrame("贪吃蛇小游戏");
jFrame.setBounds(10, 10, 900, 720);
jFrame.setResizable(false);//窗口大小不可变
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//正常游戏界面应该出现在面上
jFrame.add(new gamePanel());
jFrame.setVisible(true);
}
}
package com.company.snake;
import javax.swing.*;
import java.awt.*;
public class gamePanel extends JPanel {
//绘制面板,我们游戏中所有东西,都是用这个画笔画
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
}
}
package com.company.snake;
import javax.swing.*;
import java.net.URL;
public class Data {
//相对路径 直接取jpg
//绝对路径 /相当于当前目录
public static URL headerURL= Data.class.getResource("/statics/header.png");
public static URL upURL= Data.class.getResource("/statics/up.png");
public static URL downURL= Data.class.getResource("/statics/down.png");
public static URL leftURL= Data.class.getResource("/statics/left.png");
public static URL rightURL= Data.class.getResource("/statics/right.png");
public static ImageIcon header=new ImageIcon(headerURL);
public static ImageIcon up=new ImageIcon(upURL);
public static ImageIcon down=new ImageIcon(downURL);
public static ImageIcon left=new ImageIcon(leftURL);
public static ImageIcon right=new ImageIcon(rightURL);
public static URL bodyURL= Data.class.getResource("/statics/body.png");
public static ImageIcon body=new ImageIcon(bodyURL);
public static URL foodURL= Data.class.getResource("/statics/food.png");
public static ImageIcon food=new ImageIcon(foodURL);
}
这里没有使用面向对象思想,直接在面板gamePanel进行初始化静态小蛇
package com.company.snake;
import javax.swing.*;
import java.awt.*;
public class gamePanel extends JPanel {
//定义蛇的数据结构
int length;
int[] snakeX=new int[600];//蛇的x坐标
int[] snakeY=new int[500];//蛇的y坐标
String fx;
//当前游戏状态
boolean isStart=false;
//构造器
public gamePanel(){
init();
}
//初始化方法
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100;//脑袋坐标
snakeX[1]=75;snakeY[1]=100;//第一个身体坐标
snakeX[2]=50;snakeY[2]=100;//第二个身体坐标
fx="R";//初始方法向右
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
//画小蛇 通过方向来判断初始化头朝向
if(fx.equals("R")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("L")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("U")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("D")){
Data.right.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,40));//字体
g.drawString("按下空格开始游戏",300,400);
}
}
}
1、实现监听(仍然在面板gamePanel操作)
public class gamePanel extends JPanel implements KeyListener
2、重写方法
//键盘监听事件
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获得键盘按钮是哪一个
if (keyCode ==KeyEvent.VK_SPACE){
isStart=!isStart;//取反,可以实现停止和开始
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
3、构造器获取监听事件
public gamePanel() {
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//获得键盘监听事件
}
implements KeyListener, ActionListener
//定时器 以ms为单位1000ms=1s
Timer timer=new Timer(100,this);//100毫秒一次
//事件监听---需要通过固定时间刷新,1s10次
@Override
public void actionPerformed(ActionEvent e) {
if(isStart){
//如果开始,就动起来
//右移
for (int i = length-1; i >0 ; i--) {
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
//走向
if(fx.equals("R")){
snakeX[0]=snakeX[0]+25;
if(snakeX[0]>850){
snakeX[0]=25; } //边界判断
}else if(fx.equals("L")){
snakeX[0]=snakeX[0]-25;
if(snakeX[0]<25){
snakeX[0]=850; } //边界判断
}else if(fx.equals("U")){
snakeY[0]=snakeY[0]-25;
if(snakeY[0]<75){
snakeY[0]=650; } //边界判断
}else if(fx.equals("D")){
snakeY[0]=snakeY[0]+25;
if(snakeY[0]>650){
snakeY[0]=75; } //边界判断
}
repaint();//重画画面
}
timer.start();//计时器
}
}
//食物坐标
int foodX;
int foodY;
Random random=new Random();//随机生成
foodX=25+25*random.nextInt(34);foodY=75+25*random.nextInt(24);
//画食物Data.body.paintIcon(this,g,foodX,foodY);
//吃食物
if(snakeX[0]==foodX&&snakeY[0]==foodY){
length++;
//两次随机食物
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
}
//积分
int score;
score=0;
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度" + length, 300, 35);
g.drawString("分数" + score, 300, 52);
//分数加10
score=score+10;
package com.company.snake;
import sun.security.util.Length;
import javax.swing.*;
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;
public class gamePanel extends JPanel implements KeyListener, ActionListener {
//定义蛇的数据结构
int length;
int[] snakeX = new int[600];//蛇的x坐标
int[] snakeY = new int[500];//蛇的y坐标
String fx;
//食物坐标
int foodX;
int foodY;
Random random=new Random();//随机生成
//积分
int score;
//当前游戏状态
boolean isStart = false;
//游戏失败状态
boolean isFail = false;
//定时器 以ms为单位1000ms=1s
Timer timer=new Timer(100,this);//100毫秒一次
//构造器
public gamePanel() {
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//获得键盘监听事件
timer.start();
}
//初始化方法
public void init() {
length = 3;
snakeX[0] = 100;
snakeY[0] = 100;//脑袋坐标
snakeX[1] = 75;
snakeY[1] = 100;//第一个身体坐标
snakeX[2] = 50;
snakeY[2] = 100;//第二个身体坐标
fx = "R";//初始方法向右
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
score=0;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
this.setBackground(Color.WHITE);
Data.header.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);
//画小蛇 通过方向来判断初始化头朝向
if (fx.equals("R")) {
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (fx.equals("L")) {
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (fx.equals("U")) {
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (fx.equals("D")) {
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]);
}
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度" + length, 300, 35);
g.drawString("分数" + score, 300, 52);
//画食物
Data.food.paintIcon(this,g,foodX,foodY);
//游戏状态
if (isStart == false) {
g.setColor(Color.white);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));//字体
g.drawString("按下空格开始游戏", 300, 400);
}
//游戏失败
if (isFail) {
g.setColor(Color.red);
g.setFont(new Font("微软雅黑", Font.BOLD, 50));//字体
g.drawString("游戏失败", 360, 400);
}
}
//键盘监听事件
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获得键盘按钮是哪一个
if (keyCode ==KeyEvent.VK_SPACE){
if(isFail){
//重新开始
isFail=false;
init();
}else{
isStart=!isStart;//取反,可以实现停止和开始
}
repaint();
}
//小蛇移动
if (keyCode == KeyEvent.VK_UP){
fx="U";
}else if (keyCode == KeyEvent.VK_DOWN){
fx="D";
}else if (keyCode == KeyEvent.VK_LEFT){
fx="L";
}else if (keyCode == KeyEvent.VK_RIGHT){
fx="R";
}
}
@Override
public void keyReleased(KeyEvent e) {
}
//事件监听---需要通过固定时间刷新,1s10次
@Override
public void actionPerformed(ActionEvent e) {
if(isStart && isFail==false){
//如果开始,就动起来
//吃食物
if(snakeX[0]==foodX&&snakeY[0]==foodY){
length++;
//分数加10
score=score+10;
//两次随机食物
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
}
//右移
for (int i = length-1; i >0 ; i--) {
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
//走向
if(fx.equals("R")){
snakeX[0]=snakeX[0]+25;
if(snakeX[0]>850){
snakeX[0]=25; } //边界判断
}else if(fx.equals("L")){
snakeX[0]=snakeX[0]-25;
if(snakeX[0]<25){
snakeX[0]=850; } //边界判断
}else if(fx.equals("U")){
snakeY[0]=snakeY[0]-25;
if(snakeY[0]<75){
snakeY[0]=650; } //边界判断
}else if(fx.equals("D")){
snakeY[0]=snakeY[0]+25;
if(snakeY[0]>650){
snakeY[0]=75; } //边界判断
}
//失败的判定
for (int i = 1; i <length ; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isFail=true;
}
}
repaint();//重画画面
}
timer.start();//计时器
}
}