Skip to content

Latest commit

 

History

History
190 lines (165 loc) · 5.16 KB

File metadata and controls

190 lines (165 loc) · 5.16 KB

Week 11 – Student Tasks: Timed Coin Collector

Task A: Run and play

  1. Open week11.py in Thonny
  2. Press Run (F5)
  3. Try to collect as many coins as possible before time runs out
  4. Note your final score
  5. Play again and try to beat your score!

Task B: Make modifications

  1. Change game duration

    • Find: GAME_SECONDS = 20
    • Try: GAME_SECONDS = 30 (easier - more time)
    • Try: GAME_SECONDS = 10 (harder - less time)
    • Try: GAME_SECONDS = 60 (long game)
  2. Change movement speed

    • Find: STEP = 20
    • Try: STEP = 30 (move faster to collect more)
    • Try: STEP = 10 (slower, more challenging)
  3. Change collection distance

    • Find: if hero.distance(coin) < 25:
    • Try: < 40 (easier)
    • Try: < 15 (harder)
  4. Change colors

    • Find: coin.color("gold")
    • Try different colors: "red", "blue", "green"
    • Change hero color: hero.color("blue")

Task C: Add enhancements

  1. Show final score

    • Modify the game_over() function:
    def game_over():
        writer.goto(-120, 20)
        writer.write("GAME OVER", font=("Arial", 24, "bold"))
        writer.goto(-100, -20)
        writer.write(f"Final Score: {score}", font=("Arial", 18, "normal"))
  2. Add a win condition

    • Win if you reach score of 15 before time runs out:
    def check_collect():
        global score
        if check_time():
            game_over()
            return
        if hero.distance(coin) < 25:
            score += 1
            place_coin()
            if score >= 15:  # Win condition!
                writer.goto(-70, 0)
                writer.write("YOU WIN!", font=("Arial", 24, "bold"))
                return
        draw_ui()
  3. Add time warning

    • Change text color when time is running out:
    def draw_ui():
        remaining = max(0, int(GAME_SECONDS - (time.time() - start_time)))
        writer.clear()
        
        # Warning when less than 5 seconds
        if remaining <= 5 and remaining > 0:
            writer.color("red")
            writer.write(f"Score: {score}   Time: {remaining}  HURRY!", 
                        font=("Arial", 16, "bold"))
            writer.color("black")
        else:
            writer.write(f"Score: {score}   Time: {remaining}", 
                        font=("Arial", 16, "normal"))
  4. Count-up timer

    • Show elapsed time instead of countdown:
    def draw_ui():
        elapsed = int(time.time() - start_time)
        writer.clear()
        writer.write(f"Score: {score}   Time: {elapsed}s", 
                    font=("Arial", 16, "normal"))

Task D: Creative challenges

  1. Difficulty levels

    • Add this at the start:
    print("Choose difficulty:")
    print("1 = Easy (30 seconds)")
    print("2 = Normal (20 seconds)")
    print("3 = Hard (10 seconds)")
    choice = input("Enter 1, 2, or 3: ")
    
    if choice == "1":
        GAME_SECONDS = 30
    elif choice == "3":
        GAME_SECONDS = 10
    else:
        GAME_SECONDS = 20
  2. Shrinking coin challenge

    • Coin gets smaller as time passes:
    def place_coin():
        coin.goto(random.randint(-320, 320), random.randint(-200, 200))
        time_passed = time.time() - start_time
        size = max(0.5, 1.5 - (time_passed / GAME_SECONDS))
        coin.shapesize(size, size)
  3. Moving coin

    • Coin moves on its own every few seconds:
    last_move = time.time()
    
    def check_collect():
        global score, last_move
        if check_time():
            game_over()
            return
        
        # Auto-move coin every 3 seconds
        if time.time() - last_move > 3:
            place_coin()
            last_move = time.time()
        
        if hero.distance(coin) < 25:
            score += 1
            place_coin()
        draw_ui()
  4. Multiple coins with timer

    • Combine week 10's multiple coins with timer:
    # Create list of 5 coins
    coins = []
    for _ in range(5):
        c = turtle.Turtle()
        c.shape("circle")
        c.color("gold")
        c.penup()
        c.goto(random.randint(-320, 320), random.randint(-200, 200))
        coins.append(c)
    
    # Check all coins
    def check_collect():
        global score
        if check_time():
            game_over()
            return
        for coin in coins:
            if hero.distance(coin) < 25:
                coin.goto(random.randint(-320, 320), random.randint(-200, 200))
                score += 1
                break
        draw_ui()
  5. High score tracking

    • Keep track of best score:
    high_score = 0
    
    def game_over():
        global high_score
        if score > high_score:
            high_score = score
        writer.goto(-120, 20)
        writer.write("GAME OVER", font=("Arial", 24, "bold"))
        writer.goto(-140, -20)
        writer.write(f"Score: {score}  High: {high_score}", 
                    font=("Arial", 16, "normal"))

Success checklist

  • I can play the timed game and get a score
  • I understand how time.time() tracks elapsed time
  • I changed the game duration successfully
  • I added at least one enhancement (final score, win condition, or warning)
  • I can beat my previous high score!