I was trying to create Pac-man game and set random movement to enemies. But when I run it. It repeats same movements how can I stop the enemies making same movements and make enemies move all around the map. Please slove my problem and tell me why this thing happen. """"""""Press "space bar" so that the game starts""""""""
from turtle import Screen, Turtle
import random
FONT = ('Arial', 18, 'bold')
runs=False
cruns=False
notout=True
def computermove(game):
for i in game.enemy:
a=game.enemy[i]
random.choice([game.enemy[i].go_up(game),game.enemy[i].go_down(game),game.enemy[i].go_left(game),game.enemy[i].go_right(game)])
class Creator(Turtle):
def __init__(self, row, col, sprite, colorcode, width, height, game):
self.game=game
super().__init__(shape=sprite)
self.row = row
self.col = col
self.color(colorcode)
self.shapesize(width, height, 3)
self.penup()
self.speed('fastest')
self.goto(self.coords(row, col))
@staticmethod
def coords(row, col):
x = col * 25 - 250
y = 137.5 - row * 25
return x, y
@staticmethod
def inv_coords(x, y):
col = round((x + 250) / 25)
row = round((137.5 - y) / 25)
return row, col
def go_left1(self):
global runs
if runs==False:
runs=True
position = self.xcor() - 25, self.ycor()
key = self.inv_coords(*position)
if key not in self.game.wall:
if ((key in self.game.food)and(self.game.food[key].isvisible()==True)):
self.game.food[key].hideturtle()
self.game.score += 1
self.game.running.writer.clear()
self.game.running.writer.write("score:{}".format(self.game.score), font=FONT)
if self.game.score==102:
screen.clear()
self.game.running.writer.clear()
self.game.running.writer.goto(0,0)
slef.game.running.writer.write('game over')
self.goto(self.coords(*key))
runs=False
def go_right1(self):
global runs
if runs==False:
runs=True
position = self.xcor() + 25, self.ycor()
key = self.inv_coords(*position)
if key not in self.game.wall:
if ((key in self.game.food)and(self.game.food[key].isvisible()==True)):
self.game.food[key].hideturtle()
self.game.score += 1
self.game.running.writer.clear()
self.game.running.writer.write("score:{}".format(self.game.score), font=FONT)
if self.game.score==102:
screen.clear()
self.game.running.writer.clear()
self.game.running.writer.goto(0,0)
self.game.running.writer.write('game over')
self.goto(self.coords(*key))
runs=False
def go_up1(self):
global runs
if runs==False:
runs=True
position = self.xcor(), self.ycor() + 25
key = self.inv_coords(*position)
if key not in self.game.wall:
if ((key in self.game.food)and(self.game.food[key].isvisible()==True)):
self.game.food[key].hideturtle()
self.game.score += 1
self.game.running.writer.clear()
self.game.running.writer.write("score:{}".format(self.game.score), font=FONT)
if self.game.score==102:
screen.clear()
self.game.running.writer.clear()
self.game.running.writer.goto(0,0)
self.game.running.writer.write('game over')
self.goto(self.coords(*key))
runs=False
def go_down1(self):
global runs
if runs==False:
runs=True
position = self.xcor(), self.ycor() - 25
key = self.inv_coords(*position)
if key not in self.game.wall:
if ((key in self.game.food)and(self.game.food[key].isvisible()==True)):
self.game.food[key].hideturtle()
self.game.score += 1
self.game.running.writer.clear()
self.game.running.writer.write("score:{}".format(self.game.score), font=FONT)
if self.game.score==102:
screen.clear()
self.game.running.writer.clear()
self.game.running.writer.goto(0,0)
self.game.running.writer.write('game over')
self.goto(self.coords(*key))
runs=False
class Enemy_creator(Turtle):
def __init__(self, row, col, sprite, colorcode, width, height):
super().__init__(shape=sprite)
self.row = row
self.col = col
self.color(colorcode)
self.shapesize(width, height, 3)
self.penup()
self.speed('slowest')
self.goto(self.coords(row, col))
@staticmethod
def coords(row, col):
x = col * 25 - 250
y = 137.5 - row * 25
return x, y
@staticmethod
def inv_coords(x, y):
col = round((x + 250) / 25)
row = round((137.5 - y) / 25)
return row, col
def go_left(self,game):
global cruns, notout
if cruns==False:
cruns=True
self.game=game
position = self.xcor() - 25, self.ycor()
key = self.inv_coords(*position)
if position == (game.player[9,9].xcor(),game.player[9,9].ycor()):
screen.clear()
notout=False
if key not in game.wall:
self.goto(self.coords(*key))
cruns=False
def go_right(self,game):
global cruns,notout
if cruns==False:
cruns=True
self.game=game
position = self.xcor() + 25, self.ycor()
key = self.inv_coords(*position)
if position == (game.player[9,9].xcor(),game.player[9,9].ycor()):
screen.clear()
notout=False
if key not in game.wall:
self.goto(self.coords(*key))
cruns=False
def go_up(self,game):
global cruns,notout
if cruns==False:
cruns=True
self.game=game
position = self.xcor(), self.ycor() + 25
key = self.inv_coords(*position)
if position == (game.player[9,9].xcor(),game.player[9,9].ycor()):
screen.clear()
notout=False
if key not in game.wall:
self.goto(self.coords(*key))
cruns=False
def go_down(self,game):
global cruns,notout
if cruns==False:
cruns=True
self.game=game
position = self.xcor(), self.ycor() - 25
key = self.inv_coords(*position)
if position == (game.player[9,9].xcor(),game.player[9,9].ycor()):
screen.clear()
notout=False
if key not in game.wall:
self.goto(self.coords(*key))
cruns=False
class Running:
def __init__(self, game):
self.game = game
self.writer = Turtle(visible=False)
self.writer.penup()
self.writer.color('blue')
self.writer.goto(200, 170)
self.writer.write("score:{}".format(game.score), font=FONT)
screen.onkey(game.player[9, 9].go_left1, 'a')
screen.onkey(game.player[9, 9].go_right1, 'd')
screen.onkey(game.player[9, 9].go_up1, 'w')
screen.onkey(game.player[9, 9].go_down1, 's')
screen.onkey(self.movingcomputer,'space')
screen.onkey(self.exit,'q')
screen.listen()
def exit(self):
global notout
notout=False
exit()
def movingcomputer(self):
while notout:
computermove(self.game)
class Pacman:
def __init__(self):
self.wall = {}
self.player = {}
self.food = {}
self.enemy = {}
self.score = 0
screen.tracer(0)
self.setup()
screen.tracer(1)
self.running = Running(self)
def setup(self):
for row in range(11):
for col in range(20):
if level[row][col] == 1:
self.wall[(row, col)] = Creator(row, col, 'square', 'blue', 1, 1, self)
elif level[row][col] == 2:
color=random.choice(['red','green','skyblue','gray'])
self.enemy[(row, col)] = Enemy_creator(row, col, 'triangle', color, 1, 1)
elif level[row][col] == 3:
self.player[(row, col)] = Creator(row, col, 'circle', 'yellow', 1, 1, self)
else:
self.food[(row, col)] = Creator(row, col, 'circle', 'white', 0.1, 0.1, self)
level = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 1],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 2, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
screen = Screen()
screen.bgcolor('black')
game = Pacman()
screen.mainloop()