Advertisement
Guest User

Untitled

a guest
Feb 17th, 2014
2,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. /*
  2. Flappy Bit Version 1.0
  3. By Daniel Ratcliffe
  4. http://www.twitter.com/DanTwoHundred
  5. */
  6.  
  7. // CONFIGURATION
  8. // Set these values to the pin numbers you're using on your Arduino.
  9. // You may set any value to zero to ignore this input or output
  10. int rowPins[] = { 5, 10, A3, 8, 2, A2, A0, A1 };
  11. int columnPins[] = { 9, 4, 3, 6, A5, 7, 11, 12 };
  12. int buttonPin = A4;
  13.  
  14. // STATE
  15. boolean menu = true;
  16. boolean gameOver = false;
  17. int lastButtonState = 0;
  18.  
  19. int birdPos = 2;
  20. int pipePos = 8;
  21. int pipeGap = 3;
  22. int ticks = 0;
  23.  
  24. int inGameScreen[] = {
  25. 0,0,0,0,0,0,0,0,
  26. 0,0,0,0,0,0,0,0,
  27. 0,0,0,0,0,0,0,0,
  28. 0,0,0,0,0,0,0,0,
  29. 0,0,0,0,0,0,0,0,
  30. 0,0,0,0,0,0,0,0,
  31. 0,0,0,0,0,0,0,0,
  32. 0,0,0,0,0,0,0,0,
  33. };
  34.  
  35. int menuScreen[] = {
  36. 0,0,0,1,1,0,0,0,
  37. 0,0,1,0,0,1,0,0,
  38. 0,0,1,0,1,1,0,0,
  39. 1,1,1,0,0,1,0,0,
  40. 1,0,0,1,1,1,1,0,
  41. 1,0,1,0,1,0,0,1,
  42. 0,1,0,0,1,1,1,0,
  43. 0,0,1,1,1,0,0,0,
  44. };
  45.  
  46. // UTILITY
  47. void drawInGameScreen( int colour )
  48. {
  49. if( birdPos >= 0 && birdPos < 8 )
  50. {
  51. if( !gameOver || (gameOver && ((ticks / 24) % 2) == 1) )
  52. {
  53. inGameScreen[ 1 + birdPos*8 ] = colour;
  54. }
  55. }
  56. for( int y=0; y<8; ++y )
  57. {
  58. if( y < pipeGap || y >= pipeGap + 3 )
  59. {
  60. if( pipePos >= 0 && pipePos < 8 )
  61. {
  62. inGameScreen[ pipePos + y*8 ] = colour;
  63. }
  64. if( pipePos >= -1 && pipePos < 7 )
  65. {
  66. inGameScreen[ pipePos + 1 + y*8 ] = colour;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. // MAIN
  73. void setup()
  74. {
  75. // Set all pin modes
  76. for( int n=0; n<8; ++n )
  77. {
  78. int column = columnPins[n];
  79. if( column > 0 )
  80. {
  81. pinMode( column, OUTPUT );
  82. digitalWrite( column, HIGH );
  83. }
  84. int row = rowPins[n];
  85. if( row > 0 )
  86. {
  87. pinMode( row, OUTPUT );
  88. digitalWrite( row, LOW );
  89. }
  90. }
  91. if( buttonPin > 0 )
  92. {
  93. pinMode( buttonPin, INPUT );
  94. }
  95. }
  96.  
  97. void loop()
  98. {
  99. // Update
  100. int buttonState = (buttonPin > 0 && digitalRead( buttonPin ) == HIGH) ? 1 : 0;
  101. if( menu )
  102. {
  103. // IN MENU
  104. ++ticks;
  105. if( (ticks % 12) == 0 )
  106. {
  107. // Animate the eye
  108. if( rand()%30 == 0 )
  109. {
  110. menuScreen[ 19 ] = 1;
  111. menuScreen[ 20 ] = 0;
  112. }
  113. else
  114. {
  115. menuScreen[ 19 ] = 0;
  116. menuScreen[ 20 ] = 1;
  117. }
  118. }
  119.  
  120. if( buttonState != lastButtonState && buttonState )
  121. {
  122. // Start game
  123. menu = false;
  124. ticks = 0;
  125. birdPos = 2;
  126. pipePos = 20;
  127. pipeGap = 3;
  128. gameOver = false;
  129. drawInGameScreen( 1 );
  130. }
  131. }
  132. else
  133. {
  134. // IN GAME
  135. // Clear the screen
  136. drawInGameScreen( 0 );
  137.  
  138. // Update the state
  139. ++ticks;
  140. if( !gameOver && ((ticks % 12) == 0) )
  141. {
  142. // Move the pipe
  143. int lastPipePos = pipePos;
  144. pipePos--;
  145. if( pipePos < -1 )
  146. {
  147. pipePos = 7;
  148. pipeGap = 1 + rand()%4;
  149. }
  150.  
  151. // Move the bird
  152. int lastBirdPos = birdPos;
  153. if( buttonState )
  154. {
  155. birdPos = max( birdPos - 1, 0 );
  156. }
  157. else
  158. {
  159. birdPos = birdPos + 1;
  160. if( birdPos >= 8 )
  161. {
  162. gameOver = true;
  163. pipePos = lastPipePos;
  164. birdPos = lastBirdPos;
  165. ticks = 0;
  166. }
  167. }
  168.  
  169. // Test for pipe collision
  170. if( (pipePos == 1 || pipePos == 0) && (birdPos < pipeGap || birdPos >= pipeGap + 3) )
  171. {
  172. gameOver = true;
  173. pipePos = lastPipePos;
  174. birdPos = lastBirdPos;
  175. ticks = 0;
  176. }
  177. }
  178. else if( gameOver && ticks >= 96 )
  179. {
  180. // Return to menu
  181. menu = true;
  182. }
  183.  
  184. // Redraw the screen
  185. if( !menu )
  186. {
  187. drawInGameScreen( 1 );
  188. }
  189. }
  190. lastButtonState = buttonState;
  191.  
  192. // Render
  193. int* screen = menu ? menuScreen : inGameScreen;
  194. for( int x=0; x<8; ++x )
  195. {
  196. for( int y=0; y<8; ++y )
  197. {
  198. int row = rowPins[y];
  199. if( row > 0 )
  200. {
  201. if( screen[ (7-y)+x*8 ] > 0 ) // Uncomment to rotate screen orientation
  202. //if( screen[ x+y*8 ] > 0 )
  203. {
  204. digitalWrite( row, HIGH );
  205. }
  206. else
  207. {
  208. digitalWrite( row, LOW );
  209. }
  210. }
  211. }
  212. int column = columnPins[x];
  213. if( column > 0 )
  214. {
  215. digitalWrite( column, LOW );
  216. }
  217. delay( 2 );
  218. if( column > 0 )
  219. {
  220. digitalWrite( column, HIGH );
  221. }
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement