how to make a snake game in c 2b 2b

Solutions on MaxInterview for how to make a snake game in c 2b 2b by the best coders in the world

showing results for - "how to make a snake game in c 2b 2b"
Joaquín
02 Aug 2016
1#include<iostream>
2#include<cmath>
3#include<iomanip>
4#include<conio.h>
5using namespace std;
6
7bool youlose;
8const int wide=30;
9const int height = 20;
10int x,y,fruitX,fruitY,score;
11enum direction {STOP,LEFT,RIGHT,UP,DOWN};
12direction direct;
13int tailx[100];
14int taily[100];
15int ntail;
16void setup(){
17youlose=false;
18direct=STOP;
19x = wide/2;
20y = height/2;
21fruitX= rand()% wide;
22fruitY = rand()% height;
23score = 0;
24}
25
26
27
28
29
30void draw(){
31
32    system("cls");
33    for(int i=0;i< wide;i++){cout<< "#";}
34    cout<< endl;
35    for(int i=0;i< height;i++){
36            for(int j=0;j<wide;j++){
37                if(j==0)
38                    cout<<"#";
39              if(i == y&& j == x)
40
41                cout<< "<>";
42              else if(i== fruitY-1&&j == fruitX-1)
43              cout<< "O";
44                else{
45                    for(int k=0;k<ntail;k++){
46                       bool print = false;
47                        if(tailx[k] == j&& taily[k]== i){
48                            cout<< "o";
49                             print = true;
50                        }
51
52
53
54                    }
55                      cout<< " ";
56                }
57                    cout<< " ";
58                if(j== wide-1)
59                    cout<< "#";
60            }
61           cout<< endl;
62    }
63    system("cls");
64    for(int i=0;i< wide+2;i++){cout<< "#";}
65    cout<< endl;
66    cout<< "score: "<< score << endl;
67}
68void input(){
69  if(_kbhit()){
70    switch(_getch()){
71   case 'w':
72    direct = UP;
73    break;
74   case 'a':
75    direct = LEFT;
76    break;
77   case 'd':
78    direct = RIGHT;
79    break;
80   case 's':
81    direct = DOWN;
82    break;
83   case 'x':
84    youlose = true;
85    cout<< "GAME OVER";
86
87
88    }
89  }
90}
91void logic()
92{
93    int prevx = tailx[0];
94    int prevy = taily[0];
95    int prev2x;
96    int prev2y;
97    tailx[0]= x;
98    taily[0]= y;
99    for(int i=1;i< ntail;i++)
100
101    {
102    prev2x = tailx[i];
103    prev2y= taily[i];
104    tailx[i] = prevx;
105    taily[i] = prev2y;
106    prevx = prev2x;
107    prev2y = prevy;
108    }
109 switch(direct){
110  case LEFT:
111      x--;
112      break;
113  case RIGHT:
114      x++;
115    break;
116  case UP:
117      y--;
118    break;
119  case DOWN:
120      y++;
121    break;
122  default:
123    break;
124 }
125 if(x> wide || x < 0|| y > height|| y <0)
126
127  direct = STOP;
128  /*for(int i=0;i< ntail;i++)
129     if(tailx[i]==x && taily[i]== y)
130     youlose = true;*/
131
132 if(x == fruitX || y == fruitY){score += 10;
133 fruitX= rand()% wide;
134 fruitY = rand()% height;
135 ntail++;
136 }
137
138}
139
140
141
142
143
144
145using namespace std;
146
147
148  int main(){
149
150
151setup();
152while(!youlose){
153    draw();
154    input();
155    logic();
156
157}
158
159
160
161   return 0;
162  }
163
164