申明 非原创
今天蛮开心的,之前学Java都是毫无路线的那种。就想着做个小游戏感受一下。就去b站搜到了狂神说java的视频,就照着视频一步一步做下来了。
侵删
游戏画面
下面是代码目录,分了三个类,还有一个存数据的第三方。
首先是StartGames,实现静态的游戏开始界面
package snake;
import javax.swing.JFrame;
public class StartGames {
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 snake;
import java.net.URL;
import javax.swing.ImageIcon;
public class Date {
public static URL headreURL = Date.class.getResource("/statics/header1.png");
public static ImageIcon header = new ImageIcon(headreURL);
public static URL upURL = Date.class.getResource("/statics/up.png");
public static URL downURL = Date.class.getResource("/statics/down.png");
public static URL LeftURL = Date.class.getResource("/statics/left.png");
public static URL rightURL = Date.class.getResource("/statics/right.png");
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 = Date.class.getResource("/statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Date.class.getResource("/statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
public static URL backgroundURL = Date.class.getResource("/statics/background.jpg");
public static ImageIcon background = new ImageIcon(backgroundURL);
}
最后是实现功能的面板
package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int lenth;
int[] snakeX = new int[600];
int[] snakeY = new int[500];
String fx;
boolean isStart = false;
Timer timer = new Timer(100, this);
int foodx;
int foody;
Random random = new Random();
boolean isFail = false;
int score;
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init() {
lenth=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) {
// TODO Auto-generated method stub
super.paintComponent(g);
this.setBackground( Color.white);
Date.header.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);
Date.background.paintIcon(this, g, 0, 75);
if(fx.equals("R")){
Date.right.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("L")){
Date.Left.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("U")){
Date.up.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("D")){
Date.down.paintIcon(this, g, snakeX[0], snakeY[0]);
}
for(int i=1;i<lenth;i++){
Date.body.paintIcon(this, g, snakeX[i], snakeY[i]);
}
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 18));
g.drawString("长度:"+lenth,800,35);
g.drawString("分数:"+score,800,55);
Date.food.paintIcon(this, g, foodx, foody);
if(isStart==false){
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏", 300, 300);
}
if(isFail){
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("游戏失败,按下空格重新开始", 200, 300);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@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_LEFT){
fx="L";
}else if(KeyCode==KeyEvent.VK_RIGHT){
fx="R";
}else if(KeyCode==KeyEvent.VK_UP){
fx="U";
}else if(KeyCode==KeyEvent.VK_DOWN){
fx="D";
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(isStart&&isFail==false){
for(int i= lenth-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;}
}
if(snakeX[0]==foodx&&snakeY[0]==foody){
lenth++;
snakeX[lenth-1]=foodx-1;
snakeY[lenth-1]=foody-1;
score= score+10;
foodx = 25 + 25*random.nextInt(34);
foody = 75 + 25*random.nextInt(24);
}
for(int i=0;i<lenth;i++){
if(foodx==snakeX[i]&&foody==snakeY[i]){
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
}
}
for(int i=1;i<lenth;i++){
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isFail = true;
}
}
repaint();
}
timer.start();
}
}
因为刚学没多久,对于游戏是如何实现的我也没有很深的理解,只对狂神的代码做了点非常小的改动,强力推荐狂神的视频,B站搜狂神说java就可以。有不懂的可以留言,尽量回答。