Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

score.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #include "score.h"
  2. #include "xintc_l.h" // Provides handy macros for the interrupt controller.
  3. #define SCORE_START1 20
  4. #define SCORE_START2 360
  5. int score_position[10] = {SCORE_START1, SCORE_START1+12, SCORE_START1+24, SCORE_START1+36, SCORE_START1+48, \
  6. SCORE_START2, SCORE_START2+12, SCORE_START2+24, SCORE_START2+36, SCORE_START2+48};
  7. int score_x = 20;
  8. int score_y = 10;
  9. int number_position[4] = {100, 116, 132, 148};
  10. int number_x = 100;
  11. int number_y = 10;
  12. int tank_position[3] = {440, 480, 520};
  13. int tank_x = 440;
  14. int tank_y = 10;
  15. int lives = 3;
  16. #define GAMEOVER_X 250
  17. int gameover_position[8] = {GAMEOVER_X, GAMEOVER_X+12, GAMEOVER_X+24, GAMEOVER_X+36, GAMEOVER_X+64, GAMEOVER_X+76, GAMEOVER_X+88, GAMEOVER_X+100};
  18. int game_x = 250;
  19. int game_y = 200;
  20. int total_score = 0;
  21. #define TANK_HEIGHT 16
  22. #define TANK_WIDTH 32
  23. extern int tank[TANK_HEIGHT];
  24. void renderScore(unsigned int* framePointer) { // draw for the first time
  25. short x=0, y=0;
  26. int i;
  27. for(i=0; i<10; i++){
  28. score_x = score_position[i];
  29. switch(i){
  30. case 0:
  31. for(x = 0; x < SCORE_HEIGHT; x++) {
  32. for(y = 0; y < SCORE_WIDTH; y++){
  33. if ((s_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  34. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  35. } else {
  36. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  37. }
  38. }
  39. }
  40. break;
  41. case 1:
  42. for(x = 0; x < SCORE_HEIGHT; x++) {
  43. for(y = 0; y < SCORE_WIDTH; y++){
  44. if ((c_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  45. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  46. } else {
  47. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  48. }
  49. }
  50. }
  51. break;
  52. case 2:
  53. for(x = 0; x < SCORE_HEIGHT; x++) {
  54. for(y = 0; y < SCORE_WIDTH; y++){
  55. if ((o_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  56. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  57. } else {
  58. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  59. }
  60. }
  61. }
  62. break;
  63. case 3:
  64. for(x = 0; x < SCORE_HEIGHT; x++) {
  65. for(y = 0; y < SCORE_WIDTH; y++){
  66. if ((r_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  67. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  68. } else {
  69. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  70. }
  71. }
  72. }
  73. break;
  74. case 4:
  75. for(x = 0; x < SCORE_HEIGHT; x++) {
  76. for(y = 0; y < SCORE_WIDTH; y++){
  77. if ((e_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  78. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  79. } else {
  80. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  81. }
  82. }
  83. }
  84. break;
  85. case 5:
  86. for(x = 0; x < SCORE_HEIGHT; x++) {
  87. for(y = 0; y < SCORE_WIDTH; y++){
  88. if ((l_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  89. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  90. } else {
  91. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  92. }
  93. }
  94. }
  95. break;
  96. case 6:
  97. for(x = 0; x < SCORE_HEIGHT; x++) {
  98. for(y = 0; y < SCORE_WIDTH; y++){
  99. if ((i_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  100. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  101. } else {
  102. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  103. }
  104. }
  105. }
  106. break;
  107. case 7:
  108. for(x = 0; x < SCORE_HEIGHT; x++) {
  109. for(y = 0; y < SCORE_WIDTH; y++){
  110. if ((v_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  111. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  112. } else {
  113. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  114. }
  115. }
  116. }
  117. break;
  118. case 8:
  119. for(x = 0; x < SCORE_HEIGHT; x++) {
  120. for(y = 0; y < SCORE_WIDTH; y++){
  121. if ((e_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  122. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  123. } else {
  124. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  125. }
  126. }
  127. }
  128. break;
  129. case 9:
  130. for(x = 0; x < SCORE_HEIGHT; x++) {
  131. for(y = 0; y < SCORE_WIDTH; y++){
  132. if ((s_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  133. framePointer[(score_y+x)*640 + score_x+y] = 0x00FFFFFF;
  134. } else {
  135. framePointer[(score_y+x)*640 + score_x+y] = 0x00000000;
  136. }
  137. }
  138. }
  139. break;
  140. default :
  141. break;
  142. }
  143. }
  144. }
  145. void updateMyscore(int adding_score){
  146. total_score += adding_score;
  147. }
  148. void drawNumber(unsigned int* framePointer, int digit, int number){
  149. short x=0, y=0;
  150. for(x = 0; x < SCORE_HEIGHT; x++) {
  151. for(y = 0; y < SCORE_WIDTH; y++){
  152. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  153. }
  154. }
  155. switch(number){
  156. case 0:
  157. for(x = 0; x < SCORE_HEIGHT; x++) {
  158. for(y = 0; y < SCORE_WIDTH; y++){
  159. if ((o_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  160. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  161. } else {
  162. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  163. }
  164. }
  165. }
  166. break;
  167. case 1:
  168. for(x = 0; x < SCORE_HEIGHT; x++) {
  169. for(y = 0; y < SCORE_WIDTH; y++){
  170. if ((one_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  171. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  172. } else {
  173. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  174. }
  175. }
  176. }
  177. break;
  178. case 2:
  179. for(x = 0; x < SCORE_HEIGHT; x++) {
  180. for(y = 0; y < SCORE_WIDTH; y++){
  181. if ((two_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  182. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  183. } else {
  184. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  185. }
  186. }
  187. }
  188. break;
  189. case 3:
  190. for(x = 0; x < SCORE_HEIGHT; x++) {
  191. for(y = 0; y < SCORE_WIDTH; y++){
  192. if ((three_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  193. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  194. } else {
  195. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  196. }
  197. }
  198. }
  199. break;
  200. case 4:
  201. for(x = 0; x < SCORE_HEIGHT; x++) {
  202. for(y = 0; y < SCORE_WIDTH; y++){
  203. if ((four_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  204. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  205. } else {
  206. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  207. }
  208. }
  209. }
  210. break;
  211. case 5:
  212. for(x = 0; x < SCORE_HEIGHT; x++) {
  213. for(y = 0; y < SCORE_WIDTH; y++){
  214. if ((five_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  215. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  216. } else {
  217. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  218. }
  219. }
  220. }
  221. break;
  222. case 6:
  223. for(x = 0; x < SCORE_HEIGHT; x++) {
  224. for(y = 0; y < SCORE_WIDTH; y++){
  225. if ((six_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  226. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  227. } else {
  228. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  229. }
  230. }
  231. }
  232. break;
  233. case 7:
  234. for(x = 0; x < SCORE_HEIGHT; x++) {
  235. for(y = 0; y < SCORE_WIDTH; y++){
  236. if ((seven_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  237. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  238. } else {
  239. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  240. }
  241. }
  242. }
  243. break;
  244. case 8:
  245. for(x = 0; x < SCORE_HEIGHT; x++) {
  246. for(y = 0; y < SCORE_WIDTH; y++){
  247. if ((eight_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  248. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  249. } else {
  250. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  251. }
  252. }
  253. }
  254. break;
  255. case 9:
  256. for(x = 0; x < SCORE_HEIGHT; x++) {
  257. for(y = 0; y < SCORE_WIDTH; y++){
  258. if ((nine_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  259. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x0000FF00;
  260. } else {
  261. framePointer[(number_y+x)*640 + number_position[digit]+y] = 0x00000000;
  262. }
  263. }
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. void updateNumber(unsigned int* framePointer){
  271. int i;
  272. int first;
  273. int second;
  274. int third;
  275. int fourth;
  276. int temp_score = total_score;
  277. if(temp_score >= 1000){ // 4-digit scores
  278. for(i = 0; i < 4; i++){
  279. switch(i){
  280. case 0:
  281. fourth = 0;
  282. drawNumber(framePointer, 3, fourth);
  283. break;
  284. case 1:
  285. third = temp_score % 10;
  286. temp_score -= third;
  287. drawNumber(framePointer, 2, third);
  288. break;
  289. case 2:
  290. second = temp_score % 10;
  291. temp_score -= second;
  292. drawNumber(framePointer, 1, second);
  293. break;
  294. case 3:
  295. first = temp_score;
  296. drawNumber(framePointer, 0, first);
  297. break;
  298. default:
  299. break;
  300. }
  301. temp_score = temp_score / 10;
  302. }
  303. }
  304. else if(temp_score >= 100 && temp_score < 1000){ // 3-digit scores
  305. for(i = 0; i < 3; i++){
  306. switch(i){
  307. case 0:
  308. third = temp_score % 10;
  309. temp_score -= third;
  310. drawNumber(framePointer, 2, third);
  311. break;
  312. case 1:
  313. second = temp_score % 10;
  314. temp_score -= second;
  315. drawNumber(framePointer, 1, second);
  316. break;
  317. case 2:
  318. first = temp_score;
  319. drawNumber(framePointer, 0, first);
  320. break;
  321. default:
  322. break;
  323. }
  324. temp_score = temp_score / 10;
  325. }
  326. }
  327. else if(temp_score >= 10 && temp_score < 100){ // 2-digit scores
  328. for(i = 0; i < 2; i++){
  329. switch(i){
  330. case 0:
  331. second = temp_score % 10;
  332. temp_score -= second;
  333. drawNumber(framePointer, 1, second);
  334. break;
  335. case 1:
  336. first = temp_score;
  337. drawNumber(framePointer, 0, first);
  338. break;
  339. default:
  340. break;
  341. }
  342. temp_score = temp_score / 10;
  343. }
  344. }
  345. }
  346. void renderNumber(unsigned int* framePointer) { // draw for the first time
  347. short x=0, y=0;
  348. for(x = 0; x < SCORE_HEIGHT; x++) {
  349. for(y = 0; y < SCORE_WIDTH; y++){
  350. if ((o_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  351. framePointer[(number_y+x)*640 + number_position[0]+y] = 0x0000FF00;
  352. } else {
  353. framePointer[(number_y+x)*640 + number_position[0]+y] = 0x00000000;
  354. }
  355. }
  356. }
  357. }
  358. void renderTanks(unsigned int* framePointer) { // draw for the first time
  359. short x=0, y=0;
  360. int i;
  361. for(i=0; i<3; i++){
  362. tank_x = tank_position[i];
  363. switch(i){
  364. case 0:
  365. for(x = 0; x < TANK_HEIGHT; x++) {
  366. for(y = 0; y < TANK_WIDTH; y++){
  367. if ((tank[x%TANK_HEIGHT] & (1<<(TANK_WIDTH-1-y)))) {
  368. framePointer[(tank_y+x)*640 + tank_x+y] = 0x0000FF00;
  369. } else {
  370. framePointer[(tank_y+x)*640 + tank_x+y] = 0x00000000;
  371. }
  372. }
  373. }
  374. break;
  375. case 1:
  376. for(x = 0; x < TANK_HEIGHT; x++) {
  377. for(y = 0; y < TANK_WIDTH; y++){
  378. if ((tank[x%TANK_HEIGHT] & (1<<(TANK_WIDTH-1-y)))) {
  379. framePointer[(tank_y+x)*640 + tank_x+y] = 0x0000FF00;
  380. } else {
  381. framePointer[(tank_y+x)*640 + tank_x+y] = 0x00000000;
  382. }
  383. }
  384. }
  385. break;
  386. case 2:
  387. for(x = 0; x < TANK_HEIGHT; x++) {
  388. for(y = 0; y < TANK_WIDTH; y++){
  389. if ((tank[x%TANK_HEIGHT] & (1<<(TANK_WIDTH-1-y)))) {
  390. framePointer[(tank_y+x)*640 + tank_x+y] = 0x0000FF00;
  391. } else {
  392. framePointer[(tank_y+x)*640 + tank_x+y] = 0x00000000;
  393. }
  394. }
  395. }
  396. break;
  397. default :
  398. break;
  399. }
  400. }
  401. }
  402. void blankTanks(unsigned int* framePointer, int which_tank){
  403. short x=0, y=0;
  404. tank_x = tank_position[which_tank];
  405. for(x = 0; x < TANK_HEIGHT; x++) {
  406. for(y = 0; y < TANK_WIDTH; y++){
  407. framePointer[(tank_y+x)*640 + tank_x+y] = 0x00000000;
  408. }
  409. }
  410. }
  411. void gameOver(unsigned int* framePointer){
  412. short x=0, y=0;
  413. int i;
  414. int counter;
  415. XIntc_MasterDisable(XPAR_INTC_0_BASEADDR);
  416. setGameState(0);
  417. int row = 0;
  418. int col = 0;
  419. for(row=0; row<480; row++) {
  420. for(col=0; col<640; col++) {
  421. framePointer[row*640 + col] = 0x00000000;
  422. }
  423. }
  424. for(i=0; i<8; i++){
  425. counter = 0;
  426. game_x = gameover_position[i];
  427. switch(i){
  428. case 0:
  429. for(x = 0; x < SCORE_HEIGHT; x++) {
  430. for(y = 0; y < SCORE_WIDTH; y++){
  431. if ((g_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  432. framePointer[(game_y+x)*640 + game_x+y] = 0x00FF0000;
  433. } else {
  434. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  435. }
  436. }
  437. }
  438. break;
  439. case 1:
  440. for(x = 0; x < SCORE_HEIGHT; x++) {
  441. for(y = 0; y < SCORE_WIDTH; y++){
  442. if ((a_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  443. framePointer[(game_y+x)*640 + game_x+y] = 0x0000FF00;
  444. } else {
  445. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  446. }
  447. }
  448. }
  449. break;
  450. case 2:
  451. for(x = 0; x < SCORE_HEIGHT; x++) {
  452. for(y = 0; y < SCORE_WIDTH; y++){
  453. if ((m_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  454. framePointer[(game_y+x)*640 + game_x+y] = 0x000000FF;
  455. } else {
  456. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  457. }
  458. }
  459. }
  460. break;
  461. case 3:
  462. for(x = 0; x < SCORE_HEIGHT; x++) {
  463. for(y = 0; y < SCORE_WIDTH; y++){
  464. if ((e_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  465. framePointer[(game_y+x)*640 + game_x+y] = 0x00FFFF00;
  466. } else {
  467. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  468. }
  469. }
  470. }
  471. break;
  472. case 4:
  473. for(x = 0; x < SCORE_HEIGHT; x++) {
  474. for(y = 0; y < SCORE_WIDTH; y++){
  475. if ((o_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  476. framePointer[(game_y+x)*640 + game_x+y] = 0x0000FFFF;
  477. } else {
  478. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  479. }
  480. }
  481. }
  482. break;
  483. case 5:
  484. for(x = 0; x < SCORE_HEIGHT; x++) {
  485. for(y = 0; y < SCORE_WIDTH; y++){
  486. if ((v_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  487. framePointer[(game_y+x)*640 + game_x+y] = 0x00FF00FF;
  488. } else {
  489. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  490. }
  491. }
  492. }
  493. break;
  494. case 6:
  495. for(x = 0; x < SCORE_HEIGHT; x++) {
  496. for(y = 0; y < SCORE_WIDTH; y++){
  497. if ((e_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  498. framePointer[(game_y+x)*640 + game_x+y] = 0x008888FF;
  499. } else {
  500. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  501. }
  502. }
  503. }
  504. break;
  505. case 7:
  506. for(x = 0; x < SCORE_HEIGHT; x++) {
  507. for(y = 0; y < SCORE_WIDTH; y++){
  508. if ((r_block[x%SCORE_HEIGHT] & (1<<(SCORE_WIDTH-1-y)))) {
  509. framePointer[(game_y+x)*640 + game_x+y] = 0x00448844;
  510. } else {
  511. framePointer[(game_y+x)*640 + game_x+y] = 0x00000000;
  512. }
  513. }
  514. }
  515. break;
  516. default :
  517. break;
  518. }
  519. while(counter < 3000000){
  520. counter++;
  521. }
  522. }
  523. exit(0);
  524. }
  525. int tankDied(unsigned int* framePointer){
  526. switch(lives){
  527. case 3:
  528. blankTanks(framePointer, 2);
  529. break;
  530. case 2:
  531. blankTanks(framePointer, 1);
  532. break;
  533. case 1:
  534. blankTanks(framePointer, 0);
  535. break;
  536. case 0:
  537. gameOver(framePointer);
  538. return 1;
  539. break;
  540. default:
  541. break;
  542. }
  543. lives--;
  544. return 0;
  545. }