I have a problem updating self.tiles[][].type in line 29. Once leaving the for loops in Map's __init__, the tiles list "forgets" the update somehow.
- I've tried adding a setter function for "type" in Tile but that didn't work also tried making the "type" a class level variable but that didn't and wouldn't help anyway.
- I don't suspect its something to do with reference since I'm only working on the instances and never go to the class for any variables.
- It might be that the tiles array in map doesn't contain instances in the way I think they do, but idk.
Would appreciate if anyone tell me why it doesn't update the tiles list after the for loops in __init__.
(debugging print's included) -
import tkinter as tk
-
from tkinter import *
-
-
class Tile:
-
def __init__(self, type, pos = (0, 0), isPath = False): #, cost):
-
self.type = type
-
self.pos = pos
-
self.isPath = isPath
-
#self.cost = cost
-
-
def getColor(self):
-
if (self.isPath): # Tile is a part of path
-
return "red"
-
elif (self.type == "."): # Traversable tile
-
return "white"
-
elif (self.type == "#"): # Intraversable tile
-
return "brown"
-
-
class Map:
-
def __init__(self, filename):
-
self.width = len(open(filename, "r").readline()) - 1 # Find size of first line
-
self.height = sum(1 for line in open(filename)) # Find nr of lines
-
self.tiles = [[Tile(".")] * self.width] * self.height # Ready tile
-
-
print("Map.tiles 2d list content:")
-
-
with open(filename, "r") as f: # Open given file
-
print("Updating in __init__:")
-
for y, line in enumerate(f): # For every line
-
for x, letter in enumerate(line): # For every letter
-
if (letter != "\n"): # Ignore newline
-
self.tiles[y][x].type = letter # Update tile type
-
print(self.tiles[y][x].type, end = "")
-
self.tiles[y][x].pos = (x, y) # Tell tile where it is on map
-
if (letter == "A"): # Save start index
-
self.start = (x, y)
-
elif (letter == "B"): # Save goal index
-
self.goal = (x, y)
-
print("")
-
print("")
-
-
print("Just after updating in __init__:")
-
for row in self.tiles:
-
for tile in row:
-
print(tile.type, end = "")
-
print("")
-
print("")
-
-
def AStar(self, start, goal):
-
print("")
-
-
def drawMap(self, canvas):
-
print("When displaying on screen in drawMap:")
-
tileDim = (canvas.winfo_width() / self.width, canvas.winfo_height() / self.height) # Find tile size
-
for y, row in enumerate(self.tiles): # For every row
-
for x, tile in enumerate(row): # For every column in said row
-
canvas.create_rectangle(tileDim[0] * x, # Draw tile
-
tileDim[1] * y,
-
(tileDim[0] * x) + tileDim[0],
-
(tileDim[1] * y) + tileDim[1],
-
fill = tile.getColor())
-
print(tile.type, end = "")
-
print("")
-
-
root = tk.Tk()
-
root.title("A* Pathfinding")
-
root.geometry("720x480")
-
-
canvas = tk.Canvas(root, width = root.winfo_width(), height = root.winfo_height())
-
canvas.pack(side="top", fill = "both", expand = True)
-
canvas.update()
-
-
obj = Map("./boards/board-1-1.txt")
-
obj.drawMap(canvas)
-
-
root.mainloop()
-
Output (terminal): -
Map.tiles 2d list content:
-
Updating in __init__:
-
....................
-
....................
-
.........######.....
-
...........A..#..B..
-
.........######.....
-
....................
-
....................
-
-
Just after updating in __init__:
-
....................
-
....................
-
....................
-
....................
-
....................
-
....................
-
....................
-
-
When displaying on screen in drawMap:
-
....................
-
....................
-
....................
-
....................
-
....................
-
....................
-
....................
-
Solved it by making the "self." variables in Map global using the "global" key word (and removing the "self." of course). It might not be a good thing, but it works
3 1419
Some simple print statements should show the problem. For example - self.tiles[y][x].type = letter
-
print(self.tiles[y][x])
-
-
## this should likely be
-
self.tiles[y][0][0].type
-
""" as the class instance is in the first position,
-
offset=0, of the sub-list, and the first/only position
-
in that sub-list. print() self.tiles[y][0][0] and/or
-
self.tiles[y][0][0].type to test for yourself
-
self.tiles = [[Tile(".")] * self.width] * self.height
-
"""
-
-
## A dictionary would work better here IMHO
-
self.tiles={}
-
self.tiles[Tile(".")]=self.width] * self.height
-
Appreciate the answer but...
I tried printing with self.tiles[y][0][0] with and without .type and it says "Tile object does not support indexing". The [][][] tries to fetch the a position within a 3d list, but since [[] * width] * height makes a 2d list this is understandable, so the object lies in the inner list.
As far as I understand the code should be correct (coming from a c++/java programming background, which of course doesn't mean that python behaves the same)
But the main problem is that the the 2d list "tiles" looses it's updated state once exiting the for loops updating them (in __init__). I have a print out within the same function and yet it prints only dots... so I suspect it might be something with the self and it not being a Tile object but something else, OR, it might be something with the list (or the initialization of it) that doesn't allow me to update them.
Solved it by making the "self." variables in Map global using the "global" key word (and removing the "self." of course). It might not be a good thing, but it works
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dasod |
last post by:
I would like to know if my method to remove list objects is correct in
this small test program. It seems to me that there might be a simplier
way, but I'm afraid I don't know enough about list...
|
by: claire.bell1 |
last post by:
Hi,
Im having problems initialising class member objects in the class's
constructor
e.g. My program isnt about monkeys, but there is too much code to post here.
class monkey
{
private:
int...
|
by: Eric |
last post by:
I have a static class member variable as follows:
struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};
int A::v; // define A::v in the cpp file
|
by: jt |
last post by:
Being a newbie in C++ and comming from C, I can't find information on how to
access a class member function outside its class.
Below is a snippet of the class and its member function: look at...
|
by: Just D |
last post by:
Hi,
Does anybody know is it possible to get the list of the public members of
the class during run-time using some trick if the class is, say, unknown or
I just want to write some universal...
|
by: Crirus |
last post by:
I have some question related to the other post about iteration on a class
members.
I had found a way to figure out when a member of my class is array so I can
pull out it's elements to iterate...
|
by: WXS |
last post by:
Vote for this idea if you like it here:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
|
by: .rhavin grobert |
last post by:
i have (do try to have?) the following...
& = breakpoints in debugger
// ----------------------------------------------------------------
// cx.h
class CX {
public:
CX(CX* pcx = NULL);...
|
by: stephen b |
last post by:
(apologies for cross posting from the moderated group..i'm sure you
understand)
Hello, I'm passing an array into a Constructor and hoping to use it
as
a pointer and store it as a class member...
|
by: qambary |
last post by:
In this Assignment, you will design a class member Type.
A. Each object of member Type can hold the name of a person, member ID, number of books bought, and amount spent.
B. Include the member...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |