游戏数据类
package com.ikun.javaGame;
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[]sankex=new int[600];
int[]sankey=new int[500];
String fx;
boolean isStart=false;
Timer time=new Timer(100,this);
int foodx;
int foody;
Random r=new Random();
int score;
boolean isFail=false;
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
time.start();
}
//初始化成员变量
public void init() {
length=3;
sankex[0]=100;sankey[0]=100;
sankex[1]=75;sankey[1]=100;
sankex[2]=50;sankey[2]=100;
fx="R";
foodx=25+25*r.nextInt(34);
foody=25+25*r.nextInt(24);
score=0;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color. BLACK);
Date.header.paintIcon(this,g,25,11);
g.fillRect(25, 75, 850, 600);
Date.food.paintIcon(this, g, foodx, foody);
if(fx.equals("R")) {
Date.right.paintIcon(this, g, sankex[0], sankey[0]);
}
if(fx.equals("U")) {
Date.up.paintIcon(this, g, sankex[0], sankey[0]);
}
if(fx.equals("L")) {
Date.left.paintIcon(this, g, sankex[0], sankey[0]);
}
if(fx.equals("D")) {
Date.down.paintIcon(this, g, sankex[0], sankey[0]);
}
for(int i=1;i<length;i++) {
Date.body.paintIcon(this, g, sankex[i], sankey[i]);
}
Date.food.paintIcon(this, g, foodx, foody);
if(isStart==false) {
g.setColor(Color. WHITE);
g.setFont(new Font("微软雅黑",Font.ITALIC,40));
g.drawString("按下空格开始游戏", 300, 300);
}
if(isFail==true) {
g.setColor(Color. RED);
g.setFont(new Font("微软雅黑",Font.ITALIC,40));
g.drawString("游戏失败,按下空格开始游戏", 300, 200);
}
g.setColor(Color. pink);
g.setFont(new Font("微软雅黑",Font.ITALIC,18));
g.drawString("长度"+length, 750, 35);
g.drawString("分数"+score, 750, 55);
}
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE) {
if(isFail ) {
isFail=false;
init();
}else {
isStart=!isStart;
}
}
if(fx=="U"||fx=="D") {
if(keyCode==KeyEvent.VK_D||keyCode==KeyEvent.VK_RIGHT) {
fx="R";
}else if(keyCode==KeyEvent.VK_A||keyCode==KeyEvent.VK_LEFT) {
fx="L";
}
}
if(fx=="R"||fx=="L") {
if(keyCode==KeyEvent.VK_W||keyCode==KeyEvent.VK_UP) {
fx="U";
}else if(keyCode==KeyEvent.VK_S||keyCode==KeyEvent.VK_DOWN) {
fx="D";
}
}
repaint();
}
public void actionPerformed(ActionEvent e) {
if(isStart&&isFail==false) {
//System.out.println(3);
for(int i=length-1;i>0;i--) {
sankex[i]=sankex[i-1];
sankey[i]=sankey[i-1];
}
if(fx.equals("R")) {
sankex[0]=sankex[0]+25;
if(sankex[0]>850) {
isFail=true;
}
}else if(fx.equals("L")) {
sankex[0]=sankex[0]-25;
if(sankex[0]<25) {
isFail=true;
}
} else if(fx.equals("U")) {
sankey[0]=sankey[0]-25;
if(sankey[0]<75) {
isFail=true;
}
}else if(fx.equals("D")) {
sankey[0]=sankey[0]+25;
if(sankey[0]>650) {
isFail=true;
}
}
if(sankex[0]==foodx&&sankey[0]==foody) {
length++;
score++;
foodx=25+25*r.nextInt(34);
foody=25+25*r.nextInt(24);
}
for(int i=1;i<length;i++) {
if(sankex[0]==sankex[i]&&sankey[0]==sankey[i]) {
isFail=true;
}
}
repaint();
}
time.start();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
主类main方法区,启功游戏
package com.ikun.javaGame;
import javax.swing.*;
public class startGame {
public static void main(String [] args) {
JFrame frame=new JFrame("重新写贪吃蛇小游戏");//创建窗口并设置标题
frame.setBounds(10, 10, 900, 720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GamePanel ());
frame.setVisible(true);
}
}
导入的图片,可以自己制作
package com.ikun.javaGame;
import javax.swing.*;
import java.net.URL;
import javax.swing.ImageIcon;
// Image这个类,专门用来获取游戏中所涉及的图片
public class Date{
/*
将图片进行封装为程序中的一个对象,
这样便可以在程序中对对象进行操作
*/
//将图片进行封装
public static URL bodyURL=Date.class.getResource("/images/body.png");
public static ImageIcon body=new ImageIcon(bodyURL);
public static URL headerurl=Date.class.getResource("/images/header.png");
public static ImageIcon header=new ImageIcon(headerurl);
public static URL foodurl=Date.class.getResource("/images/food.png");
public static ImageIcon food=new ImageIcon(foodurl);
public static URL upurl=Date.class.getResource("/images/up.png");
public static ImageIcon up=new ImageIcon(upurl);
public static URL downurl=Date.class.getResource("/images/down.png");
public static ImageIcon down =new ImageIcon(downurl);
public static URL lefturl=Date.class.getResource("/images/left.png");
public static ImageIcon left=new ImageIcon(lefturl);
public static URL righturl=Date.class.getResource("/images/right.png");
public static ImageIcon right=new ImageIcon(righturl);
}
零基础学习一月后一天完成Java贪吃蛇小游戏
于 2022-07-22 20:53:31 首次发布