先上图表示敬意
main.cpp源码
为更好的学习知识,下载资源,play.h设置两个积分,谢谢支持
#include <conio.h>
#include <graphics.h>
#include <Windows.h>
#include "Play.h"
int main()
{
InitPlay();
::CreateThread(0, 0, GameMove, 0, 0, 0);
while (GameWin)
{
DrawMap();
BallMove();
if (number == 0)
{
cleardevice();
DrawMap();
MessageBox(GetHWnd(), "你赢了", "提示", MB_OK);
break;
}
Sleep(400);
cleardevice();
}
getch();
return 0;
}
void DrawMap()
{
LPTSTR PrintfNumber = new TCHAR[1024];
LPTSTR PrintfNumber2 = new TCHAR[1024];
wsprintf(PrintfNumber, "砖块数:%d", number);
wsprintf(PrintfNumber2, "余剩数:%d", (50-number));
settextcolor(WHITE);
settextstyle(24, 0, "黑体");
outtextxy(80, 280, PrintfNumber);
outtextxy(80, 320, PrintfNumber2);
for (int i = 0; i < 40;i++)
{
for (int j = 0; j < 20; j++)
{
switch (map[i][j])
{
case 1:
setfillcolor(YELLOW);
setlinecolor(BLACK);
fillrectangle(j * 16, i * 16, j * 16 + 16, i * 16 + 16);
break;
case 2:
setfillcolor(BLUE);
solidcircle(j * 16 + 8, i * 16 + 8, 8);
break;
case 3:
setfillcolor(RED);
setlinecolor(WHITE);
fillrectangle(j * 16, i * 16, j * 16 + 16, i * 16 + 8);
break;
default:
break;
}
}
}
}
void InitPlay()
{
initgraph(20 * 16, 40 * 16);
number = 50;
}
void BallMove()
{
int BallR, BallC;
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 20; j++)
{
if (map[i][j] == 2)
{
BallR = i;
BallC = j;
map[i][j] = 0;
}
}
}
BallR += ball.r;
BallC += ball.c;
map[BallR][BallC] = 2;
if (BallC + ball.c<0 || BallC + ball.c>19)
{
ball.c = -ball.c;
return;
}
if (BallR + ball.r < 0)
{
ball.r = -ball.r;
return;
}
if (map[BallR + ball.r][BallC] == 1)
{
map[BallR + ball.r][BallC] = 0;
number--;
ball.r = -ball.r;
return;
}
if (map[BallR][BallC + ball.c] == 1)
{
map[BallR][BallC + ball.c] = 0;
number--;
ball.c = -ball.c;
return;
}
if (map[BallR + ball.r][BallC] == 3)
{
ball.r = -ball.r;
return;
}
if (BallR == 39)
{
GameOver();
}
}
DWORD WINAPI GameMove(LPVOID lParam)
{
char ch;
while (1)
{
ch = getch();
switch (ch)
{
case 'A':
case 'a':
if (map[39][0] == 3)
{
break;
}
for (int i = 0; i < 20; i++)
{
if (map[39][i] == 3)
{
map[39][i] = 0;
map[39][i - 1] = 3;
}
}
/*setfillcolor(BLACK);
solidrectangle(0, 640 - 16, 320, 640);
Sleep(20);
for (int i = 0; i < 20; i++)
{
if (map[39][i] == 3)
{
setfillcolor(RED);
setlinecolor(WHITE);
fillrectangle(i * 16, 640 - 16, i * 16 - 16, 640 - 8);
}
if (map[39][i] == 2)
{
DrawMap();
}
}*/
cleardevice();
DrawMap();
break;
case 'D':
case 'd':
if (map[39][19] == 3)
{
break;
}
for (int i = 19; i >= 0; i--)
{
if (map[39][i] == 3)
{
map[39][i] = 0;
map[39][i + 1] = 3;
}
}
/*setfillcolor(BLACK);
solidrectangle(0, 640 - 16, 320, 640);
Sleep(20);
for (int i = 0; i < 20; i++)
{
if (map[39][i] == 3)
{
setfillcolor(RED);
setlinecolor(WHITE);
fillrectangle(i * 16, 640 - 16, i * 16 - 16, 640 - 8);
}
if (map[39][i] == 2)
{
DrawMap();
}
}*/
cleardevice();
DrawMap();
break;
default:
break;
}
}
}
void GameOver()
{
cleardevice();
DrawMap();
GameWin = false;
MessageBox(GetHWnd(), "你输了", "提示", MB_OK);
}