视频地址:https://www.bilibili.com/video/BV1HE41127CV?p=3&spm_id_from=pageDriver
需求分析
- 界面设置
- 静态页面绘制
- 小蛇设置
- 吃食物
- 积分系统
素材文件:https://wwa.lanzoui.com/izIXjriydtg link
1.主界面绘制
package Tanchishe;
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(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
2.绘制游戏面板
package Tanchishe;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
}
}
在主界面加入游戏面板Jpanel package Tanchishe; import javax.swing.*; public class StartGame {public static void main(String[] args) { //1.绘制一个静态窗口 JFrame JFrame frame = new JFrame(“贪吃蛇小游戏”); frame.setBounds(10,10,900,720);//设置大小 frame.setResizable(false);//窗口不可变 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//2.添加游戏面板
frame.add(new GamePanel());
frame.setVisible(true);
}}
数据存放部分类
package Tanchishe;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headreurl= Data.class.getResource("/statics/header.png");
public static ImageIcon header= new ImageIcon(headreurl);
public static URL upUrl = Data.class.getResource("/statics/up.png");
public static URL downUrl = Data.class.getResource("/statics/down.png");
public static URL leftUrl = Data.class.getResource("/statics/left.png");
public static URL rightUrl = Data.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 = Data.class.getResource("/statics/body.png");
public static ImageIcon body= new ImageIcon(bodyUrl);
public static URL foodUrl = Data.class.getResource("/statics/food.png");
public static ImageIcon food= new ImageIcon(foodUrl);
}
3.画小蛇
package Tanchishe;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
int length;
int [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="U";
public GamePanel(){
init();
}
public void init()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
}
}
4.让小蛇动起来 键盘监听 定时器
package Tanchishe;
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;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
boolean isStart=false;
Timer timer =new Timer(100,this);
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (isStart==false)
{
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_SPACE){
isStart=!isStart;
repaint();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isStart) {
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] = snakeX[0] + 25;
if (snakeX[0] > 850) {
snakeX[0] = 25;
}
repaint();
}
timer.start();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
5 小蛇通过键盘控制走向
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;
}
}
package Tanchishe;
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;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
boolean isStart=false;
Timer timer =new Timer(100,this);
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (isStart==false)
{
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_SPACE){
isStart=!isStart;
repaint();
}
if(keycode==KeyEvent.VK_LEFT) {
fx="L";
}
if(keycode==KeyEvent.VK_RIGHT) {
fx="R";
}
if(keycode==KeyEvent.VK_UP) {
fx="U";
}
if(keycode==KeyEvent.VK_DOWN) {
fx="D";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isStart) {
for (int i = length - 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;
}
}
repaint();
}
timer.start();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
6 食物
package Tanchishe;
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 [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
boolean isStart=false;
Timer timer =new Timer(100,this);
int foodx;
int foody;
Random random = new Random();
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
foodx=getFoodx();
foody=getFoody();
}
private int getFoodx(){
int x=25+ 25*random.nextInt(34);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeX.length; i++)
{
if(snakeX[i]==x)
{
x=25+ 25*random.nextInt(34);
}else{
isFlag=false;
}
}
}
return x;
}
private int getFoody(){
int y=75+25*random.nextInt(24);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeY.length; i++)
{
if(snakeX[i]==y)
{
y=75+25*random.nextInt(24);
}else{
isFlag=false;
}
}
}
return y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
Data.food.paintIcon(this,g,foodx,foody);
if (isStart==false)
{
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_SPACE){
isStart=!isStart;
repaint();
}
if(keycode==KeyEvent.VK_LEFT) {
fx="L";
}
if(keycode==KeyEvent.VK_RIGHT) {
fx="R";
}
if(keycode==KeyEvent.VK_UP) {
fx="U";
}
if(keycode==KeyEvent.VK_DOWN) {
fx="D";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isStart) {
for (int i = length - 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) {
length++;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
repaint();
}
timer.start();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
7 积分系统
总代码:
package Tanchishe;
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 [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
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()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
foodx=getFoodx();
foody=getFoody();
score=0;
}
private int getFoodx(){
int x=25+ 25*random.nextInt(34);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeX.length; i++)
{
if(snakeX[i]==x)
{
x=25+ 25*random.nextInt(34);
}else{
isFlag=false;
}
}
}
return x;
}
private int getFoody(){
int y=75+25*random.nextInt(24);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeY.length; i++)
{
if(snakeX[i]==y)
{
y=75+25*random.nextInt(24);
}else{
isFlag=false;
}
}
}
return y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
Data.food.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 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";
}
if(keycode==KeyEvent.VK_RIGHT) {
fx="R";
}
if(keycode==KeyEvent.VK_UP) {
fx="U";
}
if(keycode==KeyEvent.VK_DOWN) {
fx="D";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart&&isFail==false){
if (isStart) {
for (int i = length - 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) {
length++;
score=score+10;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
for (int i = 1; i <length ; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i])
{
isFail=true;
}
}
repaint();
}
timer.start();
}}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
总代码
Data:数据类
package Tanchishe;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headreurl= Data.class.getResource("/statics/header.png");
public static ImageIcon header= new ImageIcon(headreurl);
public static URL upUrl = Data.class.getResource("/statics/up.png");
public static URL downUrl = Data.class.getResource("/statics/down.png");
public static URL leftUrl = Data.class.getResource("/statics/left.png");
public static URL rightUrl = Data.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 = Data.class.getResource("/statics/body.png");
public static ImageIcon body= new ImageIcon(bodyUrl);
public static URL foodUrl = Data.class.getResource("/statics/food.png");
public static ImageIcon food= new ImageIcon(foodUrl);
}
StartGame:开始游戏 主界面类
package Tanchishe;
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(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
GamePanel : 游戏界面 功能类
package Tanchishe;
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 [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
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()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
foodx=getFoodx();
foody=getFoody();
score=0;
}
private int getFoodx(){
int x=25+ 25*random.nextInt(34);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeX.length; i++)
{
if(snakeX[i]==x)
{
x=25+ 25*random.nextInt(34);
}else{
isFlag=false;
}
}
}
return x;
}
private int getFoody(){
int y=75+25*random.nextInt(24);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeY.length; i++)
{
if(snakeX[i]==y)
{
y=75+25*random.nextInt(24);
}else{
isFlag=false;
}
}
}
return y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
Data.food.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 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";
}
if(keycode==KeyEvent.VK_RIGHT) {
fx="R";
}
if(keycode==KeyEvent.VK_UP) {
fx="U";
}
if(keycode==KeyEvent.VK_DOWN) {
fx="D";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart&&isFail==false){
if (isStart) {
for (int i = length - 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) {
length++;
score=score+10;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
for (int i = 1; i <length ; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i])
{
isFail=true;
}
}
repaint();
}
timer.start();
}}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
改进----不能回头,多个食物,撞墙
package Tanchishe;
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 [] snakeX=new int[600];
int [] snakeY=new int[500];
String fx="R";
boolean isStart=false;
Timer timer =new Timer(100,this);
Random random = new Random();
int num=random.nextInt(10);
int foodx[]=new int[num];
int foody[]=new int[num];
boolean isFail=false;
int score;
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init()
{
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
for (int i = 0; i <num ; i++) {
foodx[i]=getFoodx();
foody[i]=getFoody();
}
score=0;
}
private int getFoodx(){
int x=25+ 25*random.nextInt(34);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeX.length; i++)
{
if(snakeX[i]==x)
{
x=25+ 25*random.nextInt(34);
}else{
isFlag=false;
}
}
}
return x;
}
private int getFoody(){
int y=75+25*random.nextInt(24);
boolean isFlag = true;
while (isFlag) {
for (int i = 0; i<snakeY.length; i++)
{
if(snakeX[i]==y)
{
y=75+25*random.nextInt(24);
}else{
isFlag=false;
}
}
}
return y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if(fx.equals("R"))
{
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("L"))
{
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("U"))
{
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
else if(fx.equals("D"))
{
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
for (int i = 0; i <num; i++) {
Data.food.paintIcon(this,g,foodx[i],foody[i]);
}
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度:"+length,750,35);
g.drawString("分数:"+score,750,55);
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 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) {
if(fx!="R")
{fx="L";}
}
if(keycode==KeyEvent.VK_RIGHT) {
if(fx!="L")
{fx="R";}
}
if(keycode==KeyEvent.VK_UP) {
if(fx!="D")
{fx="U";}
}
if(keycode==KeyEvent.VK_DOWN) {
if(fx!="U")
{fx="D";}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart&&isFail==false){
if (isStart) {
for (int i = length - 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) {
isFail=!isFail;
}
}
else if(fx.equals("L")){
snakeX[0] = snakeX[0] - 25;
if (snakeX[0] < 25) {
isFail=!isFail;
}
}
else if(fx.equals("U")){
snakeY[0] = snakeY[0] - 25;
if (snakeY[0] < 75) {
isFail=!isFail;
}
}
else if(fx.equals("D")){
snakeY[0] = snakeY[0] + 25;
if (snakeY[0] > 650) {
isFail=!isFail;
}
}
for (int i = 0; i <num; i++) {
if(snakeX[0]==foodx[i]&&snakeY[0]==foody[i]) {
length++;
score=score+10;
foodx[i] = 25 + 25 * random.nextInt(34);
foody[i] = 75 + 25 * random.nextInt(24);
}
}
for (int i = 1; i <length ; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i])
{
isFail=true;
}
}
repaint();
}
timer.start();
}}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
|