Advertisement
MagnusArias

AVR | Sterowanie

Oct 12th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. #define F_CPU 1000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include "HD44780.c"
  5. #include <stdlib.h> // tylko dla rand() i srand()
  6.  
  7. #define SW_L (1<<PA4)
  8. #define SW_L_ON !(PINA & SW_L)
  9.  
  10. #define SW_P (1<<PA5)
  11. #define SW_P_ON !(PINA & SW_P)
  12.  
  13. #define SW_UP (1<<PA6)
  14. #define SW_UP_ON !(PINA & SW_UP)
  15.  
  16. #define SW_DOWN (1<<PA7)
  17. #define SW_DOWN_ON !(PINA & SW_DOWN)
  18.  
  19. struct _punkt // struktura pozwala na używanie pól bitowych, zatem cała struktura tutaj zajmuje 5 BITÓW, zamiast 16 (8+8)
  20. {
  21.     char x:     4;
  22.     char x_last:    4;
  23.    
  24.     char y:     1;
  25.     char y_last:    1;
  26. };
  27.  
  28. void Draw_Main(char src[2][16][1]){
  29.     for (char row = 0; row < 2; row++)
  30.     {
  31.         for (char col = 0; col < 16; col++)
  32.         {
  33.             LCD_GoTo(col, row); // x, y
  34.             LCD_WriteText(&src[row][col]);
  35.         }
  36.     }
  37. }      
  38.  
  39. int main(void)
  40. {
  41.     DDRA &= ~(SW_L | SW_P | SW_UP | SW_DOWN);           // SHIT DLA PRZYCISKÓW
  42.     PORTA |= (SW_L | SW_P | SW_UP | SW_DOWN);           // SHIT DLA PRZYCISKÓW
  43.  
  44.     // 2 - wiersze, 16 - kolumny, 1- dlugosc znaku (czyli maksymalnie 1)
  45.     char ekran[2][16][1] =  {
  46.        
  47.         //       0    1    2    3    4    5    6    7    8    9    10   11   12   13  14   15    
  48.         {       ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  49.         {       ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
  50.        
  51.                             };
  52.    
  53.     LCD_Initalize();                                    // obowiązkowe
  54.     LCD_Clear();                                        // too
  55.  
  56.  
  57.     struct _punkt _gracz;      // tworzenie obiektu gracz ze wspolrzednymi
  58.    
  59.     _gracz.x = 0;
  60.     _gracz.y = 1;  
  61.    
  62.  
  63.     struct _punkt _wrog;        // tworzenie obiektu wrog ze wspolrzednymi
  64.    
  65.     _wrog.x = 14;
  66.     _wrog.y = rand() % 2;
  67.     _wrog.x_last = 15;
  68.    
  69.  
  70.     ekran[_gracz.y][_gracz.x][0] = 'X'; // ustalenie grafiki dla gracza, lolz
  71.    
  72.     Draw_Main(ekran);
  73.  
  74.     char mnoznik = 1;       // jakieś zmienne
  75.     char score = 0;         // jakieś zmenne
  76.    
  77.     while(1)
  78.     {
  79.         Draw_Main(ekran);               // na początku każdej pętli rysuj wszystko
  80.        
  81.         /*          STEROWNANIE GRACZEM     */
  82.        
  83.         if (SW_P_ON)                    // PRAWY
  84.         {
  85.             _delay_ms(5);
  86.             mnoznik = 2;           
  87.         }   else mnoznik = 1;
  88.        
  89.         if (SW_L_ON)                    // LEWY
  90.         {
  91.             _delay_ms(5);
  92.             mnoznik = 5;
  93.         }
  94.        
  95.         if (SW_UP_ON)                   // GÓRA
  96.         {
  97.             _delay_ms(5);
  98.             _gracz.y_last = _gracz.y;
  99.             if (_gracz.y > 0) _gracz.y--;
  100.             else { _gracz.y_last = 1;   _gracz.y = 0; }
  101.         }
  102.         if (SW_DOWN_ON)                 //DÓŁ
  103.         {
  104.             _delay_ms(5);
  105.             _gracz.y_last = _gracz.y;
  106.             if (_gracz.y < 1) _gracz.y++;
  107.             else { _gracz.y_last = 0;   _gracz.y = 1; }
  108.         }
  109.        
  110.  
  111.         /*  KOLIZJE I ZDARZENIA */
  112.         if ((_wrog.x - 1 == _gracz.x) && (_wrog.y== _gracz.y)) break; // zakończenie gry, wyjście złe
  113.        
  114.  
  115.  
  116.         /*      WYŚWIETLANIE GRACZA            */
  117.         ekran[_gracz.y][_gracz.x][0] = 'X';
  118.         ekran[_gracz.y_last][_gracz.x][0] = ' ';
  119.        
  120.        
  121.         if (_wrog.x > 0)
  122.         {
  123.             _wrog.x_last = _wrog.x;
  124.             _wrog.x--;
  125.         } else
  126.         {
  127.             _wrog.x = 14;
  128.             _wrog.x_last = 15;
  129.             score+=5;
  130.             _wrog.y = rand()%2;
  131.         }
  132.        
  133.         ekran[_wrog.y][_wrog.x][0] = '8';   // grafika wroga
  134.         ekran[_wrog.y][_wrog.x_last][0] = ' ';
  135.        
  136.         if (score == 255) break; // how awsum, zakończenie gry, wyjscie dobre
  137.        
  138.     }  
  139.  
  140.  
  141.     LCD_Clear();   
  142.     LCD_GoTo(3,0);
  143.  
  144.     if(score<255) LCD_WriteText("Game Over");
  145.     else LCD_WriteText("You Won");
  146.  
  147.     LCD_GoTo(3,1);
  148.     LCD_WriteText("Score : ");
  149.     LCD_WriteText(itoa(score,"",10));
  150.    
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement