0

I'm making a small game where you have to guess if the answer is cp1 or cp2 and I want to give the user some sort of hint. One way of doing that I think is by flashing the answer for a nano or milisecond to user, i.e. a new window would open with a : picture like this. Any ideas as to how to do this?

Nawrez
  • 3,314
  • 8
  • 28
  • 42
Husun
  • 29
  • 1
  • 7
  • Without showing any [MCVE] and without mention of any graphics library, your question is too broad. Please **edit your question** to improve it a lot. – Basile Starynkevitch Dec 31 '17 at 11:20

2 Answers2

1

[...] flashing the answer for a nano or millisecond to user [....] in a new window [...]

A millisecond is too short (both for the human player -read about the persistence of vision- and for the Python interpreter); your screen is probably refreshed at 60Hz. Consider flashing for at least one tenth of a second (and probably more than that, you'll need to experiment, and you might make the flashing delay or period configurable). How to do that depends upon the widget toolkit you are using.

If using something above GTK, you'll need to find the Python binding to g_timeout_add, see also this.

If you use something above libSDL (e.g. pygame_sdl2), you need something related to its timers.

There are many other widgets or graphical frameworks usable from Python, and you need to choose one (look also into PyQt). Each of them has its own way to deal with timing, delays, windows, graphical display of text inside a window, etc...

If your system is Linux, see also time(7) for a general overview of time related things. Event loops (like those in graphics libraries) are built above a multiplexing system call such as poll(2) (or the old select, etc...).

You need to spend several days in reading more, choosing your graphical toolkit, before coding a single line of code of your game (which might need more code than what you imagine now).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

I think the closest you can easily get to this affect is to just print to the console, but don't print the new-line (\n), just the carriage return (\r). This way you can write over the text after a couple of milliseconds. We do need to know the length of the thing we are printing so that we can be sure to completely override it with the next print.

The code for this would look something like:

import time
ms = 100
s = 'cp1'
print(s, end='\r')
time.sleep(ms / 1000)
print(' ' * len(s))
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54