0

This is a follow up question to the comments in my last question. I was told that the command prompt screen is flickering because i didnt limit the frames per second. I have been doing some research on how to limit frames per second and i found this:

def limit_frames(FPS): 
    return 1/ FPS

while True: # The main game loop
    # rest of the code
    time.sleep(limit_frames())

Limit_frames is an inverse function that returns smaller value if the FPS is greater, and this returned value is used in the main loop as an argument to time.sleep, so that greater FPS input to limit_frames function lowers the sleep time, hence more frames. However, this still dosent work for me and the window is still flickering. Any help would be appreciated. Thanks

Edit :

This is the map class:

    class Map:
        def __init__(self):
            self.map = [[" " for i in range(50)]for i in range(40)]
            self.n = 0 # counter used for boundry

        def update(self, thing, x, y):  
            self.map[y][x] = thing


        def draw(self):
            for layer in self.map: 
                for item in layer:
                    print(item, end= " ") 
                print('')


        def boundry(self):
            for i in range(50): # TOP
                self.update("_", self.n, 0)
                self.n += 1
                if self.n >= 49:
                    self.n = 0

            for i in range(40): # LEFT
                self.update("|", 0, self.n)
                self.n += 1
                if self.n >= 39:
                    self.n = 0

            for i in range(40): # RIGHT
                self.update("|", 49, self.n)
                self.n += 1
                if self.n >= 39:
                    self.n = 0

            for i in range(50): # BOTTOM
                self.update("_", self.n, 39)
                self.n += 1
                if self.n >= 49:
                    self.n = 0

I am using the update function to update the map. I have called it in different classes to such as player to add them to the map.An example is as follows:

    def draw(self):
        game_map.update(self.player, self.x, self.y)

The following is the entire game loop:

start = time.time()
FPS = 10000
while True:
    end = time.time()
    difference = end - start

    game_map.draw()
    game_map.boundry()

    os.system("cls")
    player.draw()
    player.move()
    player.boundries()
    player.print_score()

    game.generate_bullet()
    game.shoot()
    game.bullet_boundries()



    game.generate_enemies(difference)
    game.draw_enemies()
    game.move_enemies()
    game.enemy_boundries()
    game.hit()

    time.sleep(1/FPS)
  • It sounds like the rate at which you update the screen is the main cause of the flicker. Perhaps it _how_ you update the text screen (or what the library does it if you're using one). – martineau Feb 15 '20 at 11:42
  • @martineau I updated the question with how i am updating the text screen –  Feb 15 '20 at 11:58
  • Also i tried different numbers as FPS ranging from 1 to a few thousand and none of them provide a stable screen. –  Feb 15 '20 at 12:04
  • That's what I said, it's not how fast or slow you're updating the screen that's causing it to flicker — so FPS doesn't matter. – martineau Feb 15 '20 at 12:08
  • I cant think of any other way to update the screen to reduce flickering :( –  Feb 15 '20 at 12:10
  • 1
    Your code clears the screen with an `os.system("cls")` each frame and then redraws it. That alone may cause the flickering because of the inherent need to simulate a text console on a Windows' graphical display (which is what the `cmd` prompt does). – martineau Feb 15 '20 at 12:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207882/discussion-between-hippozhipos-and-martineau). –  Feb 15 '20 at 12:41

1 Answers1

0

you should run time.timeit(run) in that run is your game script so when It finishes you do

time.sleep(1/fps-run_time if 1/fps-run_time>0 else 0)
Flair
  • 2,609
  • 1
  • 29
  • 41