game_21_canvas.java

来自「手机在线系统 采用Java 中的J2ME, JSP 跟MySql 运行环」· Java 代码 · 共 464 行

JAVA
464
字号
/**
 * @(#)Game_21_Canvas.java	
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	
 */
package com.j2me.games.point_21;	

import com.j2me.games.*;
import com.j2me.language.*;
import javax.microedition.lcdui.*;

/**
 *此类是游戏主界面,并判断用户跟庄家的斗争
 */
public class Game_21_Canvas extends Canvas implements CommandListener
{
	private Display display = null;			//显示屏幕
	private GameMenu gameMenu = null;		//游戏菜单
	public AboutGame aboutGame = null;
	private Command wantCommand = null;  //定义玩家要牌软键
	private Command noWantCommand = null; //定义玩家不要牌软键
	private Command openCardCommand = null;	 //定义开牌软键
	private Command startCommand = null;			//定义开始软键
	private Command reStartCommand = null;			//定义重新开始软键
	private Command backCommand = null;			//定义离开软键
	private Command aboutCommand = null;
	private int isWhoWin; //假如1为庄家胜,2为玩家胜 
	private int bankerNumber;	//记录庄家点数
	private int playNumber;		//记录玩家点数
    private int move = 50; //显示图片的位移量
	private boolean isStart = false; //按确定时开始
	private boolean isWant = false;	   //判断要牌否
	private boolean isNoWant = false;  //判断不要牌否
	private boolean isOpenCard = false; //判断开牌否
	private Game_21Point [] banker = new Game_21Point[5]; 	//定义庄家牌对象数组
	private Game_21Point [] play =  new Game_21Point[5]; 	//定义玩家对象数组
	private int playDegree = 0; //定义玩家要牌次数变量
	private int bankerDegree = 0; //定义庄家要牌次数变量

	public Alert winAlert =  null;
	public Alert lossAlert = null; 
	public Alert standoffAlert = null;

	/*定义庄家与玩家的金币数量*/
	private int bankerMoney;
	private int playMoney;

   /**
    *初始化界面
	*/
	public Game_21_Canvas()
	{	
		this.banker[1] = new Game_21Point();	  //new扑克牌对象
		/*每方10000个金币*/
		this.bankerMoney = 10000;
		this.playMoney = 10000;
		this.backCommand = new Command("返回", Command.EXIT, 1);
		this.startCommand = new Command("开始", Command.OK, 2);
		this.reStartCommand = new Command("重新开始", Command.OK, 2);
		this.wantCommand = new Command("玩家要牌", Command.OK, 2);
		this.noWantCommand = new Command("庄家要牌", Command.OK, 2);
		this.openCardCommand = new Command("开牌", Command.OK, 2);
		this.aboutCommand = new Command("关于", Command.OK, 2);
		Image image = null;
		try
		{
			image = Image.createImage("/icon/Jabout.png");	 //创建图片用于显示结果界面图案
		}
		catch(Exception e)
		{
			System.out.println("Loand Error!");
		}
		this.winAlert = new Alert("提示", "恭喜恭喜! 你赢了100个金币!", image, AlertType.INFO);
	    this.lossAlert = new Alert("提示", "不好意思! 你输了100个金币", image, AlertType.INFO); 
	    this.standoffAlert = new Alert("提示", "打和了", image, AlertType.INFO);
   		this.winAlert.setTimeout(1000);		  //等待1秒种
		this.lossAlert.setTimeout(1000);
		this.standoffAlert.setTimeout(1000);
		this.addCommand(startCommand);
		this.addCommand(reStartCommand);
		this.addCommand(openCardCommand);
		this.addCommand(wantCommand);
		this.addCommand(noWantCommand);
		this.addCommand(backCommand);
		this.addCommand(aboutCommand);
		this.setCommandListener(this);
	}

	/*显示窗体的方法*/
	public void showForm(Display display, GameMenu gameMenu)  
	{			
		this.gameMenu = gameMenu;
	   	this.display = display;
		this.display.setCurrent(this);
	}	

	/*获得玩家每张牌的点数,即把没张牌点数转换为1到10的方法*/
	public int getNumber(int i)	   
	{
		int number = 0;
		if (i > 0 && i <= 10)	   //小于10时正常算。//大于10<13,即为 j O K,则算10计算
		{
			number = number + i; 
		}

		else if (i >= 11 && i <= 13)
		{
			number = number + 10;
		}

		else if (i >= 14 && i <= 23)
		{
			number = number + (i - 13);
		}

		else if (i >= 24 && i <= 26)
		{
			number = number + 10;
		}

		else if (i >= 27 && i <= 36)
		{
			number = number + (i - 26);
		}

		else if (i >= 37 && i <= 39)
		{
			number = number +10;
		}

		else if (i >= 40 && i <= 49)
		{
			number = number + (i -39);
		}

		else if (i >= 50 && i <= 52)
		{
			number = number + 10;
		}

		return number;
	}

    /*绘制图形的方法*/
	public void paint(Graphics g)		//绘制扑克牌
	{
		g.setColor(255, 255, 255);					   //设置底色为白色
		g.fillRect(0, 0, this.getWidth(), this.getHeight());				 //填充为白色
		if (isStart == false)
		{
			g.setColor(255, 0, 0);
			g.setFont(Font.getFont(0, 1, 16));
			g.drawString("欢迎来到21点游戏",  this.getWidth()/2,  this.getHeight()/2, g.TOP|g.HCENTER);
			this.play[0] = new Game_21Point();
		}
		if (isStart == true)	  //开始时显示牌
		{
			g.setColor(255, 0, 0);	
			g.drawString("庄家", move , 15, g.TOP|g.HCENTER);
			g.drawString("玩家", move, this.getHeight()/3, g.TOP|g.HCENTER);
			g.drawString("庄家的金币:" + bankerMoney, move*3 -20, 15, g.TOP|g.HCENTER);
			g.drawString("玩家的金币:" + playMoney, move*3 - 20 , this.getHeight()/3, g.TOP|g.HCENTER);
			
			g.drawImage(banker[0].getBankerImage(), move*4, this.getHeight()/3, g.VCENTER|g.HCENTER);
			g.drawImage(banker[0].getBankerImage(), move, this.getHeight()/5, g.VCENTER|g.HCENTER);
		    g.drawImage(banker[1].getBankerImage(), move + 20, this.getHeight()/5, g.VCENTER|g.HCENTER);
	        g.drawImage(play[0].getGameImage(), move, this.getHeight()/2, g.VCENTER|g.HCENTER);
		    g.drawImage(play[1].getGameImage(), move + 20, this.getHeight()/2, g.VCENTER|g.HCENTER);
			if (isOpenCard == true)
			{
				g.drawImage(banker[0].getGameImage(), move, this.getHeight()/5, g.VCENTER|g.HCENTER);
				g.drawImage(banker[1].getGameImage(), move + 20, this.getHeight()/5, g.VCENTER|g.HCENTER);
			}
		}

		if (isWant == true)	//要牌时加牌
		{
			for (int i = 2; i < playDegree + 2; i++)
			{
			    g.drawImage(play[i].getGameImage(), move + 20 * i , this.getHeight()/2, g.VCENTER|g.HCENTER);
			}
		}
		
		if (isNoWant == true)
		{
			for (int i = 2; i < bankerDegree + 2; i++)
			{
			    g.drawImage(banker[i].getBankerImage(), move + 20 * i , this.getHeight()/5, g.VCENTER|g.HCENTER);			   
			}
		}

		if (isOpenCard == true)
		{
			for (int i = 2; i < bankerDegree + 2; i++)
			{
			    g.drawImage(banker[i].getGameImage(), move + 20 * i , this.getHeight()/5, g.VCENTER|g.HCENTER);
			}
		}
		
	}
	
	/*开始的方法*/
	public void start()	   
	{
		    this.play[1] = new Game_21Point(); //分开new是为了得到不同的牌面	
			this.banker[0] = new Game_21Point();
			if (this.getNumber(banker[0].getI()) == 1)	 //获得庄家点数,黑桃就变11
			{
			   this.bankerNumber = 11 +	 this.getNumber(banker[1].getI());
			}
			else if (this.getNumber(banker[1].getI()) == 1)
			{
				this.bankerNumber = 11 +  this.getNumber(banker[0].getI());
			}
			else if (this.getNumber(banker[0].getI()) == 1 && this.getNumber(banker[1].getI()) == 1)
			{
				this.bankerNumber = 11 +  11;
			}
			else 
			{				
				this.bankerNumber = this.getNumber(banker[0].getI()) + this.getNumber(banker[1].getI());
			}			

			if (this.getNumber(play[0].getI()) == 1)   //获得玩家点数,黑桃就变11
			{
				this.playNumber = 11 + this.getNumber(play[1].getI());
			}
			else if (this.getNumber(play[1].getI()) == 1)
			{
				this.playNumber = 11 + this.getNumber(play[0].getI());
			}
			else if (this.getNumber(play[0].getI()) == 1 && this.getNumber(play[1].getI()) == 1)
			{
				this.bankerNumber = 11 +  11;
			}
			else
			{  
				this.playNumber = this.getNumber(play[0].getI()) + this.getNumber(play[1].getI());	//调用方法getNumber转换成1到10的数据
			} 
			
			isStart = true;	  //确定开始
			this.removeCommand(startCommand);	  //移除一些软键按妞
			this.repaint();
	}

	/*玩家要牌的方法*/
	public void playWant()
	{
		playDegree++;
		switch (playDegree)	  //判断要了多少牌,并new相同牌数的对象, 并获得点数
		{
			case 1 :  this.play[2] = new Game_21Point();
				          if (this.getNumber( play[2].getI()) == 1)	   //判断是否有黑桃
				          {
							  this.playNumber = this.playNumber + 10;
				          }
						  else
				          {
							  this.playNumber = this.playNumber + this.getNumber( play[2].getI());
						  } break;

			case 2 :  this.play[3] = new Game_21Point();
			              if (this.getNumber( play[2].getI()) == 1)	   //判断是否有黑桃
				          {
							  this.playNumber = this.playNumber - 9;
				          }
				          this.playNumber = this.playNumber + play[3].getNumber(); break;

			case 3 :  this.play[4] = new Game_21Point(); 
				           this.playNumber = this.playNumber + play[4].getNumber(); break;
			}			
		   	isWant = true;
			this.repaint();
	}

	public void bankerWant()
	{
		if (bankerNumber < 16 || bankerNumber < playNumber || bankerNumber < 21)
			{
				bankerDegree++;
				switch (bankerDegree)	//判断要了多少牌,并new相同牌数的对象 ,并获得点数
				{
					case 1 :  this.banker[2] = new Game_21Point(); 
					          if (this.getNumber(banker[2].getI()) == 1)	   //判断是否有黑桃
							  {
								  this.bankerNumber = this.bankerNumber + 10;
							  }
						      else
				              {
							      this.bankerNumber = this.bankerNumber + this.getNumber(banker[2].getI());
						      } break;

				    case 2 :  this.banker[3] = new Game_21Point();
					          if (this.getNumber(banker[2].getI()) == 1)	   //判断是否有黑桃
							  {
								  this.bankerNumber = this.bankerNumber - 9;
							  }
				              this.bankerNumber = this.bankerNumber + this.banker[3].getNumber(); break;

				    case 3 :  this.banker[4] = new Game_21Point(); 
				              this.bankerNumber = this.bankerNumber + banker[4].getNumber(); break;
				}
				isNoWant = true;			
				this.repaint();
			}
	}

	/*重新开始的方法*/
	public void reStart()		
	{
		   isStart = false;
		   isOpenCard = false;	 //开牌
		   isWant = false;
		   isNoWant = false;
		   this.playDegree = 0;	   //玩家要牌次数为0
		   this.bankerDegree = 0;  //庄家要牌次数为0
		   this.playNumber = 0;		//玩家点数为0
		   this.bankerNumber = 0;	 //庄家点数为0
		   this.addCommand(startCommand);
		   this.addCommand(wantCommand);
		   this.addCommand(noWantCommand);
		   this.addCommand(openCardCommand);
		   this.repaint();			 //重画
	}
	
	/*开牌的方法*/
	public void openCard()	 
	{
		isOpenCard = true;	 
		System.out.println("play" + playNumber);
		System.out.println("banker" + bankerNumber);
		if (isStart == false && this.playNumber == 22 && this.bankerNumber != 22 )
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else if (this.playNumber == 21 && this.bankerNumber != 21 )
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else if ((this.playNumber > 21 && this.bankerNumber >21) || this.playNumber == this.bankerNumber)
		{
			this.display.setCurrent(standoffAlert);
		}

		else if ((this.playNumber > 16 && this.playNumber < 21) && this.playNumber > this.bankerNumber)
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else if (this.playNumber < 16 && this.bankerNumber > 21)
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else if (this.playNumber < 16 && this.bankerNumber < 16 && this.playNumber > this.bankerNumber)
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else if (this.playNumber < 21 && this.bankerNumber > 21 )
		{
			this.display.setCurrent(winAlert);
			this.bankerMoney = this.bankerMoney - 100;
			this.playMoney = this.playMoney + 100;
		}

		else
		{
			this.display.setCurrent(lossAlert);
			this.bankerMoney = this.bankerMoney + 100;
			this.playMoney = this.playMoney - 100;
		}
		this.repaint();
		this.removeCommand(startCommand);	  //移除一些软键按妞
		this.removeCommand(wantCommand);
		this.removeCommand(noWantCommand);
		this.removeCommand(openCardCommand);
	}

	public void commandAction(Command c,Displayable s)
	{
		if(c == backCommand)
		{
			this.display.setCurrent(gameMenu);
		}

		if (c == startCommand)	 //开始
		{
			this.start();
		}

		if (c == reStartCommand)	//全部初始化开始变量,重新开始
		{
		   	 this.reStart();
		}

		if (c == openCardCommand ) //开牌
		{
			this.openCard();
		}

		if (c == wantCommand)  //玩家要牌
		{
			this.playWant();
		}

		if (c == noWantCommand)	//玩家不要牌的时候庄家判断要不要牌
		{
			this.bankerWant();			
		}

		if (c == aboutCommand)
		{
			//定义关于信息,并初始化,信息内容
		    String copyrightChines =    "  欢迎使用手机在线动感娱乐!\n"
			                        + "  希望我们的软件给你带来愉快!\n"
									+ "  21点游戏是中国传统牌类游戏\n"
									+ "  游戏规则是玩家与庄家的点数如果超过\n"
									+ "  21点与点数相同时为打和\n"
									+ "  如果一方点数不过16点,而对方如果超过16点\n"
									+ "  则他赢,否则看谁的点数大\n"
									+ "  版本信息:1.0\n"
									+ "  技术支持:020-38491064\n"
									+ "  无花工作室版权所有 2004-2005。\n"
									+ "                     作者: 饶荣庆\n";	
			String copyrightEnglish = "    Welcome to Innervation Disport On-Line!\n"
			                         + "   Best wish with we! \n" 
									 + "   version: 1.0\n"
									 + "   Telphone: 020-34891064\n"
									 + "   Copyright (c) 2004-2005 Wuhua Workroom, Inc. All rights reserved.\n"
									 + "                       @author 	Rao Rongqing\n"
									 + "                       @author  Yu yihui\n";  
	        if (Language.isEnglish)
	        {					
				this.aboutGame = new AboutGame(null, copyrightEnglish);
	        }
			if (Language.isEnglish == false)
			{				
				this.aboutGame = new AboutGame(copyrightChines, null);
			}
			this.aboutGame.showAbout(display);	  //显示关于游戏窗口
		}
	}
}
	




⌨️ 快捷键说明

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