贪吃蛇 小成

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//蛇的活动场所
public class Yard extends Frame {

	PaintThread paintThread = new PaintThread();
	//游戏是否结束
	boolean isGameOver = false; 
	
	Button b1 = new Button("uhi");
	
	private Font fontGameOver = new Font("楷体", Font.BOLD, 25);
	private Font fontGameOver1 = new Font("宋体", Font.BOLD, 80);
	private Font fontGameOver2 = new Font("楷体", Font.BOLD, 40);
	private Font fontGameOver3 = new Font("宋体", Font.BOLD, 55);
	
	public int score = 0;
	public int count = 0;
	public int topScore = 200;
	
	Snake s = new Snake(this);  //先new出来一个蛇头
	Egg e = new Egg(this);  //先new出来一个蛋
	
	Image offScreenImage = null;
	
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] imgs = {
		tk.getImage(Snake.class.getClassLoader().getResource("images/asdf.jpg")),
		tk.getImage(Snake.class.getClassLoader().getResource("images/gameover.jpg"))
	};
	
	public void launch() {
		
		FlowLayout f1 = new FlowLayout();
		this.setLayout(f1);
		this.setLocation(100, 80);
		this.setSize(1000, 700);
		this.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
		this.setVisible(true);
		this.addKeyListener(new KeyMonitor());
		
		new Thread(paintThread).start();
	}
	
	public static void main(String[] args) {
		new Yard().launch();
	}
	
	public void stop() {
		isGameOver = true;
	}
	
	public void paint(Graphics g) {
		if(isGameOver){
			Panel p = new Panel();
			p.setBounds(400, 300, 123, 123);
			//p.setBackground(Color.BLACK);
			//this.add(p);
			//this.add(b1);
		}
		if(!isGameOver) {
			g.drawImage(imgs[0], 0, 0, 1000, 700, null);
			//g.drawString(+ score, 200, 73);
			g.setFont(fontGameOver);
			g.drawString("你的分数为:" + score, 100, 73);
			g.drawString("跟随灵兽数:" + count, 100, 113);
			g.drawString("最高纪录为:" + topScore, 100, 153);
		}
		else {
			
			g.drawImage(imgs[1], 0, 0, 1000, 700, null);
			g.setFont(fontGameOver1);
			g.drawString("游戏结束", 360, 100);
			g.setFont(fontGameOver2);
			g.drawString("你的分数为:" + score, 340, 180);
			g.drawString("跟随灵兽数:" + count, 340, 240);
			g.drawString("最高纪录为:" + topScore, 340, 300);
			g.drawString("等级:", 340, 400);
			g.setFont(fontGameOver3);
			Color c = g.getColor();
			g.setColor(Color.BLACK);
			g.fillOval(580, 365, 30, 30);
			g.setColor(c);
			//this.setLayout(null);
			//this.p.add(b1);
			if((score > 0 || score == 0) && score < 140) {
				g.drawString("平凡  初入谷民", 450, 400);
			}
			else if(score > 120 && score < 280) {
				g.drawString("平凡  小有名气", 450, 400);
			}
			else if(score > 260 && score < 460) {
				g.drawString("境界  渐窥堂奥", 450, 400);
			}
			else if(score > 440 && score < 660) {
				g.drawString("境界  清净玄妙", 450, 400);
			}
			else if(score > 640 && score < 980) {
				g.drawString("修道  大乘", 450, 400);
			}
			else if(score > 960 && score < 1400) {
				g.drawString("修道  飞升", 450, 400);
			}
			else if(score > 1380) {
				g.drawString("修道  成仙", 450, 400);
			}
		}
		
		s.eat(e);
		e.draw(g);
		s.draw(g);
	}
	
	public void update(Graphics g) {
		if(offScreenImage == null) {
			offScreenImage = this.createImage(1000, 700);
		}
		Graphics gOff = offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0,  null);
	}
	
	private class PaintThread implements Runnable {
		private boolean running = true;
		private boolean pause = false;
		public void run() {
			while(running) {
				if(pause) continue; 
				else repaint();
				
				try {
					Thread.sleep(130);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		public void reStart() {
			this.pause = false;
			s = new Snake(Yard.this);
		}
	}
		
	private class KeyMonitor extends KeyAdapter {

		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if(key == KeyEvent.VK_F2) {
				paintThread.reStart();
			}
			s.keyPressed(e);
		}
		
	}
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class Snake {
	private Yard yd;
	
	public Node head = null;
	private Node tail = null;
	private int size = 0;
	
	//蛇头
	private Node nd = new Node(840, 330, Dir.L);
	
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] imgs = {
		tk.getImage(Snake.class.getClassLoader().getResource("images/a.gif")),
		tk.getImage(Snake.class.getClassLoader().getResource("images/b.gif"))
	};
	
	public class Node {
		int w = 30;
		int h = 30;
		//位置(坐标)
		int x , y;
		Dir dir = Dir.L;
		
		Node next = null;
		Node previous = null;
		
		
		
		Node(int x, int y, Dir dir) {
			this.x = x;
			this.y = y;
			this.dir = dir;
		}
		
		void draw(Graphics g) {
			//Color c = g.getColor();
			//g.setColor(Color.BLACK);
			//g.fillRect(x, y, w, h);
			//g.setColor(c);
			if(this == head) {
				g.drawImage(imgs[1], x, y, w, h, null);
			}
			else {
				g.drawImage(imgs[0], x, y, w, h, null);
			}
		}
	}
	public Snake() {
		
	}
	public Snake(Yard yd) {
		//开始new出来的蛇头,成为Node.root
		head = nd;
		tail = nd;
		size = 1;
		this.yd = yd;
	}
	
	
	public void addToTail() {
		//先给一个node指向null
		Node node = null;
		switch(tail.dir) { //根据蛇尾的方向和位置确定新加入的蛇尾的方向和位置
		case L :
			node = new Node(tail.x + 30, tail.y, tail.dir);
			break;
		case U :
			node = new Node(tail.x, tail.y + 30, tail.dir);
			break;
		case R :
			node = new Node(tail.x - 30, tail.y, tail.dir);
			break;
		case D :
			node = new Node(tail.x, tail.y - 30, tail.dir);
			break;
		}
		tail.next = node;  //把新new出来的node加到蛇尾
		node.previous = tail;
		tail = node;
		size ++;
	}
	
	public void addToHead() {
		Node node = null;
		switch(head.dir) {
		case L :
			node = new Node(head.x - 30, head.y, head.dir);
			break;
		case U :
			node = new Node(head.x, head.y - 30, head.dir);
			break;
		case R :
			node = new Node(head.x + 30, head.y, head.dir);
			break;
		case D :
			node = new Node(head.x, head.y + 30, head.dir);
			break;
		}
		node.next = head;
		head.previous = node;
		head = node;
		size ++;
	}
	
	public void draw(Graphics g) {
		if(size <= 0) return;
		move();
		if(!yd.isGameOver) {
			for(Node n = head; n != null; n = n.next) {  //画出整条蛇
				n.draw(g);
			}
		}
	}
	
	private void move() {
		addToHead();
		deleteFromTail();
		checkDead();
	}
	
	private void deleteFromTail() {
		if(size == 0) return;
		tail = tail.previous;
		tail.next = null;
		
	}


	private void checkDead() {
		if(head.y < 30 || head.x < 90 || head.y > 630 || head.x > 870)  {
			yd.stop();
		}
		
		for(Node n = head.next; n != null; n = n.next) {
			if(head.y == n.y && head.x == n.x) {
				yd.stop();
			}
		}
	}

	

	
	public void eat(Egg e) {
		if(this.getRect().intersects(e.getRect())) {
			yd.score += 20;
			yd.count ++;
			if(yd.score > 200) {
				yd.topScore = yd.score;
			}
			e.reAppear();
			this.addToTail();
			//yd.setScore(yd.getScore() + 5);
		}
	}
	
	private Rectangle getRect() {
		return new Rectangle(head.x, head.y, head.w, head.h);
	}
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch(key) {
		case KeyEvent.VK_LEFT :
			if(head.dir != Dir.R)
				head.dir = Dir.L;
			break;
		case KeyEvent.VK_UP :
			if(head.dir != Dir.D)
				head.dir = Dir.U;
			break;
		case KeyEvent.VK_RIGHT :
			if(head.dir != Dir.L)
				head.dir = Dir.R;
			break;
		case KeyEvent.VK_DOWN :
			if(head.dir != Dir.U)
				head.dir = Dir.D;
			break;
		}
	}
}




import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Random;

//import Snake.Node;

public class Egg {
	Yard yd;
	Snake sk = new Snake();
	int x, y;
	int w = 30;
	int h = 30;
	private static Random r = new Random();
	private Color color = Color.GREEN;
	

	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] imgs = {
		tk.getImage(Snake.class.getClassLoader().getResource("images/c.gif"))
	};

	public Egg(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public Egg(Yard yd) {
		this((r.nextInt(26) + 3)*30, (r.nextInt(21) + 1)*30);
		this.yd = yd;
	}
	
	public void reAppear() {
		this.x = (r.nextInt(26) + 3)*30;
		this.y = (r.nextInt(21) + 1)*30;
		isCan();
		
	}
	
	public void isCan() {
		for(Snake.Node n = sk.head; n != null; n = n.next) {  //画出整条蛇
			if(n.x == this.x && n.y == this.y) {
				this.x = (r.nextInt(26) + 3)*30;
				this.y = (r.nextInt(21) + 1)*30;
				isCan();
			}
		}
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, w, h);
	}

	public void draw(Graphics g) {
		//Color c = g.getColor();
		//g.setColor(color);
		if(!yd.isGameOver) {
			//sg.fillOval(x, y, w, h);
			g.drawImage(imgs[0], x, y, w, h, null);
		}
		//g.setColor(c);
		//实现蛋的闪烁
		//if(color == Color.GREEN) color = Color.RED;
		//else color = Color.GREEN;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
}

public enum Dir {
	L, U, R, D
}


贪吃蛇所需图片,自己ps的 呵呵

点击打开链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值