Menu
CoddyTech

A Reset Function

Part of the Game Rules section of Coddy's Pygame journey. Lesson 4 of 29.

Play a game, score a few points, lose, press R. The new game starts with the old score. Every value a game changes must go back to its start, and one is often forgotten.

Put all the start values in one function and call it both before the first game and on R:

def reset():
    global player, rock, score
    player = pygame.Rect(220, 300, 40, 40)
    rock = pygame.Rect(random.randint(0, 456), -24, 24, 24)
    score = 0


reset()

The global line matters. A variable you assign inside a function belongs to that function unless you say otherwise. Without global, score = 0 would make a new local score that is gone when the function ends, and the game's score would not change.

Now the start of a game is written once. When you add a value later, such as lives, you add it in one place and both the first game and every new game get it.

challenge icon

Challenge

Medium
  1. Write reset() that sets player, rock and score to their start values, with global player, rock, score as its first line.
  2. Call reset() once before the loop, in place of the three lines that set them.
  3. On R, call reset() and set the state to "play".

Try it yourself

import random
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Meteor Dodge")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)

player = pygame.Rect(220, 300, 40, 40)
rock = pygame.Rect(random.randint(0, 456), -24, 24, 24)
score = 0
state = "menu"

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN and state == "menu":
                state = "play"
            if event.key == pygame.K_r and state == "over":
                player.x = 220
                rock.y = -24
                rock.x = random.randint(0, 456)
                state = "play"

    if state == "play":
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player.x -= 6
        if keys[pygame.K_RIGHT]:
            player.x += 6
        player.clamp_ip(screen.get_rect())

        rock.y += 4
        if rock.top > 360:
            rock.y = -24
            rock.x = random.randint(0, 456)
            score += 1
        if rock.colliderect(player):
            state = "over"

    screen.fill((30, 30, 46))
    if state == "menu":
        title = font.render("Meteor Dodge", True, (240, 240, 240))
        screen.blit(title, title.get_rect(center=(240, 140)))
        hint = font.render("Press Enter to play", True, (240, 240, 240))
        screen.blit(hint, hint.get_rect(center=(240, 200)))
    else:
        pygame.draw.rect(screen, (255, 196, 0), player)
        pygame.draw.rect(screen, (235, 80, 80), rock)
        score_text = font.render(f"Score: {score}", True, (240, 240, 240))
        screen.blit(score_text, (10, 10))
        if state == "over":
            message = font.render("Game over", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 160)))
            hint = font.render("Press R to play again", True, (240, 240, 240))
            screen.blit(hint, hint.get_rect(center=(240, 200)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Rules

Practice on your own: Pygame playground