I just started learning programming languages and I want to make a character (point '*') in a table move.
This is my code
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
int v;
int x = 50, y = 10;
int i, j;
char screen[80][25];
// declare and initialize table
for (i = 0; i < 80; i++)
for (j = 0; j < 25; j++)
screen[i][j] = ' ';
// coordinate system
for (i = 0; i < 80; i++) screen[i][12] = '-';
for (j = 0; j < 25; j++) screen[40][j] = '|';
// point, position
screen[x][y] = '*';
// print result
for (j = 0; j < 25; j++) {
for (i = 0; i < 80; i++)
printf("%c", screen[i][j]);
printf("\n");
}
}
This prints a table of size 80x25 and a coordinate system with a centre in (40,12). I set the position of the character '*' on coordinates (x,y). I defined x and y as 50 and 10.
Now I want to move my star by changing x and y. How do I change x and y (position of the star)? Maybe with scanf function? I tried to use scanf like this:
int v;
...
scanf("%d", &v);
if(v == 1)
{
y--;
}
but then everything (table, coordinate system and the character) disappeared. Please help.
Thanks.
答案 0 :(得分:2)
请使用[n]curses(3)。
如果您使用的是* nix或OS X,则应该已经安装了它。如果您使用的是Windows,则可以在
处获得PdCurses的分发但是,从设计角度来看,您应该将模型(80 x 25阵列)与用户界面分开。你有两个任务:
这将引导您进入名为模型 - 视图 - 控制器或 MVC 的设计模式。在经典的MVC中,你有:
这种关注点分离使得(除其他事项外)更容易编写和测试代码。