C语言打砖块小游戏
发布时间
阅读量:
阅读量

#define _CRT_SECURE_NO_WARNINGS 1
//头文件
#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
//宏定义
#define WIDHT 100
#define RIGHT 25
#define INITGRAW 800
#define INITGRAH 800
//0消除砖块
int map[5][8] = { 0 };
IMAGE mm;
//初始化地图
void initmap()
{
srand((unsigned int)time(NULL));
loadimage(&mm, L"./写真.jpg", INITGRAW, INITGRAH);
initgraph(INITGRAW, INITGRAH);
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 8; k++)
{
map[i][k] = rand() % 3 + 1;
}
}
}
//画地图
void drawmap()
{
setlinecolor(BLACK);
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 8; k++)
{
int x = WIDHT * k;
int y = RIGHT * i;
switch (map[i][k])
{
case 0:
break;
case 1://绿色
setfillcolor(GREEN);
fillrectangle(x, y, x + 100, y + 25);
break;
case 2://蓝色
setfillcolor(BLUE);
fillrectangle(x, y, x + 100, y + 25);
break;
case 3://黄色
setfillcolor(YELLOW);
fillrectangle(x, y, x + 100, y + 25);
break;
}
}
}
}
//木板部分
struct Board
{
//木板的左上角坐标
int x;
int y;
//木板的宽度和高度
int w;
int h;
//木板的移动速度和颜色
COLOR16 COLOR;
int speed;
}board;
//初始化木板
void initBoard()
{
board = { (WIDHT * 8) / 2 - 75,(WIDHT * 8) - 25,150,25,GREEN,5 };
}
//画木板
void drawBoard()
{
setfillcolor(board.COLOR);
fillrectangle(board.x, board.y, board.x + board.w, board.y + board.h);
}
//木板的按键操作
void keyDown()
{
if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && board.x >= 0)
{
board.x -= board.speed;
}
if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && board.x + board.w <= WIDHT * 8)
{
board.x += board.speed;
}
}
//球的部分
struct Ball
{
//球的坐标
int x;
int y;
//球的半径
int r;
//木板的移动速度和颜色
int dx;
int dy;
}ball;
//初始化球
void initBall()
{
ball.x = (WIDHT * 8) / 2;
ball.y = (WIDHT * 8) - 40;
ball.r = rand() % 15 + 1;
ball.dx = rand() % 5 + 1;
ball.dy = -5;
}
//画球
void drawBall()
{
setfillcolor(YELLOW);
solidcircle(ball.x, ball.y, ball.r);
}
//定时器
int Timer(int duration, int id)
{
static int starTime[10];
int endTime = clock();//获取程序运行当前的时间
if (endTime - starTime[id] > duration)
{
starTime[id] = endTime;
return 1;
}
return 0;
}
//碰木板
int hitBoard()
{
if (ball.y + ball.r >= board.y)
{
if (ball.x >= board.x && ball.x <= board.x + board.w)
{
return 1;
}
}
return 0;
}
//碰砖块
int hitBircks()
{
int j = ball.x / WIDHT;
int i = (ball.y - ball.r) / RIGHT;
if (i < 5 && j < 8 && map[i][j] != 0)
{
map[i][j] = 0;
return 1;
}
return 0;
}
//小球的运动
void ballRun()
{
ball.x += ball.dx;
ball.y += ball.dy;
if (hitBoard() || hitBircks() || ball.y - ball.r < 0)
ball.dy = -ball.dy;
if (ball.x + ball.r > getwidth() || ball.x - ball.r < 0)
ball.dx = -ball.dx;
}
//判断游戏输赢
void Game_Judge()
{
if (ball.y > 800 - 25)
{
int select = MessageBox(GetHWnd(), L"输了呢?", L"low B!", MB_OKCANCEL);
if (select == IDOK)//再来一把
{
//重新初始化
initmap();
initBall();
initBoard();
}
else
exit(0);
}
}
int main()
{
initmap();
initBall();
initBoard();
BeginBatchDraw();
while (1)
{
cleardevice();
putimage(0, 0, &mm);
drawmap();
drawBoard();
drawBall();
if (Timer(10, 0))
ballRun();
Game_Judge();
keyDown();
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
全部评论 (0)
还没有任何评论哟~
