ARTICLE AD BOX
In my code pasted below, I try to save the new level status(what level the player is on) in stats.txt. When I read the text file after saving it in the code, the file has the correct value. But when I click the x to close the program, the file reverts back to before I ran it. If I force close the program with an error or if I close the program while I am in the level I unlocked, the file saves properly. What do I do?
from tsapp import* from random import* from time import* w = GraphicsWindow() framerate = 60 w.framerate = 60 level_choice = 0 ###LOADING SCREEN### def loading_screen(): sprites = [] CoordinateGrid0 = Sprite("CoordinateGrid.jpg",-79.0, -29.0) sprites.append(CoordinateGrid0) CoordinateGrid0.scale = 1.1499999999999995 CoordinateGrid0.x = -79.0 CoordinateGrid0.y = -29.0 Job1 = TextLabel("LuckiestGuy-Regular.ttf",100.0, 331.0, 111.20000000000002, 500, "Job", (255, 0, 0)) sprites.append(Job1) Swap2 = TextLabel("LuckiestGuy-Regular.ttf",100.0, 361.0, 183.20000000000002, 500, "Swap", (255, 0, 0)) sprites.append(Swap2) Loading3 = TextLabel("Roboto-Black.ttf",77.0, 222.5, 361.0, 775, "Loading Game...", (0, 0, 0)) sprites.append(Loading3) ArrowRight4 = Sprite("ArrowRight.png",259.5, 69.0) sprites.append(ArrowRight4) ArrowRight4.scale = 0.6749999999999997 ArrowRight4.x = 259.5 ArrowRight4.y = 69.0 ArrowRight4.angle = -60 ArrowRight4.x = 259.5 ArrowRight4.y = 69.0 ArrowRight5 = Sprite("ArrowRight.png",470.5, 12.5) sprites.append(ArrowRight5) ArrowRight5.scale = 0.6749999999999997 ArrowRight5.x = 470.5 ArrowRight5.y = 12.5 ArrowRight5.angle = 130 ArrowRight5.x = 470.5 ArrowRight5.y = 12.5 Swap6 = TextLabel("LuckiestGuy-Regular.ttf",95.0, 367.0, 180.20000000000002, 500, "Swap", (255, 215, 0)) sprites.append(Swap6) Job7 = TextLabel("LuckiestGuy-Regular.ttf",96.0, 336.0, 109.60000000000002, 500, "Job", (255, 215, 0)) sprites.append(Job7) for sprite in sprites: w.add_object(sprite) w.finish_frame() return sprites from load_levels import* current_sprites = loading_screen() w.finish_frame() def save(value): with open("stats.txt","w") as file: file.write(str(value)) file.flush() file.close() print("saved\n") for sprite in current_sprites: sprite.destroy() current_sprites = load_menu() for sprite in current_sprites: w.add_object(sprite) w.finish_frame() ###Settings Setup### with open("stats.txt","r") as file: level_status = file.read() level_status = int(level_status) while w.is_running: save(level_status) mouse_x, mouse_y = get_mouse_position() ###PLAY BUTTON### for button in current_sprites: if type(button) == TextLabel: continue if button.image == "EmptyButton.png" or button.image == "EmptyButtonPressed.png": if button.x < w.width/2: if not button.is_colliding_point(mouse_x, mouse_y): button.image = "EmptyButton.png" elif is_mouse_down() and button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButton.png": button.image = "EmptyButtonPressed.png" if was_mouse_released() and button.is_colliding_point(mouse_x, mouse_y): save(level_status) in_game = True while in_game and w.is_running: ###LEVEL SELECT### save(level_status) for sprite in current_sprites: sprite.destroy() current_sprites = load_select() bg = Sprite("CoordinateGrid.jpg",0,0,2) bg.center_x = w.width/2 bg.center_y = w.height/2 w.add_object(bg) for sprite in current_sprites: w.add_object(sprite) level_buttons = [] unlocked_buttons = [] tutorial_but = current_sprites[1] level_buttons.append(tutorial_but) l1_but = current_sprites[2] level_buttons.append(l1_but) l2_but = current_sprites[3] level_buttons.append(l2_but) l3_but = current_sprites[4] level_buttons.append(l3_but) l4_but = current_sprites[5] level_buttons.append(l4_but) l1_lock = current_sprites[9] l2_lock = current_sprites[8] l3_lock = current_sprites[7] l4_lock = current_sprites[6] ###Create buttons with correct unlocking print(level_status) if level_status == 0: for button in level_buttons: if button.x == 40.0: unlocked_buttons.append(button) continue button.image = "EmptyButtonPressed.png" button.flip_y = True elif level_status == 1: unlocked_buttons.append(tutorial_but) unlocked_buttons.append(l1_but) l1_lock.text = "1" l2_but.image = "EmptyButtonPressed.png" l3_but.image = "EmptyButtonPressed.png" l4_but.image = "EmptyButtonPressed.png" elif level_status == 2: unlocked_buttons.append(tutorial_but) unlocked_buttons.append(l1_but) unlocked_buttons.append(l2_but) l1_lock.text = "1" l2_lock.text = "2" l3_but.image = "EmptyButtonPressed.png" l4_but.image = "EmptyButtonPressed.png" elif level_status == 3: unlocked_buttons.append(tutorial_but) unlocked_buttons.append(l1_but) unlocked_buttons.append(l2_but) unlocked_buttons.append(l3_but) l1_lock.text = "1" l2_lock.text = "2" l3_lock.text = "3" l4_but.image = "EmptyButtonPressed.png" elif level_status == 4: unlocked_buttons.append(tutorial_but) unlocked_buttons.append(l1_but) unlocked_buttons.append(l2_but) unlocked_buttons.append(l3_but) unlocked_buttons.append(l4_but) l1_lock.text = "1" l2_lock.text = "2" l3_lock.text = "3" l4_lock.text = "4" elif level_status == 5: unlocked_buttons.append(tutorial_but) unlocked_buttons.append(l1_but) unlocked_buttons.append(l2_but) unlocked_buttons.append(l3_but) unlocked_buttons.append(l4_but) l1_lock.text = "1" l2_lock.text = "2" l3_lock.text = "3" l4_lock.text = "4" not_selected = True while w.is_running and not_selected: save(level_status) mousex, mousey = get_mouse_position() for button in unlocked_buttons: if was_mouse_released() and button.is_colliding_point(mousex, mousey) and button.image == "EmptyButtonPressed.png": level_choice = unlocked_buttons.index(button) not_selected = False if button.is_colliding_point(mousex, mousey) and is_mouse_down(): button.image = "EmptyButtonPressed.png" else: button.image = "EmptyButton.png" w.finish_frame() for sprite in current_sprites: sprite.destroy() current_sprites = loading_screen() for sprite in current_sprites: w.add_object(sprite) w.finish_frame() for sprite in current_sprites: sprite.destroy() if level_choice == 0: sprites, decor, ui = load_tutorial() if level_choice == 1: sprites, decor, ui = load_l1() if level_choice == 2: sprites, decor, ui = load_l2() if level_choice == 3: sprites, decor, ui = load_l3() if level_choice == 4: sprites, decor, ui = load_l4() current_sprites = sprites ####IN GAME!!!#### x = 0 y = 0 vy = 0 vx = 0 gravity = 0.3 friction = 0.87 air_friction = 0.98 floor = False jump_power = 7 cling = -2.3 do_cling = False walk_speed = 0.19 max_walkspeed = 8 framerate = 60 max_vy = 20 left_wall = False right_wall = False running = True w = GraphicsWindow() ###Place Level code between here### bg = Sprite("CoordinateGrid.jpg",0,0,2) bg.center_x = w.width/2 bg.center_y = w.height/2 w.add_object(bg) for sprite in sprites: w.add_object(sprite) ###Place Level code between here### ##Game Setup### player = Sprite("StudentMiddleSchool1.png",0,0,0.12) player.center_x = w.width/2 player.center_y = w.height/2 + 20 hitbox = Sprite("StudentMiddleSchool1.png",0,0,0.12) hitbox.center_x = w.width/2 hitbox.center_y = w.height/2 + 20 hitbox.visible = False hitbox_x = Sprite("StudentMiddleSchool1.png",0,0,0.12) hitbox_x.center_x = w.width/2 hitbox_x.center_y = w.height/2 + 19 hitbox_x.visible = False for sprite in sprites: w.add_object(sprite) w.add_object(player) w.add_object(hitbox) w.add_object(hitbox_x) ###MAIN LOOP### w.framerate = framerate completed_level = False while w.is_running and running: ###Physics Update if floor == False and -max_vy < vy < max_vy: if do_cling and vy < 0: vy = cling else: vy -= gravity x += vx y += vy ###RESET IF FELL### if y < -2000: y = 0 #destroy old sprites player.destroy() bg.destroy() hitbox.destroy() for sprite in sprites: sprite.destroy() #Redefine new sprites w = GraphicsWindow() w.framerate = framerate bg = Sprite("CoordinateGrid.jpg",0,0,2) bg.center_x = w.width/2 bg.center_y = w.height/2 w.add_object(bg) sprites, decor, ui = load_tutorial() for sprite in sprites: w.add_object(sprite) player = Sprite("StudentMiddleSchool1.png",0,0,0.12) player.center_x = w.width/2 player.center_y = w.height/2 + 20 hitbox = Sprite("StudentMiddleSchool1.png",0,0,0.12) hitbox.center_x = w.width/2 hitbox.center_y = w.height/2 + 20 hitbox.visible = False hitbox_x = Sprite("StudentMiddleSchool1.png",0,0,0.12) hitbox_x.center_x = w.width/2 hitbox_x.center_y = w.height/2 + 19 hitbox_x.visible = False w.add_object(player) w.add_object(hitbox) w.add_object(hitbox_x) ##Movement with dictionary## do_cling = False do_friction = True ###If you are colliding left wall if left_wall: left_wall = False ##Move hitbox on sprite hitbox_x.x += max_walkspeed for sprite in sprites: sprite.show_bounds = False #Skp text labels if type(sprite) == TextLabel: continue if sprite.is_colliding_rect(hitbox_x): sprite.show_bounds = True left_wall = True #Move hitbox back where it should be hitbox_x.x -= walk_speed if right_wall: right_wall = False ##Move hitbox on sprite hitbox_x.x -= max_walkspeed for sprite in sprites: sprite.show_bounds = False #Skip text labels if type(sprite) == TextLabel: continue if sprite.is_colliding_rect(hitbox_x): sprite.show_bounds = True right_wall = True #Move hitbox back where it should be hitbox_x.x += walk_speed ###Move to the left if is_key_down(K_a): if vx < max_walkspeed and right_wall == False: if vx < 0: vx *= friction vx += walk_speed player.flip_x = True do_friction = False left_wall = False if right_wall and floor == False: do_cling = True ###Move to the right elif is_key_down(K_d): if vx > (-max_walkspeed) and left_wall == False: if vx > 0: vx *= friction vx -= walk_speed player.flip_x = False do_friction = False right_wall = False if left_wall and floor == False: do_cling = True ###Jump if was_key_pressed(K_w) and (floor == True or (left_wall or right_wall)): if left_wall and do_cling and not floor: vx = 7 left_wall = False if right_wall and do_cling and not floor: vx = -7 right_wall = False for sprite in sprites: if sprite in ui: continue sprite.y += 1 vy = jump_power if do_friction == True: if floor: vx *= friction else: vx *= air_friction floor = False hitbox.y -= vy hitbox_x.x -= vx ###CHECKING COLLISIONS### for sprite in sprites: if (not sprite.visible) or sprite in ui: continue if sprite in decor: continue if type(sprite) == TextLabel: if "🏁" in sprite.text and hitbox_x.is_colliding_rect(sprite): running = False completed_level = True continue sprite.show_bounds = False if hitbox.is_colliding_rect(sprite): ###ON TOP OF SPRITE### if player.y + player.height - 5 + vy< sprite.y: hitbox.y = player.y floor = True vy = 0 difference = (player.y + player.height -1) - sprite.y for sprite2 in sprites: if sprite2 in ui: continue sprite2.y += difference ###ON BOTTOM### elif player.y > sprite.y+sprite.height: vy *= -0.5 hitbox.y = player.y difference = (player.y) - (sprite.y + sprite.height) for sprite2 in sprites: if sprite2 in ui: continue sprite2.y -= difference ###ON EITHER SIDE### if hitbox_x.is_colliding_rect(sprite): ###ON RIGHT### if player.x > sprite.x and vx > 0: right_wall = True vx = 0 hitbox_x.x = player.x difference = (sprite.x + sprite.width) - player.x for sprite2 in sprites: if sprite2 in ui: continue sprite2.x -= difference ###LEFT### if player.x < sprite.x and vx < 0: left_wall = True vx = 0 hitbox_x.x = player.x difference = (player.x + player.width ) - sprite.x for sprite2 in sprites: if sprite2 in ui: continue sprite2.x += difference ##FINAL MOVEMENT### for sprite in sprites: if sprite in ui: continue sprite.x += vx sprite.y += vy if sprite.x > w.width or sprite.x + sprite.width < 0: sprite.visible = False else: sprite.visible = True hitbox_x.x = player.x hitbox.y = player.y hitbox.y -= vy hitbox_x.x -= vx w.finish_frame() ###BACK TO MENU/END TUTORIAL### print("completed: " + str(completed_level) + " level: " +str(level_choice)+ " Best level: " +str(level_status)) if completed_level == True and (int(level_choice) == int(level_status)): level_status = int(level_status) +1 save(level_status) ###LEVEL SELECT INSTEAD OF TUTORIAL### ###SETTINGS BUTTON### if button.x > w.width/2: if not button.is_colliding_point(mouse_x, mouse_y): button.image = "EmptyButton.png" elif is_mouse_down() and button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButton.png": button.image = "EmptyButtonPressed.png" if was_mouse_released() and button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButtonPressed.png": for sprite in current_sprites: sprite.destroy() current_sprites = load_settings() for sprite in current_sprites: w.add_object(sprite) w.finish_frame() settings_open = True while w.is_running and settings_open: mouse_x, mouse_y = get_mouse_position() for button in current_sprites: if type(button) == TextLabel: continue if not button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButtonPressed.png": button.image = "EmptyButton.png" elif is_mouse_down() and button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButton.png": button.image = "EmptyButtonPressed.png" if was_mouse_released() and button.is_colliding_point(mouse_x, mouse_y) and button.image == "EmptyButtonPressed.png": for sprite in current_sprites: sprite.destroy() settings_open = False w = GraphicsWindow() w.framerate = framerate current_sprites = load_menu() for sprite in current_sprites: w.add_object(sprite) w.finish_frame() w.finish_frame()