打砖块代码

Posted 皓月肥波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打砖块代码相关的知识,希望对你有一定的参考价值。

#if 0
作业:
添加  打砖块的检测 消除砖块
提示1:给zk数组赋值可以消除砖块
提示2:循环检测
提示3:打掉了的,就别再检测了
提示4:注意看球碰撞板子的检测方式
#endif

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#define WIDTH 300
#define HEIGHT 400
#define BRICK_WIDTH  50
#define BRICK_HEIGHT 15
#define ROWS 4
#define COLS 6
#define BOARD_WIDTH  100
#define BOARD_HEIGHT 15
#define RADIUS 10 
int board_x, board_y;
int ball_x, ball_y;
int ball_xfx = 3, ball_yfx = -3;

bool brick[ROWS][COLS] = { 0 };       //把0认为真,初始砖块都存在
bool isContinue = true;

void DrawBrick(int x, int y);
void DrawAllBrick();
void DrawBoard();
void DrawBall();
void DrawGame();
void BallMove();
void check();
void ctolGame();

void RemoveBrick()
{
	for (int i = ROWS; i >0; i--)
	{
		for (int j =0; j <COLS ; j++)
			if ((ball_yfx <0) &&
				(ball_y <= (ROWS *BRICK_HEIGHT- RADIUS)) &&//球y坐标
				(ball_x >= COLS * BRICK_WIDTH) &&
				(ball_x <= (COLS * BRICK_WIDTH + BOARD_WIDTH))) {
				brick[i - 1][j] = false;
			}
	}
}

int main()
{
	initgraph(WIDTH,HEIGHT, SHOWCONSOLE);
	board_x = (WIDTH - BOARD_WIDTH) / 2;
	board_y =HEIGHT - BOARD_HEIGHT;

	ball_x = WIDTH / 2;
	ball_y = HEIGHT - BOARD_HEIGHT - RADIUS;
	while (isContinue) {
		DrawGame();
		BallMove();
		ctolGame();
		Sleep(10); 
	}

	settextcolor(RED);
	LOGFONT font = { 0 };
	font.lfHeight = 40;
	font.lfWidth = 20;
	settextstyle(&font);
	outtextxy(WIDTH / 2 - 100,HEIGHT / 2,"Game Over!!!");
	
	/*Sleep(2000);
	cleardevice();
	brick[1][1] = brick[2][2] = 1;
	DrawAllBrick();*/
	while (1);
}
void DrawBrick(int x, int y)
{
	if (1 == brick[y][x])	return;
	setlinecolor(RED);                   //无,默认白色
	setfillcolor(GREEN);
	fillrectangle(x * BRICK_WIDTH, y * BRICK_HEIGHT, x * BRICK_WIDTH + BRICK_WIDTH, y * BRICK_HEIGHT + BRICK_HEIGHT);
}
void DrawAllBrick()
{
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			DrawBrick(j, i);

		}
	}
}
void DrawBoard()
{
	setlinecolor(YELLOW);
	setfillcolor(YELLOW);
	fillrectangle(board_x, board_y, board_x + BOARD_WIDTH, HEIGHT);
}
void DrawBall()
{
	setlinecolor(RED);
	setfillcolor(RED);
	fillcircle(ball_x, ball_y, RADIUS);
}

void BallMove()
{
	ball_x += ball_xfx;
	ball_y += ball_yfx;
}
void DrawGame()
{
	BeginBatchDraw();

	cleardevice();
	DrawAllBrick();
	DrawBoard();
	DrawBall();
	EndBatchDraw();

}
//碰撞检测
void check() {
	//检测球是否撞上右边沿
	if ((ball_xfx > 0) && //球往右边走
		(ball_x >= (WIDTH - RADIUS))) {//撞上了右边沿
		ball_xfx = -3;
	}
	//检测球是否撞上左边沿
	if ((ball_xfx < 0) && //球往左边走
		(ball_x <= RADIUS)) {//撞上了左边沿
		ball_xfx = 3;
	}
	//检测球是否撞上上边沿
	if ((ball_yfx < 0) && //球往上边走
		(ball_y <= RADIUS)) {//撞上了上边沿
		ball_yfx = 3;
	}
	//检测球是否撞上下边沿
	if ((ball_yfx > 0) && //球往下边走
		(ball_y >= (HEIGHT - RADIUS))) {//撞上了下边沿
		//ball_yfx = -3;
		isContinue = false;
	}

	//检测球是否撞上板子
	if ((ball_yfx > 0) &&
		(ball_y >= (board_y - RADIUS)) &&//球y坐标
		(ball_x >= board_x) &&
		(ball_x <= (board_x + BOARD_WIDTH))) {
		ball_yfx = -3;
	}

	//检测撞砖块

	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			//  zk[i][j]      x:j*BRICK_WIDTH    y:i*BRICK_HEIGHT

			//左上角坐标 (j*BRICK_WIDTH,i*BRICK_HEIGHT)   
			//右下角坐标(j*BRICK_WIDTH+BRICK_WIDTH,i*BRICK_HEIGHT+BRICK_HEIGHT)

			if (3 == ball_xfx) {//检测从左往右撞

			/*	if ((false == brick[i][j])
					&& (ball_x > (j * BRICK_WIDTH - RADIUS))    
					&& (ball_y > i * BRICK_HEIGHT)
					&& (ball_y < (i * BRICK_HEIGHT + BRICK_HEIGHT))
					) {
					brick[i][j] = true;
					ball_xfx = -3;
				}
			*/
			}
			if (-3 == ball_xfx) {//检测从右往左撞
				
			/*	if ((false == brick[i][j])
				&& (ball_x <(j* BRICK_WIDTH + BRICK_WIDTH + RADIUS))  
				&& (ball_y>i* BRICK_HEIGHT)
				&& (ball_y < (i* BRICK_HEIGHT + BRICK_HEIGHT))
				){
					brick[i][j] = true;
					ball_xfx = 3;
				}
			*/
			}
			if (-3 == ball_yfx) {//检测从下往上撞
				if ((false == brick[i][j])			
					&& (ball_x > j * BRICK_WIDTH)    
					&& (ball_x < (j * BRICK_WIDTH + BRICK_WIDTH))
					&& (ball_y < (i * BRICK_HEIGHT + BRICK_HEIGHT + RADIUS))
					) {
						brick[i][j] = true;
						ball_yfx = 3;
				}
			}
			if (3 == ball_yfx) {//检测从上往下撞
				
			/*	if ((false == brick[i][j])
				&& (ball_x > j * BRICK_WIDTH)    
				&& (ball_x < (j * BRICK_WIDTH + BRICK_WIDTH))
				&& (ball_y > (i * BRICK_HEIGHT - RADIUS))
				){
					brick[i][j] = true;
					ball_yfx = -3;
				}
			*/
			}
		}
	}
}

//控制    是否按键  根据按键来处理
void ctolGame() {
	check();//不管有没有按键,都要碰撞检测

	int ch;
	if (true == _kbhit()) {//检测是否有按键
		ch = _getch();//获取按键的值
		printf("ch:%d\\n", ch);

		switch (ch) {
		case 'a':
		case 'A':
		case 75:
			board_x -= 30;
			break;
		case 'd':
		case 'D':
		case 77:
			board_x += 30;
			break;
		}
	}
}


以上是关于打砖块代码的主要内容,如果未能解决你的问题,请参考以下文章

一百多行js代码实现打砖块小游戏

曾经苹果的创始人沃兹和乔布斯花了4天时间才开发出来的打砖块小游戏?如今用180行代码轻松搞定!

曾经苹果的创始人沃兹和乔布斯花了4天时间才开发出来的打砖块小游戏?如今用180行代码轻松搞定!

曾经苹果的创始人沃兹和乔布斯花了4天时间才开发出来的打砖块小游戏?如今用180行代码轻松搞定!

C语言项目实战:《打砖块》零基础项目丨225 行源码注释示例

原生javascript面向对象开发儿时经典打砖块小游戏