鱼C论坛

 找回密码
 立即注册
查看: 115|回复: 1

喋喋不休

[复制链接]
发表于 2025-4-4 15:13:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;

  5. namespace 贪吃蛇
  6. {
  7. public partial class GameForm : Form
  8. {
  9. private Timer timer;
  10. private List<Point> snake;
  11. private Point food;
  12. private string direction;
  13. private int score;

  14. public GameForm()
  15. {
  16. InitializeComponent();
  17. InitializeGame();
  18. }

  19. private void InitializeGame()
  20. {
  21. this.Width = 400;
  22. this.Height = 400;
  23. this.Text = "贪吃蛇";
  24. this.BackColor = Color.Black;

  25. timer = new Timer();
  26. timer.Interval = 100;
  27. timer.Tick += Timer_Tick;

  28. snake = new List<Point> { new Point(10, 10), new Point(10, 11), new Point(10, 12) };
  29. direction = "Up";
  30. score = 0;

  31. GenerateFood();

  32. this.KeyDown += GameForm_KeyDown;
  33. timer.Start();
  34. }

  35. private void Timer_Tick(object sender, EventArgs e)
  36. {
  37. MoveSnake();
  38. CheckCollision();
  39. this.Invalidate();
  40. }

  41. private void MoveSnake()
  42. {
  43. Point head = snake[0];
  44. Point newHead = head;

  45. switch (direction)
  46. {
  47. case "Up":
  48. newHead.Y -= 1;
  49. break;
  50. case "Down":
  51. newHead.Y += 1;
  52. break;
  53. case "Left":
  54. newHead.X -= 1;
  55. break;
  56. case "Right":
  57. newHead.X += 1;
  58. break;
  59. }

  60. snake.Insert(0, newHead);

  61. if (newHead == food)
  62. {
  63. score++;
  64. GenerateFood();
  65. }
  66. else
  67. {
  68. snake.RemoveAt(snake.Count - 1);
  69. }
  70. }

  71. private void CheckCollision()
  72. {
  73. Point head = snake[0];

  74. if (head.X < 0 || head.X >= this.Width / 10 || head.Y < 0 || head.Y >= this.Height / 10)
  75. {
  76. GameOver();
  77. }

  78. for (int i = 1; i < snake.Count; i++)
  79. {
  80. if (head == snake[i])
  81. {
  82. GameOver();
  83. }
  84. }
  85. }

  86. private void GenerateFood()
  87. {
  88. Random rand = new Random();
  89. food = new Point(rand.Next(this.Width / 10), rand.Next(this.Height / 10));
  90. }

  91. private void GameOver()
  92. {
  93. timer.Stop();
  94. MessageBox.Show("游戏结束!得分:" + score);
  95. }

  96. private void GameForm_KeyDown(object sender, KeyEventArgs e)
  97. {
  98. switch (e.KeyCode)
  99. {
  100. case Keys.W:
  101. if (direction != "Down") direction = "Up";
  102. break;
  103. case Keys.S:
  104. if (direction != "Up") direction = "Down";
  105. break;
  106. case Keys.A:
  107. if (direction != "Right") direction = "Left";
  108. break;
  109. case Keys.D:
  110. if (direction != "Left") direction = "Right";
  111. break;
  112. }
  113. }

  114. protected override void OnPaint(PaintEventArgs e)
  115. {
  116. base.OnPaint(e);
  117. Graphics g = e.Graphics;

  118. foreach (Point p in snake)
  119. {
  120. g.FillRectangle(Brushes.Green, p.X * 10, p.Y * 10, 10, 10);
  121. }

  122. g.FillRectangle(Brushes.Red, food.X * 10, food.Y * 10, 10, 10);
  123. }
  124. }
  125. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-4-4 15:14:33 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;

  5. namespace 贪吃蛇
  6. {
  7.     public partial class GameForm : Form
  8.     {
  9.         private Timer timer;
  10.         private List<Point> snake;  // 修正:添加泛型参数
  11.         private Point food;
  12.         private string direction;
  13.         private int score;

  14.         public GameForm()
  15.         {
  16.             InitializeComponent();
  17.             InitializeGame();
  18.         }

  19.         private void InitializeGame()
  20.         {
  21.             // ...(其他初始化代码保持不变)

  22.             snake = new List<Point> { new Point(10, 10), new Point(10, 11), new Point(10, 12) }; // 修正:明确泛型
  23.             // ...(其他代码保持不变)
  24.         }

  25.         private void MoveSnake()
  26.         {
  27.             Point head = snake[0];  // 修正:访问列表第一个元素
  28.             // ...(剩余代码保持不变)
  29.         }

  30.         private void CheckCollision()
  31.         {
  32.             Point head = snake[0];  // 修正:访问列表第一个元素

  33.             // 修正碰撞检测逻辑
  34.             for (int i = 1; i < snake.Count; i++)
  35.             {
  36.                 if (head.Equals(snake[i]))  // 修正:比较坐标而非引用
  37.                 {
  38.                     GameOver();
  39.                 }
  40.             }
  41.         }

  42.         private void GenerateFood()
  43.         {
  44.             Random rand = new Random();
  45.             do {
  46.                 food = new Point(rand.Next(this.ClientSize.Width / 10),
  47.                                rand.Next(this.ClientSize.Height / 10));
  48.             } while (snake.Contains(food)); // 新增:确保食物不在蛇身上
  49.         }
  50.     }
  51. }
复制代码


主要修正点:
  • 泛型集合声明:将 List snake 改为 List<Point>
  • 蛇头获取方式:使用 snake[0] 代替直接使用列表
  • 碰撞检测:使用 Equals() 比较坐标值而非引用
  • 食物生成:增加循环检测确保食物不会出现在蛇身上
  • 使用 ClientSize 代替 Width/Height 获取有效绘图区域


仍可改进的方面:
  • 添加游戏重启功能
  • 增加难度等级(速度随分数提升)
  • 添加音效和更丰富的UI


遇到具体运行问题可以贴出错误信息,我会继续帮你分析~

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 09:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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