• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Nokia 5110 Arduino Snake Game

By Abhishek Ghosh July 18, 2017 9:12 am Updated on July 18, 2017

Nokia 5110 Arduino Snake Game

Advertisement

Previously, we talked about Nokia 5110 wiring details. Here is circuit diagram and code to play Nokia 5110 Arduino Snake Game. This Snake is like original Nokia Mobile’s famous Snake II game. Nokia 5110 is a versatile LCD display and capable to perform almost everything. The basic problem is inconsistent quality of these Nokia 5110 LCDs – these are refurbished from 2 decade old thrown away mobile phones.

Nokia 5110 Arduino Snake Game

This thing we have not coded but we found from a GitHub repo, from there we got the original creator website gameinstance.com. The game is a single player game, uses two buttons for turning left and turning right like Nokia 3310 game. You are snake controller who will eat up randomly appearing foods. More food your snake will eat, longer the snake will become. If the snake touches itself or the boundary then game is over. Simple game :)

 

Nokia 5110 Arduino Snake Game

 

Apart from Arduino, we need 3 push buttons, one breadboard and few resistors. Here is the original Arduino code file on Github :

Advertisement

---

Vim
1
https://github.com/gameinstance/app-arduino/blob/master/SnakeOnArduino.ino

Obviously the setup is like our guide on how to setup Nokia 5110 display, but it is practical to look at the original developer’s website for wiring :

Vim
1
https://www.gameinstance.com/post/15/The-Snake-game-for-Arduino

Also, here is the code :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* The Snake game for Arduino
*
*  GameInstance.com
*  2016
*/
 
#include <SPI.h>
#include <LCDNokia5100.h>
 
 
/*
* Snake block
*
* An element of the snake body
*/
class Block {
 
  public:
 
    /// default constructor
    Block() {
      //
      m_x = 0;
      m_y = 0;
      m_d = 0;
    };
    /// parametric constructor
    Block(byte x, byte y, byte d) {
      //
      m_x = x;
      m_y = y;
      m_d = d;
    };
    /// destructor
    virtual ~Block() {};
 
    /// the block coordinates
    byte m_x, m_y;
    /// the block's direction [1-4] with 0 meaning null
    byte m_d;
};
 
/*
* Snake food item
*
*/
class Food {
 
  public:
 
    /// default constructor
    Food() {
      //
      m_x = m_y = m_index = 0;
    };
    /// destructor
    virtual ~Food() {};
 
    /// generates random x,y coordinates for this
    /// not really working unless a noisy analog input is used for seeding
    void Random(byte width, byte height, byte weight) {
      //
      m_x = weight * random((width - 2) / weight) + 1;
      m_y = weight * random((height - 2) / weight) + 1;
    };
    /// generates the next x,y pair from a predetermined array of ints
    bool Next(byte width, byte height, byte weight) {
      //
      m_x = weight * (m_Points[m_index] % ((width - 2) / weight)) + 1;
      m_y = weight * (m_Points[m_index] % ((height - 2) / weight)) + 1;
      m_index ++;
      if (m_index >= 10 - 1) {
        //
        m_index = 0;
        return false;
      }
      return true;
    };
    /// draws this onto a given LCD display
    void Draw(LCDNokia5100 &lcd, byte weight) {
      //
      lcd.Rect(m_x, m_y, m_x + weight, m_y + weight - 1, true, true);
    };
 
    /// the food coordinates
    byte m_x, m_y;
    /// the index in the array
    byte m_index;
    /// a predetermined array used for generating on-screen coordinates
    unsigned int m_Points[10] = {1301, 2392, 4812, 9831, 6127, 9928, 3123, 4123, 6512, 4812};
};
 
 
/*
* The snake
*
*/
class Snake {
 
  public:
 
    /// the maximum length of the snake
    static const byte BLOCK_COUNT_MAX = 20;
 
    /// default constructor
    Snake() {
      //
      m_Size = 0;
      m_HeadX = 31;
      m_HeadY = 31;
      m_Speed = 1;
      m_Direction = 2;
      m_Weight = 1;
      m_bSelfTouch = false;
      memset(m_Blocks, 0, BLOCK_COUNT_MAX * sizeof(Block));
    };
    /// destructor
    virtual ~Snake() {
      //
      for (byte i = 0; i < m_Size; i ++) {
        //
        delete m_Blocks[i];
      }
    };
 
    /// sets the weight of the snake
    void SetWeight(byte w) {
      //
      m_Weight = w;
    };
    /// adds a block at the end of the snake
    void AddBlock() {
      //
      byte dx, dy;
      if (m_Size == 0) {
        // first block to be added
        m_Blocks[m_Size] = new Block(m_HeadX, m_HeadY, m_Direction);
      } else {
        // not the first block added
        GetDirection(dx, dy, m_Blocks[m_Size - 1]->m_d);
        m_Blocks[m_Size] = new Block(m_Blocks[m_Size - 1]->m_x + dx * m_Weight, m_Blocks[m_Size - 1]->m_y + dy * m_Weight, m_Blocks[m_Size - 1]->m_d);
      }
      m_Size ++;
    };
    /// executes the slithering movement
    void Execute(byte dt = 1) {
      //
      byte dx, dy;
      for (byte i = 0; i < m_Size; i ++) {
        //
        GetDirection(dx, dy, m_Blocks[i]->m_d);
        m_Blocks[i]->m_x -= m_Speed * dt * dx * m_Weight;
        m_Blocks[i]->m_y -= m_Speed * dt * dy * m_Weight;
      }
      m_HeadX = m_Blocks[0]->m_x;
      m_HeadY = m_Blocks[0]->m_y;
      for (byte i = m_Size - 1; i > 0; i --) {
        //
        m_Blocks[i]->m_d = m_Blocks[i - 1]->m_d;
        if ((m_HeadX == m_Blocks[i]->m_x) && (m_HeadY == m_Blocks[i]->m_y)) {
          // snake head touches own body
          m_bSelfTouch = true;
        }
      }
      m_Blocks[0]->m_d = m_Direction;
    };
    /// sets the snake's direction
    void SetDirection(byte d) {
      //
      if (m_Direction == d) {
        // no direction change
        return;
      }
      // direction has changed
      m_Direction = d;
    };
    /// turns the snake left or right
    void Turn(byte right = 1) {
      //
      byte d = m_Direction;
      d += right;
      if (d > 4) {
        //
        d = 1;
      } else if (d < 1) {
        //
        d = 4;
      }
      SetDirection(d);
    };
    /// draws the snake onto the LCD
    void Draw(LCDNokia5100 &lcd) {
      //
      for (byte i = 0; i < m_Size; i ++) {
        //
        lcd.Rect(m_Blocks[i]->m_x, m_Blocks[i]->m_y, m_Blocks[i]->m_x + m_Weight, m_Blocks[i]->m_y + m_Weight - 1, true, true);
      }
    };
    
 
    /// speed of the snake and direction of the head
    byte m_Speed, m_Direction;
    /// the length of the snake and the size of the block element
    byte m_Size, m_Weight;
    /// snake's head coordinates
    byte m_HeadX, m_HeadY;
    /// head touching body indicator
    bool m_bSelfTouch;
 
 
  private:
 
    /// gets the (x, y) direction given the modeled direction
    void GetDirection(byte &dx, byte &dy, byte d) {
      //
      dx = (d == 2) ? 1 : ((d == 4) ? -1 : 0);
      dy = (d == 3) ? 1 : ((d == 1) ? -1 : 0);
    };
 
    /// the array of blocks forming the snake boby
    Block* m_Blocks[BLOCK_COUNT_MAX];
};
 
/*
* The main game class
*
*/
class Game {
 
  public:
 
    /// the display width in pixels
    static const byte WIDTH = 84;
    /// the display height in pixels
    static const byte HEIGHT = 48;
    /// the number of buttons in use
    static const byte BUTTON_COUNT = 2;
    /// the size of the snake block
    static const byte WEIGHT = 2;
 
    /// default constructor
    Game() {
      //
      m_state = 0;
    };
    /// destructor
    virtual ~Game() {
      //
    };
 
    /// generates an arbitrary game configuration
    void Setup() {
      //
      m_s.SetWeight(WEIGHT);
      for (byte i = 0; i < 9; i ++) {
        //
        m_s.AddBlock();
      }
      //randomSeed(analogRead(A5));
      m_f.Next(WIDTH, HEIGHT, WEIGHT);
      for (byte i = 0; i < BUTTON_COUNT; i ++) {
        //
        m_bPress[i] = false;
      }
      m_lcd.Start();
      m_lcd.Contrast(45);
      m_lcd.Light();
      m_lcd.Fill(false);
      m_lcd.Text("GameInstance", 8, 8, true);
      m_lcd.Text(".com", 30, 16, true);
      m_lcd.Text("The Snake", 12, 32, true);
      m_lcd.Update();
      m_delay = 200;
      delay(1000);
    };
    /// the game state machine
    void Execute() {
      //
      if (m_state == 0) {
        // game start
        m_state = 1;
      }
      if (m_state == 1) {
        // game on
        if (WasPressed(A0, 0)) {
          //
          m_s.Turn(1);
        }
        if (WasPressed(A1, 1)) {
          //
          m_s.Turn(-1);
        }
        m_s.Execute();
        Draw();
        if ((m_s.m_HeadX == m_f.m_x)
          && (m_s.m_HeadY == m_f.m_y)) {
          //
          m_state = 2;
        } else {
          //
          if ((m_s.m_HeadX < 1)
            || (m_s.m_HeadX > WIDTH - 2)
            || (m_s.m_HeadY < 1)
            || (m_s.m_HeadY > HEIGHT - 2)) {
            //
            m_state = 10;
          }
          if (m_s.m_bSelfTouch) {
            //
            m_state = 10;
          }
        }
      }
      if (m_state == 2) {
        // food eaten
        m_s.AddBlock();
        if (!m_f.Next(WIDTH, HEIGHT, WEIGHT)) {
          //
          m_state = 5;
        } else {
          //
          m_state = 1;
        }
      }
      if (m_state == 5) {
        // game complete
        m_lcd.Fill(false);
        m_lcd.Text("Congrats!", 14, 8, true);
        m_lcd.Text("press reset", 8, 32, true);
        m_lcd.Update();
        m_state = 11;
      }
      if (m_state == 10) {
        // game over
        m_lcd.Fill(false);
        m_lcd.Text("Game Over!", 12, 8, true);
        m_lcd.Text("press reset", 8, 32, true);
        m_lcd.Update();
        m_state = 11;
      }
      if (m_state == 11) {
        // end
      }
    };
    /// draws the game components
    void Draw() {
      //
      m_lcd.Fill(false);
      m_lcd.Line(0, 0, WIDTH - 1, 0, true);
      m_lcd.Line(0, 0, 0, HEIGHT - 1, true);
      m_lcd.Line(WIDTH - 1, HEIGHT - 1, WIDTH - 1, 0, true);
      m_lcd.Line(WIDTH - 1, HEIGHT - 1, 0, HEIGHT - 1, true);
      m_s.Draw(m_lcd);
      m_f.Draw(m_lcd, WEIGHT);
      m_lcd.Update();
    };
    
    int m_delay;
 
 
  private:
 
    /// indicates once that a button was pressed
    bool WasPressed(byte pin, byte index, int threshold = 512) {
      //
      int val = analogRead(pin);
      //Serial.println(val);
      if (val > threshold) {
        // isn't pressed
        if (m_bPress[index]) {
          // but was before
          m_bPress[index] = false;
        }
        return false;
      }
      // is pressed
      if (!m_bPress[index]) {
        // and wasn't before
        m_bPress[index] = true;
        return true;
      }
      // but was before
      return false;
    }
 
    /// the state of the automate
    byte m_state;
    /// the snake
    Snake m_s;
    /// the food
    Food m_f;
    /// the display
    LCDNokia5100 m_lcd;
    /// the array of button states
    bool m_bPress[BUTTON_COUNT];
};
 
 
/// the game instace
Game g;
 
void setup() {
  // put your setup code here, to run once:
  //Serial.begin(9600);
  g.Setup();
}
 
void loop() {
  // put your main code here, to run repeatedly:
  g.Execute();
  delay(g.m_delay);
}

Tagged With snake nokia game code arduino uno , nokia5110 lcd arduino snake pregrammeren , nokia 5110 snake game , nokia 5110 lcd snake game , lcd snake game arduino , https://thecustomizewindows com/2017/07/nokia-5110-arduino-snake-game/ , arduino snake game with nokia 5110 , arduino snake game nokia 5110 , arduino snake game , arduino snake

This Article Has Been Shared 143 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Nokia 5110 Arduino Snake Game

  • Arduino Basic 3V Motor Control : Speed Variation For Robot

    Very Easy With 100% Working Warranty. Code, Circuit Included. This Arduino Basic 3V Motor Control is Intended For Speed Variation For Robot.

  • Arduino 3V Motor Control : Level I Advanced Speed Variation

    Arduino 3V Motor Control Level I Advanced Speed Variation is Second Hard Level Guide Where Diode, Transistor and Resister Has Been Used.

  • DIY Arduino Traffic Light Pedestrian Light Push Button Control

    Here is How to Create LED DIY Arduino Traffic Light – Pedestrian Light Push Button Control. When Pedestrians Will WALK, Cars Will Stop Logic.

  • Arduino TM1637 Scrolling Text, Snake Examples

    Snake Animation Is Used In The Proprietary LED Displays To Indicate Working, Busy. Here Are Examples Of Arduino TM1637 Scrolling Text, Snake.

  • Rotary Encoder For Dummies : For Arduino, Raspberry Pi

    Some theory better to know before buying rotary encoders. Here Is Rotary Encoder For Dummies Guide Intended For Arduino, Raspberry Pi Users.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

You can subscribe to our Free Once a Day, Regular Newsletter by clicking the subscribe button below.

Click To Subscribe

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (20K Followers)
  • Twitter (4.9k Followers)
  • Facebook (5.8k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.2k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • How To Repack Installed Software on Debian/Ubuntu January 16, 2021
  • Components of Agile Software Development January 15, 2021
  • What is Conway’s Law? January 14, 2021
  • Effects of Digitization on Companies : Part XIII January 13, 2021
  • What is SoftAP Mode? January 12, 2021

 

About This Article

Cite this article as: Abhishek Ghosh, "Nokia 5110 Arduino Snake Game," in The Customize Windows, July 18, 2017, January 16, 2021, https://thecustomizewindows.com/2017/07/nokia-5110-arduino-snake-game/.

Source:The Customize Windows, JiMA.in

 

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Cookie Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2021 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy