贪吃蛇 day 02

Version 1.用一个线程来控制重画,添加Snake类,并添加键盘的方向键来控制蛇的运动

Direction.java

package com.songdanlee;

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

Snake.java

package com.songdanlee;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

//通过键盘的方向键控制蛇的运动
public class Snake {
	//位置
	private int x;
	private int y;
	//大小
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	
	//方向
	private Direction dir = Direction.D;
	
	private SnakeFrame snakeFrame;
	
	public Snake(int x,int y,SnakeFrame snakeFrame){
		this.x = x;
		this.y = y;
		this.snakeFrame = snakeFrame;
	}
	
	public Snake(int x,int y,Direction dir,SnakeFrame snakeFrame){
		this(x,y,snakeFrame);
		this.dir = dir;
	}
	/**
	 * 把原来的颜色设回来,为什么又设置回来?看不出有什么作用啊?
		答:这是为了让你知道要还原场景,在这没什么实际用处,但是如果传过来的Graphics对象是别人用过了别人可以设了颜色
		了你设过回会导致覆盖了他的所以这时最好能还原下,他再用就不会出错了。如果是新的Graphics不会有这种问题
	 * */
	public void draw(Graphics g){
		
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		//fillRect(int x,int y,int width,int height)矩形的左右边缘为x和x + width - 1 。 顶部和底部边缘在y和y + height - 1 。 所得到的矩形覆盖width像素宽, height像素高的区域。 使用图形上下文的当前颜色填充矩形。 
		g.fillRect(x, y, WIDTH, HEIGHT);
		g.setColor(c);
	}
	//按键处理事件
	public void keyPressed(KeyEvent e){
		
		int key = e.getKeyCode();
		switch(key){
		case KeyEvent.VK_LEFT:
			this.x = x - WIDTH;
			break;
		case KeyEvent.VK_UP:
			this.y = y - HEIGHT;
			break;
		case KeyEvent.VK_RIGHT:
			this.x = x + WIDTH;
			break;
		case KeyEvent.VK_DOWN:
			this.y = y + HEIGHT;
			break;
		}
		//处理边界
		if(x <= 0)
			x = 0;
		else if(x > SnakeFrame.GAME_WIDTH - WIDTH)
			x = SnakeFrame.GAME_WIDTH - WIDTH;
		if(y <= 0)
			y = 0;
		else if(y > SnakeFrame.GAME_HEIGHT - HEIGHT)
			y = SnakeFrame.GAME_HEIGHT - HEIGHT;
		
	}
	
	
}

SnakeFrame.java

package com.songdanlee;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class SnakeFrame extends Frame {
	
	static int GAME_WIDTH = 600;
	static int GAME_HEIGHT = 600;
	private int SQUEEZE = 10;
	
	Snake s = new Snake(40,40,Direction.D,this);
	public static void main(String[] args) {
		SnakeFrame  sf = new SnakeFrame();
		sf.lunchFrame();
	}

	private void lunchFrame() {
		this.setTitle("Snake Game");
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setLocation(300, 400);
		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		new Thread(new MypaintThread()).start();
		
		this.addKeyListener(new MyKeyListener());
		
	}
	
	public void paint(Graphics g){
		Color c = g.getColor();
		g.setColor(Color.GRAY);
		/*划线
		 * 
		 * */
		int col = GAME_WIDTH / SQUEEZE;
		int row = GAME_HEIGHT / SQUEEZE;
		
		for(int i=0;i<col;i++){
			g.drawLine(i*SQUEEZE, 0, i*SQUEEZE, GAME_HEIGHT);
		}
		
		for(int i=0;i<row;i++){
			g.drawLine(0, i*SQUEEZE, GAME_HEIGHT, i*SQUEEZE);
		}
		g.setColor(c);
		s.draw(g);
	}
	
	private class MypaintThread implements Runnable{
		public void run(){
			//每间隔50ms重画一次
			while(true){
				repaint(); //自动调用paint方法
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	private class MyKeyListener extends KeyAdapter{

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

Version 2.没有按方向键时,则按照原来的方向移动

Direction.java

package com.songdanlee;

public enum Direction {
	L,R,U,D,STOP
}

SnakeFrame.java

package com.songdanlee;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class SnakeFrame extends Frame {
	
	static int GAME_WIDTH = 600;
	static int GAME_HEIGHT = 600;
	private int SQUEEZE = 10;
	
	Snake s = new Snake(40,40,Direction.D,this);
	public static void main(String[] args) {
		SnakeFrame  sf = new SnakeFrame();
		sf.lunchFrame();
	}

	private void lunchFrame() {
		this.setTitle("Snake Game");
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setLocation(300, 400);
		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		new Thread(new MypaintThread()).start();
		
		this.addKeyListener(new MyKeyListener());
		
	}
	
	public void paint(Graphics g){
		Color c = g.getColor();
		g.setColor(Color.GRAY);
		/*划线
		 * 
		 * */
		int col = GAME_WIDTH / SQUEEZE;
		int row = GAME_HEIGHT / SQUEEZE;
		
		for(int i=0;i<col;i++){
			g.drawLine(i*SQUEEZE, 0, i*SQUEEZE, GAME_HEIGHT);
		}
		
		for(int i=0;i<row;i++){
			g.drawLine(0, i*SQUEEZE, GAME_HEIGHT, i*SQUEEZE);
		}
		g.setColor(c);
		s.draw(g);
	}
	
	private class MypaintThread implements Runnable{
		public void run(){
			//每间隔100ms重画一次
			while(true){
				repaint(); //自动调用paint方法
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	private class MyKeyListener extends KeyAdapter{

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

Snake.java

package com.songdanlee;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;



//通过键盘的方向键控制蛇的运动
public class Snake {
	//位置
	private int x;
	private int y;
	//大小
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	
	//方向
	private Direction dir = Direction.D;
	
	private SnakeFrame snakeFrame;
	
	public Snake(int x,int y,SnakeFrame snakeFrame){
		this.x = x;
		this.y = y;
		this.snakeFrame = snakeFrame;
	}
	
	public Snake(int x,int y,Direction dir,SnakeFrame snakeFrame){
		this(x,y,snakeFrame);
		this.dir = dir;
	}
	/**
	 * 把原来的颜色设回来,为什么又设置回来?看不出有什么作用啊?
		答:这是为了让你知道要还原场景,在这没什么实际用处,但是如果传过来的Graphics对象是别人用过了别人可以设了颜色
		了你设过回会导致覆盖了他的所以这时最好能还原下,他再用就不会出错了。如果是新的Graphics不会有这种问题
	 * */
	public void draw(Graphics g){
		
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		//fillRect(int x,int y,int width,int height)矩形的左右边缘为x和x + width - 1 。 顶部和底部边缘在y和y + height - 1 。 所得到的矩形覆盖width像素宽, height像素高的区域。 使用图形上下文的当前颜色填充矩形。 
		g.fillRect(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		move();//线程自动调用paint方法后调用move方法,实现没有按方向键时,则按照原来的方向移动
	}
	private void move() {
		if(dir == Direction.L)
			x = x - WIDTH;
		else if(dir == Direction.U)
			y = y - HEIGHT;
		else if(dir == Direction.R)
			x = x + WIDTH;
		else if(dir == Direction.D)
			y = y + HEIGHT;
			
		if(x <= 0)
		   x = 0;
		else if(x > SnakeFrame.GAME_WIDTH - WIDTH)
			x = SnakeFrame.GAME_WIDTH - WIDTH;
		if(y <= 0)
			y = 0;
		else if(y > SnakeFrame.GAME_HEIGHT - HEIGHT)
			y = SnakeFrame.GAME_HEIGHT - HEIGHT;
	}

	//按键处理事件
	public void keyPressed(KeyEvent e){
		
		int key = e.getKeyCode();
		switch(key){
		case KeyEvent.VK_LEFT:
			this.dir = Direction.L;
 			break;
		case KeyEvent.VK_UP:
			this.dir = Direction.U;
			break;
		case KeyEvent.VK_RIGHT:
			this.dir = Direction.R;
			break;
		case KeyEvent.VK_DOWN:
			this.dir = Direction.D;
			break;
		default:
			this.dir = Direction.STOP;
			break;
		}
		
	}
	
	
}

Version 3 : 重写界面

SnakeFrame.java

package com.songdanlee;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/*界面有COL*ROW的方格构成
 * */

public class SnakeFrame extends Frame {
	//定义方格的宽度和长度
	public static final int BLOCK_WIDTH = 15;
	public static final int BLOCK_HEIGHT = 15;
	//定义方格的行数和列数
	public static final int ROW = 40;
	public static final int COL = 40;
	
	
	public static void main(String[] args) {
		new SnakeFrame().lunchFrame();
	}

	private void lunchFrame() {
		this.setTitle("Snake Game");
		this.setSize(BLOCK_HEIGHT * ROW, COL * BLOCK_WIDTH);
		this.setLocation(300, 400);
		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		new Thread(new MypaintThread()).start();
	}
	
	public void paint(Graphics g){
		Color c = g.getColor();
		g.setColor(Color.GRAY);
		/*
		 *将界面画成 ROW * COL 的方格  
		 * */
		/*
		 * 画出列线,每个线的起点和终点的规律如下:
		 * 起点的横坐标 ,终点的横坐标相等,为i * BLOCK_WIDTH
		 * 起点纵坐标为0 , 终点 为 ROW*BLOCK_HEIGHT 
		 * */
		
		for(int i=0;i<COL;i++){
			g.drawLine(i * BLOCK_WIDTH, 0, i * BLOCK_WIDTH, ROW * BLOCK_HEIGHT);
		}
		/*
		 * 画出行线,每个线的起点和终点的规律如下:
		 * 起点的横坐标 固定为0 ,终点的横坐标固定为BLOCK_WIDTH * COL
		 * 起点纵坐标为i * BLOCK_HEIGHT , 终点 为 i * BLOCK_HEIGHT 
		 * */
		for(int i=0;i<ROW;i++){
			g.drawLine(0, i*BLOCK_HEIGHT, COL*BLOCK_WIDTH,i*BLOCK_HEIGHT );
		}
		g.setColor(c);
		
	}
	
	private class MypaintThread implements Runnable{
		public void run(){
			//每间隔100ms重画一次
			while(true){
				repaint(); //自动调用paint方法
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}


阅读更多
个人分类: java 贪吃蛇
上一篇贪吃蛇 java day01
下一篇贪吃蛇 day 03
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭