element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs 2645 Project - Initial Coding Steps (2) - Snake
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: el14jw
  • Date Created: 14 Apr 2016 2:13 PM Date Created
  • Views 435 views
  • Likes 0 likes
  • Comments 1 comment
  • lcd
  • c
  • snake
  • project
  • nokia_5110
  • frdm-k64f
  • game
  • code
  • microcontroller
  • 2645 project
  • 2645
  • c++
Related
Recommended

2645 Project - Initial Coding Steps (2) - Snake

el14jw
el14jw
14 Apr 2016

Snake

 

The simple snake game is a universally recognized game and one of the earliest games to ever come out on a mobile device. Nokia famously put snake on most of their mobile phones and so there's a certain irony to me coding snake to be played on a Nokia 5110 LCD. It is a reasonably simple game where the user controls a 'snake' in 4 directions and tries to collect pixels that represent food whilst trying not to crash into the walls or itself. Once the snake consumes the food it increases in length which increases the difficulty of the game. Knowing this we can make a list of the different building blocks we require to make up this game.

Firstly the snake itself can easily be though of as a series of coordinates in an array, the food can also just be though of as a coordinate that is randomly chosen to be at any point that isn't on the snake or border. So using this approach we need to consider the following points and building blocks of our code:

  1. 1 pixel per snake or food coordinate is clearly not enough so how many pixels per cell will we use?
  2. How can we apply a difficulty setting?
  3. Checking for game over conditions, i.e. Crashing into wall or self
  4. Checking for when the snakes head coordinate is the same as food coordinate
  5. Code block that creates a random food coordinate each time one is needed
  6. Code block to handle the drawing on LCD

 

Here are some of the solutions to these aspects of the coding:

  1. From very quick prototyping on how many pixels should be used per 'cell' of the snake the optimum number of pixels seems to be 2x2. This is because any smaller becomes hard to see but any larger restricts the game map since we only have 84 pixels wide by 48 pixels high to work with.
  2. Difficulty in snake can be altered by either putting obstacles in the path or altering the in game speed. Using obstacles would make for a more interesting game but sadly since the LCD allows only one colour (black) it would become difficult for the user to differentiate between obstacles and the food they are supposed to be collecting. For this reason I will be altering the speed of the snake to increase or decrease the difficulty.
  3. To check for game over conditions we simply have to check whether the first snake coordinate (The head) is greater or less than the number of allowable pixels and that would check for the boundaries. To check whether the snake has crashed into itself we have to iterate through the snake array and check for if the head coordinate is the same as any of the other coordinates and this would constitute a game over.
  4. Checking for food collection simply requires checking whether the head coordinate is the same as the food coordinate every game tick.
  5. Creating a random food coordinate will just require a random x value and random y value but then the essential step of checking that this new value does not have the same coordinates as a snake coordinate. If it does then a new food coordinate must be created.
  6. Drawing on the LCD display only needs the screen to be cleared before plotting the border, food and snake again.

 

A ticker interrupt will be used to create game ticks and in every tick each element of the snake array will be moved backwards one element as far as the length of the snake allows (which means the last element will disappear) and a new coordinate will be added to the front of the array. This new coordinate will be calculated depending on the current snake direction which in turn will depend on the direction of the joystick. All of the checks will also be done every game tick to detect game over conditions and food collection.

imageimageimageimage

  • Sign in to reply
  • Rumman_Ahmed
    Rumman_Ahmed over 1 year ago

    yo bro i am trying to implement something similar can u help me with the code 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube