| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2370 人关注过本帖, 1 人收藏
标题:飞机游戏,编译运行都没问题,但是一直闪屏,看不到“*”,vs2019
只看楼主 加入收藏
梨花压海棠
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2020-2-13
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:8 
飞机游戏,编译运行都没问题,但是一直闪屏,看不到“*”,vs2019
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int position_x, position_y;   //飞机位置
int high, width;              //游戏画面尺寸


void startup()
{
    high = 20;
    width = 30;
    position_x = high / 2;
    position_y = width / 2;
}

void show()//显示画面
{
    system("cls");
    int i, j;
    for (i = 0; i < high; i++)
    {
        for (j = 0; j < width; j++)
        {
            if ((i == position_x) && (i == position_y))
                printf("*");  //输出飞机
            else
                printf(" ");
        }
        printf("\n");
    }

}

void updateWithoutInput()  //与用户输入无关的更新
{
}

void updateWithInput()
{
    char input;
    if (_kbhit())         //判断是否有输入
    {
        input = _getch();   //判断飞机移动
        if (input == 'a')
            position_y--;
        if (input == 'd')
            position_y++;
        if (input == 'w')
            position_x--;
        if (input == 's')
            position_x++;
    }

}

void main()
{
    startup(); //数据结构化
    while (1)  //循环执行
    {
        show();
        updateWithoutInput();
        updateWithInput();
    }
}   
搜索更多相关主题的帖子: int input void 飞机 游戏 
2020-02-15 20:14
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10543
专家分:42958
注 册:2014-5-20
收藏
得分:0 
全是在当前光标处打印,没看到与x,y有关
应该先按x,y定位光标再打印
2020-02-15 22:16
叶纤
Rank: 8Rank: 8
等 级:禁止访问
威 望:1
帖 子:658
专家分:848
注 册:2019-11-22
收藏
得分:0 
程序代码:
天,刷新了我三观
还好我会改错
你逻辑关系用错了

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int position_x=0, position_y=0;   //飞机位置
int high=0, width=0;              //游戏画面尺寸


void startup()
{
    high = 20;
    width = 30;
    position_x =20/2 ;
    position_y = 10/2;
}

void show()//显示画面
{
    system("cls");
    int i, j;
    for (i = 0; i < high; i++)
    {
        for (j = 0; j < width; j++)
        {
            if ((i == position_x) &&(j == position_y))// 输出一个点要i,j
                printf("*");  //输出飞机
            else
                printf(" ");
        }
        printf("\n");
    }

}

void updateWithoutInput()  //与用户输入无关的更新
{
}

void updateWithInput()
{
    char input;
    if (_kbhit())         //判断是否有输入
    {
        input = _getch();   //判断飞机移动
        if (input == 'a')
            position_y--;
        if (input == 'd')
            position_y++;
        if (input == 'w')
            position_x--;
        if (input == 's')
            position_x++;
    }

}

int main()
{
    startup(); //数据结构化
    while (1)  //循环执行
    {
        show();
        updateWithoutInput();
        updateWithInput();
    }
}   


[此贴子已经被作者于2020-2-16 10:43编辑过]


把学习时间浪费在混坛上是傻瓜行为,更何况自己的水平连一两都没到。
2020-02-16 02:42
叶纤
Rank: 8Rank: 8
等 级:禁止访问
威 望:1
帖 子:658
专家分:848
注 册:2019-11-22
收藏
得分:5 
搞不懂你这一句  system("cls");CLS是遇到\n会把\n之前的内容删掉可是我没有发现\n符号啊

把学习时间浪费在混坛上是傻瓜行为,更何况自己的水平连一两都没到。
2020-02-16 10:51
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10543
专家分:42958
注 册:2014-5-20
收藏
得分:10 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

HANDLE hOut;
int x,y,position_x=0, position_y=0;   //飞机位置
int high=0, width=0;              //游戏画面尺寸


void startup()
{
    high = 20;
    width = 30;
    position_x =20/2 ;
    position_y = 10/2;
    x = position_x;
    y = position_y;
}

void print(SHORT x, SHORT y, char c)
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(hOut, pos);
    putchar(c);
}
void show()//显示画面
{
    print(x, y, ' ');
    x = position_x;
    y = position_y;
    print(x, y, '*');
}

void updateWithoutInput()  //与用户输入无关的更新
{
}

int updateWithInput()
{
    if (_kbhit())         //判断是否有输入
    {
        char input = _getch();   //判断飞机移动
        if (input == 'a')
        {
            position_x--;
            return 1;
        }
        else if (input == 'd')
        {
            position_x++;
            return 1;
        }
        else if (input == 'w')
        {
            position_y--;
            return 1;
        }
        else if (input == 's')
        {
            position_y++;
            return 1;
        }
    }
    return 0;
}

int main()
{
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(hOut, &cursor_info);
    startup(); //数据结构化
    show();
    while (1)  //循环执行
    {
        //updateWithoutInput();
        if (updateWithInput())
            show();
    }
    cursor_info.bVisible = 1;
    SetConsoleCursorInfo(hOut, &cursor_info);
}
2020-02-16 11:24
chocobo2001
Rank: 1
等 级:新手上路
帖 子:21
专家分:5
注 册:2015-10-7
收藏
得分:5 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct Status {
  int width;
  int height;
  int x; //飞机位置x
  int y; //飞机位置y
};

void startup(Status *a, int width, int height) {
  a->width = width; //游戏画面尺寸
  a->height = height; //游戏画面尺寸
  a->x = width / 2;
  a->y = height / 2;
}

void show(Status *a) { //显示画面
  int i, j;

  system("cls");
  updateWithoutInput(); // 建议更新和打印飞机混合成一个函数 show 来按顺序打印,否则要通过移动光标来进行特殊输出
  for (i = 0; i < a->height; i++) { // 这里是按 updateWithoutInput() 更新和飞机位置输出无关来编写
    if (i != a->y) {
      printf("\n");
    } else {
      for (j = 0; j < a->width; j++) {
        if (j == a->x) {
          printf("*");
          break;
        } else {
          printf(" ");
        }
      }
      printf("\n");
    }
  }
}

void updateWithoutInput() { //与用户输入无关的更新
}

void updateWithInput(Status *a, bool *Exit) {
  char input;

  *Exit = false;
  if (_kbhit()) {       //判断是否有输入
    input = _getch();   //判断飞机移动
    switch (input) {
      case 'a': (a->x)--;
                break;
      case 'd': (a->x)++;
                break;
      case 'w': (a->y)--;
                break;
      case 's': (a->y)++;
                break;
      case 'e': *Exit = true;
                break;
      default:  break;
    }
  }
}

int main() {
  Status a;
  bool exit;

  exit = false;

  startup(&a, 30, 20); //数据结构化
  while (1) { //循环执行
    show(&a);
    updateWithInput(&a, &exit);
    if (exit) {
      return 0;
    }
  }
}

[此贴子已经被作者于2020-2-16 14:16编辑过]

2020-02-16 13:53
梨花压海棠
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2020-2-13
收藏
得分:0 
回复 4楼 叶纤
哦哦,我是把j错打成i了,改了就可以了,那个system(“cls”)是清屏的,因为它是靠一直不停打印刷新,出现的位置的移动,多谢
2020-02-16 20:49
梨花压海棠
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2020-2-13
收藏
得分:0 
回复 5楼 吹水佬
谢谢谢谢,我之前用的是比较傻的方法,就是靠一个清屏函数,然后打印,出现位置的移动假象
2020-02-16 20:54
梨花压海棠
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2020-2-13
收藏
得分:0 
回复 6楼 chocobo2001
谢谢
2020-02-16 21:18
快速回复:飞机游戏,编译运行都没问题,但是一直闪屏,看不到“*”,vs2019
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016485 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved