Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to realize Mini Game in console

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

今天小编给大家分享一下C语言如何实现控制台打砖块小游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

这个问题是我在领扣上面看到的一道困难问题,原题是这样的:

#include "stdafx.h"#includeint a[10][10] = { { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }, { 0, 0, 1, 1, 1, 1, 0, 1, 1, 0 }, { 0, 0, 0, 0, 1, 1, 0, 1, 1, 0 }, { 0, 1, 1, 1, 1, 1, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 } };//初始化二维数组,写成这个形状便于一目了然void down(int a[10][10])//负责控制砖块下落的函数,使被赋值为3的砖块下落,下落到下界或值为1的方块之上{ int i, j; int m, n; for (i = 9; i >=0; i--) for (j = 0; j

< 10; j++) if (a[i][j] == 3) { m = i; n = j; while (a[m + 1][n] != 1&&m!=9) { a[m + 1][n] = 1; a[m][n] = 0; m++; } }}void freshen(int a[10][10])//刷新函数,用于每次打过砖块之后,检查所有砖块的松动情况,过程大概是这样的,先将全部为1的砖块赋值为3,之后将四周与墙壁相连并且值为3的砖块赋值为·1,然后再进行一次全体砖块的循环遍历,这一次将所有与1相连接(1上下左右连接的砖块并且值为3的)的砖块赋值为1,这样的操作要做四遍,为什么要做这么多遍,这个问题留给读者体会。{ int i, j; for ( i = 0; i < 10; i++) for ( j = 0; j < 10; j++) if (a[i][j]==1) a[i][j] = 3; for (i = 0; i < 10; i++) { j = 0; while (a[i][j] != 0) { a[i][j] = 1; j++; } } for (i = 0; i < 10; i++) { j = 9; while (a[i][j] != 0) { a[i][j] = 1; j--; } } for (j = 0; j < 10; j++) { i = 0; while (a[i][j] != 0) { a[i][j] = 1; i++; } } for (j = 0; j < 10; j++) { i = 9; while (a[i][j] != 0) { a[i][j] = 1; i--; } } for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) if (a[i][j] == 1) { if (a[i - 1][j] == 3) a[i - 1][j] = 1; else if (a[i + 1][j] == 3) a[i + 1][j] = 1; else if (a[i ][j-1] == 3) a[i ][j-1] = 1; else if (a[i ][j+1] == 3) a[i ][j+1] = 1; } for (i = 9; i >

=0; i--) for (j = 9; j >=0; j--) if (a[i][j] == 1) { if (a[i - 1][j] == 3) a[i - 1][j] = 1; else if (a[i + 1][j] == 3) a[i + 1][j] = 1; else if (a[i][j - 1] == 3) a[i][j - 1] = 1; else if (a[i][j + 1] == 3) a[i][j + 1] = 1; } for (i = 9; i >= 0; i--) for (j = 9; j >= 0; j--) if (a[i][j] == 1) { if (a[i - 1][j] == 3) a[i - 1][j] = 1; else if (a[i + 1][j] == 3) a[i + 1][j] = 1; else if (a[i][j - 1] == 3) a[i][j - 1] = 1; else if (a[i][j + 1] == 3) a[i][j + 1] = 1; } for (i = 9; i >= 0; i--) for (j = 9; j >= 0; j--) if (a[i][j] == 1) { if (a[i - 1][j] == 3) a[i - 1][j] = 1; else if (a[i + 1][j] == 3) a[i + 1][j] = 1; else if (a[i][j - 1] == 3) a[i][j - 1] = 1; else if (a[i][j + 1] == 3) a[i][j + 1] = 1; void view(int a[10][10])//Print brick function { for (int i = -1; i < 10; i++) { printf("0%d ", i); } printf("\n"); for (int i = 0; i < 10; i++) { printf("%d: ", i); for (int j = 0; j < 10; j++) { if (a[i][j] == 1) printf("* "); else printf(" "); } printf("\n"); void beat(int a[10][10],int i,int j)//Arkanoid function { a[i][j] = 0;}void main(){ int p,q; view(a); for (int w = 0; w < 18; w++) { printf("beat whichp?\ n"); scanf("%d", &p); printf("beat whichq?\ n"); scanf("%d", &q); beat(a, p, q); freshen(a); down(a); view(a); } getchar(); return;}

I use the compiler is VS2013, C language to write console procedures, freshman C language students can take a look at this programming idea.

The final effect is this:

The above is "C language how to achieve console Arkanoid Mini games" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report