Snake.cpp 4.99 KB
Newer Older
Nelson Phillips's avatar
Nelson Phillips committed
1
#include <iostream>
Nelson Phillips's avatar
Nelson Phillips committed
2 3
#include <thread>  
#include <unistd.h>
Nelson Phillips's avatar
Nelson Phillips committed
4
#include <vector>
Nelson Phillips's avatar
Nelson Phillips committed
5
#include "Snake.h"
Nelson Phillips's avatar
Nelson Phillips committed
6

Nelson Phillips's avatar
Nelson Phillips committed
7
using namespace std;
Nelson Phillips's avatar
Nelson Phillips committed
8
bool hi = true;
Nelson Phillips's avatar
Nelson Phillips committed
9
Snake bdy;
Nelson Phillips's avatar
Nelson Phillips committed
10 11 12 13 14
void Snake::movement(char input){
    
    
    // simple get input to change direction of snake.
        if((input == 'w'|| input == 'W')&& direction != 3 )
Nelson Phillips's avatar
Nelson Phillips committed
15 16
            direction = 1;
          
Nelson Phillips's avatar
Nelson Phillips committed
17 18
        else 
        if((input == 'a'|| input == 'A')&& direction != 2 )
Nelson Phillips's avatar
Nelson Phillips committed
19 20
            direction = 4;
          
Nelson Phillips's avatar
Nelson Phillips committed
21 22
        else
        if((input == 's'|| input == 'S')&& direction != 1 )
Nelson Phillips's avatar
Nelson Phillips committed
23
            direction = 3;
Nelson Phillips's avatar
Nelson Phillips committed
24
          
Nelson Phillips's avatar
Nelson Phillips committed
25 26
        else
        if((input == 'd'|| input == 'D')&& direction != 4 )
Nelson Phillips's avatar
Nelson Phillips committed
27
            direction = 2;
Nelson Phillips's avatar
Nelson Phillips committed
28 29 30 31 32
        
        
        
        // moves the first body part that exists on top of the head
        if(direction == 1){
Nelson Phillips's avatar
Nelson Phillips committed
33
            x--;
Nelson Phillips's avatar
Nelson Phillips committed
34
            parts[0].changeX(1);
Nelson Phillips's avatar
Nelson Phillips committed
35 36 37
        }
        else 
        if(direction == 4){
Nelson Phillips's avatar
Nelson Phillips committed
38
            y--;
Nelson Phillips's avatar
Nelson Phillips committed
39
            parts[0].changeY(1);
Nelson Phillips's avatar
Nelson Phillips committed
40 41 42
        }
        else
        if(direction == 3){
Nelson Phillips's avatar
Nelson Phillips committed
43
            x++;
Nelson Phillips's avatar
Nelson Phillips committed
44
            parts[0].changeX(0);
Nelson Phillips's avatar
Nelson Phillips committed
45 46 47
        }
        else
        if(direction == 2){
Nelson Phillips's avatar
Nelson Phillips committed
48
            y++;
Nelson Phillips's avatar
Nelson Phillips committed
49
            parts[0].changeY(0);
Nelson Phillips's avatar
Nelson Phillips committed
50
        }
Nelson Phillips's avatar
Nelson Phillips committed
51
        
Nelson Phillips's avatar
Nelson Phillips committed
52 53
        for (int count = 0; count < parts.size(); count++){
            if (count != 0)
Konrad McClure's avatar
Konrad McClure committed
54
            parts[count].follow(count, this);
Nelson Phillips's avatar
Nelson Phillips committed
55
        }
Nelson Phillips's avatar
Nelson Phillips committed
56 57
 }
 
Nelson Phillips's avatar
Nelson Phillips committed
58
 bool Snake::draw(){
Nelson Phillips's avatar
Nelson Phillips committed
59 60
    cout << "Your score is : " << score <<  endl;
    for (int i = 0; i < 29; i++){
Nelson Phillips's avatar
Nelson Phillips committed
61
     for (int q = 0; q < 60; q++){
Nelson Phillips's avatar
Nelson Phillips committed
62 63 64
         
         
        if (x < 1)return false; // end the loop if it hits a wall
Nelson Phillips's avatar
Nelson Phillips committed
65 66 67
        if (y < 1)return false;
        if (x > 26)return false;
        if (y > 57)return false;
Nelson Phillips's avatar
Nelson Phillips committed
68 69 70
        
        grid[i][q] = whtSpace; // each square is set to a white space
        if (h == i || o == q) // displayes the apple
Nelson Phillips's avatar
Nelson Phillips committed
71
            grid[h][o] = '*';
Nelson Phillips's avatar
Nelson Phillips committed
72
        if (i == 27 || i == 0)// top and bottom wall
Nelson Phillips's avatar
Nelson Phillips committed
73
            grid[i][q] = '-';
Nelson Phillips's avatar
Nelson Phillips committed
74
        if(q == 0 || q == 59)// side walls
Nelson Phillips's avatar
Nelson Phillips committed
75
            grid[i][q] = '|';
Nelson Phillips's avatar
Nelson Phillips committed
76 77
            
            
Nelson Phillips's avatar
Nelson Phillips committed
78 79 80 81 82 83
        for ( int count = 0; count < parts.size(); count++){
            
            if (grid[parts[0].getX()][parts[0].getY()] == '-') return false; // if a body connects with another you lose
            
            
            if (count == 0){// display for head
Nelson Phillips's avatar
Nelson Phillips committed
84 85
                if( direction == 2)
                    grid[parts[count].getX()][parts[count].getY()] = '>';
Nelson Phillips's avatar
Nelson Phillips committed
86
                    
Nelson Phillips's avatar
Nelson Phillips committed
87 88 89
                else 
                if ( direction == 3)
                    grid[parts[count].getX()][parts[count].getY()] = 'v';
Nelson Phillips's avatar
Nelson Phillips committed
90
                    
Nelson Phillips's avatar
Nelson Phillips committed
91 92 93
                else
                if( direction == 4)
                    grid[parts[count].getX()][parts[count].getY()] = '<';
Nelson Phillips's avatar
Nelson Phillips committed
94
                    
Nelson Phillips's avatar
Nelson Phillips committed
95 96 97
                else
                if( direction == 1)
                    grid[parts[count].getX()][parts[count].getY()] = '^';
Nelson Phillips's avatar
Nelson Phillips committed
98
                    
Nelson Phillips's avatar
Nelson Phillips committed
99 100 101
                else
                grid[parts[count].getX()][parts[count].getY()] = 'O';
            }
Nelson Phillips's avatar
Nelson Phillips committed
102
            else // body parts look like 
Nelson Phillips's avatar
Nelson Phillips committed
103
            grid[parts[count].getX()][parts[count].getY()] = '-';
Nelson Phillips's avatar
Nelson Phillips committed
104
        }
Nelson Phillips's avatar
Nelson Phillips committed
105
     }
Nelson Phillips's avatar
Nelson Phillips committed
106
    }
Nelson Phillips's avatar
Nelson Phillips committed
107
    // this is to draw the board
Nelson Phillips's avatar
Nelson Phillips committed
108
    for (int i = 0; i < 29; i++){
Nelson Phillips's avatar
Nelson Phillips committed
109
        cout << endl;
Nelson Phillips's avatar
Nelson Phillips committed
110 111 112
        for (int q = 0; q < 60; q++){
            cout << grid[i][q] << " ";
        }
Nelson Phillips's avatar
Nelson Phillips committed
113
    }
Nelson Phillips's avatar
Nelson Phillips committed
114 115 116
    
    
if (grid [x][y] == grid[h][o] ){   // randomizes the apples
Nelson Phillips's avatar
Nelson Phillips committed
117 118
    h = (rand() % 26) + 1;
    o = (rand() % 57) + 1;
Nelson Phillips's avatar
Nelson Phillips committed
119
    score++;
Konrad McClure's avatar
Konrad McClure committed
120
    newBody();
Nelson Phillips's avatar
Nelson Phillips committed
121
    bonusBody = bonusBody + 5;
Nelson Phillips's avatar
Nelson Phillips committed
122 123
    
    }else if (bonusBody > 0){ // adds body parts every loop after picking up apples
Konrad McClure's avatar
Konrad McClure committed
124
        newBody();
Nelson Phillips's avatar
Nelson Phillips committed
125 126 127 128
        bonusBody--;
    }

    return hi;
Nelson Phillips's avatar
Nelson Phillips committed
129
}
Nelson Phillips's avatar
Nelson Phillips committed
130

Nelson Phillips's avatar
Nelson Phillips committed
131 132 133 134 135 136 137 138
 int Snake::snekoScore(){
    return score;
 }
 
 void Snake::clrScreen(){
    for (int p = 0; p < 40; p++){ 
        cout << endl;
    }
Nelson Phillips's avatar
Nelson Phillips committed
139 140
 }
 
Nelson Phillips's avatar
Nelson Phillips committed
141
 int Snake::getX(){
Nelson Phillips's avatar
Nelson Phillips committed
142
    return x;
Nelson Phillips's avatar
Nelson Phillips committed
143
 }
Nelson Phillips's avatar
Nelson Phillips committed
144
 
Nelson Phillips's avatar
Nelson Phillips committed
145
 int Snake::getY(){
Nelson Phillips's avatar
Nelson Phillips committed
146
    return y;
Nelson Phillips's avatar
Nelson Phillips committed
147
 }
Nelson Phillips's avatar
Nelson Phillips committed
148 149
 
 void Snake::newBody(){
Nelson Phillips's avatar
Nelson Phillips committed
150
    Body segment (x,y);
Nelson Phillips's avatar
Nelson Phillips committed
151
    parts.push_back(segment);
Nelson Phillips's avatar
Nelson Phillips committed
152
 }
Nelson Phillips's avatar
Nelson Phillips committed
153
    
Nelson Phillips's avatar
Nelson Phillips committed
154 155 156
 void Snake::GameOver(){
    for (int p = 0; p < 40; p++){ 
        cout << endl;
Nelson Phillips's avatar
Nelson Phillips committed
157
    }
Nelson Phillips's avatar
Nelson Phillips committed
158 159 160 161 162 163 164
    cout << "   _____                         ____"<< endl <<
            "  / ____|                       / __ | " << endl <<
            " | |  __  __ _ _ __ ___   ___  | |  | |_   _____ _ __" << endl << 
            " | | |_ |/ _` | '_ ` _ | / _ | | |  | || | / / _ | '__|" << endl << 
            " | |__| | (_| | | | | | |  __/ | |__| || V /  __/ |" << endl <<
            "  |_____||__,_|_| |_| |_||___|  |____/  |_/ |___|_|" << endl;
 }
Nelson Phillips's avatar
Nelson Phillips committed
165
 
Nelson Phillips's avatar
Nelson Phillips committed
166

Nelson Phillips's avatar
Nelson Phillips committed
167 168
 
 
Nelson Phillips's avatar
Nelson Phillips committed
169 170 171
 void Body::changeX(bool updown){
    lastX = x;
    lastY = y;
Nelson Phillips's avatar
Nelson Phillips committed
172 173 174 175 176
    if (updown)
        x--;
    else
        x++;
  }
Nelson Phillips's avatar
Nelson Phillips committed
177
 
Nelson Phillips's avatar
Nelson Phillips committed
178
  void Body::changeY(bool updown){
Nelson Phillips's avatar
Nelson Phillips committed
179 180 181 182
        lastX = x;
        lastY = y;
        if (updown)
            y--;
Nelson Phillips's avatar
Nelson Phillips committed
183
    
Nelson Phillips's avatar
Nelson Phillips committed
184 185
    else
        y++;
Nelson Phillips's avatar
Nelson Phillips committed
186
 }
Nelson Phillips's avatar
Nelson Phillips committed
187
 
Nelson Phillips's avatar
Nelson Phillips committed
188
 Body::Body(int xCord, int yCord){
Nelson Phillips's avatar
Nelson Phillips committed
189 190 191 192 193
    x = xCord;
    y = yCord;
 }
 
 int Body::getX(){
Nelson Phillips's avatar
Nelson Phillips committed
194
    return x;
Nelson Phillips's avatar
Nelson Phillips committed
195
 }
Nelson Phillips's avatar
Nelson Phillips committed
196
 
Nelson Phillips's avatar
Nelson Phillips committed
197
 int Body::getY(){
Nelson Phillips's avatar
Nelson Phillips committed
198
    return y;
Nelson Phillips's avatar
Nelson Phillips committed
199
 }
Nelson Phillips's avatar
Nelson Phillips committed
200
 
Nelson Phillips's avatar
Nelson Phillips committed
201
 int Body::getlastX(){
Nelson Phillips's avatar
Nelson Phillips committed
202
    return lastX;
Nelson Phillips's avatar
Nelson Phillips committed
203
 }
Nelson Phillips's avatar
Nelson Phillips committed
204
 
Nelson Phillips's avatar
Nelson Phillips committed
205
 int Body::getlastY(){
Nelson Phillips's avatar
Nelson Phillips committed
206
    return lastY;
Nelson Phillips's avatar
Nelson Phillips committed
207 208
 }
 
Konrad McClure's avatar
Konrad McClure committed
209
 void Body::follow(int count, Snake* snake){
Nelson Phillips's avatar
Nelson Phillips committed
210 211
    lastX = x;
    lastY = y;
Konrad McClure's avatar
Konrad McClure committed
212 213
    x = snake->getPart(count - 1).getlastX();
    y = snake->getPart(count - 1).getlastY();
Nelson Phillips's avatar
Nelson Phillips committed
214
 }