Skip to content

p8nut/snake_arduboy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

Snake ARDUBOY

Requirements

Setup the tools

Install the Arduino IDE and follow the steps from the Arduboy homemade package repository.

Upload Configuration

In Tools select the following configuration:

  • Board: "Homemade Arduboy"
  • Based on: "SparkFun Pro Micro 5V - Standard wiring"
  • Display: "SH1106"
  • Bootloader: "original (Caterina)"
  • Flash selected: "Pin0/D2/Rx"

Hello World

Here is the HelloWorld which you can find in File->Examples->Arduboy->HelloWorld

/*
Hello, World! example
June 11, 2015
Copyright (C) 2015 David Martinez
All rights reserved.
This code is the most basic barebones code for writing a program for Arduboy.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/

#include "Arduboy.h"

// make an instance of arduboy used for many functions
Arduboy arduboy;


// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
 // initiate arduboy instance
 arduboy.begin();

 // here we set the framerate to 15, we do not need to run at
 // default 60 and it saves us battery life
 arduboy.setFrameRate(15);
}


// our main game loop, this runs once every cycle/frame.
// this is where our game logic goes.
void loop() {
 // pause render until it's time for the next frame
 if (!(arduboy.nextFrame()))
   return;

 // first we clear our screen to black
 arduboy.clear();

 // we set our cursor 5 pixels to the right and 10 down from the top
 // (positions start at 0, 0) 
 arduboy.setCursor(4, 9);

 // then we print to screen what is in the Quotation marks ""
 arduboy.print(F("Hello, world!"));

 // then we finaly we tell the arduboy to display what we just wrote to the display
 arduboy.display();
}

Try to upload this code to your Arduboy by clicking on this icon alt text. you must be able to read "Hello World" on your screen; if not call an Assistant

First Draw Something

in the Arduboy header you can find some functions that will help you to draw on the screen like:

Please try them with the following template (edit only the draw function).

#include "Arduboy.h"

Arduboy arduboy;

void setup() {
  arduboy.begin();
  arduboy.setFrameRate(60);
}

void draw() {
  // draw function code here
  // arduboy.drawRect(10, 10, 5, 9, 0xFF);
}

void loop() {
  if (!(arduboy.nextFrame()))
    return;

  arduboy.clear();
  draw();
  arduboy.display();
}

Make it Move

From now on you can display some shape on the screen !

But there are still not moving ... Which is sad ... Let's try to make them move.

Draw a shape and make it bounce on the border of the screen. you can use the global variables HEIGHT and WIDTH to get the size of the screen.

Do not forget that you can use globals variables to save the positions of your objects entities.

#include "Arduboy.h"

Arduboy arduboy;
// save your entities positions here

void setup() {
  arduboy.begin();
  arduboy.setFrameRate(60);
}

void update() {
  // move entities here
}

void draw() {
  // draw your entities here
}

void loop() {
  if (!(arduboy.nextFrame()))
    return;
  
  update();
  arduboy.clear();
  draw();
  arduboy.display();
}

Handle buttons

you can check the state of a button with the following functions:

Buttons are:

  • LEFT_BUTTON
  • RIGHT_BUTTON
  • UP_BUTTON
  • DOWN_BUTTON
  • A_BUTTON
  • B_BUTTON
#include "Arduboy.h"

Arduboy arduboy;

// ball position
int x = WIDTH / 2;
int y = HEIGHT / 2;
int radius = 10;
bool filled = false;

void setup() {
  arduboy.begin();
  arduboy.setFrameRate(60);
}

void update() {
  if (arduboy.pressed(RIGHT_BUTTON) && (x < (WIDTH - radius))) {
    x++;
  }

  if (arduboy.pressed(LEFT_BUTTON) && (x >= radius)) {
    x--;
  }

  if (arduboy.pressed(DOWN_BUTTON) && (y < (HEIGHT - radius))) {
    y++;
  }

  if (arduboy.pressed(UP_BUTTON) && (y >= radius)) {
    y--;
  }

  if (arduboy.pressed(A_BUTTON) && filled) {
    filled = false;
  }
  if (arduboy.pressed(B_BUTTON) && !filled) {
    filled = true;
  }
}

void draw() {
  // draw your entities here
  if (filled) {
    arduboy.fillCircle(x, y, radius, 0xFF);
  } else {
    arduboy.drawCircle(x, y, radius, 0xFF);
  }
}

void loop() {
  if (!(arduboy.nextFrame()))
    return;
    
  update();
  arduboy.clear();
  draw();
  arduboy.display();
}

Build the Head

Now let's try to make a head move in the right direction. try to make it stop if it hit a border so that it never get out of the board.

Make your own game

From now on you should be able to code any basic game with Arduboy !

HAVE FUN

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published