def keyword does. When you write def my_func():, does the code inside run immediately — or does it wait until the function is called?Fill in each blank so the function is correctly defined and called. Read the comments carefully for hints.
# Define a function to draw the snake on screen def ___1___(surface, snake): # ___1___: function name (draws the snake) for i, (col, row) in ___2___(snake): # ___2___: built-in giving (index, item) pairs colour = DKGREEN if i == ___3___ else GREEN # ___3___: index of head segment rect = (col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE - 1, CELL_SIZE - 1) pygame.draw.rect(___4___, colour, rect) # ___4___: first parameter name # Call the function in the main loop: ___5___(screen, snake) # ___5___: same as ___1___ — how do you run a function? # A function with a return value (membership test): def is_alive(head, body): return not (head ___6___ body[1:]) # ___6___: Python keyword — "is element in list?"
(d________)(e_________)(one digit)(s_______)(d________)(i_)Each code snippet below has one bug in the function definition or its body. Type the corrected line in the box.
Bug 1 — Wrong parameter list
draw_food(screen, food_x, food_y) — 3 arguments passed, but only 2 parameters defined.Bug 2 — print instead of return
if check_wall(snake[0][0], snake[0][1]): running = False — but check_wall always returns None!return, not print.Bug 3 — Missing colon
def line must end with a : to open the function body.The starter code below already draws the snake and food. Your task: write a draw_score(surface, score, font) function that renders the score as text at position (8, 8) on screen.
# --- STARTER (already working) ---
def draw_snake(surface, snake):
for i, (col, row) in enumerate(snake):
colour = DKGREEN if i == 0 else GREEN
rect = (col*CELL_SIZE, row*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1)
pygame.draw.rect(surface, colour, rect)
def draw_food(surface, food_x, food_y):
rect = (food_x*CELL_SIZE, food_y*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1)
pygame.draw.rect(surface, RED, rect)
# --- YOUR TASK: write draw_score here ---
# Requirements:
# def draw_score(surface, score, font):
# * Render the text: font.render(f"Score: {score}", True, WHITE)
# * Draw it at top-left: surface.blit(text, (8, 8))
# --- Main loop calls all three ---
screen.fill(BG)
draw_snake(screen, snake)
draw_food(screen, food_x, food_y)
draw_score(screen, score, font) # <-- calls YOUR function
pygame.display.flip()
Write a complete game_over_screen(surface, score, font) function. It should:
- Clear the screen:
surface.fill(BG) - Render "GAME OVER" centered on screen using a large font size (36)
- Render the final score below it
- Update the display:
pygame.display.flip() - Wait 2 seconds:
pygame.time.wait(2000)
Useful trick: msg.get_rect(center=(WINDOW_W//2, WINDOW_H//2)) centres a rendered text surface.
Answer all 5 questions. You need 4 or more correct to continue.
Q1: def draw_snake(body): — what is body in this line?
Q2: What does a Python function return when it has no return statement?
Q3: Why define draw_snake(body) instead of pasting the drawing code everywhere in the main loop?
Q4: What is variable scope?
Q5: def is_collision(head, body): return head in body — what type does this function return?