用 C# 开发智能手机软件:推箱子(十一)

开发者在线 Builder.com.cn 更新时间:2007-11-17作者:银河 来源:博客园

以下是引用片段:
  255 }
  256 return steps;
  257 }
  258
  259 /**//// 
  260 /// 记录通关步骤
  261 /// 
  262 public void Record()
  263 {
  264 if (HasError) return;
  265 try
  266 {
  267 db.SaveLevel(Level, stack.ToArray(), pushSteps);
  268 }
  269 catch (Exception ex)
  270 {
  271 SetExceptionMessage(ex);
  272 }
  273 }
  274
  275 /**//// 
  276 /// 保存设计数据
  277 /// 
  278 public void SaveDesign()
  279 {
  280 if (HasError) return;
  281 try
  282 {
  283 db.SaveDesign(active == Action.Create, Level);
  284 }
  285 catch (Exception ex)
  286 {
  287 SetExceptionMessage(ex);
  288 }
  289 }
  290
  291 /**//// 
  292 /// 删除最后一关
  293 /// 
  294 public void DeleteLastLevel()
  295 {
  296 if (HasError) return;
  297 try
  298 {
  299 db.DeleteLastLevel(Level);
  300 }
  301 catch (Exception ex)
  302 {
  303 SetExceptionMessage(ex);
  304 }
  305 }
  306
  307 /**//// 
  308 /// 更新主窗体客户区
  309 /// 
  310 /// 画布
  311 /// 要在其中绘画的矩形
  312 public void Draw(Graphics dc, Rectangle rectangle)
  313 {
  314 if (HasError) return;
  315 Rectangle box = PixelToBox(rectangle);
  316 Rectangle box2 = new Rectangle(box.Left, box.Top, box.Width + 1, box.Height + 1);
  317 for (int i = 1; i <= LevelSize.Height; i++)
  318 {
  319 for (int j = 1; j <= LevelSize.Width; j++)
  320 {
  321 if (!box2.Contains(j, i)) continue;
  322 DrawBox(dc, j, i);
  323 }
  324 }
  325 }
  326
  327 /**//// 
  328 /// 绘制一个单元格
  329 /// 
  330 /// 画布
  331 /// 单元格的横坐标
  332 /// 单元格的纵坐标
  333 void DrawBox(Graphics dc, int x, int y)
  334 {
  335 DrawBox(dc, db.Map[y, x], (x - 1) * boxSize.Width, (y - 1) * boxSize.Height);
  336 }
  337
  338 /**//// 
  339 /// 绘制一个单元格
  340 /// 
  341 /// 画布
  342 /// 单元格的类型: 地 槽 墙 砖 箱子 工人
  343 /// 单元格的横坐标
  344 /// 单元格的纵坐标
  345 void DrawBox(Graphics dc, int idx, int x, int y)
  346 {
  347 dc.DrawImage(img, x, y, new Rectangle(idx * boxSize.Width, 0, boxSize.Width, boxSize.Height), GraphicsUnit.Pixel);
  348 }
  349
  350 /**//// 
  351 /// 将单元格换算为像素
  352 /// 
  353 /// 单元格矩形
  354 /// 像素矩形
  355 Rectangle BoxToPixel(Rectangle box)
  356 {
  357 return new Rectangle((box.Left - 1) * boxSize.Width, (box.Top - 1) * boxSize.Height,
  358 (box.Width + 1) * boxSize.Width, (box.Height + 1) * boxSize.Height);
  359 }
  360
  361 /**//// 
  362 /// 将像素换算为单元格
  363 /// 
  364 /// 像素矩形
  365 /// 单元格矩形
  366 Rectangle PixelToBox(Rectangle pixel)
  367 {
  368 int x0 = pixel.Left / boxSize.Width + 1;
  369 int y0 = pixel.Top / boxSize.Height + 1;
  370 int x1 = (pixel.Right - 1) / boxSize.Width + 1;
  371 int y1 = (pixel.Bottom - 1) / boxSize.Height + 1;
  372 return new Rectangle(x0, y0, x1 - x0, y1 - y0);
  373 }
  374
  375 /**//// 
  376 /// 根据指定的对角顶点创建矩形
  377 /// 
  378 /// 顶点
  379 /// 对角的顶点
  380 /// 所需要的矩形
  381 Rectangle GetRectangle(Point a, Point b)
  382 {
  383 return Rectangle.FromLTRB(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Max(a.X, b.X), Math.Max(a.Y, b.Y));
  384 }
  385
  386 /**////

用户评论

  • 用户名
  • 评论内容