有人可以解释刚刚发生了什么(Python)

时间:2015-05-19 20:36:19

标签: python list pygame artificial-intelligence

我正在研究我的AI寻路(你不需要了解), 出于某种原因,当我在shell中执行此操作时,我的On [3]列表正在扩展:

tempList.append([On[1]-1]) (程序搞砸之后。)为什么?

该计划没有崩溃,但这不是我的问题。 截图(忽略额外的打印,我试图缩小导致它的代码。)The problem...

On[1]是我的Y坐标。

有问题的代码位于#Find Path (在底部。) 我的代码(超过200行。:/)



# Setup Python ----------------------------------------------- #
import pygame, sys, random, time, webbrowser, os
from datetime import datetime
# Version ---------------------------------------------------- #
Version = '1.0'
# Setup pygame/window ---------------------------------------- #
x = 100
y = 100
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Pathfinding '+(Version)+'')
WINDOWWIDTH = 200
WINDOWHEIGHT = 200
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.NOFRAME)
# Font ------------------------------------------------------- #
basicFont = pygame.font.SysFont(None, 20)
# Images ----------------------------------------------------- #
# Audio ------------------------------------------------------ #
# Colors ----------------------------------------------------- #
WHITE = (255,255,255)
BLACK = (0,0,0)
GRAY3 = (105,105,105)
GRAY = (195,195,195)
GRAY2 = (127,127,127)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
GOLD = (255,215,0)
PURPLE = (115,0,242)
# Variables -------------------------------------------------- #
Map = ['0000000000',
       '0300000000',
       '0000000000',
       '0200000000',
       '0000000000',
       '0000000000',
       '0000000000',
       '0000000000',
       '0000000000',
       '0000000000']
Column = 0
Row = 0
Nodes = {}
for whatevs in Map:
    for whatevs2 in Map[Row]:
        Nodes[''+(str(Column))+','+(str(Row))+''] = [Column,Row,int(whatevs2)]
        if whatevs2 == '3':
            On = [Column,Row,[[Column,Row]]]
        Column += 1
        if Column == 10:
            Column = 0
            Row += 1
Open = {}
Closed = {}
# Rects ------------------------------------------------------ #
# Defenitions ------------------------------------------------ #
def Distance(Location,End):
    if Location != []:
        if int(Location[0]) < End[0]:
            Dist = End[0] - int(Location[0])
        else:
            Dist = int(Location[0]) - End[0]
        if int(Location[1]) < End[1]:
            Dist2 = End[1] - int(Location[1])
        else:
            Dist2 = int(Location[1]) - End[1]
        Dist += Dist2
        if Location[2] == 1:
            return 100000
        elif Location[2] == 2:
            return 0
        else:
            return Dist
    else:
        return 100000
# FPS -------------------------------------------------------- #
FPS = 80
TrueFPSCount = 0
TrueFPS = 0
fpsOn = False
PrevNow = 0
# Text -------------------------------------------------------- #
def drawText(text, font, color, surface, x, y):
    textobj = font.render(text, 1, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    screen.blit(textobj, textrect)
# Loop ------------------------------------------------------- #
while True:
    # Black Screen ------------------------------------------- #
    screen.fill(BLACK)
    # Show Nodes --------------------------------------------- #
    for Node in Nodes:
        Rect = pygame.Rect(Nodes[Node][0]*20,Nodes[Node][1]*20,20,20)
        if Nodes[Node][2] == 0:
            pygame.draw.rect(screen,WHITE,Rect)
        elif Nodes[Node][2] == 1:
            pygame.draw.rect(screen,BLUE,Rect)
        elif Nodes[Node][2] == 2:
            pygame.draw.rect(screen,GREEN,Rect)
            End = [Nodes[Node][0],Nodes[Node][1]]
        else:
            pygame.draw.rect(screen,RED,Rect)
    for Node in Closed:
        Rect = pygame.Rect(Closed[Node][0]*20,Closed[Node][1]*20,20,20)
        pygame.draw.rect(screen,(0,100,200),Rect)
    Rect2 = pygame.Rect(On[0]*20,On[1]*20,20,20)
    pygame.draw.rect(screen,(0,200,100),Rect2)
    if [On[0],On[1]] == End:
        print('Completed.')
        print(On[2])
        input()
        time.sleep(3)
        pygame.quit()
        sys.exit()
    # Find Path ---------------------------------------------- #
    Top = []
    Bottom = []
    Right = []
    Left = []
    Closed[''+(str(On[0]))+','+(str(On[1]))+''] = [On[0],On[1]]
    try:
        Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][0])
        Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][1])
        Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][2])
        tempList = []
        tempList = On[2]
        print(On)
        tempList.append([On[0],On[1]-1])
        print(On)
        Top.append(tempList)
        for item in Closed:
            if Top != []:
                if item == ''+(str(Top[0]))+','+(str(Top[1]))+'':
                    Top = []
    except NameError:
        pass
    except KeyError:
        pass
    try:
        Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][0])
        Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][1])
        Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][2])
        tempList = []
        tempList = On[2]
        print('?')
        print(On)
        tempList.append([On[0],On[1]+1])
        print(On)
        print()
        Bottom.append(tempList)
        print('On')
        print(On)
        for item in Closed:
            if Bottom != []:
                if item == ''+(str(Bottom[0]))+','+(str(Bottom[1]))+'':
                    Bottom = []
    except NameError:
        pass
    except KeyError:
        pass
    try:
        Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][0])
        Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][1])
        Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][2])
        tempList = []
        tempList = On[2]
        tempList.append([On[0]+1,On[1]])
        Right.append(tempList)
        for item in Closed:
            if Right != []:
                if item == ''+(str(Right[0]))+','+(str(Right[1]))+'':
                    Right = []
    except NameError:
        pass
    except KeyError:
        pass
    try:
        Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][0])
        Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][1])
        Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][2])
        tempList = []
        tempList = On[2]
        tempList.append([On[0]-1,On[1]])
        Left.append(tempList)
        for item in Closed:
            if Left != []:
                if item == ''+(str(Left[0]))+','+(str(Left[1]))+'':
                    Left = []
    except NameError:
        pass
    except KeyError:
        pass
    if Top != []:
        Open[''+(str(Top[0]))+','+(str(Top[1]))+''] = [Distance(Top,End),Top[0],Top[1],Top[3]]
    if Bottom != []:
        print(':D')
        print(On)
        Open[''+(str(Bottom[0]))+','+(str(Bottom[1]))+''] = [Distance(Bottom,End),Bottom[0],Bottom[1],Bottom[3]]
    if Right != []:
        Open[''+(str(Right[0]))+','+(str(Right[1]))+''] = [Distance(Right,End),Right[0],Right[1],Right[3]]
    if Left != []:
        Open[''+(str(Left[0]))+','+(str(Left[1]))+''] = [Distance(Left,End),Left[0],Left[1],Left[3]]
    Lowest = [0,0]
    LowestNum = 100000
    try:
        del Open[''+(str(On[0]))+','+(str(On[1]))+'']
    except KeyError:
        pass
    for Place in Open:
        if Open[Place][0] < LowestNum:
            LowestNum = Open[Place][0]
            Lowest = [Open[Place][1],Open[Place][2],Open[Place][3]]
    On = Lowest
    # FPS ---------------------------------------------------- #
    NewSec = False
    TrueFPSCount += 1
    now = datetime.now()
    now = now.second
    if PrevNow != now:
        PrevNow = now
        NewSec = True
        TrueFPS = TrueFPSCount
        TrueFPSCount = 0
        TrueFPS = str(TrueFPS)
    # Buttons ------------------------------------------------ #
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == ord('x'):
                if fpsOn == True:
                    fpsOn = False
                elif fpsOn == False:
                    fpsOn = True
    # Update ------------------------------------------------- #
    if fpsOn == True:
        drawText('FPS:'+(TrueFPS)+'', basicFont, WHITE, screen, 500,12)
    pygame.display.update()
    mainClock.tick(10)
&#13;
&#13;
&#13;

我不使用课程或精灵。 :P 谢谢你看看!

1 个答案:

答案 0 :(得分:1)

因为您的On[-1]tempList是同一个对象。这就是它如何与相同的对象一起工作:

>>> x=[1,2,3]
>>> y=x
>>> y.append(10)
>>> x
[1, 2, 3, 10]
>>> y
[1, 2, 3, 10]
>>> 
相关问题