Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. typedef struct cell{
  2.     char ch;
  3.     u32 backColor;
  4.     u32 chColor;
  5. }cell;
  6.  
  7. int maxWidth = 60;
  8. int maxHeight = 34;
  9. cell** field;
  10.  
  11. void setCell(int x, int y, cell c){
  12.     field[x][y].ch = c.ch;
  13.     field[x][y].chColor = c.chColor;
  14.     field[x][y].backColor = c.backColor;
  15. }
  16.  
  17. void fillRect(int x, int y, int width, int height, cell c){
  18.     int _x;
  19.     int _y;
  20.     for(_x = x;_x < width ; _x++){
  21.         for(_y = y; _y < height; _y++){
  22.             setCell(_x, _y, c);
  23.         }
  24.     }
  25.     return;
  26. }
  27.  
  28. void update(int width, int height){
  29.     int chWidth = 8;
  30.     int chHeight = 8;
  31.     int x;
  32.     int y;
  33.     for(x = 0; x < width; x++){
  34.         for(y = 0; y < height; y++){
  35.             cell c = field[x][y];
  36.             pspDebugScreenSetBackColor(c.backColor);
  37.             pspDebugScreenPutChar(x * chWidth, y * chHeight, c.chColor, c.ch);
  38.         }
  39.     }
  40. }
  41.  
  42. struct{
  43.     int up;
  44.     int right;
  45.     int down;
  46.     int left;
  47. } keys;
  48.  
  49. void updateKeys(){
  50.     SceCtrlData pad;
  51.     sceCtrlReadBufferPositive(&pad, 1);
  52.     if (pad.Buttons){
  53.         keys.up = keys.down = keys.left = keys.right = 0;
  54.         if (pad.Buttons & PSP_CTRL_UP) keys.up = 1;
  55.         if (pad.Buttons & PSP_CTRL_DOWN) keys.down = 1;
  56.         if (pad.Buttons & PSP_CTRL_LEFT) keys.left = 1;
  57.         if (pad.Buttons & PSP_CTRL_RIGHT) keys.right = 1;
  58.     }
  59. }
  60.  
  61. int main(int argc, char* argv[])
  62. {
  63.     int bodyLength = 4;
  64.     int* body = malloc(sizeof(int) * bodyLength * 2);
  65.     int width = 60;
  66.     int height = 30;
  67.     int x;
  68.     int y;
  69.  
  70.     int i = 0;
  71.     for(; i < bodyLength * 2; i += 2){
  72.         body[i] = width / 2;
  73.         body[i + 1] = height / 2 - bodyLength - (i / 2);
  74.     }
  75.  
  76.     cell headUpCell = (cell){(char) 0x1E, 0xFFFFFF, 0x000000};
  77.     cell headRightCell = (cell){(char) 0x10, 0xFFFFFF, 0x000000};
  78.     cell headDownCell = (cell){(char) 0x1F, 0xFFFFFF, 0x000000};
  79.     cell headLeftCell = (cell){(char) 0x11, 0xFFFFFF, 0x000000};
  80.     cell headCell = headDownCell;
  81.     cell bodyCell = (cell){'0', 0xFFFFFF, 0x000000};
  82.     cell foodCell = (cell){'1', 0xFFFFFF, 0x00FF00};
  83.  
  84.     field = malloc(sizeof(cell*) * width);
  85.     for(x = 0; x < width; x++){
  86.         field[x] = malloc(sizeof(cell) * height);
  87.         for(y = 0; y < height; y++){
  88.             field[x][y] = (cell){};
  89.         }
  90.     }
  91.  
  92.     pspDebugScreenInit();
  93.     setupCallbacks();
  94.  
  95.     pspDebugScreenEnableBackColor(1);
  96.  
  97.     int foodx = rand() % width;
  98.     int foody = rand() % height;
  99.  
  100.     int velx = 0;
  101.     int vely = 1;
  102.     while(1){
  103.         updateKeys();
  104.         if(velx){
  105.             if(keys.up) vely = -1, headCell = headUpCell;
  106.             else if(keys.down) vely = 1, headCell = headDownCell;
  107.             if(vely) velx = 0;
  108.         }else if(vely){
  109.             if(keys.left) velx = -1, headCell = headLeftCell;
  110.             else if(keys.right) velx = 1, headCell = headRightCell;
  111.             if(velx) vely = 0;
  112.         }
  113.  
  114.         int* headx = &body[0];
  115.         int* heady = &body[1];
  116.         if(velx || vely){
  117.             for(i = bodyLength - 1; i >= 1; i--){
  118.                 body[i * 2 + 0] = body[(i - 1) * 2 + 0];
  119.                 body[i * 2 + 1] = body[(i - 1) * 2 + 1];
  120.             }
  121.         }
  122.         *headx += velx;
  123.         *heady += vely;
  124.         if(*headx < 0) *headx = width - 1;
  125.         if(*headx > width - 1) *headx = 0;
  126.         if(*heady < 0) *heady = height - 1;
  127.         if(*heady > height - 1) *heady = 0;
  128.  
  129.         for(i = 1; i < bodyLength; i++){
  130.             if(body[i * 2] == *headx && body[i * 2 + 1] == *heady){
  131.                 bodyLength = i;
  132.                 //body = realloc(body, sizeof(int) * bodyLength * 2);
  133.                 break;
  134.             }
  135.         }
  136.  
  137.         if(*headx == foodx && *heady == foody){
  138.             bodyLength++;
  139.             body = realloc(body, sizeof(int) * bodyLength * 2);
  140.             headx = &body[0];
  141.             heady = &body[1];
  142.             body[(bodyLength - 1) * 2 + 0] = *headx;
  143.             body[(bodyLength - 1) * 2 + 1] = *heady;
  144.             for(i = 0; i < bodyLength; i++){
  145.                 if(body[i * 2] == foodx && body[i * 2 + 1] == foody){
  146.                     foodx = rand() % width;
  147.                     foody = rand() % height;
  148.                     i = 0;
  149.                     continue;
  150.                 }
  151.             }
  152.         }
  153.         fillRect(0, 0, width, height, (cell){' ', 0x000000, 0xFFFFFF});
  154.         setCell(*headx, *heady, headCell);
  155.         setCell(foodx, foody, foodCell);
  156.         for(i = 2; i < bodyLength * 2; i += 2){
  157.             setCell(body[i], body[i + 1], bodyCell);
  158.         }
  159.         update(width, height);
  160.         sceDisplayWaitVblankStart(); updateKeys();
  161.         sceDisplayWaitVblankStart(); updateKeys();
  162.         sceDisplayWaitVblankStart(); updateKeys();
  163.         sceDisplayWaitVblankStart(); updateKeys();
  164.         sceDisplayWaitVblankStart(); updateKeys();
  165.         sceDisplayWaitVblankStart();
  166.     }
  167.  
  168.     sceKernelExitDeleteThread(0);
  169.     sceKernelExitGame();
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement