我想制作"堕落的单词"打字游戏(你在它落在控制台底部之前键入了这个词)但我被卡住了,因为我不知道我的声明是否可行。
现在这个功能:
打开带有文字列表的文本文件
从文件中选择随机单词(我没有在网上找到它)。
用下降的动画打印它(列数也是随机的)
问题是:我不知道如何比较变量'输入'目前的下降词。到目前为止,我对scanw功能的尝试失败了,我不确定fgets是否会更好。
编辑:我尝试使用strcmp和if函数,但它并没有真正帮助 - 我的意思是让这个词下降,同时我可以写下这个词。如果两个字符串相同,则应打印出#34;相同&#34;,但它不起作用,我不知道为什么......#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
int col = 0;
int row = 0;
extern WINDOW *stdscr;
int ruch()
{
FILE *fp = NULL;
char words[67];
char input[67];
int i = 0 , ran = 0;
while(1)
{
int ch = 0;
int each = 0;
int x = rand()%70;
//opening file
if ( ( fp = fopen("list.txt" , "r")) == NULL) {
perror ( "could not open file");
exit ( 1);
}
for( i = 0; fgets(words , sizeof(words) , fp) ; i++);//loop to count words in file
ran = rand() % i;//random word
rewind(fp);
for(i = 0 ; i < ran ; i++)//loop to get random word
fgets(words , sizeof(words) , fp);
fclose(fp);
if ( words[strlen(words)-1] == '\n')
words[strlen(words)-1] = '\0';// remove newline
//animation
for( i=0;i<22;i++)
{
getmaxyx(stdscr, row, col);
clear();
mvprintw(1+i ,x , words );
refresh();
usleep(500000);
ch = getch();//get a character
if ( ch != ERR) {
input[each] = ch;//add character to input
each++;
input[each] = '\0';//terminate input
mvprintw(1, 1, input);
if(strcmp(input,words) == 0) {
//mvprintw(5, 1, "SAME\n");
//return 1;
break;
}
else {
if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
clear();
mvprintw(6, 1, "YOU TYPO'D, GAME OVER");
return 2;
}
}
continue;
}
//usleep(100);
continue;
mvprintw(6, 1, "GAME OVER");
}
continue;
}
}
int main()
{
initscr();
srand(time(NULL));
nodelay ( stdscr, TRUE);//so getch does not wait for a character
raw();
ruch();
getch();
usleep(3000000);
endwin();
return 0;
}
请记住,我是C的初学者:)很想得到一些帮助。
问候。
答案 0 :(得分:0)
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
int col = 0;
int row = 0;
extern WINDOW *stdscr;
int ruch()
{
FILE *fp = NULL;
char words[67];
char input[67];
int i = 0 , ran = 0;
int ch = 0;
int each = 0;
srand(time(NULL));
int x = rand()%70;
//opening file
if ( ( fp = fopen("list.txt" , "r")) == NULL) {
perror ( "could not open file");
exit ( 1);
}
//choosing random word from the file
for(; fgets(words , sizeof(words) , fp) ; i++);//loop to count words in file
ran = rand() % i;//random word
rewind(fp);
for(i = 0 ; i < ran ; i++)//loop to get random word
fgets(words , sizeof(words) , fp);
fclose(fp);
if ( words[strlen(words)-1] == '\n')
words[strlen(words)-1] = '\0';// remove newline
//animation
for( i=0;i<22;i++)
{
getmaxyx(stdscr, row, col);
clear();
mvprintw(1+i ,x , words );
refresh();
usleep(500000);
ch = getch();//get a character
if ( ch != ERR) {
input[each] = ch;//add character to input
each++;
input[each] = '\0';//terminate input
mvprintw(1, 1, input);
if(strcmp(input,words) == 0) {
mvprintw(5, 1, "SAME\n");
return 1;
}
else {
if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
mvprintw(6, 1, "NOT SAME");
return 2;
}
}
}
usleep(100);
}
return 0;
}
int main()
{
initscr();
nodelay ( stdscr, TRUE);//so getch does not wait for a character
raw();
ruch();
getch();
usleep(3000000);
endwin();
return 0;
}
这在while循环之前打开了文件。键入斜杠,&#39; /&#39;结束循环。
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
int col = 0;
int row = 0;
extern WINDOW *stdscr;
int ruch()
{
FILE *fp = NULL;
char words[67];
char input[67];
char score[80] = {"Score\n"};
int i = 0 , ran = 0;
int ch = 0;
int each = 0;
int points = 0;
int filewords = 0;
int x = 0;
//opening file
if ( ( fp = fopen("list.txt" , "r")) == NULL) {
perror ( "could not open file");
exit ( 1);
}
for( filewords = 0; fgets(words , sizeof(words) , fp) ; filewords++);//loop to count words in file
while ( 1) {
//choosing random word from the file
ran = rand() % filewords;//random word
rewind(fp);
for(i = 0 ; i < ran ; i++)//loop to get random word
fgets(words , sizeof(words) , fp);
if ( words[strlen(words)-1] == '\n')
words[strlen(words)-1] = '\0';// remove newline
x = rand()%(col - strlen(words));
clear();
mvprintw(0, (col - strlen ( score))/2, score);
mvprintw(1, (col - strlen ( score))/2, "To end, type a '/'. Get ready...");
refresh();
usleep(2000000);
//animation
for( i=0;i<row-1;i++)
{
clear();
mvprintw(0, (col - strlen ( score))/2, score);
mvprintw(1+i ,x , words );
refresh();
usleep(400000);
ch = getch();//get a character
if ( ch != ERR) {
if ( ch == '/') {
fclose(fp);//close the file
return 0;
}
input[each] = ch;//add character to input
each++;
input[each] = '\0';//terminate input
mvprintw(1, 1, input);
if(strcmp(input,words) == 0) {
points += strlen ( words) + ( row - i);
sprintf ( score, "Score %d correct\n", points);
each = 0;
break;
}
else {
if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
points -= (int)(strlen ( words));
sprintf ( score, "Score %d typo %s %s\n", points, words, input);
each = 0;
break;
}
}
}
}
if ( i == row - 1) {
points -= row;
sprintf ( score, "Score %d timeout\n", points);
}
each = 0;
getch();
}
}
int main()
{
srand(time(NULL));
initscr();
nodelay ( stdscr, TRUE);//so getch does not wait for a character
raw();
getmaxyx(stdscr, row, col);
ruch();
getch();
usleep(30000);
endwin();
return 0;
}