Peg Game
Overview
For my mini project, I made a simple peg game using a uLCD, a joystick, and a speaker. This objective of this game is for one person to remove all but one peg from the screen by jumping over pegs with other pegs and removing them from the screen. The joystick is used to maneuver the cursor, select a peg, and then to move it to an appropriate location. When pegs are moved, the uLCD screen updates to show how many pegs are left. The speaker plays distracting music while the player is trying to win the game, as to emulate the full Cracker Barrel experience.
Components
MBed uLCD Joystick Speaker
Wiring
uLCD: https://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/
Joystick: https://developer.mbed.org/cookbook/Sparkfun-Thumb-Joystick
Speaker: https://developer.mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
Program
include the mbed library with this snippet
#include "mbed.h" #include <string> #include "uLCD_4DGL.h" #include "SparkfunAnalogJoystick.h" #include "SDFileSystem.h" #include "SongPlayer.h" #include "wave_player.h" #define M_PI 3.14159265358979323846 #define SECOND 1000 // used with threads DigitalOut led(LED1); // test led SparkfunAnalogJoystick joystick(p16,p17,p20); uLCD_4DGL uLCD(p28, p27, p30); // lcd used by the following threads SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card with .wav files AnalogOut DACout(p18); //switch pin wave_player waver(&DACout); // wavplayer object DigitalIn pb(p9); int main() { pb.mode(PullUp); uLCD.circle(64, 16, 4, 0x0000FF); uLCD.circle(50, 32, 4, 0x0000FF); uLCD.circle(78, 32, 4, 0x0000FF); uLCD.circle(36, 48, 4, 0x0000FF); uLCD.circle(64, 48, 4, 0x0000FF); uLCD.circle(92, 48, 4, 0x0000FF); uLCD.circle(22, 64, 4, 0x0000FF); uLCD.circle(50, 64, 4, 0x0000FF); uLCD.circle(78, 64, 4, 0x0000FF); uLCD.circle(106, 64, 4, 0x0000FF); uLCD.circle(8, 80, 4, 0x0000FF); uLCD.circle(36, 80, 4, 0x0000FF); uLCD.circle(64, 80, 4, 0x0000FF); uLCD.circle(92, 80, 4, 0x0000FF); uLCD.circle(120, 80, 4, 0x0000FF); //FILE *wave_file; //wave_file = fopen("/sd/Tormented.wav","r"); //waver.play(wave_file); float x; float y; int vert = 16; int horz = 64; int verttick, horztick; int count=0; int sum = 14; int hv[15] = {64,50,78,36,64,92,22,50,78,106,8,36,64,92,120}; //horizontal position int vv[15] = {16,32,32,48,48,48,64,64,64,64,80,80,80,80,80}; //vertical position int d[15] = {0x000000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000,0xFF0000}; //dot colors //set up screen //play game while(sum > 1){ if (pb==0 && count%2==1){ for (int i = 0; i <15; i++){ if (hv[i]==horz){ if (vv[i]==vert){ uLCD.circle(horz, vert, 10, 0x00FFFF); verttick=vert; horztick=horz; count+=1; } } } } if (pb==0 && count%2==0){ uLCD.circle(horztick,verttick, 10,0x000000); count+=1; //use these numbers to find what in D and change D if (horz != horztick || vert != verttick){ for (int i = 0; i < 15; i++){ if (hv[i] == ((horztick+horz)/2)){ if (vv[i] == ((verttick+vert)/2)){ d[i] = 0x000000; sum-=1; for (int i = 0; i < 15; i++){ if(hv[i] ==horztick){ if (vv[i] == verttick){ d[i]= 0x000000; } } } for (int i=0; i <15; i++){ if(hv[i]==horz){ if (vv[i] ==vert){ d[i]=0xFF00000; } } } } } } } //find the below //uLCD.circle(horztick,verttick,4, 0x000000); //uLCD.filled_circle(horz,vert, 2, 0xFF00000); } float distance=joystick.distance(); float angle=joystick.angle(); if (joystick.xAxis()>0.25){ x=distance*cos(angle*M_PI/180)*40; } else if (joystick.xAxis()<-0.25){ x=distance*cos(angle*M_PI/180)*40; } else{ x=0; } if (joystick.yAxis()>0.25) y=distance*sin(angle*M_PI/180)*40; else if (joystick.yAxis()<-0.25) y=distance*sin(angle*M_PI/180)*40; else y=0; uLCD.circle(horz,vert,6,0x000000); if (x>0){ if (horz + 14 < 128) horz = horz+14; } else if (x<0){ if (horz - 14 > 0) horz = horz-14; } else horz+=0; if (y>0){ if (vert - 16 > 0) vert = vert-16; } else if (y<0){ if (vert + 16 < 128) vert = vert+16; } else vert+=0; uLCD.circle(horz,vert,6,0x00FF00); uLCD.filled_circle(hv[0], vv[0], 2, d[0]); uLCD.filled_circle(hv[1], vv[1], 2, d[1]); uLCD.filled_circle(hv[2], vv[2], 2, d[2]); uLCD.filled_circle(hv[3], vv[3], 2, d[3]); uLCD.filled_circle(hv[4], vv[4], 2, d[4]); uLCD.filled_circle(hv[5], vv[5], 2, d[5]); uLCD.filled_circle(hv[6], vv[6], 2, d[6]); uLCD.filled_circle(hv[7], vv[7], 2, d[7]); uLCD.filled_circle(hv[8], vv[8], 2, d[8]); uLCD.filled_circle(hv[9], vv[9], 2, d[9]); uLCD.filled_circle(hv[10], vv[10], 2, d[10]); uLCD.filled_circle(hv[11], vv[11], 2, d[11]); uLCD.filled_circle(hv[12], vv[12], 2, d[12]); uLCD.filled_circle(hv[13], vv[13], 2, d[13]); uLCD.filled_circle(hv[14], vv[14], 2, d[14]); } //IN HERE PRINT THEY WON OR LOST!!!// //clear screen //congrats you won! //otherwise, don't let them know uLCD.cls(); uLCD.printf("Congrats! You won!"); }
Please log in to post comments.