lua之card 游戏

require("Card")

function main()
	local self	= CCScene:create()
	
	local layer
	local allPoints
	local allCards = {}
	local countNum =1
	local errCount = 0
	
	local function getPoints()
		allPoints = {}
		for i=0,9 do
			for j=0,5 do
				table.insert(allPoints,1,ccp(i*80,j*80))
			end
		end
	end
	local function addCards()
		
		math.randomseed(os.time())
		local c
		local randNum
		local p 
		
		for var=1,10 do
			c = Card(var)
			layer:addChild(c)
			
			randNum = math.random(table.maxn(allPoints))
			p = table.remove(allPoints,randNum)
			c:setPosition(p);
			
			table.insert(allCards,1,c)
		end
	end
	local function startGame()
			--local c = Card(2)
			--layer:addChild(c)
			--c:showBg()						
			countNum = 1
			getPoints()
			addCards()
	end
	
	local function showAllCardsBg()
		for key,var in ipairs(allCards) do
			var:showBg()
		end
	end
	local function onTouch(type,x,y)
		local p = ccp(x,y)
		for key,var in pairs(allCards) do
			if var:boundingBox():containsPoint(p) then--检查是否点击在图片的范围内
				if countNum == var.num then
					table.remove(allCards,key)
					layer:removeChild(var,true)
				    print(var.num)
					if countNum == 1 then
						showAllCardsBg()
					end
					
					if table.maxn(allCards)<= 0 then
						print ("success")
					end
					countNum = countNum+1
				else
					errCount = errCount + 1
					--if errCount >= 3 then
						--startGame()
					--end
					
				end		
				break			
			end
		
		end
	end
	local function init()
		layer = CCLayer:create()
		self:addChild(layer)
		layer:setTouchEnabled(true)
		layer:registerScriptTouchHandler(onTouch)
		startGame()		
		--local s = CCSprite:create("ma.jpg")
		--s:setPosition(ccp(300,300))
		--layer:addChild(s)
		
		--layer:setTouchEnabled(true)
		--layer:registerScriptTouchHandler(function (type,x,y)
		--print(type,x,y)
		--	if s:boundingBox():containsPoint(ccp(x,y)) then
		--		print ("mashibing click")
		--	end
		--	return true
		
		--end)
	end
	
	
		init()			

	return self
end

local function __main()
	CCDirector:sharedDirector():runWithScene(main())
	local dir = CCDirector:sharedDirector()
	dir:setDisplayStats(false)
	CCEGLView:sharedOpenGLView():setDesignResolutionSize(800,480,kResolutionShowAll)
	
	
end

__main()


function Card(num)
	local self = CCSprite:create()
	local txt,bg
	local function init()
		self.num = num
		self:setContentSize(CCSizeMake(80,80))
		self:setAnchorPoint(ccp(0,0))
		
		txt = CCLabelTTF:create(num,"Courier",50)
		txt:setPosition(ccp(40,40))
		self:addChild(txt)
		
		bg = CCSprite:create("chen.jpg")
		bg:setContentSize(CCSizeMake(80,80))--设置图片大小
		bg:setPosition(ccp(40,40))
		--bg:setTextureRect(CCRectMake(0,0,80,80))
		--bg:setAnchorPoint(ccp(0,0))
		self:addChild(bg)
		
		self:showTxt()
	end
	
	self.showTxt = function ()
		txt:setVisible(true)--设置对象的可见性 
		bg:setVisible(false)
	end
	
	self.showBg = function ()
		txt:setVisible(false)
		bg:setVisible(true)
	
	end
	
	init()
	return self
end






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值