#๐ weird issue regarding setting tuples
70 messages ยท Page 1 of 1 (latest)
@topaz nacelle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
so far this is the only code that sets Block.rect
#@cache
def render_stack(stack: Stack) -> pg.Surface:
blocks = []
prev_y = 0
max_size = 0
for block in stack.blocks:
res = render_block(block)
blocks.append((res, block, prev_y))
max_size = max(max_size, res.get_width())
prev_y += res.get_height()
if block.definition.block_type in (BlockType.STATEMENT, BlockType.HAT):
prev_y -= DEFAULT_PADDING
surf = pg.Surface((max_size, prev_y + DEFAULT_PADDING * int(stack.blocks[-1].definition.block_type in (BlockType.HAT, BlockType.STATEMENT))), pg.SRCALPHA)
for block in blocks:
block[1].rect = (0, block[2], block[0].get_width(), block[0].get_height())
surf.blit(block[0], (0, block[2]))
return surf
the main issue lies in here
@staticmethod
def get_block_index(stack: Stack, click_pos: tuple[number, number]) -> int:
for i, block in enumerate(stack.blocks):
before_rect = pg.Rect(block.rect)
rect = pg.Rect(block.rect)
rect.x += stack.position[0]
rect.y += stack.position[1]
print(i, rect, click_pos)
if rect.collidepoint(click_pos[0], click_pos[1]):
return i
here ^^^
this is the output
--[ dragging the code outside of the stack ]--
0 Rect(-185, -360, 250, 71) (-131.0, -5.0)
1 Rect(-185, -297, 241, 55) (-131.0, -5.0)
2 Rect(-185, -30, 195, 118) (-131.0, -5.0)
--[ trying to drag the first block of the new stack ]--
0 Rect(-185, 190, 195, 118) (-141.0, -4.0)
1 Rect(-185, 190, 195, 118) (-141.0, -4.0)
2 Rect(-185, 190, 195, 118) (-141.0, -4.0)
3 Rect(-185, 300, 412, 118) (-141.0, -4.0)
a
ok i changed the output a bit
0
0
63
110
157
204
251
298
Stack([Block("start", [], rect=(0, 0, 250, 71)), Block("sleep", [None], rect=(0, 251, 241, 55)), Block("sleep", [None], rect=(0, 251, 241, 55)), Block("sleep", [None], rect=(0, 251, 241, 55)), Block("sleep", [None], rect=(0, 251, 241, 55)), Block("sleep", [None], rect=(0, 251, 241, 55)), Block("repeat", [None, None], rect=(0, 298, 195, 118))], (-288.0, -317.0))
# in update function
for i, stack in enumerate(self.stacks):
rendered, prerender = self.render_stack(stack)
print(rendered.obj)
def render_stack(stack: Stack) -> pg.Surface:
blocks = []
prev_y = 0
max_size = 0
for block in stack.blocks:
res = render_block(block)
blocks.append((res, block, prev_y))
max_size = max(max_size, res.get_width())
prev_y += res.get_height()
if block.definition.block_type in (BlockType.STATEMENT, BlockType.HAT):
prev_y -= DEFAULT_PADDING
surf = pg.Surface((max_size, prev_y + DEFAULT_PADDING * int(stack.blocks[-1].definition.block_type in (BlockType.HAT, BlockType.STATEMENT))), pg.SRCALPHA)
for block in blocks:
print(block[2])
block[1].rect = (0, 0, 0, 0)
block[1].rect = (0, block[2], block[0].get_width(), block[0].get_height())
surf.blit(block[0], (0, block[2]))
return surf
self.render_stack is a bit different from render_stack, but its basically the same thing but self.render_stack contains the result from render_stack
weird issue regarding setting tuples
so
render_stack works fine
in that it sets the rect correctly
HOWEVER
things goes haywire in the update function
rendered.obj is the new stack, and is the original stack variable
and as you can see
the y values for the "sleep" blocks just repeats
aaaaaaaaa
where's the 'update' function? (as opposed to the render_stack function)
from what you're saying, you might be running into the fact that python never copies anything and that's messing you up
to 'fix' this (assuming there are no other underlying issues), you can do this
a = []
b = [] # b and a "are the same list"
b.append(5)
print(a) # [5]
a = []
b = a[:] # use slicing to make a copy
b.append(5)
print(a) # []
```for other structures, you can use `copy.copy` or `copy.deepcopy`
so heres my plan
each block have a rect attribute that is unset upon initialization
but is instead set through rendering
so
stack = Stack([Block(...)])
print(stack.blocks[0].rect)
render_stack(stack)
print(stack.blocks[0].rect)
will output
(0, 0, 0, 0)
(0, 0, W, H)
@sonic lynx ?
I'm looking at it
ok
also the update function is in a while true loop
as part of a game loop
is the first two 0s supposed to be like an offset? i.e. the top left x-y position of the block?
yeah
only for the first block
the rest of the blocks have different y values
but x is always 0
what if you tried printing the id(block) of these blocks in stack.blocks?
just making sure they're actually different blocks
hmmm lemme try
0 2142905859488
0 2142905857616
63 2142905857760
110 2142905857760
157 2142905857760
204 2142905857760
251 2142905857952
....
the fuck
yeah so they're the "same block"
how tho
cause again, python never copies anything
so when you do a = b, what you're doing is just pointing a to the same value that b is pointing to
!e so i.e.
a = ['test']
b = a
c = b
d = c
d.append('oops')
print(a)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['test', 'oops']
I mean I'd check where you're making these sleep blocks and make sure you're actually making different blocks instead of storing multiple references to the same one
what did you change?
added a copy method and used that
logical
mentally adding to a list of things to write for every class
rn i have
__hash__
copy
thanks :D
~close
!clos
!close
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.