0
\$\begingroup\$

so I am following a tutorial to make a snake game in my Xcode console. But the tutorial i am following is using windows and therefore the "system("clear")" command isn't working with my mac, and all of my research seems to show that there is no way to do that in Xcode

my question is, don't many games need to have their screen cleared constantly? how would games that are being designed in Xcode clear their screens if there is no easy way to do this?

\$\endgroup\$
1
\$\begingroup\$

The system() standard library function invokes the host operating system's command processing facility. For Windows this is essentially the console you get when running cmd.exe which lets you run DOS-like commands such as cls to clear the screen. Other than the function's existence, everything about system() is platform-specific.

If you're using Xcode, you're using a Mac, which means you have Terminal.app or similar, which will use some shell (bash, by default). clear is the program for most *nix environments to clear the screen. However, when running the program under Xcode's debugger, the TERM environment variable isn't set and the clear program won't know what to do. So it won't clear anything, even though the program would if you ran it directly from the terminal.

All the clear program does, generally, is either print a bunch of newlines or print a bunch of ANSI escape codes to manipulate the cursor and write over the console output buffer. You could do the same; the newlines in particular would be easy enough.

As for your second question: Most games don't use terminals for their main interface and so have no need to clear the terminal. Even games that do, like Nethack, use libraries like ncurses to interact with lower-level terminal APIs and "draw" to the terminal. If that's what you're going for you should investigate those sorts of third-party libraries.

Most games use the underlying OS APIs to create windows and draw into those using platform-appropriate APIs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.