java贪吃蛇简易代码 贪吃蛇java代码讲解
高分跪求用Java编写的贪吃蛇源代码!具体要求看问题补充!急急急~~~544266616@qq.com
import java.awt.Graphics;
10年的漾濞网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整漾濞建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“漾濞网站设计”,“漾濞网站推广”以来,每个客户项目都认真落实执行。
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;
life = 0;
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==objectXy==objectY)
{
object=0;
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(x0||x390||y50||y390)
{
she=null;
}
for(i=1;ilength;i++)
{
if(m[i]==xn[i]==y)
{
she=null;
}
}
}
public void upgrowth()
{
//当蛇吃到东西时的方法
if(length50)
{
length++;
}
growth--;
time=time-10;
reform();
life+=100;
if(fenshulife)
{
fenshu = life;
}
foods++;
}
public void reform()
{
for(i=length-1;i0;i--)
{
m[i]=m[i-1];
n[i]=n[i-1];
}
if(z==4)
{
m[0]=m[0]+10;
}
if(z==3)
{
m[0]=m[0]-10;
}
if(z==2)
{
n[0]=n[0]+10;
}
if(z==1)
{
n[0]=n[0]-10;
}
}
public void keyPressed(KeyEvent e)
{
if(she!=null)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
if(z!=2)
{
z=1;
check();
}
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
if(z!=1)
{
z=2;
check();
}
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
if(z!=4)
{
z=3;
check();
}
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
if(z!=3)
{
z=4;
check();
}
}
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void paint(Graphics g) {
g.setColor(Color.DARK_GRAY); //设置背景
g.fillRect(0,50,400,400);
g.setColor(Color.pink);
for(i=0;i=length-1;i++)
{
g.fillRect(m[i],n[i],10,10);
}
g.setColor(Color.green); //蛇的食物
g.fillRect(objectX,objectY,10,10);
g.setColor(Color.white);
g.drawString("当前 分数"+this.life,6,60);
g.drawString("当前已吃食物数"+this.foods,6,72);
}
}
Java贪吃蛇的界面背景的代码怎么写
这是一个最基本的界面,下面又不会的地方去查API
import java.awt.*;
import javax.swing.*;
public class View {
JFrame frame;
Container c;
JLabel backgound;
JButton start;
JLabel help;
public void first(){
frame=new JFrame("我的贪吃蛇");
c=frame.getContentPane();
c.setLayout(null);
Font f1=new Font("楷体",Font.BOLD,50);
backgound=new JLabel();
backgound.setBounds(0,0,800,600);
c.add(backgound);
backgound.setLayout(null);
start=new JButton();
start.setBounds(300,70,200,58);
start.setCursor(new Cursor(Cursor.HAND_CURSOR));
start.setBorder(null);
start.setContentAreaFilled(false);
backgound.add(start);
help = new JLabel();
help.setText("游戏帮助");
help.setBounds(180,230,500,50);
help.setForeground(Color.black);
help.setFont(f1);
backgound.add(help);
frame.setSize(800,600);
frame.setResizable(false);
frame.setVisible(true);
}
用Java语言写一个约1500行代码的贪吃蛇游戏
Runnable
}
if (i.util.Date.start();args) {
Thread new Thread(new Thread1());0;one = new Thread(new Thread2()).printStackTrace();
public
if (i %
one.start();
two;class Thread2
Thread implements class Thread1 {
while(true){
i++;
System.out.println(new i = run() {
while (true) {
two = Date().toLocaleString());
} catch try {
Thread.sleep(10000);Runnable {
break;
}
}
}
}
import java;Client {
public static void main(String[] );
public void run()
}
}
}
}pre t="code" l="java"public class 4 == 0) {
System.out.println(;*******pre t="code" l="java"public implements {
private int (InterruptedException e) {
e;{
public void 100)nbsp
Java贪吃蛇的界面背景代码怎么写
这是一个最基本的界面,下面又不会的地方去查API
import java.awt.*;
import javax.swing.*;
public class View {
JFrame frame;
Container c;
JLabel backgound;
JButton start;
JLabel help;
public void first(){
frame=new JFrame("我的贪吃蛇");
c=frame.getContentPane();
c.setLayout(null);
Font f1=new Font("楷体",Font.BOLD,50);
backgound=new JLabel();
backgound.setBounds(0,0,800,600);
c.add(backgound);
backgound.setLayout(null);
start=new JButton();
start.setBounds(300,70,200,58);
start.setCursor(new Cursor(Cursor.HAND_CURSOR));
start.setBorder(null);
start.setContentAreaFilled(false);
backgound.add(start);
help = new JLabel();
help.setText("游戏帮助");
help.setBounds(180,230,500,50);
help.setForeground(Color.black);
help.setFont(f1);
backgound.add(help);
frame.setSize(800,600);
frame.setResizable(false);
frame.setVisible(true);
}
文章名称:java贪吃蛇简易代码 贪吃蛇java代码讲解
网站链接:http://pwwzsj.com/article/dochjch.html