'What's the fastest way to move every turtle to another turtles position in another list?
So I tried creating Pacman but I ran into a problem with the red and blue ghosts. "These means the ghosts which die when you touch and the ones that kill you". So I thought I would use this video https://www.youtube.com/watch?v=Ve-BvoCVN40 but then I realized that I got no clue how I would move 1 turtle to another turtles position which is in a different list. "In quick I want to move all red turtles to their counterpart blue turtles' position"
Below ive Provided my current code.
import turtle
import time
import random
import threading
delay = 0.1 #computer run at different speed
#Screen Below Video 1
wn = turtle.Screen()
wn.title("Pacman Arcade Game")
wn.bgcolor("gray")
wn.setup(width=600, height=600)
wn.tracer(0) #turns off the screen updates
#Pacman
Pac = turtle.Turtle()
Pac.speed(0) #Anaimations speed fastest
Pac.shape("square")
Pac.color("black")
Pac.penup()
Pac.goto(0,0)
Pac.directon = "stop"
#berry
Berry = turtle.Turtle()
Berry.speed(0) #Anaimations speed fastest
Berry.shape("circle")
Berry.color("cyan")
Berry.penup()
Berry.goto(0, -100)
#Lists all of the red ghosts "The one which the player can kill"
# Choose a number of enemies
number_of_enemies = 4
# Create an empty list of enemies
enemiesBlue = []
Ghost = turtle.Turtle()
# Add ememies to the list
for i in range(number_of_enemies):
#Create the enemy
enemiesBlue.append(turtle.Turtle()) #appends turtle to the list
for Ghost in enemiesBlue:
Ghost.speed(0) # Anaimations speed fastest
Ghost.shape("circle")
Ghost.color("blue")
Ghost.penup()
x = random.randint(-60, 60)
y = random.randint(0, 0)
Ghost.goto(x, y)
Ghost.pos()
#Ghost Red
GhostR = Ghost.clone()
GhostR.speed(0) #Anaimations speed fastest
GhostR.shape("circle")
GhostR.color("red")
GhostR.penup()
GhostR.goto(0,1000)
# Choose a number of enemies
number_of_enemies = 4
# Create an empty list of enemies
enemiesRed = []
Ghost = turtle.Turtle()
# Add ememies to the list
for i in range(number_of_enemies):
#Create the enemy
enemiesRed.append(turtle.Turtle()) #appends turtle to the list
for GhostR in enemiesRed:
GhostR = Ghost.clone()
GhostR.speed(0) # Anaimations speed fastest
GhostR.shape("circle")
GhostR.color("red")
GhostR.penup()
GhostR.goto(0, 1000)
#fuctions
def go_up():
if Pac.directon !="g": #Prevents you from going right if you are facing the oppistie direction "stops you fronm phasing"
Pac.directon = "up"
def go_down():
if Pac.directon !="h": #Prevents you from going right if you are facing the oppistie direction "stops you fronm phasing"
Pac.directon = "down"
def go_left():
if Pac.directon !="right": #Prevents you from going right if you are facing the oppistie direction "stops you fronm phasing"
Pac.directon = "left"
def go_right():
if Pac.directon !="left": #Prevents you from going right if you are facing the oppistie direction "stops you fronm phasing"
Pac.directon = "right"
def move():
if Pac.directon == "up":
y = Pac.ycor()
Pac.sety(y + 20)
if Pac.directon == "down":
y = Pac.ycor()
Pac.sety(y - 20)
if Pac.directon == "left":
x = Pac.xcor()
Pac.setx(x - 20)
if Pac.directon == "right":
x = Pac.xcor()
Pac.setx(x + 20)
#Keyboard bindings "connetc key press with fuction"
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
global my_timer
my_timer = 5
while True:
wn.update()
for Ghost in enemiesBlue:
if Pac.distance(Berry) < 20 and Pac.distance(GhostR) > 1000:
GhostR.goto(Ghost.pos())
Ghost.goto(1, 1000)
x = random.randint(-290,290)
y = random.randint(-290,290)
Berry.goto(x,y)
# Timer Code
def countdown():
global my_timer
for x in range(5):
my_timer = my_timer - 1
time.sleep(1)
countdown_thread = threading.Thread(target = countdown)
countdown_thread.start()
if my_timer == 0:
Ghost.goto(GhostR.pos())
GhostR.goto(0, 2000)
my_timer = 5
if Pac.distance(Ghost) < 20:
Pac.goto(200, 100)
Pac.directon ="stop"
if Pac.distance(GhostR) < 20:
GhostR.goto(0, 0)
#check for collision with the border
if Pac.xcor()>290 or Pac.xcor()<-290 or Pac.ycor()>290 or Pac.ycor()<-290:
time.sleep(1)
Pac.goto(0,0)
Pac.directon = "stop"
move()
time.sleep(delay)
wn.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
