鱼C论坛

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

为一个游戏添加边框

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

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

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

x
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
        int i,j;
        int x=0;
        int y=5;
        int velocity_y=1;
        int velocity_x=1;          
        int left=0;
        int right =20;
        int top=0;
        int bottom=10;

        while(1)
        {
                x=x+velocity_x;
                y=y+velocity_y;
                system("cls");     //清屏函数

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

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

                printf("o");           //输出小球o
                printf("\n");
                Sleep(50);               //在输出小球后等待50毫秒
                                   
                if((x==top)||(x==bottom))        //碰到上下边界要变化速度
                {
                        velocity_x = -velocity_x;
                        printf("\a");
                }
                if((y==left)||(y==right))
                {
                        velocity_y = -velocity_y;
                        printf("\a");
                }

        }
        return 0;
}


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

使用道具 举报

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

使用道具 举报

发表于 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 01:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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