Home  Contents

Snake in IronPython Mono Winforms

In this part of the Mono IronPython Winforms programming tutorial, we will create a Snake game clone.

Snake game

Snake is an older classic video game. It was first created in late 70s. Later it was brought to PCs. In this game the player controls a snake. The objective is to eat as many apples as possible. Each time the snake eats an apple, its body grows. The snake must avoid the walls and its own body. This game is sometimes called Nibbles.

Development

The size of each of the joints of a snake is 10px. The snake is controlled with the cursor keys. Initially the snake has three joints. The game starts immediately. If the game is finished, we display Game Over message in the middle of the Board.

board.py
import clr clr.AddReference("System.Drawing") clr.AddReference("System") from System.Windows.Forms import UserControl, Keys, Timer from System.Drawing import Size, Color, Bitmap, Brushes, RectangleF from System.Drawing import Font, StringAlignment, StringFormat, PointF from System import Random from System.ComponentModel import Container WIDTH = 300 HEIGHT = 300 DOT_SIZE = 10 ALL_DOTS = 900 RAND_POS = 27 x = [0] * ALL_DOTS y = [0] * ALL_DOTS class Board(UserControl): def __init__(self): self.Text = 'Snake' self.components = Container() self.BackColor = Color.Black self.DoubleBuffered = True self.ClientSize = Size(WIDTH, HEIGHT) self.left = False self.right = True self.up = False self.down = False self.inGame = True try: self.dot = Bitmap("dot.png") self.apple = Bitmap("apple.png") self.head = Bitmap("head.png") except Exception, e: print e.Message self.initGame() def OnTick(self, sender, event): if self.inGame: self.checkApple() self.checkCollision() self.move() self.Refresh() def initGame(self): self.dots = 3 for i in range(self.dots): x[i] = 50 - i * 10 y[i] = 50 self.locateApple() self.KeyUp += self.OnKeyUp self.timer = Timer(self.components) self.timer.Enabled = True self.timer.Interval = 100 self.timer.Tick += self.OnTick self.Paint += self.OnPaint def OnPaint(self, event): g = event.Graphics if (self.inGame): g.DrawImage(self.apple, self.apple_x, self.apple_y) for i in range(self.dots): if i == 0: g.DrawImage(self.head, x[i], y[i]) else: g.DrawImage(self.dot, x[i], y[i]) else: self.gameOver(g) def gameOver(self, g): msg = "Game Over" format = StringFormat() format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center width = float(self.ClientSize.Width) height = float(self.ClientSize.Height) rectf = RectangleF(0.0, 0.0, width, height) g.DrawString(msg, self.Font, Brushes.White, rectf, format) self.timer.Stop() def checkApple(self): if x[0] == self.apple_x and y[0] == self.apple_y: self.dots = self.dots + 1 self.locateApple() def move(self): z = self.dots while z > 0: x[z] = x[(z - 1)] y[z] = y[(z - 1)] z = z - 1 if self.left: x[0] -= DOT_SIZE if self.right: x[0] += DOT_SIZE if self.up: y[0] -= DOT_SIZE if self.down: y[0] += DOT_SIZE def checkCollision(self): z = self.dots while z > 0: if z > 4 and x[0] == x[z] and y[0] == y[z]: self.inGame = False z = z - 1 if y[0] >= HEIGHT - DOT_SIZE - self.TITLEBAR_HEIGHT: self.inGame = False if y[0] < 0: self.inGame = False if x[0] >= WIDTH - DOT_SIZE - self.BORDER_WIDTH: self.inGame = False if x[0] < 0: self.inGame = False def locateApple(self): rand = Random() r = rand.Next(RAND_POS) self.apple_x = r * DOT_SIZE r = rand.Next(RAND_POS) self.apple_y = r * DOT_SIZE def OnKeyUp(self, event): key = event.KeyCode if key == Keys.Left and not self.right: self.left = True self.up = False self.down = False if key == Keys.Right and not self.left: self.right = True self.up = False self.down = False if key == Keys.Up and not self.down: self.up = True self.right = False self.left = False if key == Keys.Down and not self.up: self.down = True self.right = False self.left = False

First we will define the constants used in our game.

The WIDTH and HEIGHT constants determine the size of the Board. The DOT_SIZE is the size of the apple and the dot of the snake. The ALL_DOTS constant defines the maximum number of possible dots on the Board. (900 = 300*300/10*10) The RAND_POS constant is used to calculate a random position of an apple. The DELAY constant determines the speed of the game.

 x = [0] * ALL_DOTS
 y = [0] * ALL_DOTS

These two lists store x, y coordinates of all possible joints of a snake.

In the move() method we have the key algorithm of the game. To understand it, look at how the snake is moving. You control the head of the snake. You can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.

 while z > 0:
     x[z] = x[(z - 1)]
     y[z] = y[(z - 1)]
     z = z - 1

This code moves the joints up the chain.

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

Move the head to the left.

In the checkCollision() method, we determine if the snake has hit itself or one of the walls.

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

Finish the game, if the snake hits one of its joints with the head.

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

Finish the game, if the snake hits the bottom of the Board.

The followintg image helps understand the collision of the snake object with the bottom of the board.


Collision
Figure: Collision

The locateApple() method locates an apple randomly on the form.

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

We get a random number from 0 to RAND_POS - 1.

 self.apple_x = r * DOT_SIZE
 ...
 self.apple_y = r * DOT_SIZE

These line set the x, y coordinates of the apple object.

In the OnKeyUp() method, we deternime which keys the player hit.

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

If we hit the left cursor key, we set self.left variable to True. This variable is used in the move() method to change coordinates of the snake object. Notice also, that when the snake is heading to the right, we cannot turn immediately to the left.

snake.py
#!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Application, Form, FormBorderStyle from board import Board class IForm(Form): def __init__(self): self.Text = 'Snake' self.FormBorderStyle = FormBorderStyle.FixedSingle borderWidth = (self.Width - self.ClientSize.Width) / 2 titleBarHeight = self.Height - self.ClientSize.Height - borderWidth board = Board() board.BORDER_WIDTH = borderWidth board.TITLEBAR_HEIGHT = titleBarHeight self.Controls.Add(board) self.CenterToScreen() Application.Run(IForm())

This is the main class.

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

Here we get the border width and the title bar height of the form control. These values are necessary for the collision detection of the snake with the borders.

 board.BORDER_WIDTH = borderWidth
 board.TITLEBAR_HEIGHT = titleBarHeight

We make them available for the board.


Snake
Figure: Snake

This was the Snake game programmed using the Mono Winforms library for the IronPython programming language.