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

📄 terrain.java

📁 Source-DeathFlight手机游戏,死亡飞行,适合初学者学习.
💻 JAVA
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Image;
import java.util.Random;

public class Terrain extends TiledLayer{
	
	public int milemeter;
	
	public int speed;          // it is public so we could set the speed in main menu;
	
	private int timestamp;
	
	private int tileWidth,tileHeight;
	
	private int [][] gameMap = null;
	
	private int screenWidth,screenHeight;
	
	private int wallHeight;
	
	private int Rows, Cols;
	
	private Random random = null;
	
	private int layerTop;
	
	private int [] indexMap = null;
	
	private boolean up_down;
	
	private int alterSpeed;
	
	private int alterExtent;
	
	private int upBound,lowBound,wallPos;
	
	private Helicopter player;
	
	public Terrain(int columns,int rows,Image image,int tileWidth,int tileHeight,Helicopter player){
		
		super(columns,rows,image,tileWidth,tileHeight);
		
		this.player = player;
		
		speed = 0 ; //////////////
		
		random = new Random();
		
		layerTop = -1* tileHeight;
		
		speed = 0;
		timestamp = 0;
		
		this.tileHeight = tileHeight;
		this.tileWidth = tileWidth;
		
		screenWidth = getWidth();
		screenHeight = getHeight();
		
		wallHeight = screenWidth / 4 /tileWidth;
		
		Rows = rows;
		Cols = columns;
		
		
		indexMap = new int[Rows];
		for(int i=0;i<Rows;i++)indexMap[i] = i;
		
		gameMap = new int[rows][columns];
		for(int i=0;i<Rows;i++){
			for(int j=0;j<Cols;j++){
				if(j<(Cols/7) || (Cols-j)<=(Cols/7))
					gameMap[i][j]=1;
			}
		}
		
		for(int i=0;i<Rows;i++){
			for(int j=0;j<Cols;j++){
				this.setCell(j,i,gameMap[indexMap[i]][j]);  // notice the usage of indexMap[]
			}
		}
		
		alterSpeed = alterExtent = 0;
		
		upBound = Cols/7; 
		lowBound = Cols-Cols/7;
		
	}
	
	public void update(){
		if (++timestamp > speed) {
			timestamp = 0;
			if(layerTop == 0){
				milemeter += 1;
				for(int i=upBound+1;i<lowBound;i++)gameMap[indexMap[Rows-1]][i] = 0;
				if(alterExtent > 0){
					
					if(up_down == true){upBound -= alterSpeed;lowBound -= alterSpeed;}
					else {upBound += alterSpeed; lowBound += alterSpeed;}
					if(upBound<0){upBound += alterSpeed;lowBound+= alterSpeed; }
					if(lowBound >= Cols-1){upBound -= alterSpeed;lowBound -= alterSpeed; }
					for(int i=0;i<Cols;i++){
						if(i<=upBound || i>=lowBound)
							gameMap[indexMap[Rows-1]][i] = 1;
					}
					alterExtent--;
				}
				else {
					alterExtent = random.nextInt()%10;
					if(alterExtent == 0)alterExtent=5;
					if(alterExtent > 0){
						up_down = true;
					}
					else {
						up_down = false;
						alterExtent *= -1;
					}
					alterSpeed = Math.abs(random.nextInt())%3+1;
					return;
				}
				
				// produces the wall barrier
				if(milemeter % 10 == 0){
					wallPos  = Math.abs(random.nextInt())%(Cols-wallHeight);
					for(int i=0;i<Cols;i++){
						if(i==wallPos){
							for(int j=0;j<wallHeight;j++)gameMap[indexMap[Rows-1]][i+j]=1;
							break;
						}
					}
				}
				//:~ produce the barrier
				
				// switches the whole row mapping of the tile matrix
				for(int i=0;i<Rows;i++){
					indexMap[i]=indexMap[i]-1;
					if(indexMap[i] == -1)indexMap[i] = Rows-1;
				}
				//:~ row mapping
				
				// apply the new matrix to the tiledLayer
				for(int i=0;i<Rows;i++){
					for(int j=0;j<Cols;j++){
						this.setCell(j,i,gameMap[indexMap[i]][j]);
					}
				}
				//:~ apply 
				
				// update the postion of tiledLayer
				layerTop = -1 * tileHeight;
				this.setPosition(0,layerTop);
				//:~ update
				
			}
			else this.setPosition(0,layerTop++);
		}
	}
	
	public boolean collides(){
		for(int i=player.getPlayerRect().top / tileHeight;i<=player.getPlayerRect().bottom / tileHeight;i++){
			for(int j=player.getPlayerRect().left / tileWidth;j<=player.getPlayerRect().right / tileWidth;j++){
				if( gameMap[indexMap[i]][j] == 1){
					return true;
				}
			}
		}
		return false;
	}

}

⌨️ 快捷键说明

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