আমি যখন ২০০৮ সালে প্রোগ্রামিং প্রতিযোগীতায় অংশগ্রহন করেছিলাম তখন এই প্রগ্রাম টি দিয়েছিলাম এবং লিস্ট এ ছিলাম । তখন আর সফটওয়্যার বানানো জানতাম না বলে ১ম/২য়/৩য় হতে পারিনি।অথচ এখন সফটওয়্যার বানাতে খুব ভালো লাগে।নিজের সফটওয়্যার নিজে ব্যাবহার করি।
নিচের প্রগ্রাম টি নোটপ্যাড এ paste করে sr3.cpp নামে সেভ করুন।এবং এটি C ড্রাইভ এ TC এর মধ্যে bin এর মধ্যে রাখুন ।এবার ফাইল টি compile করে দেখুন।
#include <graphics.h> #include <stdlib.h> #include <math.h> #include <stdio.h> #include <conio.h> #include <dos.h> #include <iostream.h> class Snake { public : Snake(int x,int y) { xPos=x; yPos=y; }; /* ~Snake() { ; }; */ int length,size,color,xPos,yPos,stIndex,dlIndex; int borderColor,snakeColor,scr,sp; int objX,objY; int xArr[8000]; int yArr[8000]; float speed; void menu(); void menu2(); void newgame(); void instruction(); void quit(); int moveXRight(); int moveXLeft(); int moveYUp(); int moveYDown(); void store(void); void erase(void); void init1(void); void init2(void); void init3(void); void test(int x); void putAnObject(void); void getAkey(void); void score(void); void gameOver(); void easy(); void medium(); void hard(); void logo(); }; int Snake::moveXRight() { for (int i=0 ; i<800; i++) { setcolor(4); store(); circle(xPos++,yPos,4); delay(speed); erase(); test(1); if (xPos % 10 ==0) if (kbhit()) break; } return xPos; } int Snake::moveXLeft(void) { for (int i=0 ; i<800; i++) { setcolor(4); store(); circle(xPos--,yPos,4); delay(speed); erase(); test(2); if (xPos % 10 ==0) if (kbhit()) break; } return xPos; } int Snake::moveYUp() { for (int i=0 ; i<800; i++) { setcolor(4); store(); circle(xPos,yPos--,4); delay(speed); erase(); test(3); if (yPos % 10 ==0) if (kbhit()) break; } return yPos; } int Snake::moveYDown() { for (int i=0 ; i<800; i++) { setcolor(4); store(); circle(xPos,yPos++,4); delay(speed); erase(); test(4); if (yPos % 10 ==0) if (kbhit()) break; } return yPos; } void Snake::store() { xArr[stIndex]= xPos; yArr[stIndex]= yPos; stIndex++; if (stIndex == size) stIndex=0; } void Snake::erase() { setcolor(0); if (dlIndex-length >=0) { circle(xArr[dlIndex-length],yArr[dlIndex-length],4); } else circle(xArr[size-abs(dlIndex-length)],yArr[size-abs(dlIndex-length)],4); dlIndex++; if (dlIndex == size) dlIndex =0; } void Snake::getAkey(void) { char key; for (int k=0 ;k<3 ;k++) { k=1;// infinite loop key = getche(); if (key == 'M') //scan_right asci value 77, M =77 xPos = moveXRight(); if (key == 'K') xPos = moveXLeft(); if (key == 'H') yPos = moveYUp(); if (key == 'P') yPos = moveYDown(); if (key=='b' || key== 'B') menu2(); // if(key=='\n') // menu2(); } } void Snake::test(int x) { int tst; switch (x) { case 1: //when moving right { tst = getpixel(xPos+4,yPos); if (xPos == objX*10 && yPos == objY*10) score(); if (tst== borderColor || tst == snakeColor) gameOver(); break; }// Game over //left case 2: { tst = getpixel(xPos-4,yPos); if (xPos == objX*10 && yPos == objY*10) score(); if (tst== borderColor || tst == snakeColor) gameOver(); break; }// Game over //up case 3: { tst = getpixel(xPos,yPos-4); if (xPos == objX*10 && yPos == objY*10) score(); if (tst== borderColor || tst == snakeColor) gameOver(); break; }// Game over //down case 4: { tst = getpixel(xPos,yPos+4); if (xPos == objX*10 && yPos == objY*10) score(); if (tst== borderColor || tst == snakeColor) gameOver(); break; } }// end of switch } void Snake::init1(void) { borderColor = YELLOW; setcolor(RED); settextstyle(0, 0, 4); outtextxy(50,50,"ROMEL"); gotoxy(30,6); cout<<"SCORE : 0"; gotoxy(60,6); cout<<"SPEED : 10"; settextstyle(7,0,1); // setcolor(DARKGRAY); setcolor(GREEN); outtextxy(200,450,"*** press B or b <- go_back ***"); //setbkcolor(6); setcolor(borderColor); rectangle(100,100,540,380); rectangle(105,105,535,375); rectangle(110,110,530,370); scr=0; sp=10; dlIndex=0; stIndex=0; speed=10; snakeColor = RED; size = 7999; length = 50; for (int i=0 ;i<8000; i++) { xArr[i] = 0; yArr[i] = 0; } //getch(); // menu2(); } void Snake::init2(void) { borderColor = BLUE; setcolor(RED); settextstyle(0, 1, 6); outtextxy(50,10,"ROMEL"); gotoxy(14,6); cout<<"SCORE : 0"; gotoxy(60,6); cout<<"SPEED : 10"; settextstyle(7,0,1); // setcolor(DARKGRAY); setcolor(GREEN); outtextxy(255,450,"*** press B or b <- go_back ***"); //setbkcolor(6); setcolor(borderColor); rectangle(100,100,540,380); rectangle(105,105,535,375); rectangle(110,110,530,370); line(220,220,420,220); scr=0; sp=10; dlIndex=0; stIndex=0; speed=8; snakeColor = RED; size = 7999; length = 50; for (int i=0 ;i<8000; i++) { xArr[i] = 0; yArr[i] = 0; } // getch(); /// menu(); } void Snake::init3(void) { borderColor = BLUE; setcolor(RED); settextstyle(0, 1, 6); outtextxy(50,10,"ROMEL"); gotoxy(14,6); cout<<"SCORE : 0"; gotoxy(60,6); cout<<"SPEED : 10"; settextstyle(7,0,1); // setcolor(DARKGRAY); setcolor(GREEN); outtextxy(255,450,"*** press B or b <- go_back ***"); //setbkcolor(6); setcolor(borderColor); rectangle(100,100,540,380); rectangle(105,105,535,375); rectangle(110,110,530,370); line(220,195,420,195); line(420,195,420,295); line(220,295,420,295); scr=0; sp=10; dlIndex=0; stIndex=0; speed=6; snakeColor = RED; size = 7999; length = 50; for (int i=0 ;i<8000; i++) { xArr[i] = 0; yArr[i] = 0; } // getch(); /// menu(); } void Snake::putAnObject() { int test2=0; for (int i=0 ;i<1000 ; i++) { objX = random(41)+12 ; objY = random(25)+12 ; // the less minus 10 test2 = getpixel(objX*10,objY*10); setcolor(GREEN); if (test2 != snakeColor) { circle(objX*10,objY*10,4); setfillstyle(7 ,WHITE); fillellipse(objX*10,objY*10,4,4); break; } } } void Snake::gameOver() { setcolor(WHITE); // ellipse(320, 240,30,30, 170, 25); setfillstyle(9 ,BLUE); fillellipse(320, 240, 170, 25); settextstyle(0,0,2); setcolor(RED); outtextxy(188,232,"G A M E O V E R "); sound(1100); delay(1000); nosound(); getch(); // getch(); // getch(); logo(); getch(); menu(); } void Snake::logo() { cleardevice(); setcolor(RED); settextstyle(0, 1, 6); outtextxy(50,10,"SIMANTO"); int sz=0,dl=100; for (int i=0 ;i!='\n'; i++) { if (sz < 4) sz++; cleardevice(); gotoxy(35,20); cout<<"Score: "; cout<<scr; setcolor(i); settextstyle(0,0,sz); if (scr==0) { outtextxy(150,200,"Hi looser! "); delay(dl); } else { outtextxy(150,200,"GADHA ! "); delay(dl); } } getch(); cleardevice(); menu(); } void Snake::easy() { cleardevice(); xPos=320; yPos=240; init1(); randomize(); putAnObject(); getAkey(); } void Snake::medium() { cleardevice(); xPos=320; yPos=240; init2(); randomize(); putAnObject(); getAkey(); } void Snake::hard() { cleardevice(); xPos=320; yPos=240; init3(); randomize(); putAnObject(); getAkey(); } void Snake::score() { scr+=10; sp+=2; speed=speed-0.1; //scr+=15; //scr+=20; gotoxy(14,6); cout<<"SCORE : "; cout<<scr; gotoxy(60,6); cout<<"SPEED : "; cout<<sp; sound(800); delay(80); nosound(); length+=15; //length+=15; //length+=20; putAnObject(); } void Snake::menu() { cleardevice(); setcolor(RED); settextstyle(0, 1, 6); outtextxy(50,10,"SIAMNTO"); gotoxy(14,6); borderColor=GREEN; setcolor(borderColor); rectangle(100,100,540,380); rectangle(105,105,535,375); rectangle(110,110,530,370); setcolor(GREEN); gotoxy(35,14); cout<<"1. Start Game"; gotoxy(35,16); cout<<"2. Isntruction"; gotoxy(35,18); cout<<"3. Quit"; settextstyle(7,0,1); setcolor(GREEN); outtextxy(170,450,"*** Enter 1,2 or 3 for select option ***"); char ch; ch=getch(); switch ( ch ) // detects which option was chosen { // based on the y location of the arrow case '1': menu2(); // Starts a new game break; case '2': instruction(); // shows the credits break; case '3': quit(); // quits break; } // this returns to top } void Snake::quit() { exit(1); } void Snake::instruction() { cleardevice(); gotoxy(20,8); cout<<"Enter 1 for Start Game"; gotoxy(24,10); cout<<"Enter 1 for Easy Game"; gotoxy(24,12); cout<<"Enter 2 for Medium Game"; gotoxy(24,14); cout<<"Enter 3 for Hard Game"; gotoxy(20,16); cout<<"Enter 2 for Instruction"; gotoxy(20,18); cout<<"Enter 3 for Quit"; gotoxy(20,20); cout<<"-> turn right, <- turn left"; gotoxy(20,22); cout<<"Up arrow turn up and Down arrow turn down"; settextstyle(7,0,1); setcolor(GREEN); outtextxy(240,450,"*** press ENTER <-- back***"); getch(); menu(); } void Snake::menu2() { cleardevice(); setcolor(RED); settextstyle(0, 1, 6); outtextxy(50,10,"SIMANTO"); // gotoxy(14,6); borderColor=GREEN; setcolor(borderColor); rectangle(100,100,540,380); rectangle(105,105,535,375); rectangle(110,110,530,370); setcolor(GREEN); gotoxy(35,14); cout<<"1. Easy game"; gotoxy(35,16); cout<<"2. Medium game"; gotoxy(35,18); cout<<"3. Hard game"; settextstyle(7,0,1); setcolor(GREEN); outtextxy(110,450,"Enter 1,2 or 3 for select game*** press ENTER <-- back***"); char ch; ch=getch(); switch ( ch ) { case '1': easy(); break; case '2': medium(); break; case '3': hard(); break; } //getch(); menu(); // this returns to top } void main(void) /* MAIN */ { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); randomize(); Snake snake(320,240); snake.menu(); snake.init1(); snake.init2(); snake.init3(); snake.putAnObject(); snake.getAkey(); getch(); closegraph(); }
সমস্যা হলে জানাবেন
আমি প্রোগ্রামার রোমেল। বিশ্বের সর্ববৃহৎ বিজ্ঞান ও প্রযুক্তির সৌশল নেটওয়ার্ক - টেকটিউনস এ আমি 15 বছর যাবৎ যুক্ত আছি। টেকটিউনস আমি এ পর্যন্ত 87 টি টিউন ও 732 টি টিউমেন্ট করেছি। টেকটিউনসে আমার 0 ফলোয়ার আছে এবং আমি টেকটিউনসে 0 টিউনারকে ফলো করি।
ফেসবুকে আমি http://www.facebok.com/simantoromel.bd আমার ওয়েবসাইট http://www.corposolution.com
সি বুজি না । ধন্যবাদ টিউনের জন্য