Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <plib.h>
  3. #include "PmodOLED.h"
  4. #include "OledChar.h"
  5. #include "OledGrph.h"
  6. #include "delay.h"
  7.  
  8. #pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select
  9. #pragma config FNOSC = PRIPLL // Oscillator selection
  10. #pragma config POSCMOD = XT // Primary oscillator mode
  11. #pragma config FPLLIDIV = DIV_2 // PLL input divider
  12. #pragma config FPLLMUL = MUL_20 // PLL multiplier
  13. #pragma config FPLLODIV = DIV_1 // PLL output divider
  14. #pragma config FPBDIV = DIV_8 // Peripheral bus clock divider
  15. #pragma config FSOSCEN = OFF // Secondary oscillator enable
  16.  
  17. // Global variable to pass the ADC reading to main
  18. volatile int ADC_reading1;
  19. volatile int ADC_reading2;
  20. volatile int ADC_conv;
  21.  
  22. int buttonstate = 0;
  23. int topScore = 0;
  24. int midScore = 0;
  25. int botScore = 0;
  26. int score = 0;
  27. //avatar position
  28. int cx = 0;
  29. int cy = 0;
  30. //prize position
  31. int px = 0;
  32. int py = 0;
  33. int pv = 0;
  34. //trap position
  35. int tx = 0;
  36. int ty = 0;
  37. int tv = 0;
  38. //collision detection
  39. int k = 0;
  40.  
  41. int paused = 0;
  42. //bitmaps
  43. unsigned char bitmap[] = {0x3C, 0x7E, 0xFF, 0xFF, 0xE7, 0xE7, 0x42, 0x00};
  44. unsigned char prizemap[] = {0x3c, 0x42, 0xA9, 0x85, 0x85, 0xA9, 0x42, 0x3C};
  45. unsigned char trapmap[] = {0x3C, 0x7E, 0xFF, 0xFF, 0xE7, 0xE7, 0x42, 0x00};
  46.  
  47. int lose = 0;
  48. int win = 0;
  49.  
  50. #define BUFLEN 64
  51. char buf[BUFLEN];
  52.  
  53. int diffLevel = 0;
  54.  
  55. #define OBJECTS 8
  56. #define OBJECTSM 10
  57. #define OBJECTSH 12
  58. #define HITSIZE 2
  59.  
  60. int objx[OBJECTS] = {0, 0, 0, 0, 0, 0, 0, 0}; //x co-ordinate of objects
  61. int objy[OBJECTS] = {0, 0, 0, 0, 0, 0, 0, 0}; //y co-ordinate of objects
  62. int objt[OBJECTS] = {0, 0, 0, 0, 0, 0, 0, 0}; //type of object: trap = 1, prize = 2, object exists = 0
  63.  
  64. int objxm[OBJECTSM] = {0,0,0,0,0,0,0,0,0,0};
  65. int objym[OBJECTSM] = {0,0,0,0,0,0,0,0,0,0};
  66. int objtm[OBJECTSM] = {0,0,0,0,0,0,0,0,0,0};
  67.  
  68. int objxh[OBJECTSH] = {0,0,0,0,0,0,0,0,0,0,0,0};
  69. int objyh[OBJECTSH] = {0,0,0,0,0,0,0,0,0,0,0,0};
  70. int objth[OBJECTSH] = {0,0,0,0,0,0,0,0,0,0,0,0};
  71.  
  72.  
  73.  
  74.  
  75. // The interrupt handler for the ADC
  76. // IPL7 highest interrupt priority
  77. void __ISR(_ADC_VECTOR, IPL7SRS) _ADCHandler(void) {
  78. ADC_conv++;
  79. ADC_reading1 = ReadADC10(0);
  80. ADC_reading2 = ReadADC10(1);
  81. INTClearFlag(INT_AD1);
  82. }
  83.  
  84. void ADCInit() {
  85. SetChanADC10(ADC_CH0_POS_SAMPLEA_AN2 |
  86. ADC_CH0_NEG_SAMPLEA_NVREF);
  87. OpenADC10( // config1
  88. ADC_FORMAT_INTG32 |
  89. ADC_CLK_AUTO |
  90. ADC_AUTO_SAMPLING_ON ,
  91. // config2
  92. ADC_VREF_AVDD_AVSS |
  93. ADC_SCAN_ON |
  94. ADC_SAMPLES_PER_INT_2 ,
  95. // config3
  96. ADC_SAMPLE_TIME_8 |
  97. ADC_CONV_CLK_20Tcy ,
  98. // configport
  99. ENABLE_AN2_ANA |
  100. ENABLE_AN3_ANA ,
  101. // configscan
  102. ~(SKIP_SCAN_AN2 |
  103. SKIP_SCAN_AN3)
  104. );
  105. EnableADC10();
  106.  
  107. // Set up, clear, and enable ADC interrupts
  108. INTSetVectorPriority(INT_ADC_VECTOR, INT_PRIORITY_LEVEL_7);
  109. INTClearFlag(INT_AD1);
  110. INTEnable(INT_AD1, INT_ENABLED);
  111. }
  112.  
  113. void __ISR(_CHANGE_NOTICE_VECTOR, IPL3AUTO) CNInterruptHandler(void)
  114. {
  115. //button 2 is pressed
  116. if (INTGetFlag(INT_CN))
  117. {
  118. if (PORTReadBits(IOPORT_G, BIT_6) == BIT_6)
  119. {
  120. buttonstate = 1;
  121. paused = 1;
  122. }
  123. else if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  124. {
  125. buttonstate = 0;
  126. }
  127. INTClearFlag(INT_CN);
  128. }
  129. }
  130.  
  131. void initChangeNotice()
  132. {
  133. mCNOpen(CN_ON, CN8_ENABLE | CN9_ENABLE, 0); //unsure of this
  134.  
  135. PORTRead(IOPORT_G);
  136.  
  137. INTSetPriority(INT_CN, 3);
  138.  
  139. INTClearFlag(INT_CN);
  140. INTEnable(INT_CN, 1);
  141. }
  142.  
  143. void SystemInit()
  144. {
  145. PORTSetPinsDigitalIn(IOPORT_G, BIT_6 | BIT_7);
  146. initChangeNotice();
  147.  
  148. PORTSetPinsAnalogIn (IOPORT_B, BIT_2);
  149. PORTSetPinsAnalogIn (IOPORT_B, BIT_3);
  150. PORTSetPinsDigitalOut(IOPORT_B, BIT_6);
  151. PORTSetBits (IOPORT_B, BIT_6);
  152.  
  153. ADCInit();
  154.  
  155. DelayInit();
  156. OledInit();
  157.  
  158. INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
  159.  
  160. INTEnableInterrupts();
  161.  
  162. INTEnableSystemMultiVectoredInt();
  163.  
  164. }
  165.  
  166. void movePlayer()
  167. {
  168. if (ADC_reading1 > 520 && ADC_reading2 >490 && ADC_reading2 < 520) //UD neutral, right
  169. {
  170. if (cx != 119)
  171. {
  172.  
  173. OledMoveTo(cx++, cy);
  174. OledPutBmp(8, 8, bitmap);
  175.  
  176. OledUpdate(); // look at it
  177. DelayMs(5); // short delay
  178.  
  179. OledMoveTo(cx, cy);
  180. OledPutBmp(8, 8, bitmap);
  181. OledUpdate();
  182. }
  183. //if reached right edge, remain there
  184. else
  185. {
  186. cx = 119;
  187. }
  188. }
  189. //move left
  190. if (ADC_reading1 < 490 && ADC_reading2 >490 && ADC_reading2 < 520) //UD neutral, Left
  191. {
  192. if (cx != 0)
  193. {
  194.  
  195.  
  196. OledMoveTo(cx--, cy);
  197. OledPutBmp(8, 8, bitmap);
  198.  
  199. OledUpdate(); // look at it
  200. DelayMs(5); // short delay
  201.  
  202. OledMoveTo(cx, cy);
  203. OledPutBmp(8, 8, bitmap);
  204. OledUpdate();
  205. }
  206. //if reached left edge, remain there
  207. else
  208. {
  209. cx = 0;
  210. }
  211. }
  212. //move down
  213. if (ADC_reading2 < 490 && ADC_reading1 >490 && ADC_reading1 < 520) //down
  214. {
  215. if (cy != 23)
  216. {
  217.  
  218.  
  219. OledMoveTo(cx, cy++);
  220. OledPutBmp(8, 8, bitmap);
  221.  
  222. OledUpdate(); // look at it
  223. DelayMs(5); // short delay
  224.  
  225. OledMoveTo(cx, cy);
  226. OledPutBmp(8, 8, bitmap);
  227. OledUpdate();
  228. }
  229. //if reached down edge, remain there
  230. else
  231. {
  232. cy = 23;
  233. }
  234. }
  235. //move up
  236. if (ADC_reading2 > 520 && ADC_reading1 >490 && ADC_reading1 < 520) //up
  237. {
  238. if (cy != 0)
  239. {
  240.  
  241.  
  242. OledMoveTo(cx, cy--);
  243. OledPutBmp(8, 8, bitmap);
  244.  
  245. OledUpdate(); // look at it
  246. DelayMs(5); // short delay
  247.  
  248. OledMoveTo(cx, cy);
  249. OledPutBmp(8, 8, bitmap);
  250. OledUpdate();
  251. }
  252. //if reached up edge, remain there
  253. else
  254. {
  255. cy = 0;
  256. }
  257. }
  258. //move up-right
  259. if (ADC_reading1 > 520 && ADC_reading2 > 520) //up and right
  260. {
  261. if (cx != 119 && cy != 0)
  262. {
  263. OledMoveTo(cx++, cy--);
  264. OledPutBmp(8, 8, bitmap);
  265.  
  266. OledUpdate(); // look at it
  267. DelayMs(5); // short delay
  268.  
  269. OledMoveTo(cx, cy);
  270. OledPutBmp(8, 8, bitmap);
  271. OledUpdate();
  272. }
  273. //if reached any edge, remain there
  274. else if (cx == 119)
  275. {
  276. cx = 119;
  277.  
  278. }
  279. else if (cy == 0)
  280. {
  281. cy = 0;
  282. }
  283.  
  284. }
  285. //move up-left
  286. if (ADC_reading1 < 490 && ADC_reading2 > 520) //up and left
  287. {
  288. if (cx != 0 && cy != 0)
  289. {
  290. OledMoveTo(cx--, cy--);
  291. OledPutBmp(8, 8, bitmap);
  292.  
  293. OledUpdate(); // look at it
  294. DelayMs(5); // short delay
  295.  
  296. OledMoveTo(cx, cy);
  297. OledPutBmp(8, 8, bitmap);
  298. OledUpdate();
  299. }
  300. else if (cx == 0)
  301. {
  302. cx = 0;
  303.  
  304. }
  305. else if (cy == 0)
  306. {
  307. cy = 0;
  308. }
  309.  
  310. }
  311. //move down-right
  312. if (ADC_reading1 > 520 && ADC_reading2 < 490) //down and right
  313. {
  314. if (cx != 119 && cy != 23)
  315. {
  316. OledMoveTo(cx++, cy++);
  317. OledPutBmp(8, 8, bitmap);
  318.  
  319. OledUpdate(); // look at it
  320. DelayMs(5); // short delay
  321.  
  322. OledMoveTo(cx, cy);
  323. OledPutBmp(8, 8, bitmap);
  324. OledUpdate();
  325. }
  326. //if reached any edge, remain there
  327. else if (cx == 119)
  328. {
  329. cx = 119;
  330.  
  331. }
  332. else if (cy == 23)
  333. {
  334. cy = 23;
  335. }
  336.  
  337. }
  338. //move down-left
  339. if (ADC_reading1 < 490 && ADC_reading2 < 490) //down and left
  340. {
  341. if (cx != 0 && cy != 23)
  342. {
  343. OledMoveTo(cx--, cy++);
  344. OledPutBmp(8, 8, bitmap);
  345.  
  346. OledUpdate(); // look at it
  347. DelayMs(5); // short delay
  348.  
  349. OledMoveTo(cx, cy);
  350. OledPutBmp(8, 8, bitmap);
  351. OledUpdate();
  352. }
  353. //if reached any edge, remain there
  354. else if (cx == 0)
  355. {
  356. cx = 0;
  357.  
  358. }
  359. else if (cy == 23)
  360. {
  361. cy = 23;
  362. }
  363. }
  364. }
  365.  
  366. int detectcollisionsingle(int ax, int ay, int vx, int vy)
  367. {
  368. int collx = ax - vx;
  369. int colly = ay - vy;
  370. collx = (collx < 0) ? -collx : collx;
  371. colly = (colly < 0) ? -colly : colly;
  372. if ((collx <= HITSIZE) && (colly <= HITSIZE))
  373. return 1;
  374. else
  375. return 0;
  376. }
  377.  
  378.  
  379. int detectcollision(int ax, int ay) {
  380. int i;
  381. if(diffLevel == 0)
  382. {
  383. for (i=0; i<OBJECTS; i++)
  384. if (detectcollisionsingle(ax, ay, objx[i], objy[i]))
  385. if (objt[i] > 0) // object exists
  386. return i;
  387. return -1;
  388. }
  389. else if(diffLevel == 1)
  390. {
  391. for (i=0; i<OBJECTSM; i++)
  392. if (detectcollisionsingle(ax, ay, objxm[i], objym[i]))
  393. if (objtm[i] > 0) // object exists
  394. return i;
  395. return -1;
  396. }
  397. else if (diffLevel == 2)
  398. {
  399. for (i=0; i<OBJECTSH; i++)
  400. if (detectcollisionsingle(ax, ay, objxh[i], objyh[i]))
  401. if (objth[i] > 0) // object exists
  402. return i;
  403. return -1;
  404. }
  405.  
  406. return -1;
  407.  
  408. }
  409.  
  410. //code for prizes
  411. void prizePut() {
  412. OledSetDrawColor(1);
  413. OledSetDrawMode(modOledXor);
  414. OledMoveTo(px, py);
  415. OledPutBmp(8, 8, prizemap);
  416. OledUpdate();
  417. }
  418.  
  419. //starts with invisible
  420. void prizeShow(int n) {
  421. // make prize visible (n=1) or invisible (n=0)
  422. if (pv =! n) {
  423. pv = n;
  424. prizePut();
  425. }
  426. }
  427.  
  428. //code for traps
  429. void trapPut() {
  430. OledSetDrawColor(1);
  431. OledSetDrawMode(modOledXor);
  432. OledMoveTo(tx, ty);
  433. OledPutBmp(8, 8, trapmap);
  434. OledUpdate();
  435. }
  436.  
  437. //starts with invisible
  438. void trapShow(int n) {
  439. // make trap visible (n=1) or invisible (n=0)
  440. if (tv =! n) {
  441. tv = n;
  442. trapPut();
  443. }
  444. }
  445.  
  446.  
  447.  
  448. int youWin()
  449. {
  450. int i;
  451. if (diffLevel == 0)
  452. {
  453.  
  454. for (i = 0; i < OBJECTS; i++)
  455. {
  456. //check if obj is a prize
  457. if (objt[i] == 2)
  458. {
  459. //prize exists so have not won yet
  460. return 0;
  461. }
  462. }
  463. }
  464. else if (diffLevel == 1)
  465. {
  466. for (i = 0; i < OBJECTSM; i++)
  467. {
  468. //check if obj is a prize
  469. if (objtm[i] == 2)
  470. {
  471. //prize exists so have not won yet
  472. return 0;
  473. }
  474. }
  475. }
  476. else if (diffLevel == 2)
  477. {
  478. for (i = 0; i < OBJECTSH; i++)
  479. {
  480. //check if obj is a prize
  481. if (objth[i] == 2)
  482. {
  483. //prize exists so have not won yet
  484. return 0;
  485. }
  486. }
  487. }
  488.  
  489. //no prize was found. All prizes have been claimed
  490. return 1;
  491. }
  492.  
  493. void theGameisOn()
  494. {
  495.  
  496. snprintf(buf, 16, "%d", score); //updates in the background as timer2 continues
  497. OledSetCursor(111, 0);
  498. OledPutString(buf);
  499.  
  500. movePlayer();
  501.  
  502. k = detectcollision(cx, cy); //use avatar's co-ordinates
  503.  
  504. if (k != -1)
  505. {
  506. if (diffLevel == 0)
  507. {
  508. if (objt[k] == 1)
  509. {
  510. //trap
  511. tx = objx[k];
  512. ty = objy[k];
  513. trapShow(0);
  514. score--;
  515. }
  516. else if (objt[k] == 2)
  517. {
  518. //prize
  519. px = objx[k];
  520. py = objy[k];
  521. prizeShow(0);
  522. score++;
  523. }
  524.  
  525. //erase obj
  526. objt[k] = 0;
  527. }
  528. else if (diffLevel == 1)
  529. {
  530. if (objtm[k] == 1)
  531. {
  532. //trap
  533. tx = objxm[k];
  534. ty = objym[k];
  535. trapShow(0);
  536. score--;
  537. }
  538. else if (objtm[k] == 2)
  539. {
  540. //prize
  541. px = objxm[k];
  542. py = objym[k];
  543. prizeShow(0);
  544. score++;
  545. }
  546.  
  547. objtm[k] = 0;
  548. }
  549. else if (diffLevel == 2)
  550. {
  551. if (objth[k] == 1)
  552. {
  553. //trap
  554. tx = objxh[k];
  555. ty = objyh[k];
  556. trapShow(0);
  557. score--;
  558. }
  559. else if (objth[k] == 2)
  560. {
  561. //prize
  562. px = objxh[k];
  563. py = objyh[k];
  564. prizeShow(0);
  565. score++;
  566. }
  567.  
  568. objth[k] = 0;
  569. }
  570. }
  571.  
  572. if (score < 0)
  573. {
  574. lose = 1;
  575. }
  576. else if (youWin()==1)
  577. {
  578. win = 1;
  579. }
  580. }
  581.  
  582. int main() {
  583.  
  584. SystemInit();
  585.  
  586. int random;
  587. //main menu joystick position
  588. int mx = 0;
  589. int my = 0;
  590.  
  591. //difficulty menu joystick position
  592. int dx = 0;
  593. int dy = 0;
  594.  
  595. win = 0;
  596. lose = 0;
  597.  
  598. diffLevel = 0;
  599.  
  600. OledClearBuffer();
  601.  
  602. OledSetDrawColor(1);
  603. OledSetDrawMode(modOledXor);
  604.  
  605. while (1) {
  606.  
  607. enum tState {main, difficulty, howToPlay, scoreboard, play, pause};
  608. static enum tState menu = main;
  609.  
  610. switch (menu)
  611. {
  612. case main:
  613. OledSetCursor(0, 0);
  614. OledPutString("Difficulty ");
  615. OledSetCursor(0, 1);
  616. OledPutString("How to play ");
  617. OledSetCursor(0, 2);
  618. OledPutString("Scoreboard ");
  619. OledSetCursor(0, 3);
  620. OledPutString("Play ");
  621.  
  622. win = 0;
  623. lose = 0;
  624. score = 0;
  625. srand(ReadTimer1());
  626.  
  627. //safety check
  628. if (my < 0)
  629. {
  630. my = 0;
  631. }
  632. else if (my > 3)
  633. {
  634. my = 3;
  635. }
  636.  
  637. //make that line blink
  638. OledSetCursor(0, my);
  639. OledPutString(" ");
  640. DelayMs(25);
  641.  
  642.  
  643. //Up, made less sensitive
  644. if (ADC_reading2 > 900 && ADC_reading1 > 200 && ADC_reading1 < 700)
  645. {
  646. if (my != 0)
  647. {
  648. my--; //move up one position
  649.  
  650. }
  651. else
  652. {
  653. my = 0;
  654. }
  655. }
  656. //Down
  657. if (ADC_reading2 < 100 && ADC_reading1 > 200 && ADC_reading1 < 700)
  658. {
  659. //safety checking second time
  660. if (my != 3)
  661. {
  662. my++;
  663. }
  664. else
  665. {
  666. my = 3;
  667. }
  668. }
  669.  
  670. //Pressed either left or right
  671. if (ADC_reading1 > 700 | ADC_reading1 < 200 && ADC_reading2 > 100 && ADC_reading2 < 900)
  672. {
  673. if (my == 0)
  674. {
  675. OledClear();
  676. menu = difficulty;
  677. }
  678. else if (my == 1)
  679. {
  680. //how to play
  681. OledClear();
  682. OledMoveTo(48,23);
  683. OledPutBmp(8, 8, prizemap);
  684. OledUpdate();
  685. OledMoveTo(111,23);
  686. OledPutBmp(8, 8, trapmap);
  687. OledUpdate();
  688. menu = howToPlay;
  689. }
  690. else if (my == 2)
  691. {
  692. OledClear();
  693. menu = scoreboard;
  694. }
  695. else if (my == 3)
  696. {
  697.  
  698. OledClear();
  699. cx = 0;
  700. cy = 0;
  701. OledMoveTo(cx,cy);
  702. OledPutBmp(8, 8, bitmap);
  703. OledUpdate();
  704.  
  705. int i;
  706. int j;
  707.  
  708. if (diffLevel == 0)
  709. {
  710. int i;
  711. for (i = 0; i < OBJECTS; i++)
  712. {
  713. random = rand() % 119;
  714. if(random < 8)
  715. objx[i] = random + 8;
  716. else
  717. objx[i] = random;
  718.  
  719. random = rand() % 23;
  720. if (random < 8)
  721. objy[i] = random + 8;
  722. else
  723. objy[i] = random;
  724.  
  725. //check that no traps and prizes spawn in the same location
  726. for (j = 0; j < i; j++)
  727. {
  728. if (objx[j] == objx[i])
  729. objx[i] = rand() % 119;
  730. if (objy[j] == objy[i])
  731. objy[i] = rand() % 23;
  732. }
  733.  
  734. for (j = 0; j < i; j++)
  735. {
  736. if (objx[j] == objx[i])
  737. objx[i] = rand() % 119;
  738. if (objy[j] == objy[i])
  739. objy[i] = rand() % 23;
  740. }
  741.  
  742. //i is current position
  743. //j is looping through menu
  744. for (j = 0; j < i; j++)
  745. {
  746. //if difference is less than 8
  747. //int diff = abs(objx[i]-objx[j]);
  748.  
  749. if (abs(objx[i]-objx[j]) < 8 && abs(objy[i]-objy[j]) < 8 )
  750. {
  751. //call r
  752. //objx[i] = objx[i] + (objx[i]-objx[j]);
  753. //objy[i] = objy[i] + (objx[i]-objx[j]);
  754. i--;
  755. }
  756.  
  757. }
  758. //px = objx[i];
  759. //py = objy[i];
  760.  
  761. }
  762. //number of prizes = 6
  763. //number of traps = 2
  764.  
  765.  
  766. for (i = 0; i < OBJECTS; i++)
  767. {
  768. if (i < 4)
  769. {
  770. objt[i] = 2;
  771. px = objx[i];
  772. py = objy[i];
  773. prizePut();
  774. }
  775. else
  776. {
  777. objt[i] = 1;
  778. tx = objx[i];
  779. ty = objy[i];
  780. trapPut();
  781. }
  782. }
  783. }
  784. else if (diffLevel == 1)
  785. {
  786. //number of prizes = 5
  787. //number of traps = 5
  788.  
  789.  
  790.  
  791. for (i = 0; i < OBJECTSM; i++)
  792. {
  793. random = rand() % 119;
  794. if(random < 8)
  795. objxm[i] = random + 8;
  796. else
  797. objxm[i] = random;
  798.  
  799. random = rand() % 23;
  800. if (random < 8)
  801. objym[i] = random + 8;
  802. else
  803. objym[i] = random;
  804.  
  805. //check that no traps and prizes spawn in the same location
  806. for (j = 0; j < i; j++)
  807. {
  808. if (objxm[j] == objxm[i])
  809. objxm[i] = rand() % 119;
  810. if (objym[j] == objym[i])
  811. objym[i] = rand() % 23;
  812. }
  813.  
  814. for (j = 0; j < i; j++)
  815. {
  816. if (objxm[j] == objxm[i])
  817. objxm[i] = rand() % 119;
  818. if (objym[j] == objym[i])
  819. objym[i] = rand() % 23;
  820. }
  821.  
  822. //i is current position
  823. //j is looping through menu
  824. for (j = 0; j < i; j++)
  825. {
  826. //if difference is less than 8
  827. if (abs(objxm[i]-objxm[j]) < 8 && abs(objym[i]-objym[j]) < 8 )
  828. {
  829. i--;
  830. }
  831. }
  832.  
  833.  
  834. }
  835. //number of prizes = 6
  836. //number of traps = 2
  837.  
  838.  
  839. for (i = 0; i < OBJECTSM; i++)
  840. {
  841. if (i < 5)
  842. {
  843. objtm[i] = 2;
  844. px = objxm[i];
  845. py = objym[i];
  846. prizePut();
  847. }
  848. else
  849. {
  850. objtm[i] = 1;
  851. tx = objxm[i];
  852. ty = objym[i];
  853. trapPut();
  854. }
  855. }
  856.  
  857.  
  858. }
  859.  
  860. else if (diffLevel == 2)
  861. {
  862. //number of prizes = 2
  863. //number of traps = 6
  864.  
  865.  
  866. for (i = 0; i < OBJECTSH; i++)
  867. {
  868. random = rand() % 119;
  869. if(random < 8)
  870. objxh[i] = random + 8;
  871. else
  872. objxh[i] = random;
  873.  
  874. random = rand() % 23;
  875. if (random < 8)
  876. objyh[i] = random + 8;
  877. else
  878. objyh[i] = random;
  879.  
  880. //check that no traps and prizes spawn in the same location
  881. for (j = 0; j < i; j++)
  882. {
  883. if (objxh[j] == objxh[i])
  884. objxh[i] = rand() % 119;
  885. if (objyh[j] == objyh[i])
  886. objyh[i] = rand() % 23;
  887. }
  888.  
  889. for (j = 0; j < i; j++)
  890. {
  891. if (objxh[j] == objxh[i])
  892. objxh[i] = rand() % 119;
  893. if (objyh[j] == objyh[i])
  894. objyh[i] = rand() % 23;
  895. }
  896.  
  897. //i is current position
  898. //j is looping through array
  899. for (j = 0; j < i; j++)
  900. {
  901. if (abs(objxh[i]-objxh[j]) < 8 && abs(objyh[i]-objyh[j]) < 8 )
  902. {
  903. //call r
  904. //objx[i] = objx[i] + (objx[i]-objx[j]);
  905. //objy[i] = objy[i] + (objx[i]-objx[j]);
  906. i--;
  907. }
  908. }
  909.  
  910.  
  911. }
  912.  
  913.  
  914. for (i = 0; i < OBJECTSH; i++)
  915. {
  916. if (i < 6)
  917. {
  918. objth[i] = 2;
  919. px = objxh[i];
  920. py = objyh[i];
  921. prizePut();
  922. }
  923. else
  924. {
  925. objth[i] = 1;
  926. tx = objxh[i];
  927. ty = objyh[i];
  928. trapPut();
  929. }
  930. }
  931. }
  932.  
  933. menu = play;
  934. //spawn the bitmap
  935. }
  936.  
  937. //reset my before changing state
  938. my = 0;
  939. }
  940.  
  941.  
  942. break;
  943.  
  944. case difficulty:
  945. OledSetCursor(0, 0);
  946. OledPutString("Easy ");
  947. OledSetCursor(0, 1);
  948. OledPutString("Medium ");
  949. OledSetCursor(0, 2);
  950. OledPutString("Hard ");
  951.  
  952.  
  953. if (dy < 0)
  954. {
  955. dy = 0;
  956. }
  957. else if (dy > 2)
  958. {
  959. dy = 2;
  960. }
  961.  
  962. //make that line blink
  963. OledSetCursor(0, dy);
  964. OledPutString(" ");
  965. DelayMs(25);
  966.  
  967. if (ADC_reading2 > 950 && ADC_reading1 > 200 && ADC_reading1 < 900)
  968. {
  969. if (dy != 0)
  970. {
  971. dy--; //move up one position
  972.  
  973. }
  974. else
  975. {
  976. dy = 0;
  977. }
  978. }
  979. //Down
  980. if (ADC_reading2 < 50 && ADC_reading1 > 200 && ADC_reading1 < 900)
  981. {
  982. //safety checking second time
  983. if (dy != 2)
  984. {
  985. dy++;
  986. }
  987. else
  988. {
  989. dy = 2;
  990. }
  991. }
  992.  
  993. //Pressed either left or right
  994. if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)//ADC_reading1 > 900 | ADC_reading1 < 200 && ADC_reading2 > 50 && ADC_reading2 < 950)
  995. {
  996. if (dy == 0)
  997. {
  998. diffLevel = 0;
  999. }
  1000. else if (dy == 1)
  1001. {
  1002. diffLevel = 1;
  1003. }
  1004. else if (dy == 2)
  1005. {
  1006. diffLevel = 2;
  1007. }
  1008.  
  1009. menu = main;
  1010. //don't reset dy
  1011. }
  1012.  
  1013. /*
  1014. if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  1015. {
  1016. menu = main;
  1017. }
  1018. */
  1019. break;
  1020.  
  1021. case howToPlay:
  1022.  
  1023. //display how to play game here
  1024. //with bitmap of trap and prize
  1025. OledSetCursor(0, 0);
  1026. OledPutString("Gather candles");
  1027. OledSetCursor(0, 1);
  1028. OledPutString("avoid skulls to");
  1029. OledSetCursor(0, 2);
  1030. OledPutString("make ghost happy.");
  1031. OledSetCursor(0,3);
  1032. OledPutString("Prize:");
  1033. OledSetCursor(8,3);
  1034. OledPutString("Trap:");
  1035.  
  1036.  
  1037. //display bitmaps
  1038.  
  1039. if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  1040. {
  1041. menu = main;
  1042. }
  1043. break;
  1044.  
  1045. case scoreboard:
  1046. snprintf(buf, BUFLEN, "1: %3d ", topScore); //updates in the background as timer2 continues
  1047. OledSetCursor(0,0);
  1048. OledPutString(buf);
  1049. snprintf(buf, BUFLEN, "2: %3d ", midScore); //updates in the background as timer2 continues
  1050. OledSetCursor(0,1);
  1051. OledPutString(buf);
  1052. snprintf(buf, BUFLEN, "3: %3d ", botScore); //updates in the background as timer2 continues
  1053. OledSetCursor(0,2);
  1054. OledPutString(buf);
  1055.  
  1056. if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  1057. {
  1058. menu = main;
  1059. }
  1060. break;
  1061.  
  1062. case play:
  1063.  
  1064. theGameisOn();
  1065.  
  1066. if (win == 1)
  1067. {
  1068.  
  1069. OledClear();
  1070. OledSetCursor(0, 1);
  1071. OledPutString("You win!");
  1072. DelayMs(3000);
  1073.  
  1074. //update score
  1075. if (score > topScore)
  1076. {
  1077. topScore = score;
  1078. }
  1079. else if (score > midScore)
  1080. {
  1081. midScore = score;
  1082. }
  1083. else if (score > botScore)
  1084. {
  1085. botScore = score;
  1086. }
  1087.  
  1088. menu = main;
  1089. }
  1090. else if (lose == 1)
  1091. {
  1092.  
  1093. OledClear();
  1094. OledSetCursor(0, 1);
  1095. OledPutString("You lose.");
  1096. DelayMs(3000);
  1097.  
  1098. //update score
  1099. if (score > topScore)
  1100. {
  1101. topScore = score;
  1102. }
  1103. else if (score > midScore)
  1104. {
  1105. midScore = score;
  1106. }
  1107. else if (score > botScore)
  1108. {
  1109. botScore = score;
  1110. }
  1111.  
  1112.  
  1113. menu = main;
  1114. }
  1115.  
  1116. else if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  1117. {
  1118. OledClear();
  1119.  
  1120. menu = main;
  1121. }
  1122. else if (paused == 1)
  1123. {
  1124. menu = pause;
  1125. }
  1126. break;
  1127.  
  1128. case pause:
  1129.  
  1130. //so I guess do nothing here
  1131.  
  1132.  
  1133. if (PORTReadBits(IOPORT_G, BIT_7) == BIT_7)
  1134. {
  1135. OledClear();
  1136. menu = main;
  1137. }
  1138. else if (PORTReadBits(IOPORT_G, BIT_6) == BIT_6)
  1139. {
  1140. paused = 0;
  1141. menu = play;
  1142. }
  1143. break;
  1144.  
  1145.  
  1146.  
  1147. }
  1148.  
  1149. }
  1150.  
  1151. return EXIT_SUCCESS;
  1152.  
  1153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement