Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can i move with the arrows on c? This does not work

–2 votes
asked Dec 1, 2020 by Figone (110 points) 1 flag
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define R 25
#define C 25

#define VUOTO 0
#define PERSONAGGIO 1
#define FRECCIA_SU 72
#define FRECCIA_GIU 80
#define FRECCIA_DX 77
#define FRECCIA_SX 75


void stampaCampo(int campo[R][C]){
	
	int i,j;
	
	for(i=0; i<R; i++){
		for(j=0; j<C; j++){
			if(campo[i][j] == VUOTO){
				printf(" ");
			} else if(campo[i][j]==	PERSONAGGIO){
				printf("o");				
			}
		}
		printf("\n");
	}
}


main(){
	int posX = C/2;
	int posY = R/2;
	
	int i,j;
	
	int campo[R][C];
	
	//Inizializzo la matrice
	for(i=0; i<R; i++){
		for(j=0; j<C; j++){
			campo[i][j] = VUOTO;
		}
	}
	
	campo[posY][posX] = PERSONAGGIO;
	
	while(1){
		system("cls");
		//Stampo il campo da gioco
		stampaCampo(campo);
		
		//Muovo il personaggio con le frecce
		getch();//il primo codice del getch รจ 224
		int tasto = getch();
		campo[posY][posX] = VUOTO;
		
		switch(tasto){
			case FRECCIA_SU:{
				posY--;
				if(posY<0){
					posY=0;
				}
				break;
			}
			case FRECCIA_GIU:{
				posY++;
				if(posY>=R){
					posY=R-1;
				}
				break;
			}
			case FRECCIA_SX:{
				posX--;
				if(posX<0){
					posX=0;
				}
				break;
			}
			case FRECCIA_DX:{
				posX++;
				if(posX>=C){
					posX=C-1;
				}
				break;
			}
		}
		campo[posY][posX] = PERSONAGGIO;
	}
}

1 Answer

0 votes
answered Dec 4, 2020 by Peter Minarik (86,130 points)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...