新手守护
这个我多
打地鼠:
#include <windows.h>
#include<iostream>
using namespace std;
class CMyWnd :public CFrameWnd
{
private:
CDC *m_pmdc;
CBitmap *m_pbitmap[5];
CRect myRect[6];
CString picPath[5];
int hit;
BOOL m_state[6];
int counter;
int num;
int hammer_x;
int hammer_y;
public:
CMyWnd()
{
Create(NULL,"Third App");
CClientDC dc(this);
picPath[0]="../image/background.bmp";
picPath[1]="../image/mouse1.bmp";
picPath[2]="../image/mouse2.bmp";
picPath[3]="../image/hammer1.bmp";
picPath[4]="../image/hammer2.bmp";
//
myRect[0].SetRect(30,10,130,110);
myRect[1].SetRect(190,10,290,110);
myRect[2].SetRect(340,10,440,110);
myRect[3].SetRect(30,140,130,240);
myRect[4].SetRect(190,140,290,240);
myRect[5].SetRect(340,140,440,240);
//
hit=0;
for(int i=0;i<6;i++)
m_state[i]=FALSE;
counter=0;
hammer_x=hammer_y=0;
num=0;
//不显示鼠标
//ShowCursor(FALSE);
m_pmdc=new CDC;
for(int i=0;i<5;i++)
{
m_pbitmap[i]=new CBitmap;
m_pbitmap[i]->m_hObject=(HBITMAP)::LoadImage(NULL,picPath[i],
IMAGE_BITMAP, 0,0,LR_LOADFROMFILE);
}
m_pmdc->CreateCompatibleDC(&dc);
MoveWindow(200,20,480,320);
this->SetTimer(1,1000,NULL);
}
void myPait(int flag);
~CMyWnd()
{
for(int i=0;i<5;i++)
delete m_pbitmap[i];
delete m_pmdc;
}
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
// afx_msg void OnPaint();
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
// afx_msg void OnPaint();
};
class CMyApp:public CWinApp
{
public:
BOOL InitInstance();
};
BOOL CMyApp::InitInstance()
{
CMyWnd *pf=new CMyWnd;
pf->ShowWindow(m_nCmdShow);
this->m_pMainWnd=pf;
return TRUE;
}
CMyApp FirstApp;BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_LBUTTONUP()
// ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
// ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
counter++;
m_state[num]=FALSE;
num=rand()%6;
m_state[num]=TRUE;
for(int i=0;i<6;i++)
{
if(myRect[i].PtInRect(point)&&m_state[i])
{
hit++;
}
else
hit=0;
}
CFrameWnd::OnLButtonUp(nFlags, point);
}
void CMyWnd::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this); // device context for painting
m_pmdc->SelectObject(m_pbitmap[0]);
dc.BitBlt(0,0,480,320,m_pmdc,0,0,SRCCOPY);
m_pmdc->SelectObject(m_pbitmap[3]);
dc.BitBlt(hammer_x,hammer_y,148,148,m_pmdc,0,0,SRCAND);
m_pmdc->SelectObject(m_pbitmap[4]);
dc.BitBlt(hammer_x,hammer_y,148,148,m_pmdc,0,0,SRCPAINT);
for(int i=0;i<6;i++)
{
if(m_state[i])
{
m_pmdc->SelectObject(m_pbitmap[1]);
dc.BitBlt(myRect[i].left,myRect[i].top,100,100,m_pmdc,0,0,SRCAND);
m_pmdc->SelectObject(m_pbitmap[2]);
dc.BitBlt(myRect[i].left,myRect[i].top,100,100,m_pmdc,0,0,SRCPAINT);
}
}
if(hit>=3)
{
KillTimer(1);
MessageBox("你赢了!");
}
if(counter>=10)
{
KillTimer(1);
MessageBox("你输了!");
}
CFrameWnd::OnTimer(nIDEvent);
}
void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
hammer_x=point.x;
hammer_y=point.y;
CFrameWnd::OnMouseMove(nFlags, point);
}
猜数游戏
#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
int main()
{
int count=0;
cout<<"请输入1~99999的数:\n";
srand(time(NULL));
int r=rand()%99999,s;
while(cin>>s&&s!=r)
{
if(s>r)
{
cout<<"太大了!!!"<<endl;
}
else if(s<r)
{
cout<<"太小了!!!"<<endl;
}
count++;
if(count==20)
{
cout<<"很遗憾,你没有猜中!!!正确答案是"<<r;
return 0;
}
}
cout<<"太棒了!!!你猜中了!!!";
return 0;
}
俄罗斯方块
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
using namespace std;
int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } };
void initialWindow(HANDLE hOut);
void initialPrint(HANDLE hOut);
void gotoXY(HANDLE hOut, int x, int y);
void roundBlock(HANDLE hOut, int block[4][4]);
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);
void printBlock(HANDLE hOut, int block[4][4], int x, int y);
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);
void myStop(HANDLE hOut, int block[4][4]);
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);
int main()
{
int map[21][12];
int blockA[4][4];
int blockB[4][4];
int positionX, positionY;
bool check;
char key;
int val;
int fraction;
int checkpoint;
int times;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
initialWindow(hOut);
initial:
gotoXY(hOut, 0, 0);
initialPrint(hOut);
check = true;
val = 50;
fraction = 0;
checkpoint = 1;
times = val;
for (int i = 0; i < 20; ++i)
{
for (int j = 1; j < 11; ++j)
{
map[i][j] = 0;
}
}
for (int i = 0; i < 20; ++i)
{
map[i][0] = map[i][11] = 1;
}
for (int i = 0; i < 12; ++i)
{
map[20][i] = 1;
}
srand((unsigned)time(NULL));
roundBlock(hOut, blockA);
while (true)
{
if (check)
{
eliminateRow(hOut, map, val, fraction, checkpoint);
check = false;
positionX = -3;
positionY = 4;
if (collisionDetection(blockA, map, positionX, positionY))
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
blockB[i][j] = blockA[i][j];
}
}
roundBlock(hOut, blockA);
}
else
{
gameOver(hOut, blockA, map);
goto initial;
}
}
printBlock(hOut, blockB, positionX, positionY);
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72:
myUp(hOut, blockB, map, positionX, positionY);
break;
case 75:
myLeft(hOut, blockB, map, positionX, positionY);
break;
case 77:
myRight(hOut, blockB, map, positionX, positionY);
break;
case 80:
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
break;
case 32:
myStop(hOut, blockA);
break;
case 27:
exit(0);
default:
break;
}
}
Sleep(20);
if (0 == --times)
{
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
times = val;
}
}
cin.get();
return 0;
}
void initialWindow(HANDLE hOut)
{
SetConsoleTitle("俄罗斯方块");
COORD size = { 80, 25 };
SetConsoleScreenBufferSize(hOut, size);
SMALL_RECT rc = { 0, 0, 79, 24 };
SetConsoleWindowInfo(hOut, true, &rc);
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(hOut, &cursor_info);
}
void initialPrint(HANDLE hOut)
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
for (int i = 0; i < 20; ++i)
{
cout << "■ ■☆ ☆" << endl;
}
gotoXY(hOut, 26, 0);
cout << "☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 0, 20);
cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 26, 1);
cout<< " 俄罗斯方块 Ver1.0.1";
gotoXY(hOut, 26, 2);
cout << "分 数: ";
gotoXY(hOut, 26, 3);
cout << "关 卡: ";
gotoXY(hOut, 26, 5);
cout << "下一方块:";
gotoXY(hOut, 26, 9);
cout << "操作方法:";
gotoXY(hOut, 30, 11);
cout << "↑:旋转 ↓:速降";
gotoXY(hOut, 30, 12);
cout << "→:右移 ←:左移";
gotoXY(hOut, 30, 13);
cout << "空格键:开始/暂停";
gotoXY(hOut, 30, 14);
cout << "Esc 键:退出";
gotoXY(hOut, 26, 16);
cout << "关 于:";
gotoXY(hOut, 30, 18);
cout << "俄罗斯方块1.0";
gotoXY(hOut, 30, 19);
cout << "作者:怪盗基德1102";
}
void gotoXY(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void roundBlock(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
switch (rand() % 19)
{
case 0:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
break;
case 1:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
break;
case 2:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
break;
case 3:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
break;
case 4:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
break;
case 5:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
break;
case 6:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
break;
case 7:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
break;
case 8:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
break;
case 9:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
break;
case 10:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
break;
case 11:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
break;
case 12:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
break;
case 13:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
break;
case 14:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
break;
case 15:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
break;
case 16:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
break;
case 17:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
break;
case 18:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block18[i][j];
}
}
break;
default:
break;
}
printBlock(hOut, block, 5, 15);
}
bool collisionDetection(int block[4][4], int map[21][12], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1)
{
return false;
}
}
}
return true;
}
void printBlock(HANDLE hOut, int block[4][4], int x, int y)
{
switch (block[0][0])
{
case 10:
case 11:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
break;
case 12:
case 13:
case 14:
case 15:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 16:
case 17:
case 18:
case 19:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 20:
case 21:
case 22:
case 23:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 24:
case 25:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 26:
case 27:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 28:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
default:
break;
}
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
}
}
void clearBlock(HANDLE hOut, int block[4][4], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << " ";
}
}
}
}
}
void gameOver(HANDLE hOut, int block[4][4], int map[21][12])
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 7, 8);
cout << "GAME OVER!";
gotoXY(hOut, 6, 9);
cout << "空格键:重来";
gotoXY(hOut, 6, 10);
cout << "ESC键:退出";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
return;
}
if (key == 27)
{
exit(0);
}
}
}
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y)
{
if (collisionDetection(block, map, x + 1, y))
{
clearBlock(hOut, block, x, y);
++x;
return 0;
}
if (x < 0)
{
return 2;
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
map[x + i][y + j] = 1;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
return 1;
}
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
--y;
}
}
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
++y;
}
}
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
switch (block[0][0])
{
case 10:
if (collisionDetection(block01, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
}
break;
case 11:
if (collisionDetection(block00, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
}
else if (collisionDetection(block00, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
--y;
}
else if (collisionDetection(block00, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
++y;
}
else if (collisionDetection(block00, map, x, y - 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y - 2;
}
else if (collisionDetection(block00, map, x, y + 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y + 2;
}
break;
case 12:
if (collisionDetection(block03, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
}
else if (collisionDetection(block03, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
--y;
}
else if (collisionDetection(block03, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
++y;
}
break;
case 13:
if (collisionDetection(block04, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
}
else if (collisionDetection(block04, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
--y;
}
else if (collisionDetection(block04, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
++y;
}
break;
case 14:
if (collisionDetection(block05, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
}
else if (collisionDetection(block05, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
--y;
}
else if (collisionDetection(block05, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
++y;
}
break;
case 15:
if (collisionDetection(block02, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
}
else if (collisionDetection(block02, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
--y;
}
else if (collisionDetection(block02, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
++y;
}
break;
case 16:
if (collisionDetection(block07, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
}
else if (collisionDetection(block07, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
--y;
}
else if (collisionDetection(block07, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
++y;
}
break;
case 17:
if (collisionDetection(block08, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
}
else if (collisionDetection(block08, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
--y;
}
else if (collisionDetection(block08, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
++y;
}
break;
case 18:
if (collisionDetection(block09, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
}
else if (collisionDetection(block09, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
--y;
}
else if (collisionDetection(block09, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
++y;
}
break;
case 19:
if (collisionDetection(block06, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
}
else if (collisionDetection(block06, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
--y;
}
else if (collisionDetection(block06, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
++y;
}
break;
case 20:
if (collisionDetection(block11, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
}
else if (collisionDetection(block11, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
--y;
}
else if (collisionDetection(block11, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
++y;
}
break;
case 21:
if (collisionDetection(block12, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
}
else if (collisionDetection(block12, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
--y;
}
else if (collisionDetection(block12, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
++y;
}
break;
case 22:
if (collisionDetection(block13, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
}
else if (collisionDetection(block13, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
--y;
}
else if (collisionDetection(block13, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
++y;
}
break;
case 23:
if (collisionDetection(block10, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
}
else if (collisionDetection(block10, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
--y;
}
else if (collisionDetection(block10, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
++y;
}
break;
case 24:
if (collisionDetection(block15, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
}
else if (collisionDetection(block15, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
--y;
}
else if (collisionDetection(block15, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
++y;
}
break;
case 25:
if (collisionDetection(block14, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
}
else if (collisionDetection(block14, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
--y;
}
else if (collisionDetection(block14, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
++y;
}
break;
case 26:
if (collisionDetection(block17, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
}
else if (collisionDetection(block17, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
--y;
}
else if (collisionDetection(block17, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
++y;
}
break;
case 27:
if (collisionDetection(block16, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
}
else if (collisionDetection(block16, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
--y;
}
else if (collisionDetection(block16, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
++y;
}
break;
default:
break;
}
}
void myStop(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 31, 7);
cout << "暂停!";
char key;
int num=0;
while (true)
{
key = _getch();
if (key == 32)
{
num++;
gotoXY(hOut, 30, 7);
cout << " ";
printBlock(hOut, block, 5, 15);
return;
}
if (key == 27)
{
exit(0);
}
}
}
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint)
{
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 19; i >= 0; --i)
{
int x = 0;
for (int j = 1; j < 11; ++j)
{
x += map[i][j];
}
if (x == 10)
{
fraction += 100;
if (val > 1 && fraction / 1000 + 1 != checkpoint)
{
checkpoint = fraction / 1000 + 1;
val -= 5;
}
for (int m = i; m > 0; --m)
{
for (int n = 1; n < 11; ++n)
{
map[m][n] = map[m - 1][n];
gotoXY(hOut, 2 * n, m);
if (map[m][n] == 1)
{
cout << "■";
}
else
{
cout << " ";
}
}
}
++i;
}
}
gotoXY(hOut, 36, 2);
cout << fraction;
gotoXY(hOut, 36, 3);
cout << checkpoint;
}
贪吃蛇
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<conio.h>
using namespace std;
typedef struct{ int x, y; }Point;
char map[22][22]; //定义一个22*
Point snake[400], food, Next; //定义蛇、食物、下一步蛇头的位置
int head, tail; //用于储存蛇头和蛇尾的下标
int grade, length, autotime; //游戏等级、蛇长、自动前进所需时间
char direction; //前进方向
//用inline定义内联函数节省程序运行时的调用开销
//刷新地图
inline void Update(char map[][22], int grade, int length, int autotime)
{
system("cls"); //清屏
int i, j;
printf("\n");
for (i = 0; i < 22; i++)
{
printf("\t");
for (j = 0; j < 22; j++)
printf("%c ", map[i][j]);
if (i == 0)
printf("\t等级为:%d", grade);
if (i == 2)
printf("\t长度为:%d", length);
if (i == 6)
printf("\t自动前进时间");
if (i == 8)
printf("\t间隔为:%d ms", autotime);
printf("\n");
}
}
//欢迎界面
inline void hello()
{
puts("\n\n\n\t\t\t贪吃蛇游戏即将开始!"); //准备开始
double start;
for (int i = 3; i >= 0; i--)
{
start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间
while ((double)clock() / CLOCKS_PER_SEC - start <= 1); //经过1秒之后
if (i > 0)
{
system("cls"); //清屏
printf("\n\n\n\t\t\t进入倒计时:%d\n", i); //倒计时
}
else
Update(map, grade, length, autotime); //刷新地图 }
}
//随机生成食物位置
inline void f()
{
srand(int(time(0))); //调用种子函数
do{
food.x = rand() % 20 + 1;
food.y = rand() % 20 + 1;
} while (map[food.x][food.y] != ' ');
map[food.x][food.y] = '!'; //食物为“!”
}
//初始化
inline void init()
{
int i, j;
for (i = 1; i <= 20; i++)
for (j = 1; j <= 20; j++)
map[i][j] = ' ';
for (i = 0; i <= 21; i++)
map[0][i] = map[21][i] = map[i][0] = map[i][21] = '*'; //边界
map[1][1] = map[1][2] = 'O'; //蛇身(含蛇尾)
map[1][3] = '@'; //蛇头
head = 2; tail = 0; //开始时头和尾的下标
snake[head].x = 1; snake[head].y = 3; //开始时蛇头在地图上的下标
snake[tail].x = 1; snake[tail].y = 1; //开始时蛇尾在地图上的下标
snake[1].x = 1; snake[1].y = 2; //开始时蛇身在地图上的下标
f(); //随机生成食物位置
grade = 1; length = 3; autotime = 500; //开始的等级、长度、自动前进时间
direction = 77; //初始的运动方向向右
}
//预前进
inline int GO()
{
bool timeover = true;
double start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间
//自动经过1秒或者等待1秒内的键盘输入
while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= autotime / 1000.0)) && !_kbhit());
//键盘输入
if (timeover)
{
_getch();
direction = _getch(); //获取方向
}
switch (direction)
{
case 72: Next.x = snake[head].x - 1; Next.y = snake[head].y; //向上
break;
case 80:
Next.x = snake[head].x + 1; Next.y = snake[head].y; //向下
break;
case 75:
Next.x = snake[head].x; Next.y = snake[head].y - 1; //向左
break;
case 77:
Next.x = snake[head].x; Next.y = snake[head].y + 1; //向右
break;
default:
puts("\tGame over!"); //按下非方向键游戏失败
return 0;
}
if (Next.x == 0 || Next.x == 21 || Next.y == 0 || Next.y == 21) //撞到边界
{
puts("\tGame over!");
return 0;
}
if (map[Next.x][Next.y] != ' '&&!(Next.x == food.x&&Next.y == food.y)) //吃到自己
{
puts("\tGame over!");
return 0;
}
if (length == 400) //最长蛇长
{
puts("\tGood game!");
return 0;
}
return 1;
}
//吃到食物
inline void EAT() {
length++; //长度增加1
int _grade = length / 10 + 1; //计算等级5 if (_grade != grade) {
grade = _grade;
if (autotime >= 100)
autotime = 550 - grade * 50; //增加一级自动时间减短50毫秒
}
map[Next.x][Next.y] = '@'; //蛇头位置变化
map[snake[head].x][snake[head].y] = 'O'; //原蛇头位置变化为蛇身
head = (head + 1) % 400; //蛇头下标加1
snake[head].x = Next.x; snake[head].y = Next.y; //蛇头下标变化
f(); //随机生成食物位置
Update(map, grade, length, autotime); //刷新地图
}
//没吃到食物
inline void FAILURE() {
map[snake[tail].x][snake[tail].y] = ' '; //蛇尾原来的位置变成“ ”
tail = (tail + 1) % 400; //蛇尾下标加1
map[Next.x][Next.y] = '@'; //蛇头位置变化
map[snake[head].x][snake[head].y] = 'O'; //原蛇头位置变化为蛇身
head = (head + 1) % 400; //蛇头下标加1
snake[head].x = Next.x; //蛇头下标变化
snake[head].y = Next.y; Update(map, grade, length, autotime); //刷新地图
}
//main函数
int main()
{
init(); //初始化
hello(); //欢迎界面
while (1)
{
if (GO()) //预前进
{
if (Next.x == food.x&&Next.y == food.y)
EAT(); //吃到食物
else
FAILURE(); //没吃到食物
}
else
return 0; //失败或者胜利,游戏结束
}
return 0;
}