So in response to my question (How to continuously move an image smoothly in Pygame?) I was told to set my framerate in order to do my animation. But no matter where I place it, clock.tick(60)
does nothing. I do clock = pygame.time.Clock()
BTW. So how do I do this? I have researched and found this (pygame clock.tick() vs framerate in game main loop) but I don’t really understand this.
My main game function;
def main_game(): global var_escape global var_start_game global var_menu_screen global var_camera global var_bounce global var_x global var_x_timer global var_door global var_light global camera_flip var_start_game = False var_escape = False var_menu_screen = 0 var_bounce = 0 var_x = 0 var_camera = 1 var_x_timer = 0 var_light = 0 var_door = 0 var_camera_flip = 0 pygame.mixer.init() pygame.mixer.music.load(r'audioMenu_Music.mp3') pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(1.0) while True: if var_menu_screen == 0: menu_screen() if var_menu_screen == 1: options_screen() if var_menu_screen == 2: new_game_boyz() if var_menu_screen == 3: pygame.quit() sys.quit() if var_menu_screen == 4: credits_screen() if var_start_game == True: break var_start_game = False start_game() image_door_6_off = pygame.image.load(r'texturesdoorlight offframe_6_off.png') image_door_button_off = pygame.image.load(r'texturesdoor buttondoor_button_off.png') image_light_button_off = pygame.image.load(r'textureslight buttonlight_button_off.png') image_door_6_off_size = image_door_6_off.get_rect().size centered_image_door_6_off = [(display_size[0] - image_door_6_off_size[0])/2, (display_size[1] - image_door_6_off_size[1])/2] screen.blit(image_door_6_off, centered_image_door_6_off) screen.blit(image_door_button_off, (0, 0)) screen.blit(image_light_button_off, (1113, 0)) pygame.display.update() while var_escape == False: #This is my main game loop in the function #This is where I would like to set the framerate mouse_down = False for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: quit_popup() elif event.type == MOUSEBUTTONDOWN: mouse_down = True light(mouse_down) door(mouse_down) camera_flip() #Just so I can get a smoother animation for this function below cameras() #<--- This function here #Just so I can get a smoother animation for this function above camera_buttons(mouse_down)
This is my main game loop;
while True: #Calls the function main_game()
The main game loop will not set the framerate because the main_game function has a loop inside it. This is only in a loop because when I want to restart the function I can. I just added this in to show how the function is run.
Advertisement
Answer
Use pygame.time.Clock
to control the frames per second and thus the game speed.
The method tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick()
:
This method should be called once per frame.
e.g.:
def main_game(): # [...] FPS = 60 clock = pygame.time.Clock() while var_escape == False: clock.tick(60) # [...]