Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.24 KB | None | 0 0
  1. //{ Defines
  2. //{ Port E
  3. #define GPIO_E 0x40021000
  4. #define GPIO_E_MODER ((volatile unsigned int *) (GPIO_E))
  5. #define GPIO_E_OTYPER ((volatile unsigned short *) (GPIO_E+0x4))
  6. #define GPIO_E_OSPEEDR ((volatile unsigned short *) (GPIO_E+0x8))
  7. #define GPIO_E_PUPDR ((volatile unsigned int *) (GPIO_E+0xC))
  8. #define GPIO_E_IDRLOW ((volatile unsigned char *) (GPIO_E+0x10))
  9. #define GPIO_E_IDRHIGH ((volatile unsigned char *) (GPIO_E+0x11))
  10. #define GPIO_E_ODRLOW ((volatile unsigned char *) (GPIO_E+0x14))
  11. #define GPIO_E_ODRHIGH ((volatile unsigned char *) (GPIO_E+0x15))
  12. //}
  13. //{ Port D
  14. #define GPIO_D 0x40020C00
  15. #define GPIO_D_MODER ((volatile unsigned int *) (GPIO_D))
  16. #define GPIO_D_OTYPER ((volatile unsigned short *) (GPIO_D+0x4))
  17. #define GPIO_D_OSPEEDR ((volatile unsigned short *) (GPIO_D+0x8))
  18. #define GPIO_D_PUPDR ((volatile unsigned int *) (GPIO_D+0xC))
  19. #define GPIO_D_IDRLOW ((volatile unsigned char *) (GPIO_D+0x10))
  20. #define GPIO_D_IDRHIGH ((volatile unsigned char *) (GPIO_D+0x11))
  21. #define GPIO_D_ODRLOW ((volatile unsigned char *) (GPIO_D+0x14))
  22. #define GPIO_D_ODRHIGH ((volatile unsigned char *) (GPIO_D+0x15))
  23. //}
  24. //{ LCD
  25. #define B_E 0x40 // Enable
  26. #define B_RST 0x20 // Reset
  27. #define B_CS2 0x10 // Controller Select 2
  28. #define B_CS1 8 // Controller Select 1
  29. #define B_SELECT 4 // 0 Graphics, 1 ASCII
  30. #define B_RW 2 // 0 Write, 1 Read
  31. #define B_RS 1 // 0 Command, 1 Data
  32.  
  33. #define LCD_ON 0x3F // Display on
  34. #define LCD_OFF 0x3E // Display off
  35. #define LCD_SET_ADD 0x40 // Set horizontal coordinate
  36. #define LCD_SET_PAGE 0xB8 // Set vertical coordinate
  37. #define LCD_DISP_START 0xC0 // Start address
  38. #define LCD_BUSY 0x80 // Read busy status
  39. //}
  40. //{ Typedef uint8
  41. typedef unsigned char uint8_t;
  42. //}
  43. //{ STK (Systick)
  44. #define STK_CTRL ((volatile unsigned int *)(0xE000E010))
  45. #define STK_LOAD ((volatile unsigned int *)(0xE000E014))
  46. #define STK_VAL ((volatile unsigned int *)(0xE000E018))
  47. //}
  48. //{ Typedef Struct
  49. typedef struct tPoint
  50. {
  51. uint8_t x;
  52. uint8_t y;
  53. }POINT;
  54.  
  55. #define MAX_POINTS 20
  56.  
  57. typedef struct tGeometry
  58. {
  59. int numPoints;
  60. int sizex;
  61. int sizey;
  62. POINT px[ MAX_POINTS];
  63. } GEOMETRY, *PGEOMETRY;
  64.  
  65. typedef struct tObj{
  66. PGEOMETRY geo;
  67. int dirx,diry;
  68. int posx,posy;
  69. void (* draw) (struct tObj *);
  70. void (* clear) (struct tObj *);
  71. void (* move) (struct tObj *, struct tObj *,struct tObj * );
  72. void (* set_speed) (struct tObj *, int, int);
  73. } OBJECT, *POBJECT;
  74. //}
  75. //}
  76. //{ Keyboard Handler
  77. unsigned char keyb(void){
  78. unsigned char key[]={1,2,3,0xA,4,5,6,0xB,7,8,9,0xC,0xE,0,0xF,0xD};
  79. int row, col;
  80.  
  81. for (row=1; row <=4 ; row++ ) {
  82. kbdActivate( row );
  83. if( (col = kbdGetCol () ) )
  84. {
  85. kbdActivate( 0 );
  86. return key [4*(row-1)+(col-1) ];
  87. }
  88. }
  89. kbdActivate( 0 );
  90. return 0xFF;
  91. }
  92. int kbdGetCol ( void ){
  93. /* Om någon tangent (i aktiverad rad)
  94. * är nedtryckt, returnera dess kolumnnummer,
  95. * annars, returnera 0 */
  96. unsigned char c;
  97. c = *GPIO_D_IDRHIGH;
  98. if ( c & 0x8 ) return 4;
  99. if ( c & 0x4 ) return 3;
  100. if ( c & 0x2 ) return 2;
  101. if ( c & 0x1 ) return 1;
  102. return 0;
  103. }
  104. void kbdActivate(unsigned int row){
  105. switch(row)
  106. {
  107. case 1: *GPIO_D_ODRHIGH = 0x10; break;
  108. case 2: *GPIO_D_ODRHIGH = 0x20; break;
  109. case 3: *GPIO_D_ODRHIGH = 0x40; break;
  110. case 4: *GPIO_D_ODRHIGH = 0x80; break;
  111. case 0: *GPIO_D_ODRHIGH = 0x00; break;
  112. }
  113. }
  114. //}
  115. //{ Startups
  116. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  117. void startup ( void )
  118. {
  119. __asm volatile(
  120. " LDR R0,=0x2001C000\n" /* set stack */
  121. " MOV SP,R0\n"
  122. " BL main\n" /* call main */
  123. "_exit: B .\n" /* never return */
  124. ) ;
  125. }
  126. //}
  127. //{ Delay
  128. void delay_250ns( void )
  129. {
  130. #ifdef SIMULATOR
  131. return;
  132. #endif
  133. /* SystemCoreClock = 168000000 */
  134. *STK_CTRL = 0;
  135. *STK_LOAD = ( (168/4) -1 );
  136. *STK_VAL = 0;
  137. *STK_CTRL = 5;
  138. while( (*STK_CTRL & 0x10000 )== 0 );
  139. *STK_CTRL = 0;
  140. }
  141. void delay_500ns( void )
  142. {
  143. #ifdef SIMULATOR
  144. return;
  145. #endif
  146. delay_250ns();
  147. delay_250ns();
  148. }
  149.  
  150.  
  151. void delay_micro(unsigned int us)
  152. {
  153. #ifdef SIMULATOR
  154. return;
  155. #endif
  156. #ifdef SIMULATOR
  157. us = us / 1000;//1000;
  158. us++;
  159. #endif
  160. while( us > 0 )
  161. {
  162. delay_250ns();
  163. delay_250ns();
  164. delay_250ns();
  165. delay_250ns();
  166. us--;
  167. }
  168. }
  169.  
  170. void delay_milli(unsigned int ms)
  171. {
  172. #ifdef SIMULATOR
  173. return;
  174. #endif
  175. #ifdef SIMULATOR
  176. ms = ms / 1000;// 1000;
  177. ms++;
  178. #endif
  179.  
  180. delay_micro(ms*1000);
  181. }
  182. //}
  183. //{ LCD
  184. void graphic_ctrl_bit_set(unsigned char x)
  185. {
  186. unsigned char c;
  187. c = *GPIO_E_ODRLOW;
  188. c |= x;
  189. c &= ~B_SELECT;
  190. *GPIO_E_ODRLOW = c;
  191. }
  192. void graphic_ctrl_bit_clear(unsigned char x)
  193. {
  194. unsigned char c;
  195. c=*GPIO_E_ODRLOW;
  196. c&=(~B_SELECT & ~x);
  197. *GPIO_E_ODRLOW = c;
  198. }
  199. void select_controller(unsigned char controller){
  200. switch(controller){
  201. case 0:
  202. graphic_ctrl_bit_clear(B_CS1|B_CS2);
  203. break;
  204. case B_CS1 :
  205. graphic_ctrl_bit_set(B_CS1);
  206. graphic_ctrl_bit_clear(B_CS2);
  207. break;
  208. case B_CS2 :
  209. graphic_ctrl_bit_set(B_CS2);
  210. graphic_ctrl_bit_clear(B_CS1);
  211. break;
  212. case B_CS1|B_CS2 :
  213. graphic_ctrl_bit_set(B_CS1|B_CS2);
  214. break;
  215. }
  216. }
  217. void graphic_wait_ready()
  218. {
  219. graphic_ctrl_bit_clear(B_E);
  220. *GPIO_E_MODER = 0x00005555;
  221. graphic_ctrl_bit_clear(B_RS);
  222.  
  223. graphic_ctrl_bit_set(B_RW);
  224. delay_500ns();
  225. while(1)
  226. {
  227. graphic_ctrl_bit_set(B_E);
  228. delay_500ns();
  229. uint8_t c = *GPIO_E_IDRHIGH & LCD_BUSY;
  230. graphic_ctrl_bit_clear(B_E);
  231. delay_500ns();
  232. if(c == 0) //& LCD_BUSY)
  233. break;
  234. }
  235. *GPIO_E_MODER = 0x55555555;;
  236.  
  237. }
  238. uint8_t graphic_read(unsigned char controller)
  239. {
  240. graphic_ctrl_bit_clear(B_E);
  241. *GPIO_E_MODER = 0x00005555;
  242. select_controller(controller);
  243. graphic_ctrl_bit_set(B_RS |B_RW);
  244. delay_500ns();
  245. graphic_ctrl_bit_set(B_E);
  246. delay_500ns();
  247. uint8_t rv = *GPIO_E_IDRHIGH;
  248. *GPIO_E_MODER = 0x55555555;
  249. if(controller == B_CS1)
  250. {
  251. select_controller(B_CS1);
  252. graphic_wait_ready();
  253. }
  254. if(controller == B_CS2)
  255. {
  256. select_controller(B_CS2);
  257. graphic_wait_ready();
  258. }
  259. return rv;
  260. }
  261. void graphic_write(unsigned char value, unsigned char controller)
  262. {
  263. *GPIO_E_ODRHIGH = value;
  264. select_controller(controller);
  265. delay_500ns();
  266. graphic_ctrl_bit_set(B_E);
  267. delay_500ns();
  268. graphic_ctrl_bit_clear(B_E);
  269. if(controller == B_CS1)
  270. {
  271. select_controller(B_CS1);
  272. graphic_wait_ready();
  273. }
  274. if(controller == B_CS2)
  275. {
  276. select_controller(B_CS2);
  277. graphic_wait_ready();
  278. }
  279. *GPIO_E_ODRHIGH = 0;
  280. graphic_ctrl_bit_set(B_E);
  281. select_controller(0);
  282. }
  283.  
  284. void graphic_write_command(unsigned char command,unsigned char controller)
  285. {
  286. graphic_ctrl_bit_clear(B_E);
  287. select_controller(controller);
  288. graphic_ctrl_bit_clear(B_RW);
  289. graphic_ctrl_bit_clear(B_RS);
  290. graphic_write(command,controller);
  291. }
  292. void graphic_write_data(unsigned char data,unsigned char controller)
  293. {
  294. graphic_ctrl_bit_clear(B_E);
  295. select_controller(controller);
  296. graphic_ctrl_bit_clear(B_RW);
  297. graphic_ctrl_bit_set(B_RS);
  298. graphic_write(data,controller);
  299. }
  300. unsigned char graphic_read_data(unsigned char controller)
  301. {
  302. (void) graphic_read(controller); /* returnerar nonses */
  303. return graphic_read(controller); // returnerar det riktiga värdet
  304. }
  305. void graphic_init()
  306. {
  307. graphic_ctrl_bit_set(B_E);
  308. delay_micro(10);
  309. graphic_ctrl_bit_clear(B_CS1 | B_CS2 | B_RST | B_E);
  310. delay_milli(30);
  311. graphic_ctrl_bit_set(B_RST);
  312. graphic_write_command(LCD_OFF, B_CS1|B_CS2);
  313. graphic_write_command(LCD_ON, B_CS1|B_CS2);
  314. graphic_write_command(LCD_DISP_START, B_CS1|B_CS2);
  315. graphic_write_command(LCD_SET_ADD, B_CS1|B_CS2);
  316. graphic_write_command(LCD_SET_PAGE, B_CS1|B_CS2);
  317. select_controller(0); //deatktiverar båda cs signalerna
  318. }
  319.  
  320. void graphic_clear_screen()
  321. {
  322. for(int i = 0; i <= 7; i ++)
  323. {
  324. graphic_write_command(LCD_SET_PAGE|i, B_CS1|B_CS2);
  325. graphic_write_command(LCD_SET_ADD| 0, B_CS1|B_CS2);
  326. for(int y = 0; y<= 63; y++)
  327. {
  328. graphic_write_data(0, B_CS1|B_CS2);
  329. }
  330. }
  331. }
  332.  
  333. /* Display methods */
  334. void pixel(unsigned x, unsigned y, unsigned set)
  335. {
  336. if(x < 1 || x > 128 || y < 1 || y > 64) return 0;
  337. uint8_t mask,c,index,controller;
  338. index = (y-1)/8;
  339. switch((y-1)%8)
  340. {
  341. case 0:mask = 1;break;
  342. case 1:mask = 2;break;
  343. case 2:mask = 4;break;
  344. case 3:mask = 8;break;
  345. case 4:mask = 0x10;break;
  346. case 5:mask = 0x20;break;
  347. case 6:mask = 0x40;break;
  348. case 7:mask = 0x80;break;
  349. }
  350. if(!set)
  351. mask = ~mask;
  352. /* bestäm de fysiska kordinaterna och välj styrkrets */
  353. if(x > 64){
  354. controller = B_CS2;
  355. x = x-65;
  356. }
  357. else{
  358. controller = B_CS1;
  359. x = x-1;
  360. }
  361. graphic_write_command(LCD_SET_ADD| x,controller);
  362. graphic_write_command(LCD_SET_PAGE| index,controller);
  363. c = graphic_read_data(controller);
  364. graphic_write_command(LCD_SET_ADD| x,controller);
  365. if(set ==1)
  366. mask |= c;
  367. else
  368. mask &= c;
  369. graphic_write_data(mask,controller);
  370.  
  371. }
  372.  
  373. //} LCD END
  374. //{ ASCII
  375. void ascii_ctrl_bit_set( unsigned char x )
  376. {
  377. unsigned char c;
  378. c = *GPIO_E_ODRLOW;
  379. c |= ( B_SELECT | x );
  380. *GPIO_E_ODRLOW = c;
  381. }
  382. void ascii_ctrl_bit_clear( unsigned char x )
  383. {
  384. unsigned char c;
  385. c = *GPIO_E_ODRLOW;
  386. c &= ( B_SELECT | ~x );
  387. *GPIO_E_ODRLOW = c;
  388. }
  389.  
  390. void ascii_write_controller( unsigned char c )
  391. {
  392. ascii_ctrl_bit_set( B_E );
  393. *GPIO_E_ODRHIGH = c;
  394. delay_250ns();
  395. ascii_ctrl_bit_clear( B_E );
  396. }
  397. unsigned char ascii_read_controller( void )
  398. {
  399. unsigned char c;
  400. ascii_ctrl_bit_set( B_E );
  401. delay_250ns();
  402. delay_250ns();
  403. c = *GPIO_E_IDRHIGH;
  404. ascii_ctrl_bit_clear( B_E );
  405. return c;
  406. }
  407. unsigned char ascii_read_status()
  408. {
  409. unsigned char c;
  410. *GPIO_E_MODER = 0x00005555;
  411. ascii_ctrl_bit_set( B_RW );
  412. ascii_ctrl_bit_clear( B_RS );
  413. c = ascii_read_controller( );
  414. *GPIO_E_MODER = 0x55555555;
  415. return c;
  416. }
  417. unsigned char ascii_read_data()
  418. {
  419. unsigned char c;
  420. *GPIO_E_MODER = 0x00005555;
  421. ascii_ctrl_bit_set( B_RW );
  422. ascii_ctrl_bit_set( B_RS );
  423. c = ascii_read_controller();
  424. *GPIO_E_MODER = 0x55555555;
  425. return c;
  426. }
  427. void ascii_write_cmd(unsigned char command)
  428. {
  429. ascii_ctrl_bit_clear(B_RS);
  430. ascii_ctrl_bit_clear(B_RW);
  431. ascii_write_controller(command);
  432. }
  433. void ascii_write_data(unsigned char data)
  434. {
  435. ascii_ctrl_bit_set(B_RS);
  436. ascii_ctrl_bit_clear(B_RW);
  437. ascii_write_controller(data);
  438. }
  439.  
  440. void ascii_init()
  441. {
  442. //set reader och tecken storlek
  443. while((ascii_read_status() & 0x80) == 0x80);
  444. //tänd display, markör och konstant visning
  445. delay_micro(8);
  446. ascii_write_cmd(0x38);
  447. delay_micro(40);
  448. while((ascii_read_status() & 0x80) == 0x80);
  449. delay_micro(8);
  450. ascii_write_cmd(0xC);
  451. delay_micro(40);
  452.  
  453. //clear display
  454. while((ascii_read_status() & 0x80) == 0x80);
  455. delay_micro(8);
  456. ascii_write_cmd(1);
  457. delay_milli(2);
  458. //adressing med increment
  459. while((ascii_read_status() & 0x80) == 0x80);
  460. delay_micro(8);
  461. ascii_write_cmd(0x6);
  462. delay_micro(40);
  463.  
  464. }
  465.  
  466. void ascii_write_char(unsigned char c)
  467. {
  468. while((ascii_read_status() & 0x80) == 0x80)
  469. {}
  470. delay_micro(8);
  471. ascii_write_data(c);
  472. delay_micro(43);
  473. }
  474. void ascii_gotoxy(int x,int y)
  475. {
  476. char c = x-1;
  477. if(y == 2)
  478. {
  479. c = c + 0x40;
  480. }
  481. ascii_write_cmd(0x80 | c);
  482. }
  483.  
  484. //} Ascii end
  485. //{ Objects
  486.  
  487. GEOMETRY ball_geometry =
  488. {
  489. 12, /* numpoints */
  490. 4,4, /* sizex, sizey */
  491. {/* px[0,1,2 ...] */
  492. {0,1},{0,2},{1,0}, {1,1}, {1,2},
  493. {1,3},{2,0},{2,1},{2,2},{2,3},
  494. {3,2}
  495. }
  496. };
  497.  
  498. GEOMETRY paddle_geometry =
  499. {
  500. 18, /* numpoints */
  501. 2,9, /* sizex, sizey */
  502. {/* px[0,1,2 ...] */
  503. {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},
  504. {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8}
  505. }
  506. };
  507. void move_paddle(POBJECT o, POBJECT pl, POBJECT pr)
  508. {
  509. o->clear(o);
  510. o->posy += o->diry;
  511. if(o->posy < 1) //påväg ovanför skärmen
  512. o->diry = 0;
  513. if((o->posy+o->geo->sizey)>64)
  514. o->diry = 0;
  515. o->draw(o);
  516.  
  517. }
  518. void move_ball(POBJECT o, POBJECT pl, POBJECT pr)
  519. {
  520. o->clear(o);
  521. o->posx += o->dirx;
  522. o->posy += o->diry;
  523. //kollar kanterna
  524. if(o->posx < 1) //objektet är påväg ut vänster kanten
  525. o->dirx *= -1;
  526. if((o->posx+o->geo->sizex) > 128)//påväg ut ur höger kanten, det är objektets pos + obj storlek
  527. o->dirx *= -1;
  528. if(o->posy < 1) //påväg ovanför skärmen
  529. o->diry*=-1;
  530. if((o->posy+o->geo->sizey)>64)
  531. o->diry*=-1;
  532.  
  533. //testa vänster paddeln
  534. if(o->posx <= (pl->posx+pl->geo->sizex) && (o->posy +o->geo->sizey) >= pl->posy && (pl->posy + pl->geo->sizey) >= o->posy)
  535. {
  536. o->dirx*=-1;
  537. }
  538.  
  539. //testa höger paddeln
  540. if((o->posx+o->geo->sizex) >= pr->posx && (o->posy+o->geo->sizey) >= pr->posy && o->posy < (pr->posy+pr->geo->sizey))
  541. {
  542. o->dirx*=-1;
  543. }
  544. o->draw(o);
  545. }
  546.  
  547. void set_object_speed(POBJECT o, int speedx, int speedy)
  548. {
  549. o->dirx = speedx;
  550. o->diry = speedy;
  551. }
  552. void draw_object(POBJECT o)
  553. {
  554. int pixels = o->geo->numPoints;
  555. int posx = o->posx;
  556. int posy = o->posy;
  557. for(int i = 0; i <pixels; i++)
  558. {
  559. POINT p = o->geo->px[i];
  560. pixel((posx + p.x), posy +p.y,1);
  561. }
  562. }
  563. void clear_object(POBJECT o)
  564. {
  565. int pixels = o->geo->numPoints;
  566. int posx = o->posx;
  567. int posy = o->posy;
  568. for(int i = 0; i <pixels; i++)
  569. {
  570. pixel((posx + o->geo->px[i].x), posy + o->geo->px[i].y,0);
  571. }
  572. }
  573.  
  574. static OBJECT ball =
  575. {
  576. &ball_geometry,
  577. 1,1,
  578. 64,32,
  579. draw_object,
  580. clear_object,
  581. move_ball,
  582. set_object_speed
  583. };
  584.  
  585. static OBJECT padL =
  586. {
  587. &paddle_geometry,
  588. 0,0,
  589. 10,10,
  590. draw_object,
  591. clear_object,
  592. move_paddle,
  593. set_object_speed
  594. };
  595.  
  596. static OBJECT padR =
  597. {
  598. &paddle_geometry,
  599. 0,0,
  600. 118,10,
  601. draw_object,
  602. clear_object,
  603. move_paddle,
  604. set_object_speed
  605. };
  606. //} Objects end
  607. //{ Collision Handler
  608. void setPaddleSpeed(POBJECT pd,int dir)
  609. {
  610. if(pd->posy > 1 && (pd->posy + pd->geo->sizey) < 63 )
  611. pd->set_speed(pd,0,dir);
  612. else if(pd->posy < 1 && dir < 0)
  613. pd->set_speed(pd,0,0);
  614. else if(pd->posy < 1 && dir > 0)
  615. pd->set_speed(pd,0,dir);
  616. else if(pd->posy+pd->geo->sizey >= 63 && dir > 0)
  617. pd->set_speed(pd,0,0);
  618. else if(pd->posy+pd->geo->sizey >= 63 && dir < 0)
  619. pd->set_speed(pd,0,dir);
  620.  
  621. }
  622. //}
  623.  
  624.  
  625. void print_winner(int winner)
  626. {
  627. char *s;
  628. char test2[]="2k19";
  629. ascii_gotoxy(1,1);
  630.  
  631. if(winner){ //höger vinner
  632. char test1[] ="Player 2 wins!";
  633. s=test1;
  634. while(*s){
  635. ascii_write_char(*s++);
  636. }
  637. }
  638. else{ //vänster vinner
  639. char test1[] ="Player 1 wins!";
  640. s=test1;
  641. while(*s){
  642. ascii_write_char(*s++);
  643. }
  644. }
  645.  
  646.  
  647.  
  648. ascii_gotoxy(1,2);
  649. s=test2;
  650. while(*s){
  651. ascii_write_char(*s++);
  652. }
  653. }
  654. void app_init(){
  655. *((unsigned long*) 0x40023830) = 0x18;
  656. *((unsigned long*) 0x40023844) |= 0x4000;
  657. *((unsigned long*) 0xE000ED08) = 0x2001C000;
  658. *GPIO_D_MODER = 0x55005555;
  659. *GPIO_E_MODER = 0x55555555;
  660. *GPIO_D_OTYPER &= 0x00FF;
  661. *GPIO_D_OTYPER |= 0x0000;
  662. *GPIO_D_PUPDR &= 0x0000FFFF;
  663. *GPIO_D_PUPDR |= 0X55AA0000;
  664. }
  665. void main(void)
  666. {
  667. char c,d;
  668. int i = 0;
  669. POBJECT p = &ball;
  670. POBJECT pl = &padL;
  671. POBJECT pr = &padR;
  672.  
  673.  
  674. app_init();
  675. ascii_init();
  676. graphic_init();
  677. #ifndef SIMULATOR
  678. graphic_clear_screen();
  679. #endif
  680. while(1)
  681. {
  682. pl->diry = 0;
  683. pr->diry = 0;
  684. delay_milli(40);
  685. c = keyb();
  686. switch(c)
  687. {
  688. case 1: setPaddleSpeed(pl,-2);break;
  689. case 7: setPaddleSpeed(pl,2);break;
  690. case 3: setPaddleSpeed(pr,-2);break;
  691. case 9: setPaddleSpeed(pr,2);break;
  692. }
  693. pl->move(pl,pl,pl);
  694. pr->move(pr,pr,pr);
  695. p->move(p,pl,pr);
  696. if(p->posx < 1){ //objektet är påväg ut vänster kanten
  697. print_winner(1);
  698. p->posy = 32;
  699. p->posx = 64;
  700. p->dirx = p->dirx *2;
  701. p->diry = p->diry *2;
  702. }
  703. if((p->posx+p->geo->sizex) > 128){//påväg ut ur höger kanten, det är objektets pos + obj storlek
  704. print_winner(0);
  705. p->posy = 32;
  706. p->posx = 64;
  707. p->dirx = p->dirx *2;
  708. p->diry = p->diry *2;
  709. }
  710. }
  711. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement