Console game cmd; flashing reduction

I am writing an arkanoid game in python on the Windows console, and what bothers me is annoying with a blink after each iteration of the "display" cycle. Does anyone know how to reduce it? This is part of the code:

#-*- coding: UTF-8 -*- import time import os clear = lambda: os.system('cls') def gra(): koniec='0' while koniec!='1': for i in range(4): for j in range(10): print '[___]', print '\n', for i in range(10): print '\n' for i in range(10): print ' ', print '~~~~~~~~~~' time.sleep(0.1) clear() gra() 
+5
source share
3 answers

There is a limit to what you can do, but put everything in one large line, and then printing once between screen cleanups is better than a few small prints in a loop. Run the following code and see how much better the second half of the program works than the first half:

 import time, os, random def display1(chars): os.system('cls') for row in chars: print(''.join(row)) def display2(chars): os.system('cls') print('\n'.join(''.join(row) for row in chars)) chars = [] for i in range(40): chars.append(["-"]*40) for i in range(100): r = random.randint(0,39) c = random.randint(0,39) chars[r][c] = "X" time.sleep(0.1) display1(chars) os.system('cls') time.sleep(1) chars = [] for i in range(40): chars.append(["-"]*40) for i in range(100): r = random.randint(0,39) c = random.randint(0,39) chars[r][c] = "X" time.sleep(0.1) display2(chars) 

In the editor: you can combine these ideas with the great @GingerPlusPlus idea to avoid cls . The trick is to print a large number of backspaces.

First write your own version of cls :

 def cls(n = 0): if n == 0: os.system('cls') else: print('\b'*n) 

The first time it is called, skip it and it will simply clear the screen.

The following function pushes an array of characters into the command window in one large print and returns the number of characters printed (since this is the number of back spaces needed to move the cursor):

 def display(chars): s = '\n'.join(''.join(row) for row in chars) print(s) return len(s) 

Used as follows:

 chars = [] for i in range(40): chars.append(["-"]*40) for i in range(100): n = 0 r = random.randint(0,39) c = random.randint(0,39) chars[r][c] = "X" time.sleep(0.1) cls(n) n = display(chars) 

when the above code is running, the display changes smoothly, with almost no flicker.

+1
source

anything really done with the windows console will blink, if you clear and rewrite the screen, if you don't find something pretty advanced, I don't think you can remove the blinking

My only suggestion is to limit cleanup when something really changes.

or use the tkinter text box instead of cmd. If you want to use this idea, I can help with it, although I mainly use tkinter for python 3.4

hope this helps!

0
source

You can do:

 rows_full=row_count+2 print("\033[F"*rows_full) #print the screen again 

The disadvantages are that you cannot overwrite the topmost bit of the console and know how many lines were printed. But knowing the number of lines should be trivial when working with fixed-line ASCII graphics. It is also not CLS , it just moves you to the top.

Oh, an explanation! It uses the ANSI F escape sequence, which moves to the line above. He prints it many times, which puts you at the top of the screen. In addition, you need to add 2 to the line, otherwise the top lines will be repeated.

PS: If someone knows how to make this method go to the top of the console, I will give you some cookies.

0
source

Source: https://habr.com/ru/post/1240755/


All Articles