当前位置:文档之家› Java贪吃蛇游戏源代码

Java贪吃蛇游戏源代码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class 贪吃蛇extends JFrame implements ActionListener, KeyListener,Runnable { /**
*
*/
private static final long serialVersionUID = 1L;
private JMenuBar menuBar;
private JMenu youXiMenu,nanDuMenu,fenShuMenu,guanYuMenu;
private JMenuItem kaiShiYouXi,exitItem,zuoZheItem,fenShuItem;
private JCheckBoxMenuItem cJianDan,cPuTong,cKunNan;
private int length = 6;
private Toolkit toolkit;
private int
i,x,y,z,objectX,objectY,object=0,growth=0,time;//bojectX,Y private int m[]=new int[50];
private int n[]=new int[50];
private Thread she = null;
private int life=0;
private int foods = 0;
private int fenshu=0;
public void run(){
time=500;
for(i=0;i<=length-1;i++) {
m[i]=90-i*10;n[i]=60;
}
x=m[0];
y=n[0];
z=4;
while(she!=null)
{
check();
try
{
Thread.sleep(time);
}
catch(Exception ee)
{
System.out.println(z+"");
}
}
}
public 贪吃蛇() {
setVisible(true);
menuBar = new JMenuBar();
toolkit=getToolkit();
youXiMenu = new JMenu("游戏"); kaiShiYouXi = new JMenuItem("开始游戏"); exitItem = new JMenuItem("退出游戏");
nanDuMenu = new JMenu("困难程度"); cJianDan = new JCheckBoxMenuItem("简单"); cPuTong = new JCheckBoxMenuItem("普通"); cKunNan = new JCheckBoxMenuItem("困难");
fenShuMenu = new JMenu("积分排行"); fenShuItem = new JMenuItem("最高记录");
guanYuMenu = new JMenu("关于");
zuoZheItem = new JMenuItem("关于作者");
guanYuMenu.add(zuoZheItem);
nanDuMenu.add(cJianDan); nanDuMenu.add(cPuTong); nanDuMenu.add(cKunNan); fenShuMenu.add(fenShuItem); youXiMenu.add(kaiShiYouXi); youXiMenu.add(exitItem);
menuBar.add(youXiMenu);
menuBar.add(nanDuMenu);
menuBar.add(fenShuMenu);
menuBar.add(guanYuMenu); zuoZheItem.addActionListener(this); kaiShiYouXi.addActionListener(this); exitItem.addActionListener(this); addKeyListener(this);
fenShuItem.addActionListener(this);
KeyStroke keyOpen =
KeyStroke.getKeyStroke('O',InputEvent.CTRL_DOWN_MASK); kaiShiYouXi.setAccelerator(keyOpen);
KeyStroke keyExit =
KeyStroke.getKeyStroke('X',InputEvent.CTRL_DOWN_MASK); exitItem.setAccelerator(keyExit);
setJMenuBar(menuBar);
setTitle("贪吃蛇");
setResizable(false);
setBounds(300,200,400,400);
validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new 贪吃蛇();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==kaiShiYouXi)
{
length = 6;
foods = 0;
if(she==null)
{
she=new Thread(this);
she.start();
}
else if(she!=null)
{
she=null;
she= new Thread(this); she.start();
}
}
if(e.getSource()==exitItem) {
System.exit(0);
}
if(e.getSource()==zuoZheItem)
{
JOptionPane.showMessageDialog(this, "孤独的野狼制作"+"\n\n"+" "+"QQ 号:2442701497"+"\n");
}
if(e.getSource()==fenShuItem)
{
JOptionPane.showMessageDialog(this,"最高记录为"+fenshu+"");
}
}
public void check(){
isDead();
if(she!=null)
{
if(growth==0)
{
reform(); //得到食物
}
else
{
upgrowth(); //生成食物
}
if(x==objectX&&y==objectY)
{
growth=1;
toolkit.beep();
}
if(object==0)
{
object=1;
objectX=(int)Math.floor(Math.random()*39)*10; objectY=(int)Math.floor(Math.random()*29)*10+50; }
this.repaint(); //重绘}
}
void isDead()
{
//判断游戏是否结束的方法if(z==4)
{
x=x+10;
}
else if(z==3)
{
x=x-10;
}
else if(z==2)
{
y=y+10;
}
else if(z==1)
{
y=y-10;
}
if(x<0||x>390||y<50||y>390)
{
she=null;
}
for(i=1;i<length;i++)
{
if(m[i]==x&&n[i]==y)
{
she=null;
}
}
继续阅读。

相关主题