0

im trying to create a game in python where you need to shoot moving turtles above you, but i dont know how to make the attack detect the turtle and remember it. it also has a problem of when that i shoot the turtles that need to be hit stop moving.(im new to python).

##-----------Turtle setup-----------
from turtle import *
from turtle import Turtle, Screen
import random
from time import sleep
wn=Screen()
wn.setup(600,600)
wn.listen()
game=1
##-----------Player setup-----------
p=Turtle()
p.penup()
p.goto (0,-280)
p.left(90)
p.turtlesize(100)
##-----------Controls-----------
def left():
    if p.distance(-280, -280) > 1.0:
        p.setx( p.pos()[0] -5 )

def right():
    if p.distance(280, -280) > 1.0:
        p.setx( p.pos()[0] +5 )

def fire():
    l=Turtle()
    l.penup()
    l.hideturtle()
    l.goto(p.pos())
    l.showturtle()
    l.speed('slowest')
    l.setheading(90)
    l.pendown()
    l.forward(1000)
    l.clear()
    l.penup()
    if l.distance(0, 500) > 1.0:
        l.hideturtle
        l.goto(0,0)


wn.onkeypress(left, "a")
wn.onkeypress(right, "d")
wn.onkey(fire, "w")
##-----------Enemy setup----------
height1=random.randint(0,280)
e1=Turtle()
e1.penup()
e1.hideturtle()
e1.goto(-290,height1)

height2=random.randint(0,280)
e2=Turtle()
e2.penup()
e2.hideturtle()
e2.goto(290,height2)

height3=random.randint(0,280)
e3=Turtle()
e3.penup()
e3.hideturtle()
e3.goto(-290,height3)

height4=random.randint(0,280)
e4=Turtle()
e4.penup()
e4.hideturtle()
e4.goto(290,height4)

e1.showturtle()
e2.showturtle()
e3.showturtle()
e4.showturtle()
##-----------Enemy AI----------
def looper():
    print("hello")
hp1=1
while hp1 == 1:
    e1.setheading(0)
    e1.speed('slowest')
    e1.forward(590)
    sleep(5)
    e1.setheading(180)
    e1.forward(590)
    sleep(5)
if 
if game == 1:
    print("back")
    looper()
looper()

(a lot of the code doesnt function properly but i started this 3 days ago)

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

0

You can achieve getting the distance between 2 turtles with e1.distance(e2), but I recommend not using turtle for games. If you want to make a game in python I recommend using pygame which you can install with pip install pygame, but pygame can be a bit more complicated to use.

Ollieeeee
  • 13
  • 5