This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Banele's profile picture

Help: Python 3 turtle graphics

Started by Banele, 17 September 2016 - 12:13 PM
Banele #1
Posted 17 September 2016 - 02:13 PM
Good Day.
I need help on creating a simulator of a patently shootout. It's a simple program which shows a player shooting at a goalie (random angle), the goalie either saves it or not. depending on the results the user is sent a feedback (i.e. GOAL!/SAVE!)
Please help me in pointing out what is wrong with my program or being kind enough to write a correct program.


import turtle 
import random
from math import ceil

#turtle.setup(600, 600)
wn = turtle.Screen()
wn.title("Robocup demo")
wn.bgcolor("lightgreen")

ball = turtle.Turtle()
ball.penup()
ball.shape("circle")
ball.shapesize(2)
ball.fillcolor("white")
ball.speed("fastest")
ball.setheading(180)

goalie = turtle.Turtle()
goalie.penup()
goalie.shape("circle")
goalie.shapesize(2)
goalie.fillcolor("red")
goalie.speed("fastest")
goalie.setheading(90)
goalie.goto(-300, 0)

def doNothing():
    return

def aim():
    wn.onkey(doNothing, "space")
    ball.home()
    ball.setheading(180)
    angle = random.randrange(0, 46)
    direction = random.randrange(0,2)
    if direction == 0: # left
        ball.left(angle)
    else:
        ball.right(angle)
    shoot()

def shoot():
    goalieStep = 2
    #print("b {0}, g {1}".format(ceil(ball.position()[1]), ceil(goalie.position()[1])))
    if ball.position()[0] <= -300:
        if ceil(goalie.position()[1]) == ceil(ball.position()[1]):
            goalie.write("save")
        else:
            ball.write("score")
        wn.ontimer(doNothing)
        wn.onkey(aim, "space")
    else:
        # Insert AI logic here
        goalie.forward(goalieStep)
        ball.forward(4)
        wn.ontimer(shoot, 100)

wn.onkey(aim, "space")
wn.listen()
wn.mainloop()
Lyqyd #2
Posted 17 September 2016 - 02:24 PM
This forum is intended to provide help with use of the ComputerCraft mod for Minecraft. I'll leave your post, as there may also be people here who know enough about Python and the turtle library you're using to help you, but be aware that you're likely to be better off asking your question on a python forum.
Banele #3
Posted 17 September 2016 - 02:26 PM
Any help is accepted. Thank You friend