Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "snake.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
snakepart::snakepart(int col, int row)
{
x=col;
y=row;
}
snakepart::snakepart()
{
x=0;
y=0;
}
snakeclass::snakeclass()
{
initscr();
nodelay(stdscr,true); //the program doesnt wait for the user to press the key
keypad(stdscr,true); //activates the keyboard
noecho();
curs_set(0);
getmaxyx(stdscr,maxheight,maxwidth);
//init variables
partchar='o';
oldalchar=(char)64;
foo='*';
food.x=0;
food.y=0;
for(int i=0;i<5;i++)
snake.push_back(snakepart(40+i,10));
points=0;
del=110000;
get=false;
direction='l';
srand(time(NULL)); // This macro is the value of a null pointer constan
putfood();
for (int i=0;i<maxwidth-1;i++)
{
move(0,i);
addch(oldalchar);
}
for (int i=0;i<maxwidth-1;i++)
{
move(maxheight-2,i);
addch(oldalchar);
}
for(int i=0;i<maxheight-1;i++)
{
move(i,0);
addch(oldalchar);
}
for(int i=0;i<maxheight-1;i++)
{
move(i,maxwidth-2);
addch(oldalchar);
}
for(int i=0;i<snake.size();i++)
{
move(snake[i].y,snake[i].x);
addch(partchar);
}
move(maxheight-1,0);
printw("%d",points);
move(food.y,food.x);
addch(foo);
refresh();
}
snakeclass::~snakeclass()
{
nodelay(stdscr,false);
getch();
endwin();
}
void snakeclass::putfood() //randomly places food across the terminal
{
while(1)
{
int tmpx=rand()%maxwidth+1;
int tmpy=rand()%maxheight+1;
for(int i=0; i<snake.size();i++)
if(snake[i].x==tmpx && snake[i].y==tmpy)
continue;
if(tmpx>=maxwidth-2 || tmpy>=maxheight-3)
continue;
food.x=tmpx;
food.y=tmpy;
break;
}
move(food.y, food.x);
addch(foo);
refresh();
}
bool snakeclass::collision() // collision detection with the snake, box, food
{
if(snake[0].x==0 || snake[0].x==maxwidth-1 || snake[0].y==0 || snake[0].y==maxheight-2)
return true;
for(int i=2;i<snake.size();i++)
if(snake[0].x==snake[i].x && snake[i].y==snake[0].y)
return true;
if(snake[0].x==food.x && snake[0].y==food.y)
{
get=true;
putfood();
points+=10;
move(maxheight-1,0); //increases the size of the snake
printw("%d", points);
if((points%100)==0)
del-=10000;
}else
get=false;
return false;
}
void snakeclass::movesnake()
{
int tmp=getch();
switch(tmp)// allows a variable to be tested for equality against a list of values.
{
case KEY_LEFT:
if(direction!='r')
direction='l';
break;
case KEY_UP:
if(direction!='d')
direction='u';
break;
case KEY_DOWN:
if(direction!='u')
direction='d';
break;
case KEY_RIGHT:
if(direction!='l')
direction='r';
break;
case KEY_BACKSPACE:
direction='q';
break;
}
if(!get)
{
move(snake[snake.size()-1].y, snake[snake.size()-1].x);
addch(' ');
refresh();
snake.pop_back(); //Removes the last element in the vector, effectively reducing the container size by one.
}
if(direction=='l')
{
snake.insert(snake.begin(),snakepart(snake[0].x-1, snake[0].y));
}
else if(direction=='r')
{
snake.insert(snake.begin(),snakepart(snake[0].x+1, snake[0].y));
}
else if(direction=='u')
{
snake.insert(snake.begin(),snakepart(snake[0].x, snake[0].y-1));
}
else if(direction=='d')
{
snake.insert(snake.begin(),snakepart(snake[0].x, snake[0].y+1));
}
move(snake[0].y,snake[0].x);
addch(partchar);
refresh();
}
void snakeclass::start()
{
while(1)
{
if(collision())
{
move(12,36);
printw("Game Over");
break;
}
movesnake();
if(direction=='q') //exit
break;
usleep(del); //function suspends execution of the calling thread
}
}