0

I've been trying to make a very basic ASCII game in Python, where it randomises a room (with a few things in it) and then you can move around the room. It's probably very inefficient but I've been using a list to store the room layout data, and then a function to print it in the fashion I want. However, for a reason I don't understand, the entire list gets cleared after I draw the room once. My code is as such:

import pygame
from pygame import *
pygame.init()
gridSize = 5
grid1=["@","D","E"]
for i in range((gridSize**2)-3):
    grid1.append("~")
random.shuffle(grid1)

def showGrid():
    grid2=grid1
    for i in range(gridSize):
        line=""
        for i in range(gridSize):
            line+=grid2[0]
            del grid2[0]
        print(line)
    print("\n")```
Derek T
  • 3
  • 2
  • 1
    `grid2=grid1` - this does not create a copy of the grid. Both `grid1` and `grid2` point to the same object in memory. Hence the effect you see. – Sergio Tulentsev Dec 21 '21 at 12:34

0 Answers0