吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 13661|回复: 44
上一主题 下一主题
收起左侧

[Java 转载] 贪吃蛇,完整代码

  [复制链接]
跳转到指定楼层
楼主
yy852 发表于 2016-11-15 21:40 回帖奖励
1.Yard
[Java] 纯文本查看 复制代码
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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 {

	public static final int ROWS = 30;
	public static final int COLS = 30;
	public static final int BLOCK_SIZE = 15;
	private boolean flag = true; //游戏是否结束
	
	Snake s = new Snake(this);
	Egg e = new Egg();
	
	Image offScreenImage = null;//双缓冲  取消闪烁                   //3
	
	public void launch() {
		this.setLocation(200,200);
		this.setSize(COLS*BLOCK_SIZE,ROWS*BLOCK_SIZE);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});  
		this.setVisible(true);
		this.addKeyListener(new KeyMonitor());/*222*/
		new Thread(new PaintThread()).start();//线程
	}
	

	public static void main(String[] args) {
		new Yard().launch();
	}
	
	public void stop() {            //
		flag = false;
	}
	
	@Override
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, COLS*BLOCK_SIZE, ROWS*BLOCK_SIZE);
		g.setColor(Color.DARK_GRAY);
		for(int i=1;i<ROWS;i++) {
			g.drawLine(0, BLOCK_SIZE*i, COLS*BLOCK_SIZE, BLOCK_SIZE*i);
		}
		for(int i=1;i<COLS;i++) {
			g.drawLine(BLOCK_SIZE*i, 0, BLOCK_SIZE*i, BLOCK_SIZE*ROWS);
		}
		g.setColor(c);
		s.eat(e);
		e.draw(g);
		s.draw(g);
	}
	
	@Override
	public void update(Graphics g) {
		if(offScreenImage == null) {
			offScreenImage = this.createImage(COLS*BLOCK_SIZE, ROWS*BLOCK_SIZE);
		}
		Graphics gOff = offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	
	private class PaintThread implements Runnable {

	
		public void run() {
			while(flag) {
				repaint();
				try {
					Thread.sleep(200);
				} catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	private class KeyMonitor extends KeyAdapter {

		@Override
		public void keyPressed(KeyEvent e) {
			s.KeyPressed(e);
		}
		
	}

}


2.Snake
[Java] 纯文本查看 复制代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;



public class Snake {
	private Node head = null;
	private Node tail = null;
	private int size = 0;
	private Node n = new Node(20,5,Dir.U);
	private Yard y;
	
	public Snake(Yard y) {
		head = n;
		tail = n;
		size = 1;
		this.y = y;
	}
	
	public void addToHead() {
		Node node = null;
		switch (head.dir) {
		case L:
			node = new Node(head.row,head.col-1,head.dir);
			break;
		case U:
			node = new Node(head.row-1,head.col,head.dir);
			break;
		case R:
			node = new Node(head.row,head.col+1,head.dir);
			break;
		case D:
			node = new Node(head.row+1,head.col,head.dir);
			break;
		}
		node.next = head;
		head.prev = node;
		head = node;
		size ++;
	}
	
	public void addToTail() {
		Node node = null;
		switch (tail.dir) {
		case L:
			node = new Node(tail.row,tail.col+1,tail.dir);
			break;
		case U:
			node = new Node(tail.row+1,tail.col,tail.dir);
			break;
		case R:
			node = new Node(tail.row,tail.col-1,tail.dir);
			break;
		case D:
			node = new Node(tail.row-1,tail.col,tail.dir);
		}
		tail.next = node;
		node.prev = tail;
		tail = node;
		size ++;
	}
	
	public void draw(Graphics g) {
		if(size <= 0) return;
		move();
		for(Node n = head;n != null; n = n.next) {
			n.draw(g);
		}
	}
	
	private void move() {
		addToHead();
		deleteToTail();
		checkDead();
		
	}

	private void checkDead() {
		if(head.row<0||head.col<2||head.row>Yard.ROWS||head.col>Yard.COLS) {
			y.stop();
		}
		for(Node n = head.next; n != null; n = n.next) {
			if(head.row == n.row && head.col == n.col) {
				y.stop();
			}
		}
		
	}

	private void deleteToTail() {
		if(size == 0) return;
		tail = tail.prev;
		tail.next = null;
		
	}
	
	private Rectangle getRect() {
		return new Rectangle(Yard.BLOCK_SIZE*head.col,Yard.BLOCK_SIZE*head.row,head.w,head.h);
	}
	
	public void eat(Egg e) {
		if(this.getRect().intersects(e.getRect())) {
			e.reAppear();
			this.addToHead();
		}
		
	}
	
	public class Node {
		int row,col;
		int w = Yard.BLOCK_SIZE;
		int h = Yard.BLOCK_SIZE;
		Dir dir = Dir.L;
		Node next = null;
		Node prev = null;
		
		
		Node(int row, int col, Dir dir) {
			this.row = row;
			this.col = col;
			this.dir = dir;
		}



		void draw(Graphics g) {
			Color c = g.getColor();
			g.setColor(Color.BLACK);
			g.fillRect(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);
			g.setColor(c);
		}
	}

	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;
		}
		
	}

	
}


3.egg
[Java] 纯文本查看 复制代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;


public class Egg {
	int row;
	int col;
	int w = Yard.BLOCK_SIZE;
	int h = Yard.BLOCK_SIZE;
	private static Random r = new Random();
	private Color color = Color.GREEN;
	
	public Rectangle getRect() {
		return new Rectangle(Yard.BLOCK_SIZE*col,Yard.BLOCK_SIZE*row,w,h);
	}
	
	public Egg() {
		this(r.nextInt(Yard.ROWS-2)+2,r.nextInt(Yard.COLS));
	}
	
	public void reAppear() {
		this.row = r.nextInt(Yard.ROWS-2)+2;
		this.col = r.nextInt(Yard.COLS);
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public Egg(int row, int col) {
		this.row = row;
		this.col = col;
	}
	
	public void draw(Graphics g) {
		Color c = g.getColor();
		g.setColor(color);
		g.fillOval(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);
		g.setColor(c);
		if(color == Color.GREEN) color = Color.RED;
		else color = Color.GREEN;
	}

}

4.Dir
[Java] 纯文本查看 复制代码
public enum Dir {
	L,U,R,D;

}

前几天的代码都是不完整的,今天的是我修正好的,仅供大家参考。

java2.jpg (11.13 KB, 下载次数: 110)

java2.jpg

免费评分

参与人数 11吾爱币 +4 热心值 +10 收起 理由
td520 + 1 + 1 感谢分享,要是有注释就更好了
LSA + 1 谢谢@Thanks!
52P + 1 + 1 谢谢@Thanks!
czqfjczq + 1 + 1 用心讨论,共获提升!
liangyisen + 1 我很赞同!
谁家的旭仔丢了 + 1 鐢ㄥ績璁ㄨ锛屽叡鑾锋彁鍗囷紒
Hey丶man + 1 我很赞同!
shaoxiao + 1 我很赞同!
13427546798 + 1 我很赞同!
yy1131512350 + 1 谢谢@Thanks!
myiszhy + 1 热心回复!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
 楼主| yy852 发表于 2016-11-16 19:01 |楼主
geeky 发表于 2016-11-15 22:31
我特地拿去跑了一下,不错。  应该写个死了自动重新开始

嗯  ,我也是新手啊,这个游戏就是练习一下,最近我有考试,所以这个游戏就不在往下写了,我空我再写,你可以写写,我们一起学习一下。
推荐
52pojie123 发表于 2016-12-16 23:13
ceekayshen 发表于 2016-11-16 22:51
请教一下楼主 Node类 是哪个jar提供的

哪个Node类呀。。。用的原生的吧。。没看到新添加啥东西。
头像被屏蔽
3#
geeky 发表于 2016-11-15 22:31
4#
Deteriorator 发表于 2016-11-15 22:32
有没有运行的截图?
5#
Hey丶man 发表于 2016-11-15 22:42
试一试 运行试试看! 谢了
6#
omnitx 发表于 2016-11-15 22:51 来自手机
咦!!!怎么用啊??
7#
sis_venus 发表于 2016-11-15 23:15
打个zip包下载比较好吧,谢谢分享啊。拿去玩一玩。
8#
win111jack 发表于 2016-11-16 00:44
经典的游戏啊
9#
ceekayshen 发表于 2016-11-16 09:59
略过略过,跪求源码。
10#
ceekayshen 发表于 2016-11-16 10:00
我想问一下,楼主的代码显示用的那个框架。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 17:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表