ccpuship.java

来自「J2ME写的一个潜艇大战游戏!!!!!!!!!」· Java 代码 · 共 209 行

JAVA
209
字号
/*
 * CCpuShip.java
 *
 * Created on 2006年4月16日, 下午2:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package gamepacket;

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
 *
 * @author zxh
 *========敌人潜艇===========
 */

public class CCpuShip
{
    private Random rand;//随机数
    
    public int TypeId;//敌人潜艇类型 
    
    public int dx,dy;//玩家在地图上的坐标
    private int Score;//玩家得分
    private int Speed;//玩家移动速度
    public boolean IsDead;//玩家是否死亡
    
    private int width,height;//屏幕大小
    
    //敌船图
    public Sprite spt_cpuship;
    

    public CBao m_Bao;
    public CCpuBomb Bomb;//每个敌人只有一个炸弹
    /** Creates a new instance of CCpuShip */
    public CCpuShip (int scrWidth,int scrHeight,LayerManager lm,int id)
    {
	this.IsDead=false;//玩家没有死亡
	this.width=scrWidth;
	this.height=scrHeight;
	m_Bao=new CBao(lm);
	Bomb=new CCpuBomb(width,height,lm);//炸弹
	
	
	rand=new Random();
	//this.TypeId=Math.abs (rand.nextInt ()%7);
	this.TypeId=id;
	//船属性
	switch(TypeId)
	{
	    case 0:
	    case 1:
		this.Score=10;
		this.Speed=2;
		break;

	    case 2:
	    case 3:
		this.Score=20;
		this.Speed=1;
		break;

	    case 4:
	    case 5:
		this.Score=30;
		this.Speed=3;
		break;
		
	    case 6:
	    case 7:
		this.Score=50;
		this.Speed=2;
		break;	
	}
		//确定潜艇位置
	this.SetPoint (TypeId);

	    //敌船图
		try
		{
		    spt_cpuship=new Sprite(Image.createImage ("/img/cpuship.png"),40,12);
		} catch (IOException ex){}
		
		
		
		lm.append (spt_cpuship);
		spt_cpuship.setPosition (dx,dy);
		spt_cpuship.setFrame (TypeId);
		spt_cpuship.setVisible (true);
		
    }

    
    
    //发射炸弹
    public void SendBomb()
    {
	if(Bomb.GetShow ()==false)
	    {
		Bomb.SetShow (true);//显示炸弹
		Bomb.SetPoint (this.dx+15,this.dy+6);
	    }
    }
    
    public int GetScore()
    {
	return this.Score;
    }
    
    
    //设置潜艇位置
    public void SetPoint(int id)
    {
	switch(id)
	{
	    case 0:
		dx=360;
		dy=Math.abs(rand.nextInt()) % 21 + 100;
		break;
	    case 1:
		dx=-40;
		dy=Math.abs(rand.nextInt()) % 21 + 100;
		break;
		
	    case 2:
		dx=360;
		dy=Math.abs(rand.nextInt()) % 21 + 120;
		break;
	    case 3:
		dx=-40;
		dy=Math.abs(rand.nextInt()) % 21 + 120;
		break;
		
	    case 4:
		dx=360;
		dy=Math.abs(rand.nextInt()) % 21 + 140;
		break;
	    case 5:
		dx=-40;
		dy=Math.abs(rand.nextInt()) % 21 + 140;
		break;

	    case 6:
		dx=360;
		dy=Math.abs(rand.nextInt()) % 21 + 160;
		break;
	    case 7:
		dx=-40;
		dy=Math.abs(rand.nextInt()) % 21 + 160;
		break;
	}
    }
    
    
    
	public void Move (CPlayerShip player)
	{
	    //敌人何时发射炸弹
	    if((dx<player.dx+30) && (dx>player.dx-20))
	    {
		this.SendBomb();
	    }
	    //敌人移动
		if(IsDead==false)
		{
		    spt_cpuship.setVisible (true);
		    switch(TypeId)
		    {
			case 1:
			case 3:
			case 5:
			case 7:
			    dx+=Speed;//向右移动
			    
			if(dx>360)
			{
			    IsDead=true;//死亡
			    spt_cpuship.setVisible (false);//不显示敌船
			    this.SetPoint (TypeId);
			}
			 break;
			//===============    
			case 0:
			case 2:
			case 4:
			case 6:
			    dx-=Speed;//向右移动
			    
			if(dx<=-40)
			{
			    IsDead=true;//死亡
			    spt_cpuship.setVisible (false);//不显示敌船
			    this.SetPoint (TypeId);
			}
			    break;
			    
		    }
		    spt_cpuship.setPosition (this.dx,this.dy);//设定船坐标
		}		    

	    }   

}

⌨️ 快捷键说明

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