第12天 用java做贪吃蛇
package com.Mxs.www;
import javax.swing.*;
public class Mgeme {
public static void main(String[] args) {
// 绘制jframe
JFrame frame = new JFrame("马晓帅贪吃蛇");
frame.setBounds(10,10,900,720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Meme1());
frame.setVisible(true);
}
}
绘制静态面板
package com.Mxs.www;
import javax.swing.*;
import java.net.URL;
public class Data {
// 广告头部
public static URL headerUrl=Data.class.getResource("/static/header.png");
public static ImageIcon header = new ImageIcon(headerUrl);
// 蛇头
public static URL upUrl=Data.class.getResource("/static/up.png");
public static URL downUrl=Data.class.getResource("/static/down.png");
public static URL leftUrl=Data.class.getResource("/static/left.png");
public static URL rightUrl=Data.class.getResource("/static/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=Data.class.getResource("/static/body.png");
public static URL footUrl=Data.class.getResource("/static/food.png");
public static ImageIcon body = new ImageIcon(bodyUrl);
public static ImageIcon foot = new ImageIcon(footUrl);
}
引入资源图
package com.Mxs.www;
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 Meme1 extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakesX = 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 Meme1(){
init();
// 获取键盘监听
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init(){
length = 3;
snakesX[0] = 100;
snakeY[0]=100;
snakesX[1] = 75;
snakeY[1]=100;
snakesX[2] = 50;
snakeY[2]=100;
fx = "R";
foodx = 25+25*random.nextInt(34);
foody = 75+25*random.nextInt(24);
score=0;
}
// 画板 化界面 化蛇
protected void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.white);
// 绘制广告栏
Data.header.paintIcon(this,g,25,11);
g.fillRect(15,75,850,600);
// 画静态小蛇
Data.right.paintIcon(this,g, snakesX[0], snakeY[0]);
// Data.body.paintIcon(this,g, snakesX[1], snakeY[1]);
// Data.body.paintIcon(this,g, snakesX[2], snakeY[2]);
if (fx.equals("R")){
Data.right.paintIcon(this,g, snakesX[0], snakeY[0]);
}else if (fx.equals("l")){
Data.left.paintIcon(this,g, snakesX[0], snakeY[0]);
}else if (fx.equals("U")){
Data.up.paintIcon(this,g, snakesX[0], snakeY[0]);
}else if (fx.equals("D")){
Data.down.paintIcon(this,g, snakesX[0], snakeY[0]);
}
for (int i=1;i<length;i++){
Data.body.paintIcon(this,g, snakesX[i], snakeY[i]);
}
// 花食物
Data.foot.paintIcon(this,g,foodx,foody);
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度"+length,750,35);
g.drawString("分数"+score,750,50);
//提示
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) {
//键盘桥下
}
@Override
public void keyReleased(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_LEFT){
fx="l";
}else if (KeyCode == KeyEvent.VK_RIGHT){
fx="R";
}else if (KeyCode == KeyEvent.VK_DOWN){
fx="D";
}else if (KeyCode == KeyEvent.VK_UP){
fx="U";
}
}
@Override
// 执行定时操作
public void actionPerformed(ActionEvent e) {
// 如果游戏开始
if (isStart && isFail == false){
for (int i=length-1;i>0;i--){
snakesX[i]=snakesX[i-1];
snakeY[i]=snakeY[i-1];
}
if (fx.equals("R")){
snakesX[0] = snakesX[0]+25;
if (snakesX[0] > 850){ snakesX[0]=25;}
} else if (fx.equals("l")){
snakesX[0]=snakesX[0]-25;
if (snakesX[0] < 25){ snakesX[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 (snakesX[0] == foodx && snakeY[0]==foody){
length++;
score = score+10;
foodx = 25+25*random.nextInt(34);
foody = 75+25*random.nextInt(24);
}
for (int i = 1; i < length; i++) {
if (snakesX[0] == snakesX[i] && snakeY[0] == snakeY[i]){
isFail=true;
}
}
repaint();
}
timer.start();
}
// 定时器
}
逻辑实现
用到键盘事件 计时器等 完成一共用了两天事件 总共两小时 java初学者 效果图 v