Facebook Twitter Gplus YouTube E-mail RSS
magnify
magnify
formats

PDCurses – Snake Game: TODO List

Need

  • Bug – fix the snake being able to walk through the walls (confirmed on  right side)
  • Refine the speed at which you go up and down to it is even with the left – right
  • Find a comfortable speed at which is not too hard or easy
  • Make it so the game doesn’t end after you have died by pressing any button
  • Make the food (mice) have AI

Want

  • A menu screen with the option of PLAY or CREDITS
  • Change the quit key to something like ESC
  • Add “Score” in front of the score variable

Like

  • Add walls in the play zone
  • Add random generating walls
  • Add different difficulty levels – speed of player/mice
  • Add more comments
  • Make vaiable names more meaningful ‘etel’ for example

main.cpp

snake.h

snake.cpp

 

 

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
1 Comment  comments 
formats

Current PDCurses Tutorial

http://www.youtube.com/watch?v=K_Fl46lqKu8

http://www.linuxdoc.org/HOWTO/NCURSES-Programming-HOWTO/index.html

http://codepaste.net/8wpvo3 - snake.cpp

http://codepaste.net/km6z1v - snake.h

http://codepaste.net/w444sa – main.cpp


#include "snake.h"
#include <Windows.h>
#include <time.h>
using namespace std;

void uSleep(int waitTime) { //this function is for millisecond sleep
__int64 time1 = 0, time2 = 0, freq = 0;

QueryPerformanceCounter((LARGE_INTEGER *) &time1);
QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

do {
QueryPerformanceCounter((LARGE_INTEGER *) &time2);
} while((time2-time1) < waitTime);
}

snakepart::snakepart(int col,int row)
{
x=col;
y=row;
}

snakepart::snakepart()
{
x=0;
y=0;
}
snakeclass::snakeclass()
{
initscr();
nodelay(stdscr,true); //don't wait for keypress
keypad(stdscr,true); //init the keyboard
noecho(); //don't write
curs_set(0); //cursor invisible
getmaxyx(stdscr,maxheight,maxwidth);
partchar='O';
oldalchar=(char)219;
etel='*';
food.x=0;
food.y=0;
for(int i=0;i<5;i++)
snake.push_back(snakepart(40+i,10));
points=0;

get=0;
direction='l';
srand(time(NULL));
putfood();
//make the game-board -- up-vertical
for(int i=0;i<maxwidth-1;i++)
{
move(0,i);
addch(oldalchar);
}
//left-horizontal
for(int i=0;i<maxheight-1;i++)
{
move(i,0);
addch(oldalchar);
}
//down-vertical
for(int i=0;i<maxwidth-1;i++)
{
move(maxheight-2,i);
addch(oldalchar);
}
//right-horizontal
for(int i=0;i<maxheight-1;i++)
{
move(i,maxwidth-2);
addch(oldalchar);
}
//draw the snake
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(etel);
refresh();
}

snakeclass::~snakeclass()
{
nodelay(stdscr,false); //turn back
getch(); //wait until a key is pressed
endwin();
}

void snakeclass::putfood()
{
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(etel);
refresh();
}

bool snakeclass::collision()
{
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[0].y==snake[i].y)
return true;
}
//collision with the food
if(snake[0].x==food.x && snake[0].y==food.y)
{
get=true;
putfood();
points+=10;
move(maxheight-1,0);
printw("%d",points);
if((points%100)==0)
del-=10000;
}else
get=false;
return false;
}
void snakeclass::movesnake()
{
//detect key
int tmp=getch();
switch(tmp)
{
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 there wasn't a collision with food
if(!get)
{
move(snake[snake.size()-1].y,snake[snake.size()-1].x);
printw(" ");
refresh();
snake.pop_back();
}
if(direction=='l')
{
snake.insert(snake.begin(),snakepart(snake[0].x-1,snake[0].y));
del=210000*.65;
}else if(direction=='r'){
snake.insert(snake.begin(),snakepart(snake[0].x+1,snake[0].y));
del=210000*.65;

}else if(direction=='u'){
del=210000;
snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y-1));
}else if(direction=='d'){
del=210000;
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); //delay
}
}

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

Jon Jones Vs Vitor Belfort – Results + Videos

Coming into the fight I was backing Vitor to win because of what Jones did at UFC151 (old news now), however I did not think Vitor had what it takes to beat Jones. The first round I and most likely alot of people was shocked to see Jon Jones in a full extended armbar that looked like it did big damage to his arm.

~Unfinished

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
formats

PDCurses – Installing & Setting up with Visual Studio 2010

Step 1: Download PDCurses

Step 2: Create a folder called pdcurs34 in C:\

Step 3: Extract the PDCurses files in the pdcurs34 folder

Step 4: Copy this: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin

Step 5: In command prompt (cmd.exe – admin) type cd “C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin” (make sure you include the “) and press enter

Step 6: Type vcvars32.bat and press enter. This should appear underneath “Setting environment for using Microsoft Visual Studio 2010 x86 tools.”

Step 7: Type set PDCURSES_SRCDIR=C:\pdcurs34 and press enter

Step 8: Type cd C:\pdcurs34\win32 and press enter

Step 9: Type nmake -f vcwin32.mak and press enter. This should make loads of text appear in the console – files are being created

Step 10: Go to Visual Studio 2010 (I have the C++ edition) – Make a new project – in visual c++ choose Win32 then click Win32 Project and give it a name – Click Ok – Click Next – Change from windows application to Console application and click the option Empty project - Click finish

Step 11: In Solution Explorer (VS2010) right click the folder Source Files and hover over Add – Click New Item – Click C++ File & give it a name

Step 12: Go to Project – <projectname> Properties – In configuration properties go to VC++ Directories – In the include directories click the arrow (left of bar) and click edit and then locate to C:\pdcurs34 click ok

Step 13: Do the same with Library Directories but instead point it to C:\pscurs34\win32

Step 14: Go the C/C++ dropdown – Code Generation – Change the option in Runtime Library to Multi-threaded Debug (\MTd)

Step 15: Go to Linker dropdown – Input – Additional Dependencies – Arrow – Edit and type pscurses.lib – press ok

The PDCurses set-up should now be complete you should test it by putting some simple code in the .cpp you made, here is some code to copy:

#include <curses.h>

int main ()

{

initscr();

printw(“Hello World!”);

refresh();

getch();

endwin();

 

return 0;

}

 

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
1 Comment  comments