-1
我做了一個簡單的蛇遊戲下面。一切工作正常,但是我想實現一個功能,允許用戶通過輸入我目前使用的操作步驟1.用戶提示再次播放不工作
再次玩遊戲..而...循環的遊戲
然而,這裏是問題所在,程序在輸入1時會重新啓動,但是 沒有任何字符(tx,jx等)產生,並且遊戲只是在後臺循環無限循環(退出時有一定數量的printf(「Number of 「),printf(」ripperoni「)在後臺打印
幫助讚賞!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "display.h"
#include "move.h"
#include "place.h"
#define MAX 799
#define DEAD 0
#define TRUE 1
#define FALSE 0
int main()
{
//initialize variables
struct pos { //positions
int fx[MAX];
int fy[MAX];
int tx;
int ty;
int jx;
int jy;
} pos;
struct score { //scores
int move;
int F;
int J;
} score;
int d,q,i,k,f,followc;
int p;
char *z="Number of Jujus collectd ";
char *y="The number of followers ";
do {
score.F=score.J = score.move = 0;
i=k=f=followc=0;
draw_map();
place(&pos.tx,&pos.ty);
sleep(1);
draw_symbol(pos.tx, pos.ty, 'S');
place(&pos.jx, &pos.jy);
display_score(score.move);
debug_wds(5,z);
debug_wds(8,y);
debug_num(6,score.J);
debug_num(9,score.F);
while(d!=DEAD && q != 'q' && q != 'Q')
{
pos.fx[i]=pos.tx; //store timmy's position in follower
pos.fy[i]=pos.ty; //store timmy's position in follower
draw_symbol(pos.jx, pos.jy, '$');
display_score(score.move);
debug_wds(5,z);
debug_num(6,score.J);
debug_wds(8,y);
debug_num(9,score.F);
q = move(&pos.tx, &pos.ty); //move timmy
score.move++;
draw_symbol(pos.tx,pos.ty,'S'); //update timmy's position
if((pos.tx ==pos.jx)&&(pos.ty == pos.jy)) //if timmy collects a juju
{
score.J++; //update juju score
score.F++; //update follower score
display_score(score.move);
place(&pos.jx,&pos.jy);
followc++; //update follower count
}
for (f=0,k=i+1-followc; f<followc; f++,k++) //check to see if timmy is same position as followers
{
if(pos.tx==pos.fx[k]&&pos.ty==pos.fy[k])
{
d=DEAD;
}
draw_symbol(pos.fx[k],pos.fy[k],'O'); //draw followers
}
i++;
}
clear_screen();
printf("Number of Jujus = %d\n", score.J);
printf("Ripperoni... You really need to update your server(s)..*___*\n");
printf("Enter 1 to keep playing\n");
scanf("%d",&p);
} while(p==1);
}
你的問題是,控制檯輸入是行緩衝,和你的scanf()的調用不消耗整條生產線,從而回報立即..有許多重複SO - 搜索「scanf無限循環」。 – Clifford