贪吃蛇源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//本程序用于初学者间的学习和交流,程序粗糙简陋在刷新重画时总出现白条画面,希望大家能提出宝贵意见。
QQ:609429837
import java.awt.Color;
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.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class myTcs extends JFrame implements ActionListener, KeyListener,
Runnable {
// private Graphics g;
// private JPanel jp;
File file = new File("分数.txt");
ArrayList<String> re = null;// 用于存储得分
String fenshu = null;// 用于显示得分
private JMenuBar menuBar;
private JMenu youXiMenu, nanDuMenu, fenShuMenu, guanYuMenu;
private JMenuItem kaiShiYouXi, exitItem, zuoZheItem, fenShuItem,
maxfenShuItem;
private JRadioButton cJianDan, cPuTong, cKunNan;
private ButtonGroup bg = new ButtonGroup();
private int m[] = new int[60];
private int n[] = new int[60];
private Thread she = null;
private int life = 3;// 生命数量
private int length = 6;// 蛇的长度
private static int foods = 0;// 本局吃了多少苹果private boolean isApple = false;// 蛇要吃的苹果private static int maxfenshu = 0;// 本局等分private int x = 230;
private int y = 200;
private int time = 500;// 用于设置难易程度private String direction = "right";// 蛇运动的方向
// 构造方法
public myTcs() {
// 实例化组件
// jp=new JPanel();
menuBar = new JMenuBar();
youXiMenu = new JMenu("游戏");
nanDuMenu = new JMenu("难度");
fenShuMenu = new JMenu("分数");
guanYuMenu = new JMenu("关于");
kaiShiYouXi = new JMenuItem("开始");
exitItem = new JMenuItem("退出");
cJianDan = new JRadioButton("简单");
cPuTong = new JRadioButton("普通");
cKunNan = new JRadioButton("困难");
zuoZheItem = new JMenuItem("作者");
fenShuItem = new JMenuItem("上局得分");
maxfenShuItem = new JMenuItem("最高得分");
// 添加组件
menuBar.add(youXiMenu);
menuBar.add(nanDuMenu);
menuBar.add(fenShuMenu);
menuBar.add(guanYuMenu);
youXiMenu.add(kaiShiYouXi);
youXiMenu.add(exitItem);
bg.add(cJianDan);
bg.add(cPuTong);
bg.add(cKunNan);
nanDuMenu.add(cJianDan);
nanDuMenu.add(cPuTong);
nanDuMenu.add(cKunNan);
fenShuMenu.add(fenShuItem);
fenShuMenu.add(maxfenShuItem);
guanYuMenu.add(zuoZheItem);
// 添加监听
kaiShiYouXi.addActionListener(this);
exitItem.addActionListener(this);
cJianDan.addActionListener(this);
cPuTong.addActionListener(this);
cKunNan.addActionListener(this);
fenShuItem.addActionListener(this);
maxfenShuItem.addActionListener(this);
zuoZheItem.addActionListener(this);
addKeyListener(this);
// 设置窗体
this.setJMenuBar(menuBar);
this.setVisible(true);
this.setSize(400, 400);
this.setLocation(100, 100);
this.setTitle("贪吃蛇");
this.setResizable(false);
this.validate();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String aa[]) {
new myTcs();
}
@Override
public void actionPerformed(ActionEvent e) {
if ("开始".equals(e.getActionCommand())) {
this.isApple = true;
life = 3;
if (she == null) {
// System.out.println(x + " " + y);
she = new Thread(this);
she.start();
} else if (she != null) {
she = null;
she = new Thread(this);
she.start();
}
}
if ("退出".equals(e.getActionCommand())) {
System.exit(0);
}
if ("简单".equals(e.getActionCommand())) {
time = 1000;
}
if ("普通".equals(e.getActionCommand())) {
time = 500;
}
if ("困难".equals(e.getActionCommand())) {
time = 200;
}
if ("上局得分".equals(e.getActionCommand())) {
this.defen();
JOptionPane.showMessageDialog(this, fenshu);
}
if ("最高得分".equals(e.getActionCommand())) {
this.defen();
JOptionPane.showMessageDialog(this, maxfenshu);
}
if ("作者".equals(e.getActionCommand())) {
JOptionPane.showMessageDialog(this, "制作:飞" + "\r\n"
+ "QQ: 609429837" + "\n");
}
this.repaint();// 重画
}
// 绘图
public void paint(Graphics g) {
super.paint(g);
// g.setColor(Color.DARK_GRAY); // 设置背景
g.fillRect(0, 50, 400, 400);
g.setColor(Color.BLUE);
for (int i = 0; i <= length - 1; i++) {
g.fillRect(m[i], n[i], 10, 10);
}
if (isApple == true) {
g.setColor(Color.RED);
g.fillRect(x, y, 10, 10);
}
}
@Override
public void run() {
for (int i = 0; i <= length - 1; i++) {
m[i] = 50 - i * 10;
n[i] = 60;
}
while (she != null) {
check();
try {
Thread.sleep(time);
} catch (Exception ee) {
System.out.println(direction);
}
}
}
// 判断运动方向并设置活动范围
public synchronized void check() {
if (she != null) {
for (int i = length - 1; i >= 1; i--) {
m[i] = m[i - 1];
n[i] = n[i - 1];
}
if (direction.equals("up")) {
n[0] = n[0] - 10;
if (n[0] == (y - 10) && m[0] >= x & m[0] <= (x + 10)) {
this.foods++;
this.length++;
this.apple();
if (foods == 50 || life == 0) {
this.isApple = false;
}
}
}
if (direction.equals("down")) {
n[0] = n[0] + 10;
if (n[0] == (y + 10) && m[0] >= x & m[0] <= (x + 10)) { this.foods++;
this.length++;
this.apple();
if (foods == 50 || life == 0) {
this.isApple = false;
}
}
}
if (direction.equals("left")) {
m[0] = m[0] - 10;
if (m[0] == (x + 10) && n[0] >= y && n[0] <= (y + 10)) { this.foods++;
this.length++;
this.apple();
if (foods == 50 || life == 0) {
this.isApple = false;
}
}
}
if (direction.equals("right")) {
m[0] = m[0] + 10;
if (m[0] == (x) && n[0] >= y && n[0] <= (y + 10)) {
this.foods++;
this.length++;
this.apple();
if (foods == 50 || life == 0) {
this.isApple = false;
}
}
}
if (m[0] <= -5 || m[0] >= 395 || n[0] <= 45 || n[0] >= 395) { --life;
she = null;
if (life <= 0) {
this.save();
} else {
if (she == null) {
// System.out.println(x + " " + y);
she = new Thread(this);
this.length = 6;
this.direction = "right";
she.start();
} else if (she != null) {
she = null;
she = new Thread(this);
this.length = 6;
this.direction = "right";
she.start();
}
}
}
}
this.repaint();// 重画
}
// 产生苹果并实现蛇吃苹果
public void apple() {
x = (int) Math.floor(Math.random() * 39) * 10;
y = (int) Math.floor(Math.random() * 29) * 10 + 50; }
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (!direction.equals("down")) {
direction = "up";
check();
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (!direction.equals("up")) {
direction = "down";
check();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (!direction.equals("right")) {
direction = "left";
check();
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (!direction.equals("left")) {
direction = "right";
check();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void save() {
boolean append = false;
if (file.exists()) {
append = true;
}
FileWriter fc = null;
BufferedWriter bf = null;
try {
fc = new FileWriter("分数.txt", append);
bf = new BufferedWriter(fc);
bf.append(Integer.toString(foods * 10) + "\r\n");
} catch (IOException e) {
e.toString();
}
try {
bf.flush();
bf.close();
fc.close();
} catch (IOException e) {
e.toString();
}
}
public void defen() {
re = new ArrayList<String>();
FileReader fr = null;
BufferedReader bfr = null;
if (file.exists()) {
try {
fr = new FileReader(file);
bfr = new BufferedReader(fr);
fenshu = bfr.readLine();
re.add(fenshu);
while (fenshu != null) {
fenshu = bfr.readLine();
re.add(fenshu);
}
if (re.size() >= 2) {
fenshu = re.get(re.size() - 2);
} else {
fenshu = "无记录";
}
} catch (IOException e) {
e.toString();
}
maxfenshu = Integer.valueOf(re.get(0));
for (int i = 0; i < re.size() - 1; i++) {
if (maxfenshu < Integer.valueOf(re.get(i))) {
maxfenshu = Integer.valueOf(re.get(i));
System.out.println(i);
}
}
} else {
fenshu = "无记录";
}
}
}。