⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heads.java

📁 简单的贪吃蛇源码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.LinkedList;


public class heads extends Block{
	public explode e = null;
	private Direction dir = Direction.D;
	private Direction Olddir = null;
	private int Oldx,Oldy;	
	private boolean live = true;
	private Snake s = null;
	
	public  heads(int x,int y,Color c,Direction dir,Snake s){
		super(x,y,c);		
		this.dir = dir;
		this.s = s;
	}
	
	public void Draw(Graphics g){
		
		pongme(s.y);		
		if(!live){	
			e.draw(g);	
			move();
		}
		else{
			super.Draw(g);
			move();
		}		
	}
	
	private void move(){
		Oldx = x;
		Oldy = y;
		if(Direction.U == dir){
			y -= 20;
		}
		if(Direction.D == dir){
			y += 20;
		}
		if(Direction.L == dir){
			x -= 20;
		}
		if(Direction.R == dir){
			x += 20;
		}
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, this.BLOCKWH,this.BLOCKWH);
	}
	
	public void pongme(Yishen y){
		if(this.x>Snake.GameWidth-20||this.y>Snake.GameHeight-20||this.x<0||this.y<30){
			live = false;	
			y.setLive(false);
			this.dir = Direction.STOP;
			this.x = Oldx;
			this.y = Oldy;			
			e = new explode(this.x,this.y);
		}
		else{
			for(int i=0;i<y.blocks.size();i++){
				if(this.getRect().intersects(y.blocks.get(i).getRect())){
					live = false;
					this.dir = Direction.STOP;	
					y.setLive(false);					
					e = new explode(this.x,this.y);				
				}
			}	
		}
		
		
	}
	
	
	public boolean EatBlood(Blood b){		
		Rectangle r = getRect();
		if(r.intersects(b.getRect())){			
			return true;
		}
		return false;
	}
	
	public void keyPressed(KeyEvent e) {		
		switch(e.getKeyCode()){
		case KeyEvent.VK_UP:
			if(live&&Direction.D!=dir&&!s.y.isPasue())
			dir = Direction.U;
			break;
		case KeyEvent.VK_DOWN:
			if(live&&Direction.U!=dir&&!s.y.isPasue())
			dir = Direction.D;
			break;
		case KeyEvent.VK_LEFT:
			if(live&&Direction.R!=dir&&!s.y.isPasue())
			dir = Direction.L;
			break;
		case KeyEvent.VK_RIGHT:
			if(live&&Direction.L!=dir&&!s.y.isPasue())
			dir = Direction.R;
			break;
		case KeyEvent.VK_F2:			
			if (!live){				
				this.live =true;				
				s.y = new Yishen(Color.CYAN,Color.ORANGE,this);	
			}
			else{
				if(s.y.isPasue()){
					this.dir = Olddir;
					s.y.setPasue(false);				
				}
				else {					
					s.y.setPasue(!s.y.isPasue());
					Olddir = this.dir;
					this.dir = Direction.STOP;	
				}				
				
			}
			
			break;
		}
		
	}

	public int getOldx() {
		return Oldx;
	}

	public int getOldy() {
		return Oldy;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public boolean isLive() {
		return live;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -