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
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:
- A5.write(0); // GND
- A7.write(1); // VCC
- var BUTTON = B3;
- pinMode(BUTTON,"input_pulldown");
- var SPEED = 0.5;
- var BIRDIMG = {
- width : 8, height : 8, bpp : 1,
- transparent : 0,
- buffer : new Uint8Array([
- 0b00000000,
- 0b01111000,
- 0b10000100,
- 0b10111010,
- 0b10100100,
- 0b10000100,
- 0b01111000,
- 0b00000000,
- ]).buffer
- };
- var g;
- var birdy, birdvy;
- var wasPressed = false;
- var running = false;
- var barriers;
- var score;
- function newBarrier(x) {
- barriers.push({
- x1 : x-5,
- x2 : x+5,
- y : 10+Math.random()*28,
- gap : 8
- });
- }
- function gameStart() {
- running = true;
- birdy = 48/2;
- birdvy = 0;
- barriers = [];
- newBarrier(42);
- newBarrier(84);
- score = 0;
- }
- function gameStop() {
- running = false;
- }
- function draw() {
- var buttonState = BUTTON.read();
- g.clear();
- if (!running) {
- g.drawString("Game Over!",25,10);
- g.drawString("Score",10,20);
- g.drawString(score,10,26);
- g.flip();
- if (buttonState && !wasPressed)
- gameStart();
- wasPressed = buttonState;
- return;
- }
- if (buttonState && !wasPressed)
- birdvy -= 2;
- wasPressed = buttonState;
- score++;
- birdvy += 0.2;
- birdvy *= 0.8;
- birdy += birdvy;
- if (birdy > g.getHeight())
- gameStop();
- // draw bird
- //g.fillRect(0,birdy-3,6,birdy+3);
- g.drawImage(BIRDIMG, 0,birdy-4);
- // draw barriers
- barriers.forEach(function(b) {
- b.x1-=SPEED;
- b.x2-=SPEED;
- var btop = b.y-b.gap;
- var bbot = b.y+b.gap;
- g.drawRect(b.x1+1, -1, b.x2-2, btop-5);
- g.drawRect(b.x1, btop-5, b.x2, btop);
- g.drawRect(b.x1, bbot, b.x2, bbot+5);
- g.drawRect(b.x1+1, bbot+5, b.x2-1, g.getHeight());
- if (b.x1<6 && (birdy-3<btop || birdy+3>bbot))
- gameStop();
- });
- while (barriers.length && barriers[0].x2<=0) {
- barriers.shift();
- newBarrier(84);
- }
- g.flip();
- }
- function onInit() {
- // Setup SPI
- var spi = new SPI();
- spi.setup({ sck:B1, mosi:B10 });
- // Initialise the LCD
- g = require("PCD8544").connect(spi,B13,B14,B15, function() {
- //g.setContrast(0.43);
- gameStart();
- setInterval(draw, 50);
- });
- }
- // Finally, start everything going
- onInit();
Code
Credits
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...