Make A Portable Flappy Bird Game From Scratch!

About the project

This is a really quick version of the Flappy Bird Game. Press the button to 'flap' and go up in the air against gravity, and try and avoid the obstacles!

Project info

Difficulty: Moderate

Platforms: Espruino

Estimated time: 1 hour

License: Apache License 2.0 (Apache-2.0)

Items used in this project

Hardware components

2621  ARM Espruino Pico 2621 ARM Espruino Pico x 1
338 Display Development Tools Nokia 5110/3310 Monochrome LCD 338 Display Development Tools Nokia 5110/3310 Monochrome LCD x 1
Breadboard Breadboard x 1

Story


Introduction

This is a really quick version of the Flappy Bird Game. Press the button to 'flap' and go up in the air against gravity, and try and avoid the obstacles!


Check out the video above for more information!

Everything's detailed in the video above, but if you just want the software then it's copied below:


  1. A5.write(0); // GND
  2. A7.write(1); // VCC
  3.  
  4. var BUTTON = B3;
  5. pinMode(BUTTON,"input_pulldown");
  6. var SPEED = 0.5;
  7. var BIRDIMG = {
  8. width : 8, height : 8, bpp : 1,
  9. transparent : 0,
  10. buffer : new Uint8Array([
  11. 0b00000000,
  12. 0b01111000,
  13. 0b10000100,
  14. 0b10111010,
  15. 0b10100100,
  16. 0b10000100,
  17. 0b01111000,
  18. 0b00000000,
  19. ]).buffer
  20. };
  21.  
  22.  
  23. var g;
  24. var birdy, birdvy;
  25. var wasPressed = false;
  26. var running = false;
  27. var barriers;
  28. var score;
  29.  
  30. function newBarrier(x) {
  31. barriers.push({
  32. x1 : x-5,
  33. x2 : x+5,
  34. y : 10+Math.random()*28,
  35. gap : 8
  36. });
  37. }
  38.  
  39. function gameStart() {
  40. running = true;
  41. birdy = 48/2;
  42. birdvy = 0;
  43. barriers = [];
  44. newBarrier(42);
  45. newBarrier(84);
  46. score = 0;
  47. }
  48.  
  49. function gameStop() {
  50. running = false;
  51. }
  52.  
  53. function draw() {
  54. var buttonState = BUTTON.read();
  55.  
  56. g.clear();
  57. if (!running) {
  58. g.drawString("Game Over!",25,10);
  59. g.drawString("Score",10,20);
  60. g.drawString(score,10,26);
  61. g.flip();
  62. if (buttonState && !wasPressed)
  63. gameStart();
  64. wasPressed = buttonState;
  65. return;
  66. }
  67.  
  68. if (buttonState && !wasPressed)
  69. birdvy -= 2;
  70. wasPressed = buttonState;
  71.  
  72. score++;
  73. birdvy += 0.2;
  74. birdvy *= 0.8;
  75. birdy += birdvy;
  76. if (birdy > g.getHeight())
  77. gameStop();
  78. // draw bird
  79. //g.fillRect(0,birdy-3,6,birdy+3);
  80. g.drawImage(BIRDIMG, 0,birdy-4);
  81. // draw barriers
  82. barriers.forEach(function(b) {
  83. b.x1-=SPEED;
  84. b.x2-=SPEED;
  85. var btop = b.y-b.gap;
  86. var bbot = b.y+b.gap;
  87. g.drawRect(b.x1+1, -1, b.x2-2, btop-5);
  88. g.drawRect(b.x1, btop-5, b.x2, btop);
  89. g.drawRect(b.x1, bbot, b.x2, bbot+5);
  90. g.drawRect(b.x1+1, bbot+5, b.x2-1, g.getHeight());
  91. if (b.x1<6 && (birdy-3<btop || birdy+3>bbot))
  92. gameStop();
  93. });
  94. while (barriers.length && barriers[0].x2<=0) {
  95. barriers.shift();
  96. newBarrier(84);
  97. }
  98.  
  99. g.flip();
  100. }
  101.  
  102. function onInit() {
  103. // Setup SPI
  104. var spi = new SPI();
  105. spi.setup({ sck:B1, mosi:B10 });
  106. // Initialise the LCD
  107. g = require("PCD8544").connect(spi,B13,B14,B15, function() {
  108. //g.setContrast(0.43);
  109. gameStart();
  110. setInterval(draw, 50);
  111. });
  112. }
  113.  
  114.  
  115. // Finally, start everything going
  116. onInit();


Code

Source code

Credits

Photo of Espruino

Espruino

Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.

   

Leave your feedback...