Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #include <time.h>
- #define height 25
- #define width 60
- #define lewo 75
- #define prawo 77
- #define gora 72
- #define dol 80
- typedef struct {
- char a;
- int x;
- int y;
- int wynik;
- }Snake;
- typedef struct{
- char field[height][width];
- }map;
- int koniec = 1;
- void initScreen();
- void initMap(map *f, Snake *g, Snake *h, Snake *e);
- //void ruch(Snake *g, Snake *h);
- void move(Snake *g, Snake *h);
- int move1(Snake *g);
- int move2(Snake *h);
- void logic(Snake *g, Snake *h);
- void logic1(Snake *g);
- void logic2(Snake *h);
- void putcharXY(int x, int y, unsigned char z);
- void rysuj(const map *f);
- void food(map *f, Snake *g, Snake *h, Snake *e);
- void putcharXY(int x, int y, unsigned char z){
- COORD a;
- a.Y = x;
- a.X = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), a);
- printf("%c", z);
- }
- void initScreen(void){
- // Wymaga: windows.h
- // wyłączenie mrugania kursora - działa od windowsa XP
- CONSOLE_CURSOR_INFO cciInfo;
- cciInfo.dwSize = 1;
- cciInfo.bVisible = 0;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cciInfo);
- system("chcp 852");
- system("cls");
- }
- int move1(Snake *g){
- static int dir1 = 3;
- while (_kbhit()){
- switch (_getch()){
- case lewo:
- if (dir1 != 2 && g->y>1){
- /* putcharXY(g->x, g->y, 32);
- g->y--;
- putcharXY(g->x, g->y, g->a);*/
- dir1 = 1;
- }
- break;
- case prawo:
- if (dir1 != 1 && g->y<width - 2){
- /*putcharXY(g->x, g->y, 32);
- g->y++;
- putcharXY(g->x, g->y, g->a);*/
- dir1 = 2;
- }
- break;
- case gora:
- if (dir1 != 4 && g->x>1){
- /*putcharXY(g->x, g->y, 32);
- g->x--;
- putcharXY(g->x, g->y, g->a);*/
- dir1 = 3;
- }
- break;
- case dol:
- if (dir1 != 3 && g->x<height - 2){
- /*putcharXY(g->x, g->y, 32);
- g->x++;
- putcharXY(g->x, g->y, g->a);*/
- dir1 = 4;
- }
- break;
- }
- }
- return dir1;
- }
- int move2(Snake *h){
- static int dir2 = 3;
- while (_kbhit()){
- switch (_getch()){
- case 'a':
- if (dir2 != 2 && h->y>1){
- /* putcharXY(g->x, g->y, 32);
- g->y--;
- putcharXY(g->x, g->y, g->a);*/
- dir2 = 1;
- }
- break;
- case 'd':
- if (dir2 != 1 && h->y<width - 2){
- /*putcharXY(g->x, g->y, 32);
- g->y++;
- putcharXY(g->x, g->y, g->a);*/
- dir2 = 2;
- }
- break;
- case 'w':
- if (dir2 != 4 && h->x>1){
- /*putcharXY(g->x, g->y, 32);
- g->x--;
- putcharXY(g->x, g->y, g->a);*/
- dir2 = 3;
- }
- break;
- case 's':
- if (dir2 != 3 && h->x<height - 2){
- /*putcharXY(g->x, g->y, 32);
- g->x++;
- putcharXY(g->x, g->y, g->a);*/
- dir2 = 4;
- }
- break;
- }
- }
- return dir2;
- }
- void move(Snake *g, Snake *h){
- move1(g);
- move2(h);
- }
- void logic1(Snake *g){
- if (move1(g) == 1){
- putcharXY(g->x, g->y, 32);
- g->y--;
- putcharXY(g->x, g->y, g->a);
- }
- else if (move1(g) == 2){
- putcharXY(g->x, g->y, 32);
- g->y++;
- putcharXY(g->x, g->y, g->a);
- }
- else if (move1(g) == 3){
- putcharXY(g->x, g->y, 32);
- g->x--;
- putcharXY(g->x, g->y, g->a);
- }
- else if (move1(g) == 4){
- putcharXY(g->x, g->y, 32);
- g->x++;
- putcharXY(g->x, g->y, g->a);
- }
- }
- void logic2(Snake *h){
- if (move2(h) == 1){
- putcharXY(h->x, h->y, 32);
- h->y--;
- putcharXY(h->x, h->y, h->a);
- }
- else if (move2(h) == 2){
- putcharXY(h->x, h->y, 32);
- h->y++;
- putcharXY(h->x, h->y, h->a);
- }
- else if (move2(h) == 3){
- putcharXY(h->x, h->y, 32);
- h->x--;
- putcharXY(h->x, h->y, h->a);
- }
- else if (move2(h) == 4){
- putcharXY(h->x, h->y, 32);
- h->x++;
- putcharXY(h->x, h->y, h->a);
- }
- }
- void logic(Snake*g, Snake *h){
- move1(g);
- move2(h);
- logic1(g);
- logic2(h);
- }
- /*void ruch(Snake *g, Snake *h){
- while (_kbhit()){
- switch (_getch()){
- case lewo:
- if (g->y>1){
- putcharXY(g->x, g->y, 32);
- g->y--;
- putcharXY(g->x, g->y, 176);
- }
- break;
- case prawo:
- if (g->y<width - 2){
- putcharXY(g->x, g->y, 32);
- g->y++;
- putcharXY(g->x, g->y, 176);
- }
- break;
- case gora:
- if (g->x>1){
- putcharXY(g->x, g->y, 32);
- g->x--;
- putcharXY(g->x, g->y, 176);
- }
- break;
- case dol:
- if (g->x<height - 2){
- putcharXY(g->x, g->y, 32);
- g->x++;
- putcharXY(g->x, g->y, 176);
- }
- break;
- case 'a':
- if (h->y>1){
- putcharXY(h->x, h->y, 32);
- h->y--;
- putcharXY(h->x, h->y, 178);
- }
- break;
- case 'd':
- if (h->y<width - 2){
- putcharXY(h->x, h->y, 32);
- h->y++;
- putcharXY(h->x, h->y, 178);
- }
- break;
- case 'w':
- if (h->x>1){
- putcharXY(h->x, h->y, 32);
- h->x--;
- putcharXY(h->x, h->y, 178);
- }
- break;
- case 's':
- if (h->x<height - 2){
- putcharXY(h->x, h->y, 32);
- h->x++;
- putcharXY(h->x, h->y, 178);
- }
- break;
- }
- }
- }
- */
- void initMap(map *f, Snake *g, Snake *h, Snake *e){
- int i, j;
- for (i = 0; i<height; i++){
- for (j = 0; j<width; j++){
- if (i == 0 || i == height - 1){
- f->field[i][j] = 219;
- // printf("%c", 219);
- }
- else if (j == 0 || j == width - 1){
- // printf("%c", 219);
- f->field[i][j] = 219;
- }
- else if (i == g->x && j == g->y){
- // printf("%c", 176);
- f->field[i][j] = 176;
- }
- else if (i == h->x && j == h->y){
- // printf("%c", 178);
- f->field[i][j] = 178;
- }
- else if (i == e->x && j == e->y){
- f->field[i][j] = 177;
- }
- else
- // printf(" ");
- f->field[i][j] = 32;
- }
- }
- }
- void rysuj(const map *f){
- int i, j, tmp;
- static int pirszyroz = 1;
- static char kasze[height][width];
- for (i = 0; i<height; i++){
- for (j = 0; j<width; j++){
- tmp = f->field[i][j];
- if (pirszyroz){
- putcharXY(i, j, tmp);
- kasze[i][j] = tmp;
- }
- else{
- if (kasze[i][j] != tmp){
- putcharXY(i, j, tmp);
- kasze[i][j] = tmp;
- }
- }
- }
- }
- pirszyroz = 0;
- }
- void food(map *f, Snake *g, Snake *h, Snake *e){
- if (g->x == e->x && g->y == e->y){
- e->x = rand() % 13 + 1;
- e->y = rand() % 28 + 1;
- putcharXY(e->x, e->y, e->a);
- g->wynik++;
- //putcharXY(6, 35, (int)g->wynik);
- }
- else if (h->x == e->x && h->y == e->y){
- e->x = rand() % 13 + 1;
- e->y = rand() % 28 + 1;
- putcharXY(e->x, e->y, e->a);
- h->wynik++;
- //putcharXY(11, 35, (int)h->wynik);
- }
- }
- main(){
- initScreen();
- srand(time(NULL));
- Snake s1 = { 176, 14, 20, 49 };
- Snake s2 = { 178, 14, 40, 49 };
- Snake japko = { 177, rand() % 13 + 1, rand() % 28 + 1 };
- map mapa;
- char field[height][width];
- int h = 0;
- initMap(&mapa, &s1, &s2, &japko);
- while (1){
- rysuj(&mapa);
- move(&s1, &s2);
- //move1(&s1);
- //logic1(&s1);
- //move2(&s2);
- logic(&s1, &s2);
- //logic2(&s2);
- //ruch(&s1, &s2);
- food(&mapa, &s1, &s2, &japko);
- Sleep(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement