I’m new to Python and trying out Turtle through Tutorials. This question turned out to be verbose, my bad.
I have designed 2 Games handled by 2 Functions (in a separate module) which are called from MAIN –
F1 : Plays an Etch a Sketch Game where user inputs are used to move the Turtle to draw
F2 : Plays a Turtle Race where 5 Turtles (I have used a list to handle 5 Objects) move at randomized paces to see who wins.
The ISSUE : I tried mainloop() but that didn’t solve it, unless I used it wrong.
- The games are fine but I have a problem with the Screen when the Games are running one after the other, and went through numerous Questions before posting.
- I have initialized a create_screen() method in a separate config.py file which I import in each FN to create a screen. I then use exitonclick() to end the display. (Should I use Global – Just wanted to see how to make it work WITHOUT Global)
- The Games work fine individually on the first iteration but I have a loop in MAIN where user can choose games multiple times. On the second iteration, the screen pops up but I don’t see the Turtles and when I click I get Turtle Terminator error.
- I need FN1 to stop listening and the ask user if he wants FN2, then move to FN2 and I can’t seem to get that done without exitonclick() on FN1. How can I keep the screen alive, displayed, ready for FN2 ? It’s the only reason I am using Screen on config file and creating with FNs. I’m sure there is a way, I just don’t know it yet.
Solutions I have checked indicate the Turtle.RUNNING = True Flag which can be changed, but what is a better way to implement this without changing a Class Variable (or using Global) ? If someone can point me to the some sources (other than TURTLE DOCS) for Screen and Event Listeners to solve this, I’ll do the learning myself !
Code Skeleton : I’m loading config.py into FN Module and the Game Functions from MAIN
# Module : config.py
from turtle import Turtle, Screen
squad = []
for i in range(0, 5): # List of Turtle Objects available to all Modules
raphael = Turtle()
raphael.speed(0)
squad.append(raphael)
def create_screen():
"""Creates and Returns a screen Object"""
screen = Screen()
screen.screensize(400, 400)
screen.colormode(255)
return screen
# Module : gamefunction.py
from config import squad, create_screen
def etch_sketch_game():
"""Plays the Etch a Sketch Game"""
screen_fn = create_screen()
def forwards(): # Other FNs designed but not included for this Question
squad[0].fd(30)
screen_fn.listen()
screen_fn.onkey(key="w", fun=forwards)
screen_fn.exitonclick()
def turtle_race(): # Only adde
"""Plays the Turtle Race Game"""
screen_fn = create_screen()
squad[0].setpos(-200, 30)
squad[1].setpos(-200, 20)
squad[2].setpos(-200, 0)
squad[3].setpos(-200, -20)
squad[4].setpos(-200, -30)
def forwards(): # Other FNs designed but not included for this Question
squad[0].fd(30)
screen_fn.onkey(key="w", fun=forwards)
screen.exitonclick()