
正文
连连看html代码,h5连连看源代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
C++连连看算法
////////////////////////////////////////////////////////////////////////// // LLKAg.h//////////////////////////////////////////////////////////////////////////// 版权所有// 作者:董波// 日期:2008.12.26// 简介:连连看算法//////////////////////////////////////////////////////////////////////////#ifndef _ND_LLKAG_H_ #define _ND_LLKAG_H_#if _MSC_VER 1000 #pragma once#endif#include vector // for/* 约定: 0代表空白,其它编号从1开始向后排。。。*/static const int BLANK_GRID = 0;typedef std::vector POINT2D PT2D_VEC;class CLLKAg {public:CLLKAg( int iRow = 9, int iCol = 16 );~CLLKAg();public:// 开始游戏,服务器调用void Start( int iCardNum = 20 );// 获得数据信息,用于客户端渲染,也可用于服务器获得初始化信息后下发void GetState( std::vectorint vec ) const;// 客户端调用,从网络数据获取状态 void SetState( const int* pStates, unsigned uSize );// 获得内部指针,但是不能修改,只读的const int* GetMap() const;int GetRow() const;int GetCol() const;// 是否是可消除的,是否是可连接的。 bool IsLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 判断是否已经胜利bool IsWin() const;// 清除两个棋子,客户端调用之前通常需要调用IsLinkbool ClearPair( POINT2D ptFirst, POINT2D ptSecond );// 用于调试 #if defined( _DEBUG ) || defined( DEBUG )int* GetMap_D(); // 返回内部指针void RePermutation(); // 重新排列#endif // #if defined( _DEBUG ) || defined( DEBUG )// 内部实现的函数,外部不需要调用。 protected:// 是否是同一直线连通 bool DirectLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 1直角接口连通 bool OneCornerLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 2直角接口连通bool TwoCornerLink( POINT2D ptFirst, POINT2D ptSecond ) const;private:int* m_pMap; // 用于代表地图int m_iRow; // 行数int m_iCol; // 列数};////////////////////////////////////////////////////////////////////////// // 得到地图inline const int* CLLKAg::GetMap() const{return m_pMap;}// 得到行数 inline int CLLKAg::GetRow() const{return m_iRow;}// 得到列数inline int CLLKAg::GetCol() const{return m_iCol;}#if defined( _DEBUG ) || defined( DEBUG ) // 用于调试 inline int* CLLKAg::GetMap_D(){return m_pMap;}#endif // #if defined( _DEBUG ) || defined( DEBUG )#endif // #ifndef _ND_LLKAG_H_////////////////////////////////////////////////////////////////////////// // LLKAg.h//////////////////////////////////////////////////////////////////////////// 版权所有// 作者:董波// 日期:2008.12.26// 简介:连连看算法实现//////////////////////////////////////////////////////////////////////////// 使用MFC的时候请解注释下面这行: #include "stdafx.h"#include "LLKAg.h"#include cassert #include stdexcept#include ctime#include algorithm/* 一些模板函数*/template class T T _max( T lhs, T rhs ){return lhs rhs ? lhs : rhs;}template class T T _min( T lhs, T rhs ){return lhs rhs ? lhs : rhs;}// 构造,初始化一些内存 CLLKAg::CLLKAg( int iRow /* = 9 */, int iCol /* = 16 */ ):m_iRow(iRow),m_iCol(iCol),m_pMap(NULL){assert( m_iRow * m_iCol = 2 );m_pMap = new int[ m_iRow*m_iCol ];if( NULL == m_pMap ) { throw std::bad_alloc( "内存分配失败" );}memset( m_pMap, BLANK_GRID, m_iCol * m_iRow );}CLLKAg::~CLLKAg() {if( NULL != m_pMap ){ delete [] m_pMap;}}// 服务器调用,这将随机生成一个棋盘(地图、桌面) void CLLKAg::Start( int iCardNum /* = 20 */){assert( ( m_iCol * m_iRow % 2 == 0 ) "必须为偶数" );// 根据系统时间初始化种子 srand( static_castunsigned( time(NULL) ) );// 初始化的基本思想: // 成对的随机填充,然后随机打乱。int iSize = m_iCol * m_iRow;int i = 0;while ( i iSize ){ int iTarget = rand() % iCardNum + 1; m_pMap[i] = iTarget; ++i; m_pMap[i] = iTarget; ++i;}std::random_shuffle( m_pMap, m_pMap + iSize ); }// 客户端调用,使用网络传递来的信息来初始化游戏状态 void CLLKAg::SetState( const int* pStates, unsigned uSize ){assert( uSize == m_iRow * m_iCol * sizeof (int) );memcpy( m_pMap, pStates, uSize ); }// 得到地图状态 void CLLKAg::GetState( std::vectorint vec ) const{vec.assign( m_pMap, m_pMap + m_iRow*m_iCol );}// 判断是否已经获胜 bool CLLKAg::IsWin() const{const int iSize = m_iCol * m_iRow;for( int i=0; i iSize; ++i ){ if( BLANK_GRID != m_pMap[i] ) { return false; }}return true; }// 是否是直接连通! bool CLLKAg::DirectLink( POINT2D ptFirst, POINT2D ptSecond )const{// 根本不可能在一条直线上的时候直接返回falseif( (ptFirst.x != ptSecond.x) (ptFirst.y != ptSecond.y) ){ return false;}// 不应该是相同的点,这是不能接受的! if( (ptFirst.x == ptSecond.x) ( ptFirst.y == ptSecond.y ) ){ return false;}// 分情况// 同一x if( ptFirst.x == ptSecond.x ){ int iMin = _min( ptFirst.y, ptSecond.y ); int iMax = _max( ptFirst.y, ptSecond.y ); for( int i=iMin +1; i iMax; ++i ) { if( m_pMap[i*m_iCol + ptFirst.x] != BLANK_GRID ) { return false; } } return true; }else{ int iMin = _min( ptFirst.x, ptSecond.x ); int iMax = _max( ptFirst.x, ptSecond.x ); for( int i=iMin+1; iiMax; ++i ) { if( m_pMap[ptFirst.y*m_iCol + i] != BLANK_GRID ) { return false; } } return true; }}// 一折型的 // 就是找出矩形的另外两个顶点然后判断他们是否是直线相连的bool CLLKAg::OneCornerLink( POINT2D ptFirst, POINT2D ptSecond )const{// 函数到这里的时候应该保证两个点的x、y都互不相等,否则调用就有错// 分步骤,看第一个顶点 POINT2D ptFirstCorner = { ptFirst.x, ptSecond.y };// 两个直通的话就是相连的if( ( m_pMap[ptFirstCorner.y*m_iCol + ptFirstCorner.x ] == BLANK_GRID ) DirectLink( ptFirst, ptFirstCorner ) DirectLink( ptSecond, ptFirstCorner ) ){ return true;}// 判断第二个顶点是否是直连的 POINT2D ptSecondCorner = { ptSecond.x, ptFirst.y };if( ( m_pMap[ptSecondCorner.y*m_iCol + ptSecondCorner.x ] == BLANK_GRID ) DirectLink( ptFirst, ptSecondCorner) DirectLink( ptSecondCorner, ptSecond ) ){ return true;}return false; }// 二折型的 bool CLLKAg::TwoCornerLink( POINT2D ptFirst, POINT2D ptSecond )const{// 先扫描x方向,这是指在矩阵中横向移动// 先向左int i =0;int j =0;for( i=ptFirst.x-1; i=0; --i ){ if( m_pMap[ptFirst.y*m_iCol +i] != BLANK_GRID ) { break; } POINT2D ptOne = { i, ptFirst.y }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 再向右 for( i=ptFirst.x+1; im_iCol; ++i ){ if( m_pMap[ptFirst.y*m_iCol +i] != BLANK_GRID ) { break; } POINT2D ptOne = { i, ptFirst.y }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 扫描y方向// 向上 for( i=ptFirst.y-1; i=0; --i ){ if( m_pMap[i*m_iCol+ptFirst.x] != BLANK_GRID ) { break; } POINT2D ptOne = { ptFirst.x, i }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 向下 for( i=ptFirst.y+1; im_iRow; ++i ){ if( m_pMap[i*m_iCol+ptFirst.x] != BLANK_GRID ) { break; } POINT2D ptOne = { ptFirst.x, i }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}return false; }bool CLLKAg::IsLink( POINT2D ptFirst, POINT2D ptSecond )const {// 首先必须是放置的相同的if( m_pMap[ptFirst.y*m_iCol + ptFirst.x] != m_pMap[ptSecond.y*m_iCol + ptSecond.x] ){ return false;}// 如果是同样的点,则直接返回错误 if( ptFirst.x == ptSecond.x ptFirst.y == ptSecond.y ){ return false;}// 如果任何一个为空,也返回 if( m_pMap[ ptFirst.y*m_iCol + ptFirst.x] == BLANK_GRID || m_pMap[ ptSecond.y*m_iCol + ptSecond.x] == BLANK_GRID ){ return false;}// 如果是直线相连,就返回了 if( DirectLink( ptFirst, ptSecond ) ){ return true;}// 否则做一折检查 if( OneCornerLink( ptFirst, ptSecond ) ){ return true;}// 否则做二折检查 if( TwoCornerLink( ptFirst, ptSecond ) ){ return true;}// 什么都不符合,则返回false return false;}// 清除一对Pair,不要乱调用哦! bool CLLKAg::ClearPair(POINT2D ptFirst, POINT2D ptSecond){m_pMap[ptFirst.y*m_iCol + ptFirst.x] = m_pMap[ptSecond.y*m_iCol + ptSecond.x] = BLANK_GRID;return true; }#if defined( _DEBUG ) || defined( DEBUG ) // 用于调试 void CLLKAg::RePermutation(){std::random_shuffle( m_pMap, m_pMap + m_iCol*m_iRow );}#endif // #if defined( _DEBUG ) || defined( DEBUG )
相关问答
Q1: 请问怎样用flash制作连连看的游戏?要具体步骤。谢谢!
在设计采用单机模式,当在规定的时间内消完全部的图片则当前关卡通过,若果在规定的时间内没能消完所有的图片则游戏结束,重新开始游戏。游戏规则是模仿普通的连连看游戏,主要是鼠标两次点击的图片是否消去的问题,当前,前提是点击两张相同的图片,若点击的是同一张图片或者两张不同的图片,则不予处理。在两张相同的图片用三根以内的直线连在一起,就可以消去;否则,不予处理。
游戏过程,如果玩家在一定的时间内消完则提示玩家胜利,并进入下一关。如果在一定的时间内图片没有消完则提示玩家时间到。每关以此类推。
一、 所有图片都是按约定好的种类数和在同一区域的重复次数随机出现,并且每张图片的出现次数为偶数,时间会有限制,每一关的图片数量或时间是不同的,这样就增加了游戏的难度。
二、 在同一区域中,图片出现的种类数和重复数是可以由玩家选择的,时间由游戏约定。不过玩家选择的种类数和重复次数必须是偶数才可以顺利完成游戏,否则游戏虽然可以正常运行,但无法完成游戏。
在一种方案中,由于出现的图像按种类数和重复数都由软件约定,这样就缺乏玩家自主选择的空间,只是在完系统已经是设定好的游戏,不能改变什么,这样就在无意中降低了玩家在游戏过程中的乐趣,最后致使玩家放弃继续玩下去。我们参考了网络上的连连看游戏,考虑到游戏的娱乐性。所以我们放弃第一种方案的设计思想,参考网络上流行的连连看游戏,设计第二种方案。
3主要问题
开始制作游戏时,主要解决问题有以下几个方面:如何设置整个游戏的界面;如何控制连连看游戏中随机图片的生成且每种图片必须为偶数个;游戏开始后,判断鼠标两次点击的图片能否消去,即图片是否相同且图片之间路径的判断;如何判断游戏是否结束以及输赢问题等。
3.4技术要求
本游戏软件可以再大多数计算机上运行,游戏中能正确判断鼠标两次点下的图片是否可以消去、能正确判断游戏是否已经结束。
4、 系统设计:
针对上面的需求分析,我们把整个软件分成两个模块:1、整体界面的设计和图片的随机生成;2、图片路径判断函数;
一下就是系统结构图:
4.1基本思路
4.1.1游戏画面问题的思路
画面,对于设计者来说,可以算是最简单的地方;但对于玩家,这却是最重要的,一般玩家不会关心你是怎么实现的,他所关心的是画面的美观,漂亮,是不是能让人赏心悦目。
.2获取图片位置的思路
通过数组从图片库随即获取规定个数的图片,随机分布在画布上。图片个数一定是个偶数个。
4.1.3 路径判断的思路
连连看所要求的是:
1:两个目标是相同的
2:两个目标之间连线的折点不超过两个。(连接线由x轴和y轴的平行线组成)那么分析一下连接的情况可以看到,一般分三种情况
1:直线相连2:一个折点3:两个折点;
可以发现,如果有折点,每个折点必定有且至少有一个坐标(x或者y)是和其中一个目标点是相同的,也就是说,折点必定在两个目标点所在的x方向或y方向的直线上。
所以设计思路就是:
假设目标点p1,p2,如果有两个折点分别在z1,z2那么,所要进行的是
1:如果验证p1,p2直线连线,则连接成立
2:搜索以p1,p2的x,y方向四条直线(可能某两条直线会重合)上的有限点,每次取两点作为z1,z2,验证p1到z1/z1到z2/z2到p2是否都能直线相连,是则连接成立。
4.1.4其他问题的思路
其他功能将在后面的具体各个部分的设计过程当中分别进行介绍。
4.2主界面的设计
由于这个程序的界面并不是很复杂,所以用到的控件也不多,主要核心内容还是后台的代码设计。图片的随机生成主要是用到一个random()函数将随机数赋值给flag[ ]数组中的每个元素,然后根据数组元素值,来显示图片。
4.2.1界面的设计
1、色彩上:总结人们的视觉习惯和色彩对眼睛的健康影响,决定对于画布采用黑色背景,神秘而大方;右边的控制区采用天蓝色,配合左边纯黑的背景,就像黑夜中的蓝天,纯洁而大方。
2、功能上:背景就是窗体,右侧是一个groupbox控件,用来放置控制按钮,下方是一个grogressbar控件,用来显示时间条。
4.2.2图片的随机生成
实现这个功能要分很多个步骤:
1. 程序运行时即载入游戏需要的N张图片,默认情况下图片种类是18,重复数是4(重复数必须是偶数),并且可以选择是否重列。通过一个循环,加载随机的选择N种图片。具体载入图片的代码如下:
private void InteBmp(int maxnum)
{
g_g=this.Creatphics();
for(int i=0;iMAPWIDTH;i++)
for(int j=0;jWAPHEIGHT;j++)
gamp[i,j]=0;
IniteRandoMap(ref gamp,maxnum);
AI=new Kernal(ref gmap);
for(int i=0;imaxnum;i++)
{
ResourceManager rm=new ResourceManager(“LLK data”,Assembly,GetExecut ingAssembly() );
img[i]=(Image)rm.GetObject(i.ToString( )+”.bmp”);
//img[i]=(Image)Bitmap.FormFile(“Images\\”+(i+1). ToString( )+”.bmp”);
}
for(int i=0;i6;i++)
{
//bombimg[i]=(Image)Bitmap.FromFile(“Image\\B”+(i++). ToString( )+”.bmp”);.
}
}
2. 当确认游戏开始时,通过画图过程完成图片生成,画图的过程代码如下
private bool CheckWin(ref int[,]map)
{
Bool Win=true;
for(int i=0;i0)
{
for(int i=0;imultipic;i++)
{
Int xrandom=r.Next(19);
Int yrandom=r,Next(11);
If(map[xrandom,yrandom]==0)
{
map[xrandom,yrandom]=num;
}
else
i--;
}
num--;
}
}
private void FreshMap(ref int[,]map)
{
random r=new Random();
for (int i=0;iMAPWIDTH;i++)
for(int j=0;jMAPHEIGHT;j++)
{
if(gmap[i,j]!=0)
{
int x=r.Next(19);
int y=r.Next(11);
int temp=gmap[x,y];
gmap[x,y]=gmap[i,j]
gmap[i,j] =temp;
}
TransportMap(ref gmap);
}
private void TransportMap(ref int[,]map)
{
for (int i=0;iMAPWIDTH;i++)
for(int j=0;jMAPHEIGHT;j++)
{
AI.GiveMapValue(i,j,map[i,j]);
}
}
//在指定位置画指定图
private void Draw(Graphics g,Image scrImg,int PicxX,int PicV)
{
g.DrawImage(scrImg,new Point(PicX,PicV));
}
private void Forml_Paint(object sender,PaintEventArg e)
{
g_g.DrawLine(new.Pen(newSolidBrush(Color.DeepSkyBlue),5),0,11*34+5,19*34
,11*34+5);
If(bStart)
{
For(int i=0;i209)
{
MessageBox.Show(“游戏区域内最多只有209个孔,您选的数据太多!请重新选!”);
textBox1.Text=”18”;
textBox2.Text=”4”;
return;
}
IniteBmp(picnum);
If(bStart)
{
MessageBox.Show(“游戏已在运行!”);
return;
}
else
{
bStart=true;
this.Invalidate();
music.Play(“Sounds\\ bg-03.mid”);
}
}
重新实现代码如下:
Private void RefreshMap(ref int[,] map)
{
if ( int i=0;iMAPWIDTH;i++)
for(int jMAPHEIGHT;j++)
{
If(gmap[I,j]!=0)
{
Draw(g_g,img[gmap[I,j]-1],i*PICWIDTH,j*PICHEIGHT);
}
}
}
private void FreshMap(ref int[,] map)
{
Random r=new Random();
for(int i=0;jMAPWIDTH;i++)
for(int j=0;jMAPHEIGHT;j++)
{
if(gmap[I,j]!=0)
{
int x=r,Nex(19);
int y=r,Nex(11);
int temp=gmap[x,y];
gmap[x,y]=gmap[I,j];
gmap[I,j]=temp;
}
}
TransportMap(ref gmap);
}
Private void button2_Click(object sender,EventArgs e)
{
Refreshplayer.Play();
FreshMap(ref gmap);
This.Invalidate();
}
4.2.4得分设置
本游戏一改前人风格,采用全新计分方式,使人们在寻找相同图片的同时还注意路径的选择,更增加了游戏的趣味性,具体规则:直连得10分,一个拐点的20,两个拐点得40.用一个Label控件存储得分。
实现代码:
Switch(corner[2].X)
{
Case1;
Score+=20;//一个拐点加20;
g_g.DrawLine(pen,new Point(p1.X*31+15,p1.Y*34+17),new
Point(corner[0].X*31+15,corner[0],Y*34+17)),;
g_g.DrawLine(pen,new point(p2.X*31+15,p2.Y*34+17),new
Point(corner[0].X*31+15,corner[0],Y*34+17));
Thread.Sleep(100);
EraseBlock(g_g,p1,p2);
g_g.DrawLine(bkpen,new Point(p1.X*31+15,p1.Y*34+17)new
Point(corner[0].X*31+15,corner[0],Y*34+17);
g_g.DrawLine(bkpen,new Point(p1.X*31+15,p2.Y*34+170new
Point(corner[0].X*31+15,corner[0],Y*34+17);
break;
case 2;
score+=40;
Point[ ]ps={new Point(p1.X*31+15,p1.Y*34+17),newbr/ Point(corner[1].X*31+15,corner[1],Y*34+17),newbr/ Point(corner[0].X*31+15,corner[0],Y*34+17),newPoint(p2.X*31+15,p2.Y*34+17));br/ g_g.DrawLine(pen,ps);br/ Thread.Sleep(100);br/ EraseBlock(g_g,p1,p2);br///foreach(Point mp in ps)br///{br/ //MessageBox.Box.Show(“+mp.X.ToString( )+”,”+mp.Y.ToString( )+”)”));br///}
break;
case 0;
score+=10;
g_g.DrawLine(pen,ps);Point(corner[0].X*31+15,corner[0],Y*34+17),newPoint(p2.X*31+15,p2.Y*34+17));
Thread.Sleep(100);
EraseBlock(g_g,p1,p2);
g_g.DrawLine(pen,ps);Point(corner[0].X*31+15,corner[0],Y*34+17),newPoint(p2.X*31+15,p2.Y*34+17));
break;
default:break;
}
//RefreshMap(ref gmap)
Label5.Text=score.ToString( );
下面还有
Q2: 连连看JAVA源代码是什么?
importjavax.swing.*;\x0d\x0aimportjava.awt.*;\x0d\x0aimportjava.awt.event.*;\x0d\x0apublicclasslianliankanimplementsActionListener\x0d\x0a{\x0d\x0aJFramemainFrame;//主面板\x0d\x0aContainerthisContainer;\x0d\x0aJPanelcenterPanel,southPanel,northPanel;//子面板\x0d\x0aJButtondiamondsButton[][]=newJButton[6][5];//游戏按钮数组\x0d\x0aJButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮\x0d\x0aJLabelfractionLable=newJLabel("0");//分数标签\x0d\x0aJButtonfirstButton,secondButton;//分别记录两次被选中的按钮\x0d\x0aintgrid[][]=newint[8][7];//储存游戏按钮位置\x0d\x0astaticbooleanpressInformation=false;//判断是否有按钮被选中\x0d\x0aintx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标\x0d\x0ainti,j,k,n;//消除方法控制\x0d\x0apublicvoidinit(){\x0d\x0amainFrame=newJFrame("JKJ连连看");\x0d\x0athisContainer=mainFrame.getContentPane();\x0d\x0athisContainer.setLayout(newBorderLayout());\x0d\x0acenterPanel=newJPanel();\x0d\x0asouthPanel=newJPanel();\x0d\x0anorthPanel=newJPanel();\x0d\x0athisContainer.add(centerPanel,"Center");\x0d\x0athisContainer.add(southPanel,"South");\x0d\x0athisContainer.add(northPanel,"North");\x0d\x0acenterPanel.setLayout(newGridLayout(6,5));\x0d\x0afor(intcols=0;cols=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0awhile(grid[cols][rows]!=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0a}\x0d\x0athis.grid[cols][rows]=save[n];\x0d\x0an--;\x0d\x0a}\x0d\x0amainFrame.setVisible(false);\x0d\x0apressInformation=false;//这里一定要将按钮点击信息归为初始\x0d\x0ainit();\x0d\x0afor(inti=0;ij){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边\x0d\x0afor(i=y-1;i=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮\x0d\x0aif(grid[x][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=1说明通过了第一次验证\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0alinePassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(yx){\x0d\x0afor(n=x0;n=x+1;n--){\x0d\x0aif(grid[n][j]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[n][j]==0n==x+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0afor(i=0;ii){\x0d\x0afor(j=x-1;j=i;j--){\x0d\x0aif(grid[j][y]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0arowPassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(xy){\x0d\x0afor(n=y0;n=y+1;n--){\x0d\x0aif(grid[i][n]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[i][n]==0n==y+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0apublicvoidlinePassOne(){\x0d\x0aif(y0j){//第一按钮同行空按钮在左边\x0d\x0afor(i=y0-1;i=j;i--){//判断第一按钮同左侧空按钮之间有没按钮\x0d\x0aif(grid[x0][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=2说明通过了第二次验证\x0d\x0a}\x0d\x0a}\x0d\x0aif(y0i){\x0d\x0afor(j=x0-1;j=i;j--){\x0d\x0aif(grid[j][y0]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0a}\x0d\x0aif(x0
回答于 2022-12-14
Q3: 想用C++写一个连连看的小游戏,求思路
一下是我的思路 我也是菜鸟 愿交流
1.用一个线程来专门负责刷帧 (就是定时重绘界面)
2.用一个数组来存储游戏的数据
3.从数组来绘制画面
4.从用户输入 改变 数组
类的话应该有
GameView ---用于负责怎个游戏的绘制 里面新建线程刷帧
GameData ---用于存储游戏数据 和 改变数据
GameContrl ---由于接受用户输入
Game ---控制怎个游戏
Q4: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
关于连连看html代码和h5连连看源代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







