Confused beginner needs some advice

Hi, I’m trying to help my son getting started with Python 3.7 We follow a book on how to write simple games. The first game is super trivial. It draws an apple on the screen and if you click the apple it moves to a random location. If you miss (click somewhere else than on the apple) the game quits.

The code (from the book) looks like this:

apple=Actor("apple")
from random import randint

def draw():
    screen.clear()
    apple.draw()

def place_apple():
    apple.x=randint(10,800)
    apple.y=randint(10,600)
    
def on_mouse_down(pos):
    if apple.collidepoint(pos):
        print("Nice shot, shoot again!")
        place_apple()
    else:
        print("Oops, you missed!")
        quit()
        
place_apple()

This code works as expected, but why is the draw() function needed? I never call it, but without it the apple is never drawn.

Now, my son wish to add some more challenge to it and make the apple move randomly every 2 seconds (even when not being clicked).

I tried to add a timer and call the place_apple() function every two seconds (new code below). But for some reason it doesn’t redraw the apple in it’s new position. Why not? What am I missing?

apple=Actor("apple")
from random import randint
from threading import Timer

def draw():
    screen.clear()
    apple.draw()

def place_apple():
    apple.x=randint(10,800)
    apple.y=randint(10,600)
    
def on_mouse_down(pos):
    if apple.collidepoint(pos):
        print("Nice shot, shoot again!")
        place_apple()
    else:
        print("Oops, you missed!")
        quit()
        
#place_apple()

t=Timer(2.0, place_apple)
t.start()

Hello,
You’ve found the place for discussions about Python. While your program is written in Python, you’re also using some other piece of technology, which all of your questions seem to be about. From the code, it looks like you’re using Pygame Zero.
Unfortunately, not many people here will be familiar with that. To get better answers, you should try to find a place for discussing Pygame Zero, or even better, the particular book you’re following.

I’ve tried Pygame Zero once, so I recognized it, but my answers come from its documentation rather than practical experience. So, no guarantees on correctness:

Pygame Zero calls a function called draw automatically, see its documentation.

The docs linked above say that Pygame Zero only calls draw in certain specific situations. Timer is not included in that list. I assume that’s causing your issue.
threading.Timer comes from Python, and it’s probably not integrated with Pygame Zero, which means it’s on you to do things like notify the draw-calling code that something has changed.
Also, be aware that the `Timer’s action will only be triggered once, not every two seconds.
You will probably get better results with PyGame Zero’s Clock.

Hi and thanks for your help. You’re absolutley correct its PyGame Zero, but I didn’t realize that using a package like PyGame would mean that “conventional” Python won’t work (kind of odd writing a book for beginners on how to code in Python - and do it in a way that doesn’t work with Python :slight_smile:

I’ll look into the PyGame Zero documentation as you suggested.

Again thanks for the help!

Oh, it will work! You just need to combine the two correctly :‌)
This is a good discussion forum for Python issues, but not that much for issues specific to Pygame Zero. For the issue of combining with threading (which is part of Python itself), this forum might be the right place to ask, but you should mention you’re using Pygame Zero.