鱼C论坛

 找回密码
 立即注册
查看: 2973|回复: 2

为一个游戏添加边框

[复制链接]
发表于 2017-11-19 20:54:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<windows.h>
  4. int main()
  5. {
  6.         int i,j;
  7.         int x=0;
  8.         int y=5;
  9.         int velocity_y=1;
  10.         int velocity_x=1;         
  11.         int left=0;
  12.         int right =20;
  13.         int top=0;
  14.         int bottom=10;

  15.         while(1)
  16.         {
  17.                 x=x+velocity_x;
  18.                 y=y+velocity_y;
  19.                 system("cls");     //清屏函数

  20.                 for(i=0;i<x;i++)   //输出小球上面的空行
  21.                 {
  22.                         printf("\n");
  23.                 }

  24.                 for(j=0;j<y;j++)      //输出小球左边的空格
  25.                 {
  26.                         printf(" ");
  27.                 }

  28.                 printf("o");           //输出小球o
  29.                 printf("\n");
  30.                 Sleep(50);               //在输出小球后等待50毫秒
  31.                                    
  32.                 if((x==top)||(x==bottom))        //碰到上下边界要变化速度
  33.                 {
  34.                         velocity_x = -velocity_x;
  35.                         printf("\a");
  36.                 }
  37.                 if((y==left)||(y==right))
  38.                 {
  39.                         velocity_y = -velocity_y;
  40.                         printf("\a");
  41.                 }

  42.         }
  43.         return 0;
  44. }
复制代码



代码如上,现在我的目的是为这个东东加一个边框,我自己添加的边框就闪了一下就没了,我想要的是静态的边框,求助大家
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-19 21:10:52 | 显示全部楼层
估计是编译器的问题
在return 0;前面加一句
system("pause");
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-20 09:50:18 | 显示全部楼层
你这代码里也没写加边框的程序呀。我给你写了一下,不过会闪烁,可以用句柄,如果没猜错的话你在学那个C语言游戏什么的哪本书吧?我也在学,这个是我以前写的,你可以参考下
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>

  4. int main(void)
  5. {
  6.         int i, j;
  7.         int x = 1, y = 5;
  8.         int top = 1, bottom = 10, left = 1, right = 20;                // 上下左右边界
  9.         int velocity_x = 1, velocity_y  = 1;        // 下落和移动距离量

  10.         while ( 1 )
  11.         {
  12.                 x += velocity_x;
  13.                 y += velocity_y;
  14.                
  15.                 system("cls");                // 清屏

  16.                 /* 打印小球位置以上的边框 */
  17.                 for ( i = top-1; i < x; i++ )
  18.                 {
  19.                         if ( i == top-1 )
  20.                         {
  21.                                 for ( j = left-1; j <= right+1; j++)
  22.                                         printf("*");
  23.                                 printf("\n");
  24.                         }
  25.                         else
  26.                         {
  27.                                 printf("*");
  28.                                 for ( j = left; j <= right; j++)
  29.                                         printf(" ");
  30.                                 printf("*\n");
  31.                         }
  32.                 }
  33.                
  34.                 /* 打印小球所在行小球前的边框 */
  35.                 for ( i = left-1; i < y; i++ )
  36.                 {
  37.                         if ( i == left-1 )
  38.                                 printf("*");
  39.                         else
  40.                                 printf(" ");
  41.                 }
  42.                
  43.                 printf("o");
  44.                
  45.                 /* 打印小球所在行小球后的边框 */
  46.                 for ( i += 1 ; i <= right+1; i++ )
  47.                 {
  48.                         if ( right+1 == i )
  49.                                 printf("*\n");
  50.                         else
  51.                                 printf(" ");
  52.                 }
  53.                
  54.                 /* 打印小球位置以下的边框 */
  55.                 for ( i = x+1; i <= bottom+1; i++ )
  56.                 {
  57.                         if ( i == bottom+1 )
  58.                         {
  59.                                 for ( j = left-1; j <= right+1; j++)
  60.                                         printf("*");
  61.                                 printf("\n");
  62.                         }
  63.                         else
  64.                         {
  65.                                 printf("*");
  66.                                 for ( j = left; j <= right; j++)
  67.                                         printf(" ");
  68.                                 printf("*\n");
  69.                         }
  70.                 }
  71.                
  72.                 Sleep(50);                // 等待50ms
  73.                
  74.                 /* 打到边界反向 */
  75.                 if ( x == bottom || x == top )
  76.                 {
  77.                         printf("\a");                // 碰撞响铃
  78.                         velocity_x = -velocity_x;
  79.                 }
  80.                 if ( y == right || y == left )
  81.                 {
  82.                         printf("\a");
  83.                         velocity_y = -velocity_y;
  84.                 }
  85.         }
  86.        
  87.         return 0;
  88. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 06:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表