Archive for January 19th, 2011

Một số hàm mở rộng trên C/C++

Sau đây là một số hàm mở rộng ở console của C/C++ nhưng trong Codeblock không có.
Để dùng được nó các bạn phải tự viết hàm. Các hàm này sẽ rất hữu ích khi các bạn muốn viết một game nho nhỏ trên console với Codeblock.

Trình biên dịch: Codeblock

1. Hàm xoá màn hình
Header file:
#include <stdlib.h>

system(“CLS”);

2. Hàm tạo số ngẫu nhiên
Header file:
#include <stdlib.h>
#include <time.h>

srand(time(NULL));
rand()%100+1; //tạo số nguyên ngẫu nhiên từ 1-100

Ví dụ in ra màn hình 20 số ngẫu nhiên từ 0-90

#include <iostream>
using namespace std;
main()
{
char i;
srand(time(NULL));
for(i=0;i<20;i++)
cout<<rand()%91<<” “;
}

3. Hàm chuyển con trỏ đến toạ độ x,y trên màn hình console: gotoxy(x,y)
(màn hình console khi chưa resize được chia thành 80 cột và 24 hàng)
Header file:
#include <windows.h>

void gotoxy(int x,int y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x-1,y-1};
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput , Cursor_an_Pos);
}

4. Các hàm tạo màu cho text và màu background cho console
+ hàm tạo màu cho text: textcolor(WORD color)
WORD sẽ mang giá trị từ 0-15
Header file:
#include <windows.h>

void textcolor(WORD color)
{
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
WORD wAttributes = screen_buffer_info.wAttributes;
color &= 0×000f;
wAttributes &= 0xfff0;
wAttributes |= color;
SetConsoleTextAttribute(hConsoleOutput, wAttributes);
}

Bảng mã màu:

+ hàm tạo màu background
Header file:
#include <stdlib.h>

system(“Color ab”);

Trong đó a là màu background, b là màu text (ta có thể dùng hàm này để thay đổi màu cho text nhưng tốc độ thực thi chậm hơn textcolor).
Ví dụ: nền trắng, chữ hồng

#include <iostream>
#include <stdlib.h>
using namespace std;
main()
{
system(“Color FD”);
cout<<”Hello world”;
}

Bảng mã màu:

5. Hàm thay đổi kích thước Console: resizeConsole(int width, int height)
Header file:
#define _WIN32_WINNT 0×0500
#include <windows.h>

void resizeConsole(int width, int height)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, width, height, TRUE);
}

6. Hàm Delay()
Trong codeblock không hỗ trợ hàm Delay() vì thế ta có hàm thay thế là Sleep(), chức năng trì hoãn chương
trình trong một khoảng thời gian
Header file:
#include <windows.h>

Sleep(1200); //trì hoãn 1200ms = 1.2 giây

7. Hiển thị một ký tự c tại toạ độ x, y với màu color
Request: một số hàm đã định nghĩa ở trên

void multiPut(int x,int y,char c, int color)
{
gotoxy(x,y);
textcolor(color);
printf(“%c”,c);
gotoxy(40,20);
}

8. Thay đổi tiêu đều (Caption) cho chương trình
Header file:
#include <windows.h>

SetConsoleTitle(“My program…”); //title = “My program…”

9. Vẽ một hình chữ nhật có toạ độ 2 đỉnh (trên, trái – dưới, phải) là (x1,y1)-(x2,y2), với màu sắc color, có tiêu đề title và màu tiêu đề là ttColor
Request: một số hàm đã định nghĩa ở trên

class cBoard
{
public:
int x1,y1,x2,y2,width,height;
inline void drawBoard(int x1, int y1, int x2, int y2, int color=14,char *title=NULL,int ttColor=15)
{
int i;
multiPut(x1,y1,(char)218,color); //top-left
multiPut(x2,y1,(char)191,color); //top-right
multiPut(x1,y2,(char)192,color); //bottom-left
multiPut(x2,y2,(char)217,color); //bottom-right
textcolor(color);
for(i=x1;i<x2-1;i++) //top border */
multiPut(i+1,y1,(char)196,color);
for(i=x1;i<x2-1;i++) //bottom border */
multiPut(i+1,y2,(char)196,color);
for(i=0;i<y2-y1-1;i++) /* left border */
multiPut(x1,y1+i+1,(char)179,color);
for(i=0;i<y2-y1-1;i++) /* right border */
multiPut(x2,y1+i+1,(char)179,color);
if (title!=NULL&&strlen(title)<(x2-x1))
for(i=0;i<strlen(title);i++)
multiPut(i+(x2+x1)/2-strlen(title)/2,y1,*(title+i),ttColor);
}
}board;

Ví dụ:

int main()
{
clrscr();
SetConsoleTitle(“My program…”);
system(“Color 80″);
gotoxy(2,2);
textcolor(12); //red
cout<<char(3); //heart
board.drawBoard(1,1,30,20,14,” This is demo program “,15);
gotoxy(2,5);
textcolor(10);
cout<<”What next?”;
board.drawBoard(5,7,20,12,9);
resizeConsole(300,350);
getch();
return 0;
}

Kết quả:

Code in ra màn hình bảng mã Ascii của các ký tự.

#include <stdio.h>
#include <conio.h>
main()
{
for(int i=0;i<255;i++)
printf(“%3d:%c”,i,i);
getch();
}


10. Một số hàm khác với console

system(“help”); //các lệnh trên DOS
// các hàm trong thư viện wincon.h

Một trang web rất hữu ích về Console với C/C++ (các bạn sẽ tìm thấy hàm bắt sự kiện của chuột như ở part5).
http://www.adrianxw.dk/SoftwareSite/Consoles/Consoles1.html
Tra cứu các hàm chuẩn trong C/C++
http://virus.dtvt.org/Documents/cpp.chm
Một số code mẫu của các hàm phía trên.
&&

Source game trên Console với C++
(game shoot đã hoàn thành, game xếp hình đang dở dang)
http://www.mediafire.com/?h9a4ksqhw9v7ag4
Ngoài ra còn 1 game (thả boom) dùng đồ hoạ trên DOS nữa ở đây
http://dtvt.org/dtvt.asp?TID=5894&title=c-game-th%E1%BA%A3-bom-c%E1%BB%B1c-kh%E1%BB%A7ng-graphics-h%C4%91t
Game development ebook
http://www.box.net/shared/xp1g5t6o0s
Một số game đơn giản trên nền console: xếp hình, shoot, đua xe đơn giản, rắn săn mồi… hoặc có thể tìm hiểu thêm về OpenGL hay DirectX, trí thông minh nhân tạo… để phát triển game xài đồ hoạ trên nền win (codeblock có hỗ trợ cái này).

Hoặc ai pro hơn có thể viết cái này.

Have fun! Đốt pháo ...