public class She extends JFrame implements ActionListener, WindowListener {
JLabel bt1;
JLabel bt2;
MyPaint mp;
JMenuItem kaishi;
JMenuItem zanting;
JMenuItem tuichu;
JRadioButton chu;
JRadioButton zhong;
JRadioButton gao;
JRadioButton chao;
JMenuItem guanyu;
Font f;
public She() {
f = new Font("宋体", Font.PLAIN, 12);
mp = new MyPaint();
mp.setShe(this);
JMenuBar caidan = new JMenuBar();
JMenu shezhi = new JMenu("设置");
shezhi.setFont(f);
JMenu nandu = new JMenu("难度");
nandu.setFont(f);
JMenu bangzhu = new JMenu("帮助");
bangzhu.setFont(f);
caidan.add(shezhi);
caidan.add(nandu);
caidan.add(bangzhu);
kaishi = new JMenuItem("开始游戏 F2");
kaishi.setFont(f);
kaishi.addActionListener(this);
zanting = new JMenuItem("暂停/继续 F3");
zanting.setFont(f);
zanting.addActionListener(this);
tuichu = new JMenuItem("退出 F4");
tuichu.setFont(f);
tuichu.addActionListener(this);
chu = new JRadioButton("初级,最快速度195", true);
chu.setFont(f);
chu.addActionListener(this);
zhong = new JRadioButton("中级,最快速度225");
zhong.setFont(f);
zhong.addActionListener(this);
gao = new JRadioButton("高级,最快速度255");
gao.setFont(f);
gao.addActionListener(this);
chao = new JRadioButton("超级,最快速度275");
chao.setFont(f);
chao.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(chu);
bg.add(zhong);
bg.add(gao);
bg.add(chao);
nandu.add(chu);
nandu.add(zhong);
nandu.add(gao);
nandu.add(chao);
guanyu = new JMenuItem("关于");
guanyu.setFont(f);
guanyu.addActionListener(this);
shezhi.add(kaishi);
shezhi.add(zanting);
shezhi.add(tuichu);
bangzhu.add(guanyu);
bt1 = new JLabel(" 长度:0");
bt1.setFont(f);
bt2 = new JLabel("移动:5");
bt2.setFont(f);
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, 2));
p.add(bt1);
p.add(bt2);
this.addWindowListener(this);//添加窗体监听器
this.setJMenuBar(caidan);
this.add(p, BorderLayout.NORTH);
this.add(mp);
this.add(new JLabel(" "), BorderLayout.WEST);
this.setSize(335, 385);
this.setResizable(false);
this.setLocation(300, 150);
this.setTitle("MyPaint");
this.setVisible(true);
}
public void setDS(int a, int b) {
bt1.setText(" 长度:" + a);
bt2.setText("移动:" + b);
}
public void setChu() {
chu.setSelected(true);
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
chu.setEnabled(true);
zhong.setEnabled(true);
gao.setEnabled(true);
chao.setEnabled(true);
}
public void setNanDu() {
chu.setEnabled(false);
zhong.setEnabled(false);
gao.setEnabled(false);
chao.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == zanting) {
mp.setZanTing();
}
else if (e.getSource() == kaishi) {
mp.setKaiShi();
}
else if (e.getSource() == tuichu) {
mp.setTuiChu();
}
else if (e.getSource() == guanyu) {
JLabel gy = new JLabel("小游戏 贪吃蛇 V1.0");
gy.setFont(f);
JOptionPane.showMessageDialog(this, gy);
}
else if (e.getSource() == chu) {
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
}
else if (e.getSource() == zhong) {
mp.setSD(250, 80);
this.setDS(0, 305 - 250);
}
else if (e.getSource() == gao) {
mp.setSD(200, 50);
this.setDS(0, 305 - 200);
}
else if (e.getSource() == chao) {
mp.setSD(150, 30);
this.setDS(0, 305 - 150);
}
}
public static void main(String[] args) {
new She(); }
public void setShe(She ss) {
s = ss;
}
//暂停/开始
public void setZanTing() {
if (zanting) {
zanting = false;
}
else {
zanting = true;
}
}
public void setKaiShi() {
JLabel ks = new JLabel("是否重新开始?");
ks.setFont(f);
int a = JOptionPane.showConfirmDialog(s, ks);
if (a == 0) {
x = 150;//蛇开始坐标X
y = 0;//蛇开始坐标Y
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
fx = 2;//方向键1上2下3左4右
sw = 0;//食物数量
sd = 300;//停滞时间
cs = 0;//转向次数
zanting = false;//暂停/开始
siwang = false;//撞到身体或墙了
shenti = false;//撞到身体
s.setChu();
s.setDS(sw, 305 - sd);
}
}
public void setTuiChu() {
JLabel tc = new JLabel("确定退出吗?");
tc.setFont(f);
int a = JOptionPane.showConfirmDialog(s, tc);
if (a == 0) {
s.dispose();
System.exit(0);
}
}
public void setSD(int a, int b) {
sd = a;
d = b;
}
public void paint(Graphics g) {
//记录每次转向的坐标
zx[cs] = x + "/" + y;
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, 300, 300);
//画食物
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
//画蛇头
g.setColor(Color.BLUE);
g.fillRect(x, y, 10, 10);
//画蛇身体
g.setColor(Color.RED);
for (int i = 1; i <= sw; i++) {
int j = cs - i;
if (j < 0) {
j = cs-i+999+1;
}
String[] s = zx[j].split("/");
if (x == Integer.parseInt(s[0]) && y == Integer.parseInt(s[1])) {//蛇撞到自己身上了
siwang = true;
shenti = true;
}
if (sjx == Integer.parseInt(s[0]) && sjy == Integer.parseInt(s[1])) {//食物在蛇身体上
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
g.setColor(Color.RED);
continue;
}
g.fillRect(Integer.parseInt(s[0]), Integer.parseInt(s[1]), 10, 10);
}
}
public void run() {
while (true) {
cs++;//移动一次加一次
//如果数组下标等于1000,就从头再来
if (cs == 1000) {
cs = 0;
}
switch (fx) {
case 1:
y-=10;//上
break;
case 2:
y+=10;//下
break;
case 3:
x-=10;//左
break;
case 4:
x+=10;//右
break;
}
this.repaint();
//蛇头碰到食物,再随机出个食物
if (x == sjx && y == sjy) {
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
sw++;//食物数量加1
if (sw == 1) {
s.setNanDu();
}
//速度设置
if (sd <= d) {
sd = d;
}
else {
sd-=5;
}
s.setDS(sw, 305 - sd);
}
//撞到墙了 或 撞到身体了
if (x < 0 || x > 290 || y < 0 || y > 290 || shenti) {
JOptionPane.showMessageDialog(s, "游戏结束!");
siwang = true;
shenti = false;
}
//暂停
while (zanting || zanting1) {
try {
Thread.sleep(100);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
while (siwang) {
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
try {
Thread.sleep(sd);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
}
}
}
JLabel bt1;
JLabel bt2;
MyPaint mp;
JMenuItem kaishi;
JMenuItem zanting;
JMenuItem tuichu;
JRadioButton chu;
JRadioButton zhong;
JRadioButton gao;
JRadioButton chao;
JMenuItem guanyu;
Font f;
public She() {
f = new Font("宋体", Font.PLAIN, 12);
mp = new MyPaint();
mp.setShe(this);
JMenuBar caidan = new JMenuBar();
JMenu shezhi = new JMenu("设置");
shezhi.setFont(f);
JMenu nandu = new JMenu("难度");
nandu.setFont(f);
JMenu bangzhu = new JMenu("帮助");
bangzhu.setFont(f);
caidan.add(shezhi);
caidan.add(nandu);
caidan.add(bangzhu);
kaishi = new JMenuItem("开始游戏 F2");
kaishi.setFont(f);
kaishi.addActionListener(this);
zanting = new JMenuItem("暂停/继续 F3");
zanting.setFont(f);
zanting.addActionListener(this);
tuichu = new JMenuItem("退出 F4");
tuichu.setFont(f);
tuichu.addActionListener(this);
chu = new JRadioButton("初级,最快速度195", true);
chu.setFont(f);
chu.addActionListener(this);
zhong = new JRadioButton("中级,最快速度225");
zhong.setFont(f);
zhong.addActionListener(this);
gao = new JRadioButton("高级,最快速度255");
gao.setFont(f);
gao.addActionListener(this);
chao = new JRadioButton("超级,最快速度275");
chao.setFont(f);
chao.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(chu);
bg.add(zhong);
bg.add(gao);
bg.add(chao);
nandu.add(chu);
nandu.add(zhong);
nandu.add(gao);
nandu.add(chao);
guanyu = new JMenuItem("关于");
guanyu.setFont(f);
guanyu.addActionListener(this);
shezhi.add(kaishi);
shezhi.add(zanting);
shezhi.add(tuichu);
bangzhu.add(guanyu);
bt1 = new JLabel(" 长度:0");
bt1.setFont(f);
bt2 = new JLabel("移动:5");
bt2.setFont(f);
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, 2));
p.add(bt1);
p.add(bt2);
this.addWindowListener(this);//添加窗体监听器
this.setJMenuBar(caidan);
this.add(p, BorderLayout.NORTH);
this.add(mp);
this.add(new JLabel(" "), BorderLayout.WEST);
this.setSize(335, 385);
this.setResizable(false);
this.setLocation(300, 150);
this.setTitle("MyPaint");
this.setVisible(true);
}
public void setDS(int a, int b) {
bt1.setText(" 长度:" + a);
bt2.setText("移动:" + b);
}
public void setChu() {
chu.setSelected(true);
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
chu.setEnabled(true);
zhong.setEnabled(true);
gao.setEnabled(true);
chao.setEnabled(true);
}
public void setNanDu() {
chu.setEnabled(false);
zhong.setEnabled(false);
gao.setEnabled(false);
chao.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == zanting) {
mp.setZanTing();
}
else if (e.getSource() == kaishi) {
mp.setKaiShi();
}
else if (e.getSource() == tuichu) {
mp.setTuiChu();
}
else if (e.getSource() == guanyu) {
JLabel gy = new JLabel("小游戏 贪吃蛇 V1.0");
gy.setFont(f);
JOptionPane.showMessageDialog(this, gy);
}
else if (e.getSource() == chu) {
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
}
else if (e.getSource() == zhong) {
mp.setSD(250, 80);
this.setDS(0, 305 - 250);
}
else if (e.getSource() == gao) {
mp.setSD(200, 50);
this.setDS(0, 305 - 200);
}
else if (e.getSource() == chao) {
mp.setSD(150, 30);
this.setDS(0, 305 - 150);
}
}
public static void main(String[] args) {
new She(); }
public void setShe(She ss) {
s = ss;
}
//暂停/开始
public void setZanTing() {
if (zanting) {
zanting = false;
}
else {
zanting = true;
}
}
public void setKaiShi() {
JLabel ks = new JLabel("是否重新开始?");
ks.setFont(f);
int a = JOptionPane.showConfirmDialog(s, ks);
if (a == 0) {
x = 150;//蛇开始坐标X
y = 0;//蛇开始坐标Y
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
fx = 2;//方向键1上2下3左4右
sw = 0;//食物数量
sd = 300;//停滞时间
cs = 0;//转向次数
zanting = false;//暂停/开始
siwang = false;//撞到身体或墙了
shenti = false;//撞到身体
s.setChu();
s.setDS(sw, 305 - sd);
}
}
public void setTuiChu() {
JLabel tc = new JLabel("确定退出吗?");
tc.setFont(f);
int a = JOptionPane.showConfirmDialog(s, tc);
if (a == 0) {
s.dispose();
System.exit(0);
}
}
public void setSD(int a, int b) {
sd = a;
d = b;
}
public void paint(Graphics g) {
//记录每次转向的坐标
zx[cs] = x + "/" + y;
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, 300, 300);
//画食物
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
//画蛇头
g.setColor(Color.BLUE);
g.fillRect(x, y, 10, 10);
//画蛇身体
g.setColor(Color.RED);
for (int i = 1; i <= sw; i++) {
int j = cs - i;
if (j < 0) {
j = cs-i+999+1;
}
String[] s = zx[j].split("/");
if (x == Integer.parseInt(s[0]) && y == Integer.parseInt(s[1])) {//蛇撞到自己身上了
siwang = true;
shenti = true;
}
if (sjx == Integer.parseInt(s[0]) && sjy == Integer.parseInt(s[1])) {//食物在蛇身体上
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
g.setColor(Color.RED);
continue;
}
g.fillRect(Integer.parseInt(s[0]), Integer.parseInt(s[1]), 10, 10);
}
}
public void run() {
while (true) {
cs++;//移动一次加一次
//如果数组下标等于1000,就从头再来
if (cs == 1000) {
cs = 0;
}
switch (fx) {
case 1:
y-=10;//上
break;
case 2:
y+=10;//下
break;
case 3:
x-=10;//左
break;
case 4:
x+=10;//右
break;
}
this.repaint();
//蛇头碰到食物,再随机出个食物
if (x == sjx && y == sjy) {
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
sw++;//食物数量加1
if (sw == 1) {
s.setNanDu();
}
//速度设置
if (sd <= d) {
sd = d;
}
else {
sd-=5;
}
s.setDS(sw, 305 - sd);
}
//撞到墙了 或 撞到身体了
if (x < 0 || x > 290 || y < 0 || y > 290 || shenti) {
JOptionPane.showMessageDialog(s, "游戏结束!");
siwang = true;
shenti = false;
}
//暂停
while (zanting || zanting1) {
try {
Thread.sleep(100);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
while (siwang) {
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
try {
Thread.sleep(sd);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
}
}
}
有一个MyPaint类,接收键盘操作应该在那个类里。
public void run() {
while (true) {
cs++;// 移动一次加一次
// 如果数组下标等于1000,就从头再来
if (cs == 1000) {
cs = 0;
}
switch (fx) {
case 1:
y -= 10;// 上
break;
case 2:
y += 10;// 下
break;
case 3:
x -= 10;// 左
break;
case 4:
x += 10;// 右
break;
}
this.repaint();
// 蛇头碰到食物,再随机出个食物
if (x == sjx && y == sjy) {
sjx = r.nextInt(300) / 10 * 10;// 食物随机X坐标
sjy = r.nextInt(300) / 10 * 10;// 食物随机Y坐标
sw++;// 食物数量加1
if (sw == 1) {
s.setNanDu();
}
// 速度设置
if (sd <= d) {
sd = d;
} else {
sd -= 5;
}
s.setDS(sw, 305 - sd);
}
// 撞到墙了 或 撞到身体了
if (x < 0 || x > 290 || y < 0 || y > 290 || shenti) {
JOptionPane.showMessageDialog(s, "游戏结束!");
siwang = true;
shenti = false;
}
//暂停
while (zanting || zanting1) {
try {
Thread.sleep(100);//时间停滞(毫秒)
} catch (Exception e) {
}
}
while (siwang) {
try {
Thread.sleep(100);
} catch (Exception e) { }
}
try {
Thread.sleep(sd);//时间停滞(毫秒)
} catch (Exception e) {
}
}
}
--------fx这个变量是控制方向的,键盘的四个箭头键按了以后,会改变这个变量的值,但在你的代码中,这个变量只有使用,没有声明,所以不完整。
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;public class She extends JFrame implements ActionListener, WindowListener {
JLabel bt1;
JLabel bt2;
MyPaint mp;
JMenuItem kaishi;
JMenuItem zanting;
JMenuItem tuichu;
JRadioButton chu;
JRadioButton zhong;
JRadioButton gao;
JRadioButton chao;
JMenuItem guanyu;
Font f;
public She() {
f = new Font("宋体", Font.PLAIN, 12);
mp = new MyPaint();
mp.setShe(this);
JMenuBar caidan = new JMenuBar();
JMenu shezhi = new JMenu("设置");
shezhi.setFont(f);
JMenu nandu = new JMenu("难度");
nandu.setFont(f);
JMenu bangzhu = new JMenu("帮助");
bangzhu.setFont(f);
caidan.add(shezhi);
caidan.add(nandu);
caidan.add(bangzhu);
kaishi = new JMenuItem("开始游戏 F2");
kaishi.setFont(f);
kaishi.addActionListener(this);
zanting = new JMenuItem("暂停/继续 F3");
zanting.setFont(f);
zanting.addActionListener(this);
tuichu = new JMenuItem("退出 F4");
tuichu.setFont(f);
tuichu.addActionListener(this);
chu = new JRadioButton("初级,最快速度195", true);
chu.setFont(f);
chu.addActionListener(this);
zhong = new JRadioButton("中级,最快速度225");
zhong.setFont(f);
zhong.addActionListener(this);
gao = new JRadioButton("高级,最快速度255");
gao.setFont(f);
gao.addActionListener(this);
chao = new JRadioButton("超级,最快速度275");
chao.setFont(f);
chao.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(chu);
bg.add(zhong);
bg.add(gao);
bg.add(chao);
nandu.add(chu);
nandu.add(zhong);
nandu.add(gao);
nandu.add(chao);
guanyu = new JMenuItem("关于");
guanyu.setFont(f);
guanyu.addActionListener(this);
shezhi.add(kaishi);
shezhi.add(zanting);
shezhi.add(tuichu);
bangzhu.add(guanyu);
bt1 = new JLabel(" 长度:0");
bt1.setFont(f);
bt2 = new JLabel("移动:5");
bt2.setFont(f);
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, 2));
p.add(bt1);
p.add(bt2);
this.addWindowListener(this);//添加窗体监听器
this.setJMenuBar(caidan);
this.add(p, BorderLayout.NORTH);
this.add(mp);
this.add(new JLabel(" "), BorderLayout.WEST);
this.setSize(335, 385);
this.setResizable(false);
this.setLocation(300, 150);
this.setTitle("MyPaint");
this.setVisible(true);
}
public void setDS(int a, int b) {
bt1.setText(" 长度:" + a);
bt2.setText("移动:" + b);
}
public void setChu() {
chu.setSelected(true);
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
chu.setEnabled(true);
zhong.setEnabled(true);
gao.setEnabled(true);
chao.setEnabled(true);
}
public void setNanDu() {
chu.setEnabled(false);
zhong.setEnabled(false);
gao.setEnabled(false);
chao.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == zanting) {
mp.setZanTing();
}
else if (e.getSource() == kaishi) {
mp.setKaiShi();
}
else if (e.getSource() == tuichu) {
mp.setTuiChu();
}
else if (e.getSource() == guanyu) {
JLabel gy = new JLabel("小游戏 贪吃蛇 V1.0");
gy.setFont(f);
JOptionPane.showMessageDialog(this, gy);
}
else if (e.getSource() == chu) {
mp.setSD(300, 110);
this.setDS(0, 305 - 300);
}
else if (e.getSource() == zhong) {
mp.setSD(250, 80);
this.setDS(0, 305 - 250);
}
else if (e.getSource() == gao) {
mp.setSD(200, 50);
this.setDS(0, 305 - 200);
}
else if (e.getSource() == chao) {
mp.setSD(150, 30);
this.setDS(0, 305 - 150);
}
}
public static void main(String[] args) {
new She();
}
// TODO: Add your code here
} public void windowClosing(WindowEvent e) {
System.exit(0);//关闭窗体的时候关闭后台程序
}
public void windowClosed(WindowEvent e) {
// TODO: Add your code here
}
public void windowIconified(WindowEvent e) {
// TODO: Add your code here
}
public void windowDeiconified(WindowEvent e) {
// TODO: Add your code here
}
public void windowActivated(WindowEvent e) {
// TODO: Add your code here
}
public void windowDeactivated(WindowEvent e) {
// TODO: Add your code here
}
}
class MyPaint extends JPanel implements Runnable, KeyListener, FocusListener {
She s;
Font f = new Font("宋体", Font.PLAIN, 12);
int x = 150;//蛇开始坐标X
int y = 0;//蛇开始坐标Y
Random r = new Random();
int sjx = r.nextInt(300)/10*10;//食物随机X坐标
int sjy = r.nextInt(300)/10*10;//食物随机Y坐标
int fx = 2;//方向键1上2下3左4右
int sw = 0;//食物数量
int sd = 300;//停滞时间
int cs = 0;//转向次数
int d = 150;//最快多少;
String[] zx = new String[1000];//每次转向的坐标
boolean zanting = false;//暂停/开始
boolean zanting1 = false;//焦点暂停/开始
boolean siwang = false;//撞到身体或墙了
boolean shenti = false;//撞到身体
public MyPaint() {
Thread t = new Thread(this);//创建线程类对象
t.start();//启动线程
this.addKeyListener(this);//添加键盘监听
this.setFocusable(true);//获得焦点
this.addFocusListener(this);//添加焦点监听
}
public void setShe(She ss) {
s = ss;
}
//暂停/开始
public void setZanTing() {
if (zanting) {
zanting = false;
}
else {
zanting = true;
}
}
public void setKaiShi() {
JLabel ks = new JLabel("是否重新开始?");
ks.setFont(f);
int a = JOptionPane.showConfirmDialog(s, ks);
if (a == 0) {
x = 150;//蛇开始坐标X
y = 0;//蛇开始坐标Y
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
fx = 2;//方向键1上2下3左4右
sw = 0;//食物数量
sd = 300;//停滞时间
cs = 0;//转向次数
zanting = false;//暂停/开始
siwang = false;//撞到身体或墙了
shenti = false;//撞到身体
s.setChu();
s.setDS(sw, 305 - sd);
}
}
public void setTuiChu() {
JLabel tc = new JLabel("确定退出吗?");
tc.setFont(f);
int a = JOptionPane.showConfirmDialog(s, tc);
if (a == 0) {
s.dispose();
System.exit(0);
}
}
public void setSD(int a, int b) {
sd = a;
d = b;
}
public void paint(Graphics g) {
//记录每次转向的坐标
zx[cs] = x + "/" + y;
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, 300, 300);
//画食物
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
//画蛇头
g.setColor(Color.BLUE);
g.fillRect(x, y, 10, 10);
//画蛇身体
g.setColor(Color.RED);
for (int i = 1; i <= sw; i++) {
int j = cs - i;
if (j < 0) {
j = cs-i+999+1;
}
String[] s = zx[j].split("/");
if (x == Integer.parseInt(s[0]) && y == Integer.parseInt(s[1])) {//蛇撞到自己身上了
siwang = true;
shenti = true;
}
if (sjx == Integer.parseInt(s[0]) && sjy == Integer.parseInt(s[1])) {//食物在蛇身体上
g.setColor(Color.BLACK);
g.fillRect(sjx, sjy, 10, 10);
g.setColor(Color.RED);
continue;
}
g.fillRect(Integer.parseInt(s[0]), Integer.parseInt(s[1]), 10, 10);
}
}
public void run() {
while (true) {
cs++;//移动一次加一次
//如果数组下标等于1000,就从头再来
if (cs == 1000) {
cs = 0;
}
switch (fx) {
case 1:
y-=10;//上
break;
case 2:
y+=10;//下
break;
case 3:
x-=10;//左
break;
case 4:
x+=10;//右
break;
}
this.repaint();
//蛇头碰到食物,再随机出个食物
if (x == sjx && y == sjy) {
sjx = r.nextInt(300)/10*10;//食物随机X坐标
sjy = r.nextInt(300)/10*10;//食物随机Y坐标
sw++;//食物数量加1
if (sw == 1) {
s.setNanDu();
}
//速度设置
if (sd <= d) {
sd = d;
}
else {
sd-=5;
}
s.setDS(sw, 305 - sd);
}
//撞到墙了 或 撞到身体了
if (x < 0 || x > 290 || y < 0 || y > 290 || shenti) {
JOptionPane.showMessageDialog(s, "游戏结束!");
siwang = true;
shenti = false;
}
//暂停
while (zanting || zanting1) {
try {
Thread.sleep(100);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
while (siwang) {
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
try {
Thread.sleep(sd);//时间停滞(毫秒)
}
catch (Exception e) {
}
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case 38:
fx = 1;//上
break;
case 40:
fx = 2;//下
break;
case 37:
fx = 3;//左
break;
case 39:
fx = 4;//右
break;
case KeyEvent.VK_F3:
this.setZanTing();
break;
case KeyEvent.VK_F2:
this.setKaiShi();
break;
case KeyEvent.VK_F4:
this.setTuiChu();
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
} public void focusGained(FocusEvent e) {
zanting1 = false;
}
public void focusLost(FocusEvent e) {
zanting1 = true;
}
}
sjx = r.nextInt(300) / 10 * 10;// 食物随机X坐标
sjy = r.nextInt(300) / 10 * 10;// 食物随机Y坐标
sw++;// 食物数量加1 if (sw == 1) {
s.setNanDu();
}
// 速度设置
if (sd <= d) {
sd = d;
} else {
sd -= 5;
}
s.setDS(sw, 305 - sd);
}
蛇的长度就等于已经吃掉的食物的个数,所以,蛇的长度是用sw这个变量控制的,不信的话,你把sw++改成sw+=2,蛇吃掉一个食物,就会长两节。
if (x == sjx && y == sjy) {
sjx = r.nextInt(300) / 10 * 10;// 食物随机X坐标
sjy = r.nextInt(300) / 10 * 10;// 食物随机Y坐标
sw++;// 食物数量加1 if (sw == 1) {
s.setNanDu();
}
// 速度设置
if (sd <= d) {
sd = d;
} else {
sd -= 5;
}
s.setDS(sw, 305 - sd);
}