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)