RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java贪吃蛇源代码完整 java贪吃蛇简单代码

哪位能告诉我贪吃蛇游戏的全部代码?

//package main;

企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,成都创新互联面向各种领域:成都玻璃钢坐凳成都网站设计公司营销型网站建设解决方案、网站设计等建站排名服务。


import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class TanChiShe implements KeyListener,ActionListener{

/**

* @param args

*/

int max = 300;//蛇长最大值

final int JianJu = 15; //设定蛇的运动网格间距(窗口最大32*28格)

byte fangXiang = 4; //控制蛇的运动方向,初始为右

int time = 500; //蛇的运动间隔时间

int jianTime = 2;//吃一个减少的时间

int x,y;//蛇的运动坐标,按网格来算

int x2,y2;//暂存蛇头的坐标

int jiFenQi = 0;//积分器

boolean isRuned = false;//没运行才可设级别

boolean out = false;//没开始运行?

boolean run = false;//暂停运行

String JiBie = "中级";

JFrame f = new JFrame("贪吃蛇 V1.0");

JPanel show = new JPanel();

JLabel Message = new JLabel("级别:中级 蛇长:5 时间500ms 分数:00");

// JButton play = new JButton("开始");

JLabel sheTou;

JLabel shiWu;

JLabel sheWei[] = new JLabel[max];

static int diJi = 4; //第几个下标的蛇尾要被加上

ImageIcon shang = new ImageIcon("tuPian\\isSheTouUp.png");//产生四个上下左右的蛇头图案

ImageIcon xia = new ImageIcon("tuPian\\isSheTouDown.png");

ImageIcon zhuo = new ImageIcon("tuPian\\isSheTouLeft.png");

ImageIcon you = new ImageIcon("tuPian\\isSheTouRight.png");

JMenuBar JMB = new JMenuBar();

JMenu file = new JMenu("开始游戏");

JMenuItem play = new JMenuItem(" 开始游戏 ");

JMenuItem pause = new JMenuItem(" 暂停游戏 ");

JMenu hard = new JMenu("游戏难度");

JMenuItem gao = new JMenuItem("高级");

JMenuItem zhong = new JMenuItem("中级");

JMenuItem di = new JMenuItem("低级");

JMenu about = new JMenu(" 关于 ");

JMenuItem GF = new JMenuItem("※高分榜");

JMenuItem ZZ = new JMenuItem("关于作者");

JMenuItem YX = new JMenuItem("关于游戏");

JMenuItem QK = new JMenuItem("清空记录");

static TanChiShe tcs = new TanChiShe();

public static void main(String[] args) {

// TanChiShe tcs = new TanChiShe();

tcs.f();

}

public void f(){

f.setBounds(250,100,515,530);

f.setLayout(null);

f.setAlwaysOnTop(true);//窗口始终保持最前面

f.setBackground(new Color(0,0,0));

f.setDefaultCloseOperation(0);

f.setResizable(false);

f.setVisible(true);

// f.getContentPane().setBackground(Color.BLACK);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);//退出程序

}

});

f.setJMenuBar(JMB);

JMB.add(file);

file.add(play);

file.add(pause);

JMB.add(hard);

hard.add(gao);

hard.add(zhong);

hard.add(di);

JMB.add(about);

about.add(GF);

GF.setForeground(Color.blue);

about.add(ZZ);

about.add(YX);

about.add(QK);

QK.setForeground(Color.red);

f.add(show);

show.setBounds(0,f.getHeight()-92,f.getWidth(),35);

// show.setBackground(Color.green);

// f.add(play);

// play.setBounds(240,240,80,25);

play.addActionListener(this);

pause.addActionListener(this);

gao.addActionListener(this);

zhong.addActionListener(this);

di.addActionListener(this);

GF.addActionListener(this);

ZZ.addActionListener(this);

YX.addActionListener(this);

QK.addActionListener(this);

show.add(Message);

Message.setForeground(Color.blue);

f.addKeyListener(this);

// show.addKeyListener(this);

play.addKeyListener(this);

sheChuShi();

}

public void sheChuShi(){//蛇初始化

sheTou = new JLabel(you);//用向右的图来初始蛇头

f.add(sheTou);

sheTou.setBounds(JianJu*0,JianJu*0,JianJu,JianJu);

// System.out.println("ishere");

shiWu = new JLabel("■");

f.add(shiWu);

shiWu.setBounds(10*JianJu,10*JianJu,JianJu,JianJu);

for(int i=0;i=diJi;i++) {

sheWei[i] = new JLabel("■");

f.add(sheWei[i]);

sheWei[i].setBounds(-1*JianJu,0*JianJu,JianJu,JianJu);

}

while(true){

if(out == true){

yunXing();

break;

}

try{

Thread.sleep(200);

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public void sheJiaChang(){//蛇的长度增加

if(diJi max){

sheWei[++diJi] = new JLabel(new ImageIcon("tuPian\\isSheWei.jpg"));

f.add(sheWei[diJi]);

sheWei[diJi].setBounds(sheWei[diJi-1].getX(),sheWei[diJi-1].getY(),JianJu,JianJu);

// System.out.println("diJi "+diJi);

}

}

public void pengZhuanJianCe(){//检测蛇的碰撞情况

if(sheTou.getX()0 || sheTou.getY()0 ||

sheTou.getX()f.getWidth()-15 || sheTou.getY()f.getHeight()-105 ){

gameOver();

// System.out.println("GameOVER");

}

if(sheTou.getX() == shiWu.getX() sheTou.getY() == shiWu.getY()){

out: while(true){

shiWu.setLocation((int)(Math.random()*32)*JianJu,(int)(Math.random()*28)*JianJu);

for(int i=0;i=diJi;i++){

if(shiWu.getX()!= sheWei[i].getX() shiWu.getY()!=sheWei[i].getY()

sheTou.getX()!=shiWu.getX() sheTou.getY()!= shiWu.getY()){//如果食物不在蛇身上则退出循环,产生食物成功

break out;

}

}

}

sheJiaChang();

// System.out.println("吃了一个");

if(time100 ){

time -= jianTime;

}

else{}

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+(jiFenQi+=10)+"");

}

for(int i=0;i=diJi;i++){

if(sheTou.getX() == sheWei[i].getX() sheTou.getY() == sheWei[i].getY()){

gameOver();

// System.out.println("吃到尾巴了");

}

}

}

public void yunXing(){

while(true){

while(run){

if(fangXiang == 1){//上

y-=1;

}

if(fangXiang == 2){//下

y+=1;

}

if(fangXiang == 3){//左

x-=1;

}

if(fangXiang == 4){//右

x+=1;

}

x2 = sheTou.getX();

y2 = sheTou.getY();

sheTou.setLocation(x*JianJu,y*JianJu); //设置蛇头的坐标 网格数*间隔

for(int i=diJi;i=0;i--){

if(i==0){

sheWei[i].setLocation(x2,y2);

// System.out.println(i+" "+sheTou.getX()+" "+sheTou.getY());

}

else{

sheWei[i].setLocation(sheWei[i-1].getX(),sheWei[i-1].getY());

// System.out.println(i+" "+sheWei[i].getX()+" "+sheWei[i].getY());

}

}

pengZhuanJianCe();

try{

Thread.sleep(time);

}catch(Exception e){

e.printStackTrace();

}

}

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+(jiFenQi+=10)+"");

try{

Thread.sleep(200);

}catch(Exception e){

e.printStackTrace();

}

}

}

public void gameOver(){//游戏结束时处理

int in = JOptionPane.showConfirmDialog(f,"游戏已经结束!\n是否要保存分数","提示",JOptionPane.YES_NO_OPTION);

if(in == JOptionPane.YES_OPTION){

// System.out.println("YES");

String s = JOptionPane.showInputDialog(f,"输入你的名字:");

try{

FileInputStream fis = new FileInputStream("GaoFen.ini");//先把以前的数据读出来加到写的数据前

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String s2,setOut = "";

while((s2=br.readLine())!= null){

setOut =setOut+s2+"\n";

}

FileOutputStream fos = new FileOutputStream("GaoFen.ini");//输出到文件流

s = setOut+s+":"+jiFenQi+"\n";

fos.write(s.getBytes());

}catch(Exception e){}

}

System.exit(0);

}

public void keyTyped(KeyEvent arg0) {

// TODO 自动生成方法存根

}

public void keyPressed(KeyEvent arg0) {

// System.out.println(arg0.getSource());

if(arg0.getKeyCode() == KeyEvent.VK_UP){//按上下时方向的值相应改变

if(fangXiang != 2){

fangXiang = 1;

// sheTou.setIcon(shang);//设置蛇的方向

}

// System.out.println("UP");

}

if(arg0.getKeyCode() == KeyEvent.VK_DOWN){

if(fangXiang != 1){

fangXiang = 2;

// sheTou.setIcon(xia);

}

// System.out.println("DOWN");

}

if(arg0.getKeyCode() == KeyEvent.VK_LEFT){//按左右时方向的值相应改变

if(fangXiang != 4){

fangXiang = 3;

// sheTou.setIcon(zhuo);

}

// System.out.println("LEFT");

}

if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){

if(fangXiang != 3){

fangXiang = 4;

// sheTou.setIcon(you);

}

// System.out.println("RIGHT");

}

}

public void keyReleased(KeyEvent arg0) {

// TODO 自动生成方法存根

}

public void actionPerformed(ActionEvent arg0) {

// TODO 自动生成方法存根

JMenuItem JI = (JMenuItem)arg0.getSource();

if(JI == play){

out = true;

run = true;

isRuned = true;

gao.setEnabled(false);

zhong.setEnabled(false);

di.setEnabled(false);

}

if(JI == pause){

run = false;

}

if(isRuned == false){//如果游戏还没运行,才可以设置级别

if(JI == gao){

time = 200;

jianTime = 1;

JiBie = "高级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

if(JI == zhong){

time = 400;

jianTime = 2;

JiBie = "中级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

if(JI == di){

time = 500;

jianTime = 3;

JiBie = "低级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

}

if(JI == GF){

try{

FileInputStream fis = new FileInputStream("GaoFen.ini");

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String s,setOut = "";

while((s=br.readLine())!= null){

setOut =setOut+s+"\n";

}

if(setOut.equals("")){

JOptionPane.showMessageDialog(f,"暂无保存记录!","高分榜",JOptionPane.INFORMATION_MESSAGE);

}

else{

JOptionPane.showMessageDialog(f,setOut);

}

}catch(Exception e){

e.printStackTrace();

}

}

if(JI == ZZ){//关于作者

JOptionPane.showMessageDialog(f,"软件作者:申志飞\n地址:四川省绵阳市\nQQ:898513806\nE-mail:shenzhifeiok@126.com","关于作者",JOptionPane.INFORMATION_MESSAGE);

}

if(JI == YX){//关于游戏

JOptionPane.showMessageDialog(f,"贪吃蛇游戏\n游戏版本 V1.0","关于游戏",JOptionPane.INFORMATION_MESSAGE);

}

if(JI == QK){

try{

int select = JOptionPane.showConfirmDialog(f,"确实要清空记录吗?","清空记录",JOptionPane.YES_OPTION);

if(select == JOptionPane.YES_OPTION){

String setOut = "";

FileOutputStream fos = new FileOutputStream("GaoFen.ini");//输出到文件流

fos.write(setOut.getBytes());

}

}catch(Exception ex){}

}

}

}

//是我自己写的,本来里面有图片的,但无法上传,所以把图片去掉了,里面的ImageIcon等语句可以去掉。能正常运行。

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程序一部分,我需要下面程序代码的详细注解~~~~我会重赏的,以高分告劳~

/**

* MVC模式中得Viewer,只负责对数据的显示,而不用理会游戏的控制逻辑

*/

class SnakeView implements Observer {

SnakeControl control = null; //实例话一个SnakeControl对象是control ..SnakeControl在jdk中不存在可能是第3方的或者是自己编写的一个类吧

SnakeModel model = null;//如上

JFrame mainFrame; //创建一个面板类jframe

Canvas paintCanvas;

/**

*Canvas 组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。

*应用程序必须为 Canvas 类创建子类,以获得有用的功能(如创建自定义组件)。必须重写 paint 方法,以便在 canvas 上执行自定义图形。

*/

JLabel labelScore;//....不说了吧..

public static final int canvasWidth = 200; //常量宽度200

public static final int canvasHeight = 300;//常量高300

public static final int nodeWidth = 10;//常量宽度10

public static final int nodeHeight = 10;//常量高度10

public SnakeView(SnakeModel model, SnakeControl control) {

this.model = model;

this.control = control;

mainFrame = new JFrame("GreedSnake");//创建jframe 标题是GreedSnake

Container cp = mainFrame.getContentPane(); //得到jfram的容器

labelScore = new JLabel("Score:"); //创建jlabel 标签内容是"Score:"

cp.add(labelScore, BorderLayout.NORTH);/将jlabel添加到jfram的容器中去

paintCanvas = new Canvas(); //创建新绘图区

paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1); //设置绘图区大小

paintCanvas.addKeyListener(control);//添加键盘监听器control

cp.add(paintCanvas, BorderLayout.CENTER);//将绘图区添加到jfram容器中去.布局是BorderLayout的CENTER位置.就是东西南北中的中间

JPanel panelButtom = new JPanel();//创建一个panel

panelButtom.setLayout(new BorderLayout());//设置布局是BorderLayout

JLabel labelHelp;//标签的创建和添加开始了.......new了3遍就是实例化了3个都加到了panle的中间 ,标签的内容都是""中间的

labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.NORTH);

labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.CENTER);

labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.SOUTH);

cp.add(panelButtom, BorderLayout.SOUTH);//把这个panel添加到jframe的容器里面去

mainFrame.addKeyListener(control);//为jframe添加键盘监听器

mainFrame.pack();//调整此窗口的大小,以适合其子组件的首选大小和布局。如果该窗口和/或其所有者仍不可显示,则两者在计算首选大小之前变得可显示。在计算首选大小之后,将会验证该 Window。

mainFrame.setResizable(false);//设置此 frame 是否可由用户调整大小。false就是不能

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击又上角的就是关闭

mainFrame.setVisible(true);//设置jfram为能看到

}

void repaint() {

Graphics g = paintCanvas.getGraphics();//Graphics详细看jdk文档..我就知道是一个所有图形上下文的抽象基类..这里应该是那个画图面板

g.setColor(Color.WHITE);//设置这个颜色

g.fillRect(0, 0, canvasWidth, canvasHeight);//画一个矩形在x,y处画一个宽是200,高是300的矩形,

g.setColor(Color.BLACK);//颜色是黑的

LinkedList na = model.nodeArray;//LinkedList 类 List 接口的链接列表实现就是一个集合对象了,因为不知道SnakeModel具体是一个什么类这里就当作这个类能得到一个集合

Iterator it = na.iterator();//得到迭代器去迭代这个集合

while (it.hasNext()) {//开始用while迭代

Node n = (Node) it.next();//得到一个集合中元素.是一个Node....这里的Node不知道是什么对象..有待于研究

drawNode(g, n);//调用另外方法

}

g.setColor(Color.RED);//设置颜色是红的

Node n = model.food;//得到一个新node..看来Node这里应该是一个坐标..这里的坐标就是蛇吃的那个东西

drawNode(g, n);//画这个东西..

updateScore();//调用..

}

private void drawNode(Graphics g, Node n) {//这是一个画方的方法..动态改变方型的位置

g.fillRect(n.x * nodeWidth,

n.y * nodeHeight,

nodeWidth - 1,

nodeHeight - 1);

}

public void updateScore() {//这是更新标签的一个方法

String s = "Score: " + model.score;

labelScore.setText(s);

}

public void update(Observable o, Object arg) {//这个就是Observer监听类必须实现的方法..这里是去调用repaint()方法..repaint方法就是不断去改变画图版里面的内容的

repaint();

}

}

java 贪吃蛇代码。移动方面的问题。

你不是有个temp的标识吗?可以用这个判断撒,记录上一状态。

修改如下:

public void keyPressed(KeyEvent e) {

if (start){

switch (e.getKeyCode()){

case KeyEvent.VK_UP:

if(temp==2) break;

move(0,-1);

temp =1;

break;

case KeyEvent.VK_DOWN:

if(temp==1) break;

move(0,1);

temp =2;

break;

case KeyEvent.VK_LEFT:

if(temp==4) break;

move(-1,0);

temp =3;

break;

case KeyEvent.VK_RIGHT:

if(temp==3) break;

move(1,0);

temp =4;

break;

default:

break;

}

}

}


网站名称:java贪吃蛇源代码完整 java贪吃蛇简单代码
分享链接:http://scyingshan.cn/article/dojpiis.html