怎么用VS写cocos游戏,井字棋游戏代码的游戏

quick cocos2d-x 入门---井字棋 - 七夜狐 - 博客园
学习quick cocos2d-x 第二天 ,使用quick-x 做了一个井字棋游戏 。
我假设读者已经&http://wiki.quick-x.com/doku.php?id=zh_cn阅读了这个链接下的内容 ,并学会了如何搭建环境和创建新的工程,并假高读者有一定cocos2d-x基础&
建议读者多研究一下quick-x自带的例子coinflip。并阅读framework下的lua源码,尤其注意用lua模拟出面象对象的部分(可参考《Lua程序设计》第二版的13,16两章)。
一。准备工作:
1.如何在场景(层)添加一个Sprite
我们在MainScene中添加一个Sprite&
function MainScene:ctor()
self.bg = display.newSprite("board.png", display.cx, display.cy)
self:addChild(self.bg)
-- keypad layer, for android
self.layer = Board.new()
self:addChild(self.layer)end
display 是处理显示有关的一个&类&。
newSprite则类似cocos2d-x中的CCSprite::create()
注意:Lua中除用local 修饰的变量都是全局变量。我们self.bg这样定义,而不直接定义,目的是不污染全局环境,和把bg作为MainScene&类&(其实是表)的一个变量。
2.定义一个Layer
Board是我定义的一个层,添加在MainScene上。
定义层的方法为:
在Board.lua文件 中
local Board = class("Board", function()
return display.newLayer()
return Board
大家可以到framework下看看class是如何实现的。
3.如何增加touch事件
3.1在 Board:actor中增加以下代码&
self:addTouchEventListener(handler(self, self.onTouch))
self:setNodeEventEnabled(true)
3.2 在onEnter,onExit中分别设置和移除相关事件监听
function Board:onEnter()
self:setTouchEnabled(true)
function Board:onExit()
self:removeAllEventListeners()
3.3 在Board:onTouch中处理事件
function Board:onTouch(event, x, y)
//TODO 处理点击事件
二。定义数据&
我使用一个2维的表来描述整个棋盘(也可以使用一维表)
myBoard = {{"-","-","-"},
{"-","-","-"},
{"-","-","-"}}
theWiner = "-1"
myBoard即棋盘,&-1&表示没有棋子,&X&表示有&X&形棋子,&O&表示有&O&型棋子。
theWiner表示获胜者,初始为-1。
三。程序流程
1.玩家点击事件后,在相应的位置放置棋子,并修改myBoard数据
比如简单,直接附代码了,写的比较粗糙,因为 也刚学Lua才两三天。
turn = "O"
function Board:makeMove(x,y)
if theWiner ~= "-1" then
row,co = self:getBoardLocation(x,y)
if row == -1 then
self:makeEle(row,co)
function Board:makeEle(row,co)
local file = "piece_o.png"
if turn == "X" then
file = "piece_x.png"
file = "piece_o.png"
myBoard[row][co] =
self.ele = display.newSprite(file, display.cx+100*(co-2)
, display.cy+100*(2-row))
self:addChild(self.ele)
local ret = Board:winCheck(row,co)
print("winCheck",ret)
if ret == "O" then
self.lable:setString("O is the winer")
if ret == "X" then
self.lable:setString("X is the winner")
if ret == "He" then
self.lable:setString("No one is the winner")
if ret == "Wh" then
self.lable:setString("Continue")
if turn == "X" then
turn = "O"
turn = "X"
function Board:getBoardLocation(x,y)
if x & display.cx-150 or x &display.cx+150 then
if y & display.cy+150 or y & display.cy-150 then
if x &= display.cx - 50
elseif x & display.cx-50 and x & display.cx+50 then
if y &= display.cy - 50
elseif y & display.cy-50 and y & display.cy+50 then
return row,co
2.检查玩家是否获胜或平局
function Board:winCheck(row,co)
local cur = myBoard[row][co]
if myBoard[1][2] == cur and myBoard[1][3] == cur and myBoard[1][1] ==cur
return cur
if myBoard[2][2] == cur and myBoard[2][3] == cur and myBoard[2][1]
== cur then
return cur
if myBoard[3][2] == cur and myBoard[3][3] == cur and myBoard[3][1] == cur then
return cur
if myBoard[1][1] == cur and myBoard[2][1] == cur and myBoard[3][1] ==
return cur
if myBoard[1][2] == cur and myBoard[2][2] == cur and myBoard[3][2] == cur then
return cur
if myBoard[1][3] == cur and myBoard[2][3] == cur and myBoard[3][3] == cur then
return cur
if myBoard[1][1] == cur and myBoard[2][2] == cur and myBoard[3][3] == cur
return cur
if myBoard[1][3] == cur and myBoard[2][2] == cur and myBoard[3][1] == cur then
return cur
open = true;
for i = 1,3 do
for j = 1,3 do
if myBoard[i][j] == "-" then
open = false
if open then
return "He"
return "Wh"
搞了一天,有点累了,写的不详细,有问题请大家在评论里问吧豆丁微信公众号
君,已阅读到文档的结尾了呢~~
金乡儿童棋类游戏,儿童棋类游戏,战棋类单机游戏,战棋类游戏,策略战棋类单机游戏,棋类游戏,ios战棋类游戏,战棋类单机游戏排行榜,战棋类游戏排行,好玩的战棋类游戏
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
金乡儿童棋类游戏
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口在校麻瓜,初级扫帚飞行师
Cocos2d-x 触摸事件的创建、优先级和吞噬
触摸优先级
触摸事件的优先级,值越小,优先级越高,比如层1的优先级是-50,层2的优先级是-100,那么层2会先接收并处理触摸事件,然后才有机会轮到层1来接收处理。
注意:这是针对2.x版本,直接与层的Z-Order相关,不需要再设优先级priority,Z-Order越大,则越先触发,越小则越晚触发。
吞噬触摸事件
swallow事件,表示当前层是否吞噬掉这个触摸,即不往优先级更低的层传送。
创建触摸监听器
创建一个单点触碰监听器
auto listener = EventListenerTouchOneByOne::create();
配置触摸监听器的属性,设置监听是否吞并事件
listener-&setSwallowTouches(true);
设置监听器的回调函数
listener-&onTouchBegan = CC_CALLBACK_2(MyUIDrama, this);
listener-&onTouchMoved = CC_CALLBACK_2(MyUIDrama, this);
listener-&onTouchEnded = CC_CALLBACK_2(MyUIDrama, this);
onTouchBegan:触碰按下时调用
onTouchMoved:触碰移动时调用
onTouchEnded:触碰弹起时调用
添加触摸监听器
有两种方法
方法一:addEventListenerWithFixedPriority
Director::getInstance()-&getEventDispatcher()-&addEventListenerWithFixedPriority(listener, 1);
addEventListenerWithFixedPriority有两个参数:
参数1:Listenter 监听器
参数2:fixedPriority 固定优先级,其中0是系统占有,不能设置为0
方法二:addEventListenerWithSceneGraphPriority
Director::getInstance()-&getEventDispatcher()-&addEventListenerWithSceneGraphPriority(listener, this);
addEventListenerWithSceneGraphPriority有两个参数:
参数1:Listenter 监听器
参数2:node 根据node来确定监听器的优先级
两种方法的区别就是一个是自己手动指定Listenter的优先级,一个是根据node来决定。
没有更多推荐了,茉莉公主井字棋小游戏,在线玩,4399小游戏
选择你喜欢的角色,将三个同样的头像连成一条线,以获得胜利。
游戏加载完毕点击Play Game - 接着点击Player vs Computer/Prince vs Princess即可开始游戏
英文名:Jasmine Tic Tac Toe
分类:专题:大小:1.55M日期:
精品游戏推荐
(本游戏合适16岁以上用户)
游戏加载完毕点击Play Game - 接着点击Player vs Computer/Prince vs Princess即可开始游戏
选择你喜欢的角色,将三个同样的头像连成一条线,以获得胜利。
茉莉公主和王子快乐的生活在一起,他们每天都要玩游戏,今天他们准备玩井字棋,我们一起来看看吧~看谁能赢到最后!
文明办网文明上网举报、纠纷处理及不良内容举报电话:(转6) |&举报邮箱:&|
Copyright & 2004 -
4399.com All Rights Reserved. 四三九九网络股份有限公司&版权所有
微信扫一扫关注4399小游戏for happy,learning
C#-wpf一个简单的井字棋程序
好久没有碰过代码了,上次看C#估计还是上个学期233,这次老师突然布置了一个C#语言编写的井字棋程序让我有点措手不及.不过,仗着微软爸爸的vs的强大的代码补全能力,最后还是将这个程序做好了(遇到了很多坑).
该程序的要求还是有点多了,以下一一列举:
1. 在程序初始化时,要求输入玩家用户名,输入后出现提示文字,并选择是先手还是后手.
2. 点击Fight!后,进入游戏界面,在九宫格中下一步棋后,电脑(敌人)会判断形势并下第二部棋,如此直至有三子连成一线(玩家或电脑赢),或者格子被占满(和局).旁边有提示按钮,点击后会出现提示;得分按钮会出现具体的得分界面;退出按钮则会退出程序
3. 得分界面,点开后会显示自己的得分情况.最高得分按钮会记录玩家玩这个游戏以来所得的最高分;打印按钮则会打印自己的具体得分情况
首先制作打开程序时出现的”登录”界面,即该程序的mainwindow:
这里要注意的是,只有当输入完姓名后才能出现下面的提示文字等控件,所以应事先将其属性定为隐藏,点击”commit”按钮后触发事件
private void btm_commit_click(object sender, RoutedEventArgs e)
if (TBox1.Text.Length&0&&TBox1.Text.Length&10)//判断玩家输入的文字长度,更为严谨的做法应该是判断字母+数字的长度,以及玩家输入的字符串不符合时应出现提示信息
storage.name = TBox1.Text
TBlock2.Text = "Hi " + storage.name + ". Welcome to the battlefield!Now,lets begin the game!!!Hey,do you want to place a bomb first?"
TBlock2.Visibility = Visibility.Visible
RB1.Visibility = Visibility.Visible
RB2.Visibility = Visibility.Visible
btm_fight.Visibility = Visibility.Visible
btm_commit.IsEnabled = false
在玩家确定好是否先手后,就该点击Fight!按钮进入游戏界面了
private void to_main_page_click(object sender, RoutedEventArgs e)
if(RB1.IsChecked==true)
RButton = true;
this.Content = new mainpage();
else if (RB2.IsChecked == true)
RButton = false;
this.Content = new mainpage();
TBcheck.Visibility = Visibility.V
其次,开始制作井字棋程序最复杂的游戏部分,即mainpage
在拖好控件后,自然要去实现他们的功能.首先是九宫格,我的想法是玩家(假设是玩家先手,并永远下”X”棋)下出第一步棋后,按钮打印出”X”字母,并且在后台的对应于该按钮的值变为1;而电脑(永远下”O”棋)下棋后,按钮打印出”O”字母,并且在后台的对应于该按钮的值变为-1;而没有下任何棋的按钮则不会打印出任何字母,并且在后台的对应于该按钮的值为0.
private void Btm0_Click(object sender, RoutedEventArgs e)//按钮点击触发事件,即玩家点击后的结果.9个按钮均与此类似
if (btm[btm0]==0)
btm0.Content="X";
btm[btm0] = 1;
computer();
三字连成一线的情况总共有8种,为了更快速的判断是否出现了特殊情况(两子一线,三子一线),可以将这些情况列成一个8行3列的数组以供判断函数或者电脑使用
private void update()
array_for_judge[0] = new int[3] { btm[btm0], btm[btm1], btm[btm2] };
array_for_judge[1] = new int[3] { btm[btm3], btm[btm4], btm[btm5] };
array_for_judge[2] = new int[3] { btm[btm6], btm[btm7], btm[btm8] };
array_for_judge[3] = new int[3] { btm[btm0], btm[btm3], btm[btm6] };
array_for_judge[4] = new int[3] { btm[btm1], btm[btm4], btm[btm7] };
array_for_judge[5] = new int[3] { btm[btm2], btm[btm5], btm[btm8] };
array_for_judge[6] = new int[3] { btm[btm0], btm[btm4], btm[btm8] };
array_for_judge[7] = new int[3] { btm[btm2], btm[btm4], btm[btm6] };
每次玩家或者电脑走完一步棋后,都得判断是否出现赢或输或平局的情况,若出现,将刷新棋盘并记录信息
private void judge()//判断函数
foreach (int[] tmp in array_for_judge)
if (Array.TrueForAll(tmp, isone =& (isone == 1)) == true)
MessageBox.Show("you win!");
storage.battle++;
storage.win++;
else if (Array.TrueForAll(tmp, isone =& (isone == -1)) == true)
MessageBox.Show("you lose!");
storage.battle++;
storage.lose++;
if (btm.ContainsValue(0) == false)
MessageBox.Show("draw!");
storage.battle++;
storage.tie++;
电脑也需要根据棋盘的形势下棋,在此列出3种情况:
1. 当电脑的棋子已有2子一线,并且该线无第3子时,下第3子取得胜利
2. 当玩家的棋子已有2子一线,并且该线无第3子时,下第3子阻止玩家取得胜利
3. 当无以上情况时,随机选择并落子
private int computer()//返回值是为了跳出该函数用的,并无太大意义
if (btm.ContainsValue(1) == false&&btm.ContainsValue(-1)==false)
for (int i = 0; i & array_for_judge.L i++)
if (array_for_judge[i].Sum() == -2)
int j = Array.IndexOf(array_for_judge[i], 0);
btm[to_dic_int(i, j)] = -1;
to_dic_int(i, j).Content()="O";
if (array_for_judge[i].Sum() == 2)
int j = Array.IndexOf(array_for_judge[i], 0);
btm[to_dic_int(i, j)] = -1;
to_dic_int(i, j).Background = storage.getImage2();
Random random = new Random();
while (true)无限循环直至随机落子并且该按钮的值为0
int tmp = random.Next(24);
int itmp = tmp % 8;
int jtmp = tmp % 3;
if (array_for_judge[itmp][jtmp] == 0)
btm[to_dic_int(itmp, jtmp)] = -1;
to_dic_int(itmp, jtmp).Background = storage.getImage2();
还要设置电脑先手的情况
if (MainWindow.RButton == false)
first_computer();
Show Help按钮
private void btm_tips_click(object sender, RoutedEventArgs e)
if (text_tips.Text == "")
text_tips.Text = "Play The Battle of Waterloo! Start by placing bombs. The enemy will situate a bomb after you.";
btm_tips.Content = "Hide Help";
text_tips.Text = "";
btm_tips.Content = "Show Help";
Show Score按钮
private void main_to_score(object sender, RoutedEventArgs e)
score_page score = new score_page();
score.Show();
private void Exit_click(object sender, RoutedEventArgs e)
Application.Current.Shutdown();
最后,制作井字棋的得分界面,即score_page
拖几个文本块的事情…值得注意的是,右边的分数需事先读取玩家数据并将其显示出来
Score_player.Text = storage.name
Score_battles.Text = storage.battle.ToString()
Score_win.Text = storage.win.ToString()
Score_lose.Text = storage.lose.ToString()
Score_tie.Text = storage.tie.ToString()
Top Score和Print按钮的功能我还没有实现.Top Score应该是事先读取存储文件中的最高分的数据并用文本块显示出来;而Print则应弹出文件资源管理器让玩家选择保存数据的位置,然后将其保存
这个程序我搞了很长时间才让其具备初始的功能,仍然有许多不完善的地方,比如界面的设计啦,数值的绑定啦等等.不过在这次编写C#程序后可能我的下一次编写就又不知道是什么时候了233,因此我希望在这几天完善完善比较好.
其实老师的要求是埋下地雷而不是井字棋,游戏的名称叫做”滑铁卢战役”,实际上和井字棋一摸一样-.-,附上改”X”“O”为地雷图片的图:
没有更多推荐了,

我要回帖

更多关于 井字棋游戏 的文章

 

随机推荐