Python 乌龟在移动时留下点

Python turtle leaves dots while moving

我正在尝试使用乌龟制作吃豆人游戏。当我 运行 我的代码显示小点。乌龟移动的时候按w向上移动可以找到玩家底部的小圆点

from turtle import *

class creator(Turtle):
    def __init__(self, row, col, sprit, colorcode, width, height):
        Turtle.__init__(self)
        self.row=row
        self.col=col
        self.colorcode=colorcode
        self.sprit=sprit
        x, y=self.coords(row, col)
        self.color(colorcode)
        self.shape(sprit)
        self.shapesize(width, height, 0)
        self.pu()
        self.speed(0)
        self.goto(x, y)

    def coords(self, row, col):
        x=(-250+(col*25))
        y=(137.5-(row*25))
        return x, y

    def left(self):
        if ((self.xcor()-25, self.ycor()) not in game.running.walls):
            if ((self.xcor()-25, self.ycor()) in game.running.foods):
                foodcol=int(((self.xcor()-25)+250)/25)
                foodrow=int((137.5-self.ycor())/25)
                game.food[foodrow, foodcol].hideturtle()
                game.score+=1
                game.running.writer.clear()
                game.running.writer.write('score:{}'.format(game.score), font=('Arial',18,'bold'),move=False)
            self.goto(self.xcor()-25, self.ycor())

    def right(self):
        if ((self.xcor()+25, self.ycor()) not in game.running.walls):
            if ((self.xcor()+25,self.ycor()) in game.running.foods):
                foodcol=int(((self.xcor()+25)+250)/25)
                foodrow=int((137.5-self.ycor())/25)
                game.food[foodrow,foodcol].hideturtle()
                game.score+=1
                game.running.writer.clear()
                game.running.writer.write('score:{}'.format(game.score), font=('Arial', 18, 'bold'), move=False)
            self.goto(self.xcor()+25, self.ycor())

    def up(self):
        if ((self.xcor(), self.ycor()+25) not in game.running.walls):
            if ((self.xcor(), self.ycor()+25) in game.running.foods):
                foodcol=int(((self.xcor())+250)/25)
                foodrow=int((137.5-(self.ycor()+25))/25)
                game.food[foodrow, foodcol].hideturtle()
                game.score+=1
                game.running.writer.clear()
                game.running.writer.write('score:{}'.format(game.score), font=('Arial', 18, 'bold'), move=False)
            self.goto(self.xcor(), self.ycor()+25)

    def down(self):
        if ((self.xcor(), self.ycor()-25) not in game.running.walls):
            if ((self.xcor(), self.ycor()-25) in game.running.foods):
                foodcol=int(((self.xcor())+250)/25)
                foodrow=int((137.5-(self.ycor()-25))/25)
                game.food[foodrow, foodcol].hideturtle()
                game.score+=1
                game.running.writer.clear()
                game.running.writer.write('score:{}'.format(game.score), font=('Arial', 18, 'bold'), move=False)
            self.goto(self.xcor(), self.ycor()-25)
class running:
    def __init__(self, game):
        self.game=game
        self.writer=Turtle(visible=False)
        self.writer.pu()
        self.writer.color('blue')
        self.writer.goto(240, 140)
        print('righthere')
        self.writer.write('score:{}'.format(game.score), font=('Arial', 18, 'bold'), move=False)
        self.walls=[]
        self.foods=[]
        self.setup(game)
        listen()
        onkey(game.player[9, 9].left, 'a')
        onkey(game.player[9, 9].right, 'd')
        onkey(game.player[9, 9].up, 'w')
        onkey(game.player[9, 9].down, 's')

    def setup(self, game):
        self.game=game
        for row in range(11):
            for col in range(20):
                if level[row][col]==1:
                    self.walls.append((game.wall[row, col].xcor(), game.wall[row, col].ycor()))
##                elif level[row][col]==2:
##                    self.enemy[(row, col)]=creator(row, col, 'triangle', 'red',1,1)
##                elif level[row][col]==3:
##                    self.player[(row, col)]=creator(row, col, 'circle', 'yellow',1,1)
                elif level[row][col]==0:
                    self.foods.append((game.food[row, col].xcor(), game.food[row,col].ycor()))
##                
class pacman:
    def __init__(self):
        self.wall={}
        self.player={}
        self.food={}
        self.enemy={}
        self.score=0
        win.tracer(False)
        self.setup()
        win.tracer(True)
        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)
                elif level[row][col]==2:
                    self.enemy[(row, col)]=creator(row, col, 'triangle', 'red',1,1)
                elif level[row][col]==3:
                    self.player[(row, col)]=creator(row,col, 'circle', 'yellow',1,1)
                else:
                    self.food[(row,col)]=creator(row, col, 'circle', 'white',0.1,0.1)

        
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, 0, 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, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 1, 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, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

win =Screen()
win.bgcolor('black')
game=pacman()
win.mainloop()

我几乎对您程序的每个方面都进行了重新设计,但无法找出黄色工件(pacman 本身的一部分)的罪魁祸首。因此,我通过留下 tracer() 对代码进行了创可贴关闭整个程序并进行显式屏幕 update() 调用。

[编辑:@AnnZen 的回答确定了故障的来源。]

我还通过检查原始 dict 而不是 list 缓存来替换您的冗余实体位置逻辑。这简化了逻辑,并消除了一些潜在的浮点问题。而且,查字典实际上比查清单更快。

加上许多其他更改来简化和稳定代码:

from turtle import Screen, Turtle

FONT = ('Arial', 18, 'bold')

class 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)
        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_left(self):
        screen.onkey(None, 'a')  # disable handler inside handler

        position = self.xcor() - 25, self.ycor()

        key = self.inv_coords(*position)

        if key not in game.wall:

            if key in game.food:
                game.food[key].hideturtle()
                game.increment_score()

            self.goto(self.coords(*key))
            screen.update()

        screen.onkey(self.go_left, 'a')

    def go_right(self):
        screen.onkey(None, 'd')

        position = self.xcor() + 25, self.ycor()

        key = self.inv_coords(*position)

        if key not in game.wall:

            if key in game.food:
                game.food[key].hideturtle()
                game.increment_score()

            self.goto(self.coords(*key))
            screen.update()

        screen.onkey(self.go_right, 'd')

    def go_up(self):
        screen.onkey(None, 'w')

        position = self.xcor(), self.ycor() + 25

        key = self.inv_coords(*position)

        if key not in game.wall:

            if key in game.food:
                game.food[key].hideturtle()
                game.increment_score()

            self.goto(self.coords(*key))
            screen.update()

        screen.onkey(self.go_up, 'w')

    def go_down(self):
        screen.onkey(None, 's')

        position = self.xcor(), self.ycor() - 25

        key = self.inv_coords(*position)

        if key not in game.wall:

            if key in game.food:
                game.food[key].hideturtle()
                game.increment_score()

            self.goto(self.coords(*key))
            screen.update()

        screen.onkey(self.go_down, 's')

class Running:
    def __init__(self, game):
        self.game = game

        self.writer = Turtle(visible=False)
        self.writer.penup()
        self.writer.color('blue')
        self.writer.goto(240, 140)
        self.writer.write("score:{}".format(game.score), font=FONT)

##      self.setup()

        screen.onkey(game.player[9, 9].go_left, 'a')
        screen.onkey(game.player[9, 9].go_right, 'd')
        screen.onkey(game.player[9, 9].go_up, 'w')
        screen.onkey(game.player[9, 9].go_down, 's')
        screen.listen()

    def setup(self):

        for row in range(11):
            for col in range(20):
                if level[row][col] == 0:
                    pass
                elif level[row][col] == 1:
                    pass
##              elif level[row][col] == 2:
##                  self.enemy[(row, col)] = Creator(row, col, 'triangle', 'red', 1, 1)
##              elif level[row][col] == 3:
##                  self.player[(row, col)] = Creator(row, col, 'circle', 'yellow', 1, 1)

class Pacman:
    def __init__(self):
        self.wall = {}
        self.player = {}
        self.food = {}
        self.enemy = {}

        self.score = 0

        screen.tracer(False)
        self.setup()
        screen.update()
        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)
                elif level[row][col] == 2:
                    self.enemy[(row, col)] = Creator(row, col, 'triangle', 'red', 1, 1)
                elif level[row][col] == 3:
                    self.player[(row, col)] = Creator(row, col, 'circle', 'yellow', 1, 1)
                else:
                    self.food[(row, col)] = Creator(row, col, 'circle', 'white', 0.1, 0.1)

    def increment_score(self):
        self.score += 1
        self.running.writer.clear()
        self.running.writer.write("score:{}".format(self.score), font=FONT)

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, 0, 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, 0, 1, 0, 1],
    [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 1, 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, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 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()

修复非常简单,几乎没有什么不便:

你看,这是turtle.shapesize方法中的一个小故障,你有

self.shapesize(width, height, 0)

0 使方法出错。相反,省略 0,或使用大纲的默认值 None:

self.shapesize(width, height)

self.shapesize(width, height, None)

不需要tracer创可贴!