极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12666|回复: 8

发一个processing写的英雄连连看。请大家雅俗共赏。

[复制链接]
发表于 2013-3-29 13:08:20 | 显示全部楼层 |阅读模式
{:soso_e113:}
本来打算就看看的,结果好奇之下就做了一个连连看,算法十分简陋,请算法厉害的帮忙改进。指出错误。
在此感谢那些为我免费纠错的朋友~
此版本为0.01版本,含有大量位置bug.
但不影响演示。
[pre lang="processing" line="1"]
PImage []img;
int [][]map;
int []testMap;
int []testMap2;
Point []CanConnectA;
Point []CanConnectB;
Point []CanConnectC;
Point []CanConnectW;
CPoint point;
int buttonMouse=0;
int x1, y1, x2, y2;
String temp;
boolean GameWin;
class Point
{
  int x;
  int y;
}
class CPoint
{
  int cx;//Screen coordinates
  int cy;//Screen coordinates
  int x;//Logical coordinates
  int y;//Logical coordinates
  CPoint()
  {
    cx=0;
    cy=0;
    x=0;
    y=0;
  }
  void ScreenToLogical(int cx, int cy)
  {
    if (cx<600&&cy<400)
    {
      x=cx/50;
      y=cy/50;
    }
  }
}
////////////////////////////////////////////////////////////setup
void setup()
{
  size(640, 400);
  background(0);
  textFont(createFont("MicrosoftYaHei-80", 10));
  point =new CPoint();
  CanConnectA=new Point[100];
  CanConnectB=new Point[100];
  CanConnectC=new Point[100];
  CanConnectW=new Point[100];
  for (int i=0;i<100;i++)
  {
    CanConnectA=new Point();
    CanConnectB=new Point();
    CanConnectC=new Point();
    CanConnectW=new Point();
  }
  Arrary_Null(CanConnectA);
  Arrary_Null(CanConnectB);
  Arrary_Null(CanConnectC);
  Arrary_Null(CanConnectW);
  x1=0;
  y1=0;
  x2=0;
  y2=0;

  img=new PImage[12];
  for (int i=1;i<12;i++)
  {
    temp=i+".png";
    img=loadImage(temp);
  }
  InitGame();
}
void draw()
{
  DrawMap();
}
void InitGame()
{
  while (true)
  {
    InitMap();
    if (TestMapIsOk())
    {
      break;
    }
  }
}
void GameMain()
{
  if (IsAWiner())
  {
    GameOver();
  }
  if (buttonMouse==1)
  {
    x1=point.x;
    y1=point.y;
    println(x1+","+y1+":"+map[y1][x1]);
  }
  if (buttonMouse==2)
  {
    x2=point.x;
    y2=point.y;
    println(x2+","+y2+":"+map[y2][x2]);
    if (map[y1][x1]==map[y2][x2]&&!((x1==x2)&&(y1==y2))&&(IsCanConnect(x1, y1, x2, y2)))
    {
      DrawALine(x1, y1, x2, y2);

      println(map[y1][x1]+":"+map[y2][x2]);
      map[y1][x1]=0;
      map[y2][x2]=0;

      x1=0;
      y1=0;
      x2=0;
      y2=0;
      DrawMap();
      background(0);

      buttonMouse=0;
    }
    else
    {
      x1=0;
      y1=0;
      x2=0;
      y2=0;
      background(0);
      buttonMouse=0;
    }
  }
}
void InitMap()
{
  map = new int[8][12];
  testMap=new int[12];
  int temp=0;
  boolean bottonRandom=false;
  for (int i=0;i<8;i++)
  {
    for (int j=0;j<12;j++)
    {
      if (!bottonRandom)
      {
        temp=(int)random(1, 11);
      }
      else
      {
        temp++;
        if (temp>11)
        {
          temp=1;
        }
      }
      testMap[temp]++;
      if (testMap[temp]>8)
      {
        map[j]=temp;
      }
      else
      {
        map[j]=(int)random(1, 11);
      }
    }
  }
}
void DrawMap()
{
  for (int i=0;i<8;i++)
  {
    for (int j=0;j<12;j++)
    {
      if (map[j]==0)
      {
        continue;
      }
      image(img[map[j]], 0+j*50, 0+i*50, 50, 50);
    }
  }
}
boolean TestMapIsOk()
{
  testMap2=new int[12];
  for (int i=0;i<8;i++)
  {
    for (int j=0;j<12;j++)
    {
      testMap2[map[j]]++;
    }
  }
  for (int i=1;i<12;i++)
  {
    if (((testMap2)%2)!=0)
    {
      return false;
    }
  }
  return true;
}
boolean IsCanConnect(int x1, int y1, int x2, int y2)
{
  if (map[y1][x1]==map[y2][x2]&&map[y1][x1]!=0)
  {
    println("come in iscan");
    FindAllCanConnect(x1, y1, CanConnectA);
    for (int i=0;i<CanConnectA.length;i++)
    {
      FindAllCanConnect(CanConnectA.x, CanConnectA.y, CanConnectB);
    }
    for (int i=0;i<CanConnectB.length;i++)
    {
      FindAllCanConnect(CanConnectB.x, CanConnectB.y, CanConnectC);
    }
    for (int i=0;i<CanConnectW.length;i++)
    {
      if (CanConnectW.x==-1&&CanConnectW.y==-1)
      {
        break;
      }
      println("CanConnectWx:"+CanConnectW.x+"CanConnectWy:"+CanConnectW.y);
      if (CanConnectW.x==x2&&CanConnectW.y==y2)
      {
        return true;
      }
    }
    Arrary_Null(CanConnectA);
    Arrary_Null(CanConnectB);
    Arrary_Null(CanConnectC);
    Arrary_Null(CanConnectW);
  }
  return false;
}
void DrawALine(int x1, int y1, int x2, int y2)
{
}
void mousePressed()
{

  if (mouseButton==LEFT)
  {
    point.ScreenToLogical(mouseX, mouseY);
    buttonMouse++;
    if (buttonMouse<3&&buttonMouse>0)
    {
      GameMain();
      fill(0, 0, 255);
      rect(point.x*50, point.y*50, 50, 50);
      DrawMap();
    }
  }
}
void FindAllCanConnect(int x, int y, Point []CanConnect)
{
  if (x<0||y<0)
  {
    // println("x:"+x+"y:"+y);
  }
  else
  {
    println("come in FindAll");
    for (int i=y-1;i>0;i--)//shang
    {
      if (map[x]==0)
      {
        Arrary_Add(x, i, CanConnect);
      }
      else
        if (map[x]==map[y2][x2])
        {
          println("come in FindAll shang");
          Arrary_Add(x, i, CanConnectW);
          break;
        }
        else
        {
          break;
        }
    }
    for (int i=y+1;i<8;i++)//xia
    {
      if (map[x]==0)
      {
        Arrary_Add(x, i, CanConnect);
      }
      else
        if (map[x]==map[y2][x2])
        {
          println("come in FindAll xia");
          Arrary_Add(x, i, CanConnectW);
          break;
        }
        else
        {
          break;
        }
    }
    for (int i=x-1;i>0;i--)//zuo
    {
      if (map[y]==0)
      {
        Arrary_Add(i, y, CanConnect);
      }
      else
        if (map[y]==map[y2][x2])
        {
          println("come in FindAll zuo");
          Arrary_Add(i, y, CanConnectW);
          break;
        }
        else
        {
          break;
        }
    }
    for (int i=x+1;i<12;i++)//you
    {
      if (map[y]==0)
      {
        Arrary_Add(i, y, CanConnect);
      }
      else
        if (map[y]==map[y2][x2])
        {
          println("come in FindAll you"+map[y]+"--"+map[y2][x2]);
          Arrary_Add(i, y, CanConnectW);
          break;
        }
        else
        {
          break;
        }
    }
  }
}
void Arrary_Add(int x, int y, Point []CanConnect)
{
  int end=0;
  for (int i=0;i<CanConnect.length;i++)
  {
    if (CanConnect.x==-1&&CanConnect.y==-1)
    {
      end=i;
      break;
    }
  }

  CanConnect[end].x=x;
  CanConnect[end].y=y;
  println("end:"+end+"CanConnect[end]"+CanConnect[end].x+"  "+CanConnect[end].y);
}
void Arrary_Null(Point []CanConnect)
{
  for (int i=0;i<CanConnect.length;i++)
  {
    CanConnect.x=-1;
    CanConnect.y=-1;
  }
}
boolean IsAWiner()
{
  for (int i=0;i<8;i++)
  {
    for (int j=0;j<12;j++)
    {
      if (map[j]!=0)
      {

        return false;
      }
    }
  }
  return true;
}
void GameOver()
{
  fill(200, 0, 100);
  textSize(80);
  text("You Win!", 160, 160);
  fill(100, 200, 100);
  rect(160, 200, 150, 80);
  rect(330, 200, 150, 80);
  fill(0, 0, 100);
  textSize(60);
  text("Again", 160, 260);
  text("  Exit", 330, 260);
  if (point.x>160&&point.x<310&&point.y>200&&point.y<280)
  {
    InitGame();
    println("again");
  }
  if (point.x>330&&point.x<480&&point.y>200&&point.y<280)
  {

    println("exit");
    exit();
  }
}
[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-3-29 13:21:20 | 显示全部楼层
{:soso_e142:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-15 15:38:23 | 显示全部楼层
。。 发表于 2013-3-29 13:21

嘿嘿嘿,谢谢啦
回复 支持 反对

使用道具 举报

发表于 2013-5-8 13:57:13 | 显示全部楼层
把运行的截图视频什么的贴上来,会大大增加坛友的兴趣
回复 支持 反对

使用道具 举报

发表于 2013-9-2 10:53:32 | 显示全部楼层
我下载了  可是反正编译器里 不显示英雄
回复 支持 反对

使用道具 举报

发表于 2013-9-2 11:24:39 | 显示全部楼层
解决了 哈哈
挺好玩的  确实有挺多的BUG 好多的英雄都消不掉
支持楼主!!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-9-4 20:43:34 | 显示全部楼层
软件-蹄飞 发表于 2013-9-2 11:24
解决了 哈哈
挺好玩的  确实有挺多的BUG 好多的英雄都消不掉
支持楼主!!

哈哈,当时就是试着写写,很多算法bug都没改。
回复 支持 反对

使用道具 举报

发表于 2014-6-23 18:08:28 | 显示全部楼层
yangfanconan你真的是个有趣的人呢~竟然真的用强于交互的processing去做游戏,哈哈~不过很好玩呢,有机会多交流吧~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-7-3 14:32:53 | 显示全部楼层
linkong 发表于 2014-6-23 18:08
yangfanconan你真的是个有趣的人呢~竟然真的用强于交互的processing去做游戏,哈哈~不过很好玩呢,有机会多 ...

哈哈。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 19:08 , Processed in 0.042306 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表