Question re: objects and square grids
Dave Angel
davea at davea.name
Wed May 15 15:57:52 EDT 2013
On 05/15/2013 02:14 PM, Andrew Bradley wrote:
Please reply on the list, not privately, unless it's something like a
simple thank-you. Typically, you'd do a reply-all, then delete the
people other than the list itself. Or if you're using Thunderbird, you
could just reply-list.
> Thank you very much for your response: it seems excellent, but I'm
afraid I
> do not understand it fully. Your code here:
>
> maxrows = 10
> maxcols = 20
> grid = []
> for row in range(maxrows):
> rowdata = []
> for column in range(maxcols):
> arg1 = ...
> arg2 = ...
> arg3 = ...
> arg4 = ...
> rowdata.append(pygame.Rect(arg
> 1, arg2, arg3, arg4)
> grid.append(rowdata)
>
> Seems very good, but keep in mind I just started programming last
week, and
> this is hard for me to wrap my head around. Do I really just write grid =
> []? or is this like a def grid(): function?
This code was intended to replace the 200 lines you started, A1=
pygame... A2= A3= etc. I'd have put them inside a function, but this
is just one of the things I'd have initialized in such a function. grid
is a list of lists, not a function.
> What do you mean by rowdata = []?
[] is the way you define an empty list. Another way might be:
rowdata = list()
> And how exactly would I make the formula for a rect call?
Well, for row==0 and col==0, you say you wanted 10, 12, 43, and 43 for
the four parameters. But you never said how you were going to
(manually) calculate those numbers for other cells. Only once you've
decided that can you fill in "formulas" for arg1 and arg2. I suspect
that arg3 and arg4 are simply 43 and 43 respectively, since you want all
the cells to be the same size.
taking my clue from Ian, I might try:
x_offset = 10
y_offset = 12
width = height = 43
arg1 = column * width + x_offset
arg2 = row * height + y_offset
arg3 = width
arg4 = height
That assumes that there is no gap between cells in this grid. If you
want a gap, then the width value used in the arg1 formula would be more
than 43 (width). Likewise the height value used in the arg2 formula
would be more than 43 (height).
> If there's a good website for these kind of details, I would
appreciate that too.
You cannot begin to write a non-trivial program in Python without
understanding lists pretty thoroughly. Perhaps you should start with
Alan Gauld's tutorial, which doesn't assume previous programming experience.
http://www.alan-g.me.uk/
I haven't studied it, as Python was about my 35th programming language.
But he's very active on Python-tutor, and explains things very well.
So his website is probably very good as well.
Now, as you can see from Ian's message, writing a game using pygame will
require quite a bit of other understanding. He demonstrates with
classes to represent cells, which is indeed what I'd do. But I suspect
you're not nearly ready to consider writing classes. (You use classes
all the time. For example, 5 is an instance of class int.)
--
DaveA
--
DaveA
More information about the Python-list
mailing list