美文网首页
自制贪吃蛇小游戏代码

自制贪吃蛇小游戏代码

作者: 郭澄海 | 来源:发表于2020-11-24 16:10 被阅读0次
package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game_Start extends JFrame{

JFrame frame = new JFrame();   
JButton btn1 = new JButton("开始游戏");
JButton btn2 = new JButton("游戏介绍");
JButton btn3 = new JButton("退出游戏");
Image start = (new ImageIcon("src/icon/start.jpg")).getImage();
Font f = new Font("微软雅黑",Font.BOLD,30);

public Game_Start() {
    setSize(1400,1200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setResizable(false);
    setTitle("贪吃蛇开始界面");
    add(new Pane());
}

public class Pane extends JPanel{
    public Pane() {
        setLayout(null);
        Button();

    btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                new Operation();
                dispose();
}
    });

    btn2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog dialog=new JDialog();
            dialog.setSize(800,600);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            dialog.setTitle("游戏介绍");
            String strMsg1 = "____通过键盘的上下左右键控制小蛇的移动";   
            String strMsg2 = "____当小蛇吃到食物会增长身体一单位长度"; 
            String strMsg3="____当蛇头碰到墙壁或身体则游戏结束";
            String strMsg4="____点击空格游戏开始,游戏结束可点击空格重新开始游戏";
            String strMsg = "<html><body>" + strMsg1 + "<br>" + strMsg2 + "<br>"+strMsg3+"<br>"+strMsg4+"<body></html>";  
            JLabel label = new JLabel(strMsg);
            label.setFont(f);
            dialog.add(label);
        }
    });

    btn3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
            }
        });
    }

public void paintComponent(Graphics g) {
    super.paintComponents(g);
    g.drawImage(start,0,0,1400,1200,this);
}

public void Button() {
    add(btn1);
    add(btn2);
    add(btn3);
    btn1.setBounds(575,350,200,100);
    btn2.setBounds(575,500,200,100);
    btn3.setBounds(575,650,200,100);
    btn1.setFont(f);
    btn2.setFont(f);
    btn3.setFont(f);
}
}

    public static void main(String[]args) {
    new Game_Start();
}
}

package game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
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 Operation extends JFrame {
public Operation() {
    setSize(1400,1200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);  
    setTitle("贪吃蛇小游戏");
    add(new Panel());
}

public class Panel extends JPanel implements ActionListener, KeyListener {
    Image up = (new ImageIcon("src/icon/up.png")).getImage();
    Image down = (new ImageIcon("src/icon/down.png")).getImage();
    Image left = (new ImageIcon("src/icon/left.png")).getImage();
    Image right = (new ImageIcon("src/icon/right.png")).getImage();
    Image body = (new ImageIcon("src/icon/body.png")).getImage();
    Image food = (new ImageIcon("src/icon/food.png")).getImage();
    Image gamebg = (new ImageIcon("src/icon/gamebg.jpg")).getImage();
    String fx;
    boolean isStart = false;
    boolean isOver = false;
    int len;
    int[]snakex = new int[1400];
    int[]snakey = new int[1200];
    Timer timer = new Timer(100,this);
    int foodx;
    int foody;
    Random random = new Random();
    int score = 0;

    public Panel() {
        Snake();
        timer.start();
        this.setFocusable(true);
        this.addKeyListener(this);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.ORANGE);
        g.setFont(new Font("黑体",Font.BOLD,70));
        g.drawImage(gamebg,0,0,1400,1200,this);
        if(fx == "U") {
            g.drawImage(up, snakex[0], snakey[0], 50, 50, this);
        }else if(fx == "D"){
            g.drawImage(down, snakex[0], snakey[0], 50, 50, this);
        }else if(fx == "L") {
            g.drawImage(left, snakex[0], snakey[0], 50, 50, this);
        }else if(fx == "R") {
            g.drawImage(right, snakex[0], snakey[0], 50, 50, this);
        }
        for(int i = 1;i<len;i++) {
            g.drawImage(body,snakex[i],snakey[i],50,50,this);
        }
        g.drawImage(food, foodx, foody, 30, 30, this);
        g.drawString("分数: "+score, 1000, 100);
        if(isStart == false) {
            g.drawString("按下空格键开始游戏",380,600);
        }
        if(isOver == true) {
            g.drawString("按下空格键开始游戏", 380, 600);
        }
    }

    public void Snake() {
        len = 3;
        fx = "R";
        score = 0;
        snakex[0] = 200;
        snakey[0] = 40;
        snakex[1] = 160;
        snakey[1] = 40;
        snakex[2] = 120;
        snakey[2] = 40;
        foodx = 40*random.nextInt(31);
        foody = 40*random.nextInt(28);
    }
    
public void actionPerformed(ActionEvent e) {
    if(isStart&&!isOver) {
        for(int i = len-1;i>0;i--) {
            snakex[i] = snakex[i-1];
            snakey[i] = snakey[i-1];
        }
        if(snakex[0] == foodx && snakey[0] == foody) {
            len++;
            score++;
            foodx = 40*random.nextInt(31);
            foody = 40*random.nextInt(28);
            for(int i = len-1;i>0;i--) {
                snakex[i] = snakex[i-1];
                snakey[i] = snakey[i-1];}
        }
        if(fx == "R") {
            snakex[0] += 40;
            
        }else if(fx == "L") {
            snakex[0] -= 40;
            
        }else if(fx == "U") {
            snakey[0] -= 40;
            
        }else if(fx == "D") {
            snakey[0] += 40;
            
        }
        for(int i = 1;i<len;i++) {
            if(snakex[i] == snakex[0] && snakey[i] == snakey[0]||snakex[0]>1380||snakex[0]<0|| snakey[0]<0 || snakey[0]>1120) {
                isOver = true;
            }
        }
        repaint();
    }
    timer.start();
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_SPACE) {
            if(isOver) {
                isOver = false;
                Snake();
            }
            isStart = !isStart;
            repaint();
        }else if (keyCode == KeyEvent.VK_LEFT && fx! = "R"){
            fx="L";
        }else if (keyCode == KeyEvent.VK_DOWN && fx! = "U"){
            fx = "D";
        }else if(keyCode == KeyEvent.VK_UP && fx! = "D") {
            fx = "U";
        }else if(keyCode == KeyEvent.VK_RIGHT && fx! = "L") {
            fx = "R";
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }
}
}

相关文章

  • 自制贪吃蛇小游戏代码

  • 贪吃蛇

    js 贪吃蛇代码

  • 游戏 & Github Page

    1. snakewizard.github.io 贪吃蛇小游戏 2. mattbasile.github.io 龙...

  • 《极限贪吃蛇》

    贪吃蛇是一款非常有趣的小游戏。 益智有好玩,等你来体验

  • “贪吃蛇🐍”的一生

    前段时间,出于无聊,下载了“贪吃蛇”这个游戏,原本以为这样的小游戏没啥意思,没想到居然上头了。 “贪吃蛇”这个游戏...

  • 新贵 Rust — 贪吃蛇(1)

    今天开始用 Rust 写一个小游戏,大家可能都玩过—贪吃蛇,今天就写个贪吃蛇 snake。首先创建一个项目 sna...

  • 20行js的贪吃蛇

    本文首发于我的博客 在csdn上看到一位大神用20行代码就写出了一个贪吃蛇的小游戏,链接请点这里,感觉被惊艳到了,...

  • 自制小游戏

    做了一些安卓小游戏,欢迎大家下载玩耍哦。我会持续更新更多自制的小游戏:-) 1. 经典多阶魔方 iOS版 经典多...

  • 初级练手项目——用Python一步一步实现“智能”贪吃蛇!

    贪吃蛇作为一款经典的小游戏,想必大家一定并不陌生,今天我们就来用Python来设计与实现贪吃蛇游戏,回味一下童年的...

  • 小游戏 贪吃蛇

    内容 贪吃蛇游戏包含:1.游戏引擎,2.蛇,3.食物。 基本操作 蛇会自动走,键盘可控制方向(上下左右),碰到边界...

网友评论

      本文标题:自制贪吃蛇小游戏代码

      本文链接:https://www.haomeiwen.com/subject/zyoiiktx.html