原文: http://zetcode.com/tutorials/ironpythontutorial/snake/

在 Mono IronPython Winforms 编程教程的这一部分中,我们将创建一个贪食蛇游戏克隆。

贪食蛇游戏

贪食蛇是较旧的经典视频游戏。 它最初是在 70 年代后期创建的。 后来它被带到 PC 上。 在这个游戏中,玩家控制蛇。 目的是尽可能多地吃苹果。 蛇每次吃一个苹果,它的身体就会长大。 蛇必须避开墙壁和自己的身体。 该游戏有时称为 Nibbles 。

开发

蛇的每个关节的大小为 10px。 蛇由光标键控制。 最初,蛇具有三个关节。 游戏立即开始。 如果游戏结束,我们将在棋盘中间显示Game Over消息。

board.py

  1. import clr
  2. clr.AddReference("System.Drawing")
  3. clr.AddReference("System")
  4. from System.Windows.Forms import UserControl, Keys, Timer
  5. from System.Drawing import Size, Color, Bitmap, Brushes, RectangleF
  6. from System.Drawing import Font, StringAlignment, StringFormat, PointF
  7. from System import Random
  8. from System.ComponentModel import Container
  9. WIDTH = 300
  10. HEIGHT = 300
  11. DOT_SIZE = 10
  12. ALL_DOTS = 900
  13. RAND_POS = 27
  14. x = [0] * ALL_DOTS
  15. y = [0] * ALL_DOTS
  16. class Board(UserControl):
  17. def __init__(self):
  18. self.Text = 'Snake'
  19. self.components = Container()
  20. self.BackColor = Color.Black
  21. self.DoubleBuffered = True
  22. self.ClientSize = Size(WIDTH, HEIGHT)
  23. self.left = False
  24. self.right = True
  25. self.up = False
  26. self.down = False
  27. self.inGame = True
  28. try:
  29. self.dot = Bitmap("dot.png")
  30. self.apple = Bitmap("apple.png")
  31. self.head = Bitmap("head.png")
  32. except Exception, e:
  33. print e.Message
  34. self.initGame()
  35. def OnTick(self, sender, event):
  36. if self.inGame:
  37. self.checkApple()
  38. self.checkCollision()
  39. self.move()
  40. self.Refresh()
  41. def initGame(self):
  42. self.dots = 3
  43. for i in range(self.dots):
  44. x[i] = 50 - i * 10
  45. y[i] = 50
  46. self.locateApple()
  47. self.KeyUp += self.OnKeyUp
  48. self.timer = Timer(self.components)
  49. self.timer.Enabled = True
  50. self.timer.Interval = 100
  51. self.timer.Tick += self.OnTick
  52. self.Paint += self.OnPaint
  53. def OnPaint(self, event):
  54. g = event.Graphics
  55. if (self.inGame):
  56. g.DrawImage(self.apple, self.apple_x, self.apple_y)
  57. for i in range(self.dots):
  58. if i == 0:
  59. g.DrawImage(self.head, x[i], y[i])
  60. else:
  61. g.DrawImage(self.dot, x[i], y[i])
  62. else:
  63. self.gameOver(g)
  64. def gameOver(self, g):
  65. msg = "Game Over"
  66. format = StringFormat()
  67. format.Alignment = StringAlignment.Center
  68. format.LineAlignment = StringAlignment.Center
  69. width = float(self.ClientSize.Width)
  70. height = float(self.ClientSize.Height)
  71. rectf = RectangleF(0.0, 0.0, width, height)
  72. g.DrawString(msg, self.Font, Brushes.White, rectf, format)
  73. self.timer.Stop()
  74. def checkApple(self):
  75. if x[0] == self.apple_x and y[0] == self.apple_y:
  76. self.dots = self.dots + 1
  77. self.locateApple()
  78. def move(self):
  79. z = self.dots
  80. while z > 0:
  81. x[z] = x[(z - 1)]
  82. y[z] = y[(z - 1)]
  83. z = z - 1
  84. if self.left:
  85. x[0] -= DOT_SIZE
  86. if self.right:
  87. x[0] += DOT_SIZE
  88. if self.up:
  89. y[0] -= DOT_SIZE
  90. if self.down:
  91. y[0] += DOT_SIZE
  92. def checkCollision(self):
  93. z = self.dots
  94. while z > 0:
  95. if z > 4 and x[0] == x[z] and y[0] == y[z]:
  96. self.inGame = False
  97. z = z - 1
  98. if y[0] >= HEIGHT - DOT_SIZE - self.TITLEBAR_HEIGHT:
  99. self.inGame = False
  100. if y[0] < 0:
  101. self.inGame = False
  102. if x[0] >= WIDTH - DOT_SIZE - self.BORDER_WIDTH:
  103. self.inGame = False
  104. if x[0] < 0:
  105. self.inGame = False
  106. def locateApple(self):
  107. rand = Random()
  108. r = rand.Next(RAND_POS)
  109. self.apple_x = r * DOT_SIZE
  110. r = rand.Next(RAND_POS)
  111. self.apple_y = r * DOT_SIZE
  112. def OnKeyUp(self, event):
  113. key = event.KeyCode
  114. if key == Keys.Left and not self.right:
  115. self.left = True
  116. self.up = False
  117. self.down = False
  118. if key == Keys.Right and not self.left:
  119. self.right = True
  120. self.up = False
  121. self.down = False
  122. if key == Keys.Up and not self.down:
  123. self.up = True
  124. self.right = False
  125. self.left = False
  126. if key == Keys.Down and not self.up:
  127. self.down = True
  128. self.right = False
  129. self.left = False

首先,我们将定义游戏中使用的常量。

WIDTHHEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蛇的点。 ALL_DOTS常数定义了板上可能的最大点数。 (900 = 300 * 300 / 10 * 10RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。

  1. x = [0] * ALL_DOTS
  2. y = [0] * ALL_DOTS

这两个列表存储蛇的所有可能关节的 x,y 坐标。

move()方法中,我们有游戏的关键算法。 要了解它,请看一下蛇是如何运动的。 您控制蛇的头。 您可以使用光标键更改其方向。 其余关节在链上向上移动一个位置。 第二关节移动到第一个关节的位置,第三关节移动到第二个关节的位置,依此类推。

  1. while z > 0:
  2. x[z] = x[(z - 1)]
  3. y[z] = y[(z - 1)]
  4. z = z - 1

该代码将关节向上移动。

  1. if self.left:
  2. x[0] -= DOT_SIZE

将头向左移动。

checkCollision()方法中,我们确定蛇是否击中了自己或撞墙之一。

  1. while z > 0:
  2. if z > 4 and x[0] == x[z] and y[0] == y[z]:
  3. self.inGame = False
  4. z = z - 1

如果蛇用头撞到其关节之一,则游戏结束。

  1. if y[0] >= HEIGHT - DOT_SIZE - self.TITLEBAR_HEIGHT:
  2. self.inGame = False

如果蛇击中了棋盘的底部,则游戏结束。

下图有助于了解蛇形物体与棋盘底部的碰撞。

IronPython Mono Winforms 中的贪食蛇 - 图1

图:碰撞

locateApple()方法在表格上随机定位一个苹果。

  1. rand = Random()
  2. r = rand.Next(RAND_POS)

我们得到一个从 0 到RAND_POS-1的随机数。

  1. self.apple_x = r * DOT_SIZE
  2. ...
  3. self.apple_y = r * DOT_SIZE

这些行设置了apple对象的 x,y 坐标。

OnKeyUp()方法中,我们确定了键击玩家击键的时间。

  1. if key == Keys.Left and not self.right:
  2. self.left = True
  3. self.up = False
  4. self.down = False

如果我们按左光标键,则将self.left变量设置为True。 在move()方法中使用此变量来更改蛇对象的坐标。 还要注意,当蛇向右行驶时,我们不能立即向左转。

snake.py

  1. #!/usr/bin/ipy
  2. import clr
  3. clr.AddReference("System.Windows.Forms")
  4. from System.Windows.Forms import Application, Form, FormBorderStyle
  5. from board import Board
  6. class IForm(Form):
  7. def __init__(self):
  8. self.Text = 'Snake'
  9. self.FormBorderStyle = FormBorderStyle.FixedSingle
  10. borderWidth = (self.Width - self.ClientSize.Width) / 2
  11. titleBarHeight = self.Height - self.ClientSize.Height - borderWidth
  12. board = Board()
  13. board.BORDER_WIDTH = borderWidth
  14. board.TITLEBAR_HEIGHT = titleBarHeight
  15. self.Controls.Add(board)
  16. self.CenterToScreen()
  17. Application.Run(IForm())

这是主要的类。

  1. borderWidth = (self.Width - self.ClientSize.Width) / 2
  2. titleBarHeight = self.Height - self.ClientSize.Height - borderWidth

在这里,我们获得窗体控件的边框宽度和标题栏高度。 这些值对于蛇与边界的碰撞检测是必需的。

  1. board.BORDER_WIDTH = borderWidth
  2. board.TITLEBAR_HEIGHT = titleBarHeight

我们将它们提供给董事会。

IronPython Mono Winforms 中的贪食蛇 - 图2

图:贪食蛇

这是使用 Iron Win 编程语言的 Mono Winforms 库编程的贪食蛇游戏。