Retro game that let's the player steer a ball through a hole filled maze. Has multiple levels of increasing difficulty.

Dependencies:   LCD_ST7735 MusicEngine RETRO_BallsAndThings mbed

Ball and Holes

In this game I attempted to create somewhat natural movement of the ball by implementing gravity and friction which combined over time determine the speed of the ball. Playing with the settings (aka. the magic numbers) that are spread out all over game.cpp, gives different effects, such as an icy, rough or liquid-like surface.

It took some time to figure out how to post my very first youtube video. Sorry for the shaky recording. Trying to record the video with my phone while playing the game in one hand was quite challenging, but here it is;

The left and right buttons are used to cheat: restart the current or go to the next level. Up and down control the game-tick. During game-play the robot-button shows the accelerator graph and the ship-button mutes the sound.

BTW. If your ball happens to get stuck, tilting the console in the opposite direction will set it free. For sake of argument: these magnetic wall-ends are in the words of Bob Ross "a happy accident". Since there is no specific code for it, others might call it a bug. As it results in more interesting game-play, I didn't attempt to fix it, but left a comment for those who dare to look at the mess I call code.

Committer:
maxint
Date:
Wed Jan 28 17:32:54 2015 +0000
Revision:
0:87ab172a74b4
Child:
1:c1ee4c699517
Separated elements as objects, added gravity and bouncespeed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxint 0:87ab172a74b4 1 #include "Accelerometer.h"
maxint 0:87ab172a74b4 2
maxint 0:87ab172a74b4 3 Accelerometer::Accelerometer(int nI2cAddress) : i2cAddress(nI2cAddress), i2c(P0_5, P0_4)
maxint 0:87ab172a74b4 4 { // constructor
maxint 0:87ab172a74b4 5 this->i2c.frequency(400000); // fast I2C is 400 KHz, not 400 Hz. Default frequency is 100 KHz
maxint 0:87ab172a74b4 6 this->writeRegister(0x2A, 0x01); // initialize accelerometer (set CTRL_REG1 bit ACTIVE)
maxint 0:87ab172a74b4 7 }
maxint 0:87ab172a74b4 8
maxint 0:87ab172a74b4 9 void Accelerometer::readRegisters(char address, char* buffer, int len) {
maxint 0:87ab172a74b4 10 // int nStart=this->tWait.read_ms();
maxint 0:87ab172a74b4 11 this->i2c.write(i2cAddress, &address, 1, true);
maxint 0:87ab172a74b4 12 this->i2c.read(i2cAddress | 1, buffer, len);
maxint 0:87ab172a74b4 13 // printDouble((double)this->tWait.read_ms()-nStart, 10, 10);
maxint 0:87ab172a74b4 14 }
maxint 0:87ab172a74b4 15
maxint 0:87ab172a74b4 16 int Accelerometer::writeRegister(char address, char value) {
maxint 0:87ab172a74b4 17 char buffer[2] = { address, value };
maxint 0:87ab172a74b4 18
maxint 0:87ab172a74b4 19 return this->i2c.write(i2cAddress, buffer, 2);
maxint 0:87ab172a74b4 20 }
maxint 0:87ab172a74b4 21
maxint 0:87ab172a74b4 22 double Accelerometer::convert(char* buffer) {
maxint 0:87ab172a74b4 23 double val = ((buffer[0] << 2) | (buffer[1] >> 6));
maxint 0:87ab172a74b4 24
maxint 0:87ab172a74b4 25 if (val > 511.0)
maxint 0:87ab172a74b4 26 val -= 1024.0;
maxint 0:87ab172a74b4 27
maxint 0:87ab172a74b4 28 return val / 512.0;
maxint 0:87ab172a74b4 29 }
maxint 0:87ab172a74b4 30
maxint 0:87ab172a74b4 31 void Accelerometer::getXYZ(double& x, double& y, double& z) {
maxint 0:87ab172a74b4 32 char buffer[6];
maxint 0:87ab172a74b4 33
maxint 0:87ab172a74b4 34 this->readRegisters(0x01, buffer, 6);
maxint 0:87ab172a74b4 35
maxint 0:87ab172a74b4 36 x = this->convert(buffer);
maxint 0:87ab172a74b4 37 y = this->convert(buffer + 2);
maxint 0:87ab172a74b4 38 z = this->convert(buffer + 4);
maxint 0:87ab172a74b4 39 }