Snake.cpp 3.04 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

Nelson Phillips's avatar
Nelson Phillips committed
9
Snake bdy;
Nelson Phillips's avatar
Nelson Phillips committed
10 11
vector <Body> parts;
 
Nelson Phillips's avatar
Nelson Phillips committed
12
 void Snake::movement(char input){
Nelson Phillips's avatar
Nelson Phillips committed
13 14 15
          if((input == 'w'|| input == 'W')&& direction != 3 )
            direction = 1;
          
Nelson Phillips's avatar
Nelson Phillips committed
16
          else 
Nelson Phillips's avatar
Nelson Phillips committed
17 18 19 20 21 22
          if((input == 'a'|| input == 'A')&& direction != 2 )
            direction = 4;
          
          else
          if((input == 's'|| input == 'S')&& direction != 1 )
            direction = 3;
Nelson Phillips's avatar
Nelson Phillips committed
23 24
          
          else
Nelson Phillips's avatar
Nelson Phillips committed
25 26 27
          if((input == 'd'|| input == 'D')&& direction != 4 )
            direction = 2;
            
Nelson Phillips's avatar
Nelson Phillips committed
28
          if(direction == 1){
Nelson Phillips's avatar
Nelson Phillips committed
29
            x--;
Nelson Phillips's avatar
Nelson Phillips committed
30 31 32 33
            for ( int count = 0; count < parts.size(); count++){
                parts[count].changeX(1);
            }
          }
Nelson Phillips's avatar
Nelson Phillips committed
34
          else 
Nelson Phillips's avatar
Nelson Phillips committed
35
          if(direction == 4){
Nelson Phillips's avatar
Nelson Phillips committed
36
            y--;
Nelson Phillips's avatar
Nelson Phillips committed
37 38 39 40 41
            
            for ( int count = 0; count < parts.size(); count++){
                parts[count].changeY(1);
            }
          }
Nelson Phillips's avatar
Nelson Phillips committed
42
          else
Nelson Phillips's avatar
Nelson Phillips committed
43
          if(direction == 3){
Nelson Phillips's avatar
Nelson Phillips committed
44
            x++;
Nelson Phillips's avatar
Nelson Phillips committed
45 46 47 48
            for ( int count = 0; count < parts.size(); count++){
                parts[count].changeX(0);
            }
          }
Nelson Phillips's avatar
Nelson Phillips committed
49
          else
Nelson Phillips's avatar
Nelson Phillips committed
50
          if(direction == 2){
Nelson Phillips's avatar
Nelson Phillips committed
51
            y++;
Nelson Phillips's avatar
Nelson Phillips committed
52 53 54 55
            for ( int count = 0; count < parts.size(); count++){
                parts[count].changeY(0);
            }
          }
Nelson Phillips's avatar
Nelson Phillips committed
56 57 58 59
 }
 
 
 void Snake::draw(){
Nelson Phillips's avatar
Nelson Phillips committed
60 61 62 63 64 65
    for (int i = 0; i < 14; i++){
     for (int q = 0; q < 30; q++){
        if (x < 0)x++;
        if (y < 0)y++;
        if (x > 12)x--;
        if (y > 29)y--;
Nelson Phillips's avatar
Nelson Phillips committed
66
        
Nelson Phillips's avatar
Nelson Phillips committed
67
     
Nelson Phillips's avatar
Nelson Phillips committed
68 69 70
        if (h == i || o == q)
            grid[h][o] = '*';
        
Nelson Phillips's avatar
Nelson Phillips committed
71 72 73 74
        if (i == x && q == y)
            grid[x][y] = 'S';
        
        if ((i != h || o != q) && (i != x || q != y))
Nelson Phillips's avatar
Nelson Phillips committed
75 76
            grid[i][q] = whtSpace;
            
Nelson Phillips's avatar
Nelson Phillips committed
77 78 79 80 81 82 83 84 85 86 87 88
    for ( int count = 0; count < parts.size(); count++){
        grid[parts[count].getX()][parts[count].getY()] = 'B';
    }
    }
    }
    
    
    
    
    for (int i = 0; i < 14; i++){
        cout << endl;
    for (int q = 0; q < 30; q++){
Nelson Phillips's avatar
Nelson Phillips committed
89
        cout << grid[i][q] << " ";
Nelson Phillips's avatar
Nelson Phillips committed
90 91
        grid[i][q] == ' ';
    }
Nelson Phillips's avatar
Nelson Phillips committed
92
    }
Nelson Phillips's avatar
Nelson Phillips committed
93
if (grid [x][y] == grid[h][o] ){
Nelson Phillips's avatar
Nelson Phillips committed
94 95 96
    h = (rand() % 13);
    o = (rand() % 30);
    score++;
Nelson Phillips's avatar
Nelson Phillips committed
97 98
    bdy.lengthed();
    bdy.newBody();
Nelson Phillips's avatar
Nelson Phillips committed
99 100 101
}
 
}
Nelson Phillips's avatar
Nelson Phillips committed
102 103 104 105 106 107 108 109
 int Snake::snekoScore(){
    return score;
 }
 
 void Snake::clrScreen(){
    for (int p = 0; p < 40; p++){ 
        cout << endl;
    }
Nelson Phillips's avatar
Nelson Phillips committed
110 111
 }
 
Nelson Phillips's avatar
Nelson Phillips committed
112 113 114 115 116 117
 int Snake::getX(){
     return x;
 }
 int Snake::getY(){
     return y;
 }
Nelson Phillips's avatar
Nelson Phillips committed
118 119 120 121 122 123
 int Snake::getLeng(){
     return length;
 }
 void Snake::lengthed(){
    length++;
 }
Nelson Phillips's avatar
Nelson Phillips committed
124
 
Nelson Phillips's avatar
Nelson Phillips committed
125
 void Snake::newBody(){
Nelson Phillips's avatar
Nelson Phillips committed
126
    Body segment (x,y);//parts[bdy.getLeng()].getX(), parts[bdy.getLeng()].getY()
Nelson Phillips's avatar
Nelson Phillips committed
127
    parts.push_back(segment);
Nelson Phillips's avatar
Nelson Phillips committed
128 129
 }
 
Nelson Phillips's avatar
Nelson Phillips committed
130 131 132 133 134 135 136
 
 
 
 
 
 
 
Nelson Phillips's avatar
Nelson Phillips committed
137 138 139 140 141 142
  void Body::changeX(bool updown){
    if (updown)
        x--;
    else
        x++;
  }
Nelson Phillips's avatar
Nelson Phillips committed
143
 
Nelson Phillips's avatar
Nelson Phillips committed
144 145 146 147 148 149
  void Body::changeY(bool updown){
    if (updown)
        y--;
    else
        y++;
  }
Nelson Phillips's avatar
Nelson Phillips committed
150 151 152 153 154 155 156 157 158 159 160 161
 
 
 Body::Body(int xCord, int yCord){
    x = xCord;
    y = yCord;
 }
 
 int Body::getX(){
     return x;
 }
 int Body::getY(){
     return y;
Nelson Phillips's avatar
Nelson Phillips committed
162 163 164
 }