Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <string.h>
  6. #define UP 72
  7. #define DOWN 80
  8. #define LEFT 75
  9. #define RIGHT 77
  10. #define gspeed 5
  11. #define jspeed 50
  12. void delay(unsigned int);
  13.  
  14. typedef struct eredmeny{            //Struktúra az eredmény listához.
  15.     char nev[100];
  16.     int score;
  17.  
  18. }ERED;
  19.  
  20.  
  21.  
  22. void SetPosition(int X, int Y)
  23. {
  24.     HANDLE Screen;
  25.     Screen = GetStdHandle(STD_OUTPUT_HANDLE);
  26.     COORD Position = { X, Y };
  27.  
  28.     SetConsoleCursorPosition(Screen, Position);
  29. }
  30.  
  31. int getline(char s[], int n)
  32. {
  33.     int c, i;
  34.     for (i = 0; i<n && (c = getchar()) != EOF&&c != '\n'; ++i)
  35.         s[i] = c;
  36.     s[i] = '\0';
  37.     while (c != EOF&&c != '\n') c = getchar();
  38.     return(i);
  39. }
  40.  
  41.  
  42. int main()
  43. {
  44.  
  45.     char matrix[23][27] =
  46.     { "------------------------",
  47.     "                        |",
  48.     "                        |",
  49.     "                        |",
  50.     "                        |",
  51.     "                        |",
  52.     "                        |",
  53.     "                        |",
  54.     "                        |",
  55.     "                        |",
  56.     "                        |",
  57.     "                        |",
  58.     "                        |",
  59.     "                        |",
  60.     "                        |",
  61.     "                        |",
  62.     "                        |",
  63.     "                        |",
  64.     "                        |",
  65.     "                        |",
  66.     "                        |",
  67.     "                        |",
  68.     "                        |", };
  69.  
  70.     int i, j, n = 0, score = 0, bszam = 200, a = 1, s = 2, seged = 1, b = 1, eltolas = 0, l = 0, temp = 1;
  71.     int px = 22, py = 10, key = 0, bx = 21, by = py, k = 0, f = 0, pont, ok = 1, x = 17, sor = 5, masik = 17, w;
  72.     int masol, masolas, torol, ell, c = 1, e = 17, msor = 8;
  73.     int szamlalo = 3;
  74.     int g = 50;
  75.     char ures = ' ';
  76.     char player = 'A';
  77.     char bullet = '^';
  78.     char alian = 'V';
  79.     matrix[px][py] = 'A';
  80.     char nev[32];
  81.     FILE *fp;
  82.     int index4 = 0;
  83.     ERED *tomb;
  84.     char line[32];
  85.     int jatekszam = 0;
  86.  
  87.     matrix[5][1] = alian;
  88.     matrix[5][3] = alian;
  89.     matrix[5][5] = alian;
  90.     matrix[5][7] = alian;
  91.     matrix[5][9] = alian;
  92.     matrix[5][11] = alian;
  93.     matrix[5][13] = alian;
  94.     matrix[5][15] = alian;
  95.  
  96.     matrix[8][1] = alian;
  97.     matrix[8][3] = alian;
  98.     matrix[8][5] = alian;
  99.     matrix[8][7] = alian;
  100.     matrix[8][9] = alian;
  101.     matrix[8][11] = alian;
  102.     matrix[8][13] = alian;
  103.     matrix[8][15] = alian;
  104.  
  105.     matrix[4][1] = alian;
  106.     matrix[4][3] = alian;
  107.     matrix[4][5] = alian;
  108.     matrix[4][7] = alian;
  109.     matrix[4][9] = alian;
  110.     matrix[4][11] = alian;
  111.     matrix[4][13] = alian;
  112.     matrix[4][15] = alian;
  113.     matrix[4][17] = alian;
  114.     matrix[4][19] = alian;
  115.     matrix[4][21] = alian;
  116.  
  117.     matrix[3][2] = alian;
  118.     matrix[3][5] = alian;
  119.     matrix[3][8] = alian;
  120.     matrix[3][11] = alian;
  121.     matrix[3][14] = alian;
  122.     matrix[3][17] = alian;
  123.     matrix[3][20] = alian;
  124.  
  125.  
  126. nevbe:
  127.     printf("Felhasznalonev (max 30 karakter, angol ABC betui, esetleg szokoz): ");
  128.     if (!getline(nev, 31))
  129.     {
  130.         printf("HIBA: Hianyzo adat.\n\n");
  131.         goto nevbe;
  132.     }
  133.     if (strlen(nev) > 30)
  134.     {
  135.         printf("HIBA: A megadott nev tul hosszu!\nKerem adjon meg ujat!\n\n");
  136.         goto nevbe;
  137.     }
  138.     for (w = 0; w < strlen(nev); w++)
  139.     {
  140.         if (!isalpha(nev[w]) && !isspace(nev[w]))
  141.         {
  142.             printf("HIBA: A felhasznalonev csak az angol ABC betuit tartalmazhatja!\nKerem adjon meg ujat!\n\n");
  143.             goto nevbe;
  144.         }
  145.     }
  146.  
  147.     system("cls");
  148.  
  149.  
  150.     a--;
  151.     do
  152.     {
  153.         if (g == 50)
  154.         {
  155.  
  156.             a++;
  157.             if (a < 8)
  158.             {
  159.  
  160.  
  161.                 for (; b < x; b = b + 2)
  162.                 {
  163.                     /*  if (matrix[sor][b + 1] == bullet)
  164.                     {
  165.                     matrix[sor][b + 1] = ures;
  166.                     matrix[sor][b] = ures;
  167.  
  168.                     bx = 21;
  169.                     f = 0;
  170.                     ok = 1;
  171.                     score = score + 50;
  172.                     eltolas = eltolas + 1;
  173.  
  174.  
  175.                     }*/
  176.  
  177.                     matrix[sor][b + 1] = matrix[sor][b];
  178.                     matrix[sor][b] = ures;
  179.  
  180.  
  181.                 }
  182.  
  183.                 b = b - 16;
  184.                 b++;
  185.                 x++;
  186.  
  187.                 for (; c < e; c = c + 2)
  188.                 {
  189.                     if (matrix[msor][c + 1] == bullet)
  190.                     {
  191.                         matrix[msor][c + 1] = ures;
  192.                         matrix[msor][c] = ures;
  193.  
  194.                         bx = 21;
  195.                         f = 0;
  196.                         ok = 1;
  197.                         score = score + 50;
  198.                         eltolas = eltolas + 1;
  199.  
  200.  
  201.                     }
  202.                     matrix[msor][c + 1] = matrix[msor][c];
  203.                     matrix[msor][c] = ures;
  204.  
  205.  
  206.  
  207.  
  208.                 }
  209.                 c = c - 16;
  210.                 c++;
  211.                 e++;
  212.  
  213.             }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.             g = 0;
  220.         }
  221.         if (a == 7)
  222.         {
  223.             masolas = 1;
  224.             for (masol = 8; masol < 24; masol++, masolas++)
  225.             {
  226.  
  227.                 matrix[sor + 1][masolas] = matrix[sor][masol];
  228.             }
  229.             masolas = 1;
  230.             for (masol = 8; masol < 24; masol++, masolas++)
  231.             {
  232.  
  233.                 matrix[msor + 1][masolas] = matrix[msor][masol];
  234.             }
  235.  
  236.  
  237.             for (torol = 0; torol < 24; torol++)
  238.             {
  239.                 matrix[sor][torol] = ures;
  240.                 matrix[msor][torol] = ures;
  241.             }
  242.  
  243.  
  244.  
  245.             sor = sor + 1;
  246.             a = 0;
  247.             b = 1;
  248.             x = 17;
  249.             msor = msor + 1;
  250.             e = 17;
  251.             c = 1;
  252.         }
  253.  
  254.  
  255.         for (ell = 0; ell < 24; ell++)
  256.         {
  257.             if (matrix[sor][ell] == '^')
  258.             {
  259.                 matrix[sor][ell] = ures;
  260.  
  261.             }
  262.             if (matrix[msor][ell] == '^')
  263.             {
  264.                 matrix[msor][ell] = ures;
  265.  
  266.             }
  267.  
  268.  
  269.         }
  270.  
  271.         if (_kbhit())
  272.         {
  273.             key = _getch();
  274.  
  275.             if (key == RIGHT)
  276.             {
  277.  
  278.                 py = py + 1;
  279.                 if (py > 0 && py < 22)
  280.                 {
  281.                     matrix[px][py] = player;
  282.                     matrix[px][py - 1] = ures;
  283.                 }
  284.                 else
  285.                 {
  286.                     py = py - 1;
  287.                 }
  288.  
  289.             }
  290.             if (key == LEFT)
  291.             {
  292.  
  293.                 py = py - 1;
  294.                 if (py>-1 && py < 24)
  295.                 {
  296.                     matrix[px][py] = player;
  297.                     matrix[px][py + 1] = ures;
  298.                 }
  299.                 else
  300.                 {
  301.                     py = py + 1;
  302.                 }
  303.  
  304.  
  305.             }
  306.  
  307.  
  308.             if (key == UP)
  309.             {
  310.                 f = 1;
  311.                 if (ok > 0)
  312.                 {
  313.                     pont = py;
  314.                 }
  315.                 bszam = bszam - 1;
  316.             }
  317.         }
  318.  
  319.         if (f > 0 && szamlalo > 2)
  320.         {
  321.             szamlalo = 0;
  322.             ok = 0;
  323.             bx = bx - 1;
  324.  
  325.  
  326.  
  327.  
  328.             if (bx > 0)
  329.             {
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.                 if (matrix[bx][pont] == alian)
  337.                 {
  338.                     matrix[bx][pont] = ures;
  339.                     matrix[bx + 1][pont] = ures;
  340.  
  341.                     bx = 21;
  342.                     f = 0;
  343.                     ok = 1;
  344.                     eltolas = eltolas + 1;
  345.  
  346.                     score = score + 50;
  347.                 }
  348.  
  349.                 else
  350.                 {
  351.                     if (matrix[bx + 1][pont] != alian)
  352.                     {
  353.                         matrix[bx][pont] = bullet;
  354.                         matrix[bx + 1][pont] = ures;
  355.                     }
  356.                     else
  357.                     {
  358.                         matrix[bx][pont] = bullet;
  359.                     }
  360.  
  361.                 }
  362.             }
  363.  
  364.             if (bx == 1)
  365.             {
  366.                 matrix[bx][pont] = ures;
  367.             }
  368.             if (bx < 2)
  369.             {
  370.                 bx = 21;
  371.                 f = 0;
  372.                 ok = 1;
  373.             }
  374.  
  375.  
  376.  
  377.         }
  378.  
  379.         if (bszam == 0 || msor == 20 || eltolas == 34)
  380.         {
  381.             key = 'e';
  382.  
  383.         }
  384.  
  385.  
  386.  
  387.         for (i = 0; i < 23; i++)
  388.         {
  389.             for (j = 0; j < 27; j++)
  390.             {
  391.                 printf("%c", matrix[i][j]);
  392.             }
  393.             printf("\n");
  394.         }
  395.  
  396.  
  397.         printf("%d                     %d/14", score, (sor - 4));
  398.  
  399.         /*gotoxy(0, 0);*/
  400.         SetPosition(0, 0);
  401.  
  402.         g = g + 1;
  403.         szamlalo = szamlalo + 1;
  404.  
  405.  
  406.  
  407.  
  408.     } while (key != 'e');
  409.  
  410.  
  411.     system("cls");
  412.     printf("SCORE:%d \n\n", score);
  413.  
  414.     if (eltolas == 34)
  415.         printf("YOU WIN!!!\n\n");
  416.     else
  417.         printf("GAME OVER!!!!\n\n");
  418.  
  419.  
  420.  
  421.     fp = fopen("top.txt", "at");
  422.  
  423.     fprintf(fp, "%s %d\n", nev, score);
  424.  
  425.     fclose(fp);
  426.  
  427.  
  428.     fp = fopen("top.txt", "r");
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.     fclose(fp);
  436.  
  437.     tomb = (ERED*)malloc(sizeof(ERED) * 10);
  438.  
  439.     fp = fopen("top.txt", "r");
  440.  
  441.     while (!feof(fp))
  442.     {
  443.  
  444.         fscanf(fp, "%s %d\n", line, &pont);
  445.         strcpy(tomb[index4].nev, line);
  446.         tomb[index4].score = pont;
  447.         index4++;
  448.     }
  449.     fclose(fp);
  450.  
  451.     for (n = 0; n < index4; n++){
  452.     for (i = n + 1; i < index4; i++)
  453.         if (tomb[n].score < tomb[i].score)
  454.         {
  455.             temp = tomb[n].score;
  456.             tomb[n].score = tomb[i].score;
  457.             tomb[i].score = temp;
  458.  
  459.             strcpy(line, tomb[n].nev);
  460.             strcpy(tomb[n].nev, tomb[i].nev);
  461.             strcpy(tomb[i].nev, line);
  462.         }
  463. }
  464.  
  465.    
  466. fp = fopen("top.txt", "w");
  467.  
  468.  
  469. for(i = 0; i < 5; i++ )
  470. {
  471.    
  472.     fprintf(fp, "%s %d\n", tomb[i].nev, tomb[i].score);
  473. }
  474.    
  475. fclose(fp);
  476.    
  477. fp = fopen("top.txt", "r");
  478. i = 0;
  479. do
  480. {
  481.     fgets(line, 100, fp);
  482.     puts(line);
  483.     i++;
  484. } while (!feof(fp));
  485.  
  486. fclose(fp);
  487.    
  488.     free(tomb);
  489.     return 0;
  490.  
  491.  
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement