#include <graphics.h> // 引用图形库头文件
#include <conio.h>
#include<time.h>
#if 0
int main1()
{
initgraph(640, 480);
MOUSEMSG m;
int r;// 创建绘图窗口,大小为 640x480 像素
while (1)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_MOUSEMOVE:
setcolor(RGB(rand() % 91 + 10, rand() % 91 + 10, rand() % 91 + 10));
if (m.mkLButton)
{
circle(m.x, m.y, rand() % 91 + 10);
break;
}
else if (m.mkRButton)
{
r = rand() % 91 + 10;
rectangle(m.x - r, m.y - r, m.x + r, m.y + r);
}
}
}
_getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
}
const int BoardW = 5;
const int BoardH = 5;
const int GridSz = 100;
const int Edge = 30;
void drawGrid()
{
setbkcolor(RGB(0, 162, 232));//背景颜色
cleardevice();//用当前背景颜色全涂色
setcolor(RGB(0, 0, 0));//边框用黑线
for (int i = 0; i <= BoardH; ++i)
{
line(Edge, Edge + i*GridSz, Edge + BoardW*GridSz, Edge + i*GridSz);
}
for (int i = 0; i <= BoardH; ++i)
{
line(Edge + i*GridSz, Edge, Edge + i*GridSz, Edge + BoardH*GridSz);
}
}
void drawLayout(int layout[BoardH][BoardW])
{
int r = GridSz * 4 / 10;
int x, y;
setcolor(RGB(255, 201, 14));//棋子边的颜色
setfillcolor(RGB(255, 201, 14));//棋子的填充色
for (int i = 0; i<BoardH; i++)
{
for (int j = 0; j<BoardW; j++)
{
if (1 == layout[i][j])
{
x = Edge + j*GridSz + GridSz / 2;
y = Edge + i*GridSz + GridSz / 2;
fillcircle(x, y, r);
}
}
}
}
void UpdateLayout(int Layout[BoardH][BoardW], int x, int y)
{
if (x <= Edge || x >= Edge + BoardW*GridSz || y <= Edge || y >= Edge + BoardH*GridSz)
{
return;
}
int i, j;
i = (y - Edge) / GridSz;
j = (x - Edge) / GridSz;
Layout[i][j] = 1 - Layout[i][j];
if (i - 1 >= 0)
{
Layout[i - 1][j] = 1 - Layout[i - 1][j];
}
if (i + 1 < BoardH)
{
Layout[i + 1][j] = 1 - Layout[i + 1][j];
}
if (j - 1 >= 0)
{
Layout[i][j - 1] = 1 - Layout[i][j - 1];
}
if (j + 1 < BoardW)
{
Layout[i][j + 1] = 1 - Layout[i][j + 1];
}
}
int isGameOver(int layout[BoardH][BoardW])
{
int sum = 0;
for (int i = 0; i < BoardH; i++)
{
for (int j = 0; j<BoardW; j++)
{
sum += layout[i][j];
}
}
if (0 == sum)
{
return 1;
}
else
{
return 0;
}
}
int initLayout(int layout[BoardH][BoardW])
{
int k = 0;
srand((int)time(0));
for (int i = 0; i<BoardH; i++)
{
for (int j = 0; j<BoardW; j++)
{
k = rand() % 5;
if (0 == k || 1 == k)
{
layout[i][j] = k;
}
}
}
return layout[BoardH][BoardW];
}
int main()
{
int layout[BoardH][BoardW] = { 0 };//layout 布局 (棋盘的布局)
HWND hwnd;
hwnd = initgraph(BoardW*GridSz + 2 * Edge, BoardH*GridSz + 2 * Edge);
while (1)
{
initLayout(layout);
drawGrid();//画棋盘
drawLayout(layout);//画棋盘布局
MOUSEMSG m;
while (!isGameOver(layout))
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
UpdateLayout(layout, m.x, m.y);
break;
}
drawGrid();//画棋盘
drawLayout(layout);//画棋盘布局
}
MessageBox(hwnd, _T("恭喜过关!"), _T("提示"), MB_OK);
}
_getch();
closegraph();
return 0;
}
#endif
//贴图
#if 0
int main()
{
initgraph(640, 480);
IMAGE imgMask, imgSrc;
loadimage(&imgMask;, _T("./mask.jpg"));
loadimage(&imgSrc;, _T("./src.jpg"));
putimage(100, 200, &imgMask;, SRCAND);
putimage(100, 200, &imgSrc;, SRCPAINT);
_getch();
closegraph();
return 0;
}
#endif
//时间及文本显示
#if 0
int main()
{
initgraph(640, 480);
time_t start, end;
time(&start;);
_getch();
time(&end;);
TCHAR s[128];
_stprintf(s, _T("相隔时间:%d秒"), (end - start));
settextcolor(RGB(255, 255, 0));
settextstyle(100, 0, _T("隶书"));
outtextxy(0, 0, s);
_getch();
closegraph();
return 0;
}
#endif
//声音播放
#if 0
#include"mmsystem.h"
#pragma comment(lib,"winmm.lib")
int main()
{
mciSendString(_T("open BGM.mp3 alias bgm"), NULL, 0, NULL);
mciSendString(_T("open 123.mp3 123 bgm"), NULL, 0, NULL);
while (1)
{
char key = _getch();
switch (key)
{
case 'p':
mciSendString(_T("seek bgm to 0"), NULL, 0, NULL);
mciSendString(_T("play bgm"), NULL, 0, NULL);
break;
//case 's':
// mciSendString(_T("stop bgm"), NULL, 0, NULL);
// break;
case 't':
mciSendString(_T("pause bgm"), NULL, 0, NULL);
break;
case 'r':
mciSendString(_T("resume bgm"), NULL, 0, NULL);
break;
case 'c':
goto lp;
break;
case 'o':
mciSendString(_T("play 123"), NULL, 0, NULL);
break;
}
}
lp:
mciSendString(_T("close bgm"), NULL, 0, NULL);
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));//1970.1.1 00:00:00
for (int i = 0; i < 10; ++i)
{
printf("%d,", rand());
}
return 0;
}
/*
伪随机
*/
#endif