用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

ss

2019-05-08 作者: 羁绊举报

[java]代码库

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;


public class SnakeMap extends JPanel implements Runnable{


	ImageIcon eatIcon = new ImageIcon("food.png");
	ImageIcon heidong = new ImageIcon("heidong.png");
	SnakeMap sm;
	Snake snake = new Snake();

	public SnakeMap(){
		sm = this;
		//构造组件
		JLabel  jlTitle = new JLabel();
		jlTitle = new JLabel(new ImageIcon("title.jpg"),JLabel.CENTER);
		JLabel  jlLgio = new JLabel();
		jlLgio = new JLabel(new ImageIcon("lgio.png"),JLabel.CENTER);
		JLabel  jlMap = new JLabel();
		jlMap = new JLabel(new ImageIcon("map5.jpg"),JLabel.CENTER);
		JButton newGame = new JButton("开始");
		newGame.setBackground(new Color(168,132,98));
		newGame.setForeground(new Color(128,64,0));
		newGame.setFont(new Font("华文行楷",1,30 ));
		JButton stopGame = new JButton("退出");
		stopGame.setBackground(new Color(168,132,98));
		stopGame.setForeground(new Color(128,64,0));
		stopGame.setFont(new Font("华文行楷",1,30 ));
		JLabel jlScore = new JLabel("分数:");
		jlScore.setForeground(new Color(128,64,0));
		jlScore.setFont(new Font("华文行楷",1,22 ));
		//JLabel jlHard = new JLabel("难度");
		//jlHard.setForeground(new Color(128,64,0));
		//jlHard.setFont(new Font("华文行楷",1,22 ));
		//JSlider slider = new JSlider(5,20);
		//slider.setBackground(new Color(168,132,98));
		//slider.setSnapToTicks(true);
		//slider.setPaintTicks(false);
		//slider.setMajorTickSpacing(0);
		//slider.setMinorTickSpacing(0);

		//设定布局
		setLayout(null);
		
		//添加组件
		add(jlTitle);
		jlTitle.setBounds(0,0,800,70);
		add(newGame);
		newGame.setBounds(40, 120, 120, 40);
		add(jlScore);
		jlScore.setBounds(30,210,100,80);
		//add(jlHard);
		//jlHard.setBounds(30,220,70,40);
		add(stopGame);
		stopGame.setBounds(40,320,120,40);
		add(jlLgio);
		jlLgio.setBounds(15,370,180,230);
		add(jlMap);
		jlMap.setBounds(210,80,580,480);
		//add(slider);
		//slider.setBounds(100,230,100,20);
		
		//设置窗体颜色
		this.setBackground(new Color(168,132,98));

		//设置监听器
		newGame.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//newGame.setEnabled(false);
				if(snake.start == false && snake.failed == true){
					music();
					snake.setup();
					snake.start = true;
					requestFocus(true);
				}
				if(snake.start == true){
					requestFocus(true);
				}
				if(snake.start == false && snake.failed == false){
					snake.setup();
					snake.start = true;
					snake.failed = true;
					requestFocus(true);
				}
			}
		});

		stopGame.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});

		//添加键盘监听
		addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(snake.start && snake.failed){
					if(e.getKeyCode() == KeyEvent.VK_UP && snake.j !=20 && snake.isMove == false){
						snake.j = -20;
						snake.i = 0;
						snake.isMove = true;
					}
					if(e.getKeyCode() == KeyEvent.VK_DOWN && snake.j != -20 && snake.isMove == false){
						snake.j = 20;
						snake.i = 0;
						snake.isMove = true;
					}
					if(e.getKeyCode() ==KeyEvent.VK_LEFT && snake.i != 20 && snake.isMove == false){	
						snake.i = -20;
						snake.j = 0;
						snake.isMove = true;
					}
					if(e.getKeyCode() == KeyEvent.VK_RIGHT && snake.i != -20 && snake.isMove == false){
						snake.i = 20;
						snake.j = 0;
						snake.isMove = true;
					}

					if(e.getKeyCode() == KeyEvent.VK_SPACE && snake.pause == true)
						snake.pause = false;
					else if(e.getKeyCode() == KeyEvent.VK_SPACE && snake.pause == false){
						snake.pause = true;
						Thread thread2 = new Thread(sm);
						thread2.start();
						requestFocus(true);
					}
				}
			}
		});	
		Thread thread=new Thread(sm);
		thread.start();
	}			

	public void run(){
		while(snake.pause){
			snake.move();
			repaint(); 
 			try{
				Thread.sleep(400 - 50*snake.speed);  
			}catch(Exception e){}
		}
	}

	

		/*if(snakex[0] == 370 && snakey[0] == 400){
			snakex[0] = 610;
			snakey[0] = 280;
		}
		else if(snakex[0] == 610 && snakey[0] == 280){
			snakex[0] = 370;
			snakey[0] = 400;
		}*/
		

	public void music(){
		try{
			File f = new File("faded.wav");
			URI uri = f.toURI();
			URL url = uri.toURL();
			AudioClip aau = Applet.newAudioClip(url);
			aau.loop();
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	//调用paint方法
	public void paint(Graphics g){
		super.paint(g);
		//画出地图  左上角(210,80)  右下角(790,560)
		g.drawRect(200,70,600,500);   
		g.drawRect(210,80,580,480);
		g.fillRect(200,70,600,10);
		g.fillRect(200,70,10,500);
		g.fillRect(200,560,600,10);
		g.fillRect(790,70,10,500);

		//画出蛇、食物
		if(snake.start && snake.failed){
			eatIcon.paintIcon(this , g , snake.food.eatx , snake.food.eaty);
			heidong.paintIcon(this , g , 370 , 400);
			heidong.paintIcon(this , g , 610 , 280);
			g.setColor(new Color(255,0,0));
			g.fillOval(snake.snakex[0] , snake.snakey[0] , 20 , 20);
			Graphics2D g1 = (Graphics2D) g;
			g1.setPaint(new GradientPaint(168,132,Color.CYAN,185,210,Color.MAGENTA,true));
			for(int m = 1 ; m < snake.len ; m ++){
				g.fillOval(snake.snakex[m] , snake.snakey[m] , 20 , 20);
			}
			g.setColor(new Color(128,64,0));
			g.setFont(new Font("华文行楷",Font.BOLD, 30));
			g.drawString(""+snake.score, 100, 260);
		}

		if (!snake.failed){
			g.setColor(new Color(128,64,0));
			g.setFont(new Font("华文行楷",Font.BOLD, 30));
			g.drawString(""+snake.score, 100, 200);
            g.setColor(new Color(168,132,98)); 
            g.setFont(new Font("arial",Font.BOLD, 30));
            g.drawString("Game Over ! Press start to restart", 200, 300);
        }
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...