⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 env.cs

📁 windows mobile上开发的推箱子小游戏,用C#写的,虽然只是DEMO版但大致功能已经实现
💻 CS
📖 第 1 页 / 共 2 页
字号:
276    /// 保存设计数据
277    /// </summary>
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    /**//// <summary>
292    /// 删除最后一关
293    /// </summary>
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    /**//// <summary>
308    /// 更新主窗体客户区
309    /// </summary>
310    /// <param name="dc">画布</param>
311    /// <param name="rectangle">要在其中绘画的矩形</param>
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    /**//// <summary>
328    /// 绘制一个单元格
329    /// </summary>
330    /// <param name="dc">画布</param>
331    /// <param name="x">单元格的横坐标</param>
332    /// <param name="y">单元格的纵坐标</param>
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    /**//// <summary>
339    /// 绘制一个单元格
340    /// </summary>
341    /// <param name="dc">画布</param>
342    /// <param name="idx">单元格的类型: 地 槽 墙 砖 箱子 工人</param>
343    /// <param name="x">单元格的横坐标</param>
344    /// <param name="y">单元格的纵坐标</param>
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    /**//// <summary>
351    /// 将单元格换算为像素
352    /// </summary>
353    /// <param name="box">单元格矩形</param>
354    /// <returns>像素矩形</returns>
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    /**//// <summary>
362    /// 将像素换算为单元格
363    /// </summary>
364    /// <param name="pixel">像素矩形</param>
365    /// <returns>单元格矩形</returns>
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    /**//// <summary>
376    /// 根据指定的对角顶点创建矩形
377    /// </summary>
378    /// <param name="a">顶点</param>
379    /// <param name="b">对角的顶点</param>
380    /// <returns>所需要的矩形</returns>
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    /**//// <summary>
387    /// 设计模式下,当鼠标点击时要采取的动作
388    /// </summary>
389    /// <param name="invalid">输出:要重绘的区域</param>
390    /// <returns>是否发生动作</returns>
391    public bool Design(out Rectangle invalid)
392    {
393      invalid = Rectangle.Empty;
394      Point to;
395      if (!ValidClick(out to)) return false;
396      db.UpdateCounts(to.X, to.Y, false);
397      Block.Update(ref db.Map[to.Y, to.X], pen);
398      db.UpdateCounts(to.X, to.Y, true);
399      if (pen == Block.Man0 && HasWorker) pen = Block.Box0;
400      invalid = BoxToPixel(GetRectangle(to, to));
401      return true;
402    }
403
404    /**//// <summary>
405    /// 工人往指定方向前进一步(可能推着箱子)
406    /// </summary>
407    /// <param name="dir">前进的方向</param>
408    /// <param name="isStop">“撤销”时是否停留</param>
409    /// <param name="invalid">输出:要重绘的区域</param>
410    /// <returns>是否成功</returns>
411    public bool StepIt(Direction dir, bool isStop, out Rectangle invalid)
412    {
413      invalid = Rectangle.Empty;
414      if (HasError) return false;
415      if (Direction.None == dir) return false;
416      Point p1 = worker; // 工人前进方向一步的位置
417      Point p2 = worker; // 箱子前进方向一步的位置
418      switch (dir)
419      {
420        case Direction.East: p1.X++; p2.X += 2; break;
421        case Direction.South: p1.Y++; p2.Y += 2; break;
422        case Direction.West: p1.X--; p2.X -= 2; break;
423        case Direction.North: p1.Y--; p2.Y -= 2; break;
424      }
425      byte b1 = db.Map[p1.Y, p1.X]; // 工人前进方向一步位置上的东西
426      bool isBox = Block.IsBox(b1); // 是否推着箱子前进
427      if (!isBox && !Block.IsBlank(b1)) return false; // 如果没有推着箱子且前方不是空地则失败
428      if (isBox && !Block.IsBlank(db.Map[p2.Y, p2.X])) return false; // 如果推着箱子且箱子前方不是空地则失败
429      invalid = BoxToPixel(GetRectangle(worker, isBox ? p2 : p1)); // 要重绘的区域
430      stack.Push(new Step(dir, isBox, isStop)); // 记录走法步骤
431      Block.ManOut(ref db.Map[worker.Y, worker.X]); // 工人离开当前位置
432      Block.ManIn(ref db.Map[p1.Y, p1.X]); // 工人进入前方位置
433      if (isBox)
434      {
435        pushSteps++; // 更新推箱子步数
436        db.Boths += (db.Map[p2.Y, p2.X] - Block.Land) - (b1 - Block.Box0); // 更新已完成任务数
437        Block.BoxOut(ref db.Map[p1.Y, p1.X]); // 箱子离开当前位置
438        Block.BoxIn(ref db.Map[p2.Y, p2.X]); // 箱子进入前方位置
439      }
440      worker = p1; // 更新工人位置
441      return true; // 工人成功前进一步(可能推着条子)
442    }
443
444    /**//// <summary>
445    /// 工人后退一步(可能连带箱子一起后退)
446    /// </summary>
447    /// <param name="invalid">输出:要重绘的区域</param>
448    /// <returns>是否完成“撤消”</returns>
449    public bool Back(out Rectangle invalid)
450    {
451      invalid = Rectangle.Empty;
452      if (HasError) return true;
453      if (stack.Count == 0) return true;
454      Step step = stack.Pop(); // 当前步骤
455      Point p1 = worker; // 工人后退方向一步的位置
456      Point p2 = worker; // 箱子的当前位置
457      switch (step.Direct)
458      {
459        case Direction.East: p1.X--; p2.X++; break;
460        case Direction.South: p1.Y--; p2.Y++; break;
461        case Direction.West: p1.X++; p2.X--; break;
462        case Direction.North: p1.Y++; p2.Y--; break;
463      }
464      invalid = BoxToPixel(GetRectangle(p1, step.IsBox ? p2 : worker)); // 要重绘的区域
465      Block.ManOut(ref db.Map[worker.Y, worker.X]); // 工人离开当前位置
466      Block.ManIn(ref db.Map[p1.Y, p1.X]); // 工人进入后退方向一步的位置
467      if (step.IsBox)
468      {
469        Block.BoxOut(ref db.Map[p2.Y, p2.X]); // 箱子离开当前位置
470        Block.BoxIn(ref db.Map[worker.Y, worker.X]); // 箱子进入工人原来的位置
471        db.Boths += (db.Map[worker.Y, worker.X] - Block.Box0) - (db.Map[p2.Y, p2.X] - Block.Land); // 更新已完成任务数
472        pushSteps--; // 更新推箱子步数
473      }
474      worker = p1; // 更新工人位置
475      return step.IsStop; // 是否完成“撤消”
476    }
477
478    /**//// <summary>
479    /// 寻找一条将工人移动到鼠标点击的位置的路线
480    /// </summary>
481    /// <returns>移动的路线</returns>
482    public Queue<Direction> GetMoveInfo()
483    {
484      Point to;
485      if (!CanTo(out to)) return null;
486      return FindPath.Seek(db.Map, worker, to);
487    }
488
489    /**//// <summary>
490    /// 给出将箱子推动到鼠标点击的位置所需的信息
491    /// </summary>
492    /// <param name="dir">输出:工人移动的方向</param>
493    /// <returns>工人移动的步数</returns>
494    public int GetPushInfo(out Direction dir)
495    {
496      dir = Direction.None;
497      if (HasError) return 0;
498      Point to; // 目的地
499      if (!CanTo(out to)) return 0; // 无效的目的地
500      if (to.Y != worker.Y && to.X != worker.X) return 0; // 目的地和工人不在同一条直线上
501      int z0 = (to.Y == worker.Y) ? worker.X : worker.Y;
502      int z9 = (to.Y == worker.Y) ? to.X : to.Y;
503      if (to.Y == worker.Y) dir = (z9 > z0) ? Direction.East : Direction.West;
504      else dir = (z9 > z0) ? Direction.South : Direction.North;
505      int i0 = Math.Min(z9, z0);
506      int i9 = Math.Max(z9, z0);
507      int steps = i9 - i0; // 目的地和工人之间的距离
508      int boxs = 0;
509      for (int i = i0 + 1; i < i9; i++)
510      {
511        byte bi = (to.Y == worker.Y) ? db.Map[worker.Y, i] : db.Map[i, worker.X];
512        if (Block.IsBox(bi)) boxs++; // 计算工人和目的地之间的箱子的个数
513        else if (!Block.IsBlank(bi)) boxs += 2; // “墙”和“砖”折算为两个箱子
514      }
515      if (boxs > 1) return 0; // 最多只能推着一个箱子前进
516      return steps - boxs; // 工人移动的步数
517    }
518
519    /**//// <summary>
520    /// 检查鼠标点击位置是否可达, 并将像素换算为单元格
521    /// </summary>
522    /// <param name="to">输出:换算后的位置</param>
523    /// <returns>是否可达</returns>
524    bool CanTo(out Point to)
525    {
526      if (!ValidClick(out to)) return false;
527      if (!Block.IsMan(db.Map[worker.Y, worker.X])) throw new Exception("内部错误:工人的位置上不是工人");
528      if (!Block.IsBlank(db.Map[to.Y, to.X])) return false; // 目的地必须是“地”或“槽”
529      if (to.Y == worker.Y && to.X == worker.X) return false; // 目的地不能是工人当前的位置
530      return true; // 目的地可达
531    }
532
533    /**//// <summary>
534    /// 检查鼠标点击位置是否有效, 并将像素换算为单元格
535    /// </summary>
536    /// <param name="to">输出:换算后的位置</param>
537    /// <returns>是否有效位置</returns>
538    bool ValidClick(out Point to)
539    {
540      to = Point.Empty;
541      if (HasError) return false;
542      to.Y = toPixel.Y / boxSize.Height + 1;
543      to.X = toPixel.X / boxSize.Width + 1;
544      if (toPixel.X >= boxSize.Width * LevelSize.Width || toPixel.Y >= boxSize.Height * LevelSize.Height)
545        return false; // 目的地超出当前关的有效范围
546      return true; // 目的地有效
547    }
548  }
549}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -