K
K
KiringD2021-10-15 19:14:13
Python
KiringD, 2021-10-15 19:14:13

When you add an element to the list, do the rest of the elements change along with it?

I'm trying to create a banal snake, in one of the places I decided to use a list, but for some reason every time I add a new element, the entire list is replaced by this number. I am extremely sorry, I did not manage to repeat the mistake on a small example, so I give the whole code. (Adding to the list occurs in the del_tail() function. I also attach a screenshot

import msvcrt
import os
import time
import asyncio
import random


a=[
["#","#","#","#","#","#","#","#","#","#"],
["|","."," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["|"," "," "," "," "," "," "," "," ","|"],
["#","#","#","#","#","#","#","#","#","#"]]


async def update():
  global score
  global a
  # os.system("cls")
  print("Your score:", score)
  for i in range(len(a)):
    for b in a[i]:
      print(b,end=" ")
    print(" ")


async def check_dir():
  global direct
  if msvcrt.kbhit():
    print(2)
    input_char = msvcrt.getch()
    if ord(input_char) == 115:
      direct = "down"
    elif ord(input_char) == 119:
      direct = "up"
    elif ord(input_char) == 97:
      direct = "left"
    elif ord(input_char) == 100:
      direct = "right"

async def change_apple():
  global a
  global apple
  apple = [random.randint(1, 8),random.randint(1, 8)]
  a[apple[0]][apple[1]]="o"

async def del_tail(current,score):
  global a
  global snake

  snake.append(current)
  print(snake)

  

async def main():
  global direct
  global score
  global a
  global current
  while True:
    
    await asyncio.sleep(0.5)
    await check_dir()
    if direct == "right":
      if a[current[0]][current[1]+1]!="|":

        if a[current[0]][current[1]+1]=="o":
          score+=1
          await change_apple()
        await del_tail(current,score)
        
        a[current[0]][current[1]+1] = "."
        current[1] +=1

      await update()
    elif direct == "left":
      if a[current[0]][current[1]-1]!="|":

        if a[current[0]][current[1]-1]=="o":
          score+=1
          await change_apple()
        await del_tail(current,score)
        

        a[current[0]][current[1]-1] = "."
        current[1] -=1
      await update()
    elif direct == "up":
      if a[current[0]-1][current[1]]!="#":
        
        if a[current[0]-1][current[1]]=="o":
          score+=1
          await change_apple()
        await del_tail(current,score)
        
        a[current[0]-1][current[1]] = "."
        current[0] -=1
      await update()
    elif direct == "down":
      if a[current[0]+1][current[1]]!="#":
        
        if a[current[0]+1][current[1]]=="o":
          score+=1
          await change_apple()
        await del_tail(current,score)
        
        a[current[0]+1][current[1]] = "."
        current[0] +=1
      await update()


if __name__ == "__main__":
  score = 0
  current = [1,1]
  direct = "right"
  snake = list()
  apple = [random.randint(1, 8),random.randint(1, 8)]
  a[apple[0]][apple[1]]="o"
  loop = asyncio.get_event_loop()
  asyncio.ensure_future(main())
  loop.run_forever()


6169a8b61d8ea191037712.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-10-15
@KiringD

Snake has a reference to current , so changing current changes the value in snake .
The fix is ​​to put a copy of current in snake .

>>> current = [1,1]
>>> snake = []
>>> snake.append(current.copy())
>>>
>>> snake

>>> current[1] += 1
>>>
>>> current
[1, 2]
>>> snake

>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question