自制贪吃蛇小游戏代码
2020-11-24 本文已影响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) {
}
}
}