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 15
- #define width 30
- #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);
- void ruch2(Snake *h);
- void putcharXY(int x, int y, unsigned char z);
- void rysuj(const map *f);
- void zryj(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");
- }
- void ruch(Snake *g){
- while (_kbhit()){
- switch (_getch()){
- case lewo:
- if (g->y > 1){
- while (1){
- putcharXY(g->x, g->y, 32);
- g->y--;
- putcharXY(g->x, g->y, 176);
- Sleep(500);
- if (_getch()){
- ruch(g);
- }
- }
- }
- break;
- case prawo:
- if (g->y < width - 2){
- while (1){
- putcharXY(g->x, g->y, 32);
- g->y++;
- putcharXY(g->x, g->y, 176);
- Sleep(500);
- if (_getch()){
- ruch(g);
- }
- }
- }
- break;
- case gora:
- if (g->x > 1){
- while (1){
- putcharXY(g->x, g->y, 32);
- g->x--;
- putcharXY(g->x, g->y, 176);
- Sleep(500);
- if (_getch()){
- ruch(g);
- }
- }
- }
- break;
- case dol:
- if (g->x < height - 2){
- while (1){
- putcharXY(g->x, g->y, 32);
- g->x++;
- putcharXY(g->x, g->y, 176);
- Sleep(500);
- if (_getch()){
- ruch(g);
- }
- }
- }
- break;
- }
- }
- }
- void ruch2(Snake *h){
- while (_kbhit()){
- switch (_getch()){
- case 'a':
- if (h->y>1){
- while (1){
- putcharXY(h->x, h->y, 32);
- h->y--;
- putcharXY(h->x, h->y, 178);
- Sleep(500);
- if (_getch()){
- ruch2(h);
- }
- }
- }
- break;
- case 'd':
- if (h->y<width - 2){
- while (1){
- putcharXY(h->x, h->y, 32);
- h->y++;
- putcharXY(h->x, h->y, 178);
- Sleep(500);
- if (_getch()){
- ruch2(h);
- }
- }
- }
- break;
- case 'w':
- if (h->x>1){
- while (1){
- putcharXY(h->x, h->y, 32);
- h->x--;
- putcharXY(h->x, h->y, 178);
- Sleep(500);
- if (_getch()){
- ruch2(h);
- }
- }
- }
- break;
- case 's':
- if (h->x<height - 2){
- while (1){
- putcharXY(h->x, h->y, 32);
- h->x++;
- putcharXY(h->x, h->y, 178);
- Sleep(500);
- if (_getch()){
- ruch2(h);
- }
- }
- }
- 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 zryj(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, 7, 10, 49 };
- Snake s2 = { 178, 7, 20, 49 };
- Snake japko = { 177, rand() % 13 + 1, rand() % 28 + 1 };
- map mapa;
- char field[height][width];
- int h = 0;
- initMap(&mapa, &s1, &s2, &japko);
- DWORD WINAPI ThreadId1, ThreadId2;
- HANDLE hHandles1, hHandles2;
- hHandles1 = CreateThread(NULL, 0, ruch(&s1),
- 0, 0, &ThreadId1);
- hHandles2 = CreateThread(NULL, 0, ruch2(&s2),
- 0, 0, &ThreadId2);
- if (hHandles1 == NULL || hHandles2 == NULL)
- {
- printf("Nieudane tworzenie watkow\n");
- exit(0);
- }
- //Oczekiwanie na zakończenie funkcji
- WaitForSingleObject(hHandles1, INFINITE);
- WaitForSingleObject(hHandles2, INFINITE);
- while (1){
- rysuj(&mapa);
- ruch(&s1);
- ruch2(&s2);
- zryj(&mapa, &s1, &s2, &japko);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement