|
发表于 2017-11-20 09:50:18
|
显示全部楼层
你这代码里也没写加边框的程序呀。我给你写了一下,不过会闪烁,可以用句柄,如果没猜错的话你在学那个C语言游戏什么的哪本书吧?我也在学,这个是我以前写的,你可以参考下
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- int main(void)
- {
- int i, j;
- int x = 1, y = 5;
- int top = 1, bottom = 10, left = 1, right = 20; // 上下左右边界
- int velocity_x = 1, velocity_y = 1; // 下落和移动距离量
- while ( 1 )
- {
- x += velocity_x;
- y += velocity_y;
-
- system("cls"); // 清屏
- /* 打印小球位置以上的边框 */
- for ( i = top-1; i < x; i++ )
- {
- if ( i == top-1 )
- {
- for ( j = left-1; j <= right+1; j++)
- printf("*");
- printf("\n");
- }
- else
- {
- printf("*");
- for ( j = left; j <= right; j++)
- printf(" ");
- printf("*\n");
- }
- }
-
- /* 打印小球所在行小球前的边框 */
- for ( i = left-1; i < y; i++ )
- {
- if ( i == left-1 )
- printf("*");
- else
- printf(" ");
- }
-
- printf("o");
-
- /* 打印小球所在行小球后的边框 */
- for ( i += 1 ; i <= right+1; i++ )
- {
- if ( right+1 == i )
- printf("*\n");
- else
- printf(" ");
- }
-
- /* 打印小球位置以下的边框 */
- for ( i = x+1; i <= bottom+1; i++ )
- {
- if ( i == bottom+1 )
- {
- for ( j = left-1; j <= right+1; j++)
- printf("*");
- printf("\n");
- }
- else
- {
- printf("*");
- for ( j = left; j <= right; j++)
- printf(" ");
- printf("*\n");
- }
- }
-
- Sleep(50); // 等待50ms
-
- /* 打到边界反向 */
- if ( x == bottom || x == top )
- {
- printf("\a"); // 碰撞响铃
- velocity_x = -velocity_x;
- }
- if ( y == right || y == left )
- {
- printf("\a");
- velocity_y = -velocity_y;
- }
- }
-
- return 0;
- }
复制代码 |
|