Frame for tetris
-
Hello everyone
I'm new to pythonista and programming, trying to write tetris
For now i have buttons⬅️⬆️⬇️➡️ and a frame. But yhe frame is to much in the corner and i can't move it
Here's the codeimport arrows from scene import * colors = ['purple', 'red', 'blue', 'orange', 'yellow', 'green', 'lightblue'] sw = get_screen_size()[0] sh = get_screen_size()[1] class Board(ShapeNode): def __init__(self, parent=None, *args, **kwargs): self.rect_w = sw/2 self.rect_h = sh-100 path = ui.Path.rect(0, 0, self.rect_w, self.rect_h) path.line_width = 10 super().__init__(path, fill_color='white', stroke_color='purple', parent=parent, *args, **kwargs) def draw_lines(self): stroke(0, 0, 0) stroke_weight(2) line(l*30+50, self.rect_h+50, l*30+50, 50) class Game(Scene): def setup(self): self.background_color = 'white' Board(parent=self) self.add_buttons() def add_buttons(self): ars = arrows.Main() self.present_modal_scene(ars) if __name__ == '__main__': run(Game())
In the line
path = ui.Path.rect(0, 0, self.rect_w, self.rect_h)
if i change first two args from zero, nothing changes. So what they do? And how i can change pos of the frame?
-
This post is deleted!last edited by
-
@ccc with sw sh okay, but what this
colors = ‘purple red blue orange yellow green lightblue'.split()
gives?
-
@ccc said:
colors = ‘purple red blue orange yellow green lightblue'.split()
@Karina Try and print colors, you will see
['purple', 'red', 'blue', 'orange', 'yellow', 'green', 'lightblue']
Thus what you want 😀
-
@Karina, just to be clear, there is nothing wrong with
colors = ['purple', 'red', 'blue', 'orange', 'yellow', 'green', 'lightblue']
– for all-string arrays of this size, thestr.split()
version may just be more convenient to type.
-
@ccc @cvp @mikael I got it with the colors, but to me it’s the same amount of writing
And what means the error ‘ ‘ object has no attr _suspend_updates? When it appears?
-
@Karina said:
same amount of writing
Not really, with @ccc advice, you don't need to type quotes and comma's
-
object has no attr _suspend_updates
https://docs.python.org/3/library/functions.html are cool things to study as you learn Python. One of those is
dir(object)
which lists all the variables and methods of object. The message above means thatdir(object)
does not contain_suspend_updates
but some code is assuming that it does. Practice a lot withdir(object)
because it will help you to anticipate bugs.
-
@Karina,
_suspend_updates
is new to me, but it sounds like it is a scene-internal attribute. In addition to the detective work withdir
as @ccc suggested, it is a good idea to look at/share the full traceback.
-
@ccc @mikael I thought maybe you have met it then I’ll know what to do. And in google I also didn’t found about this error.
And if I see indir
that there is no _suspend_updates how it will help me? I just don’t understand what to do to avoid this bug
-
- Give us some code that generates this error --and/or--
- Give us the full multiline error message (stack trace) so we can understand the context of the error.
We do not even know the datatype of
object
.
-
last edited by cvp
-
Here's the code
from scene import * sw, sh = get_screen_size() rect_w = sw/3 rect_h = sh-100 class Board(ShapeNode): def __init__(self, stroke_color, line_width, parent, *args, **kwargs): self.stroke_color = stroke_color path=ui.Path.rect(0, 0, rect_w, rect_h) path.line_width = line_width self.parent = parent super.__init__(path, fill_color='white', stroke_color=self.stroke_color, parent=parent, *args, **kwargs) class Game(Scene): def setup(self): self.background_color = 'white' grey_rect = Board(stroke_color='grey', line_width=2, parent=self) rect.line_width = 10 board = Board(position=(sw/2, sh/2), stroke_color='purple', path=rect, parent=self) run(Game())
The idea is to create a rect in grey color line_width 2, and draw with it grey lines. Then draw above a purple rect above. I would draw that on paper, but i don't know how to add photos here
And the error - TypeError, 'Board' object has no attribute '_suspend_updates'
-
@Karina some small errors:
- super without parenthesis
- rect.line_width but rect is not yet defined
- mix of positional and keyword parameters
-
@Karina not yet fully solved but this (incomplete) script works
from scene import * sw, sh = get_screen_size() rect_w = sw/3 rect_h = sh-100 class Board(ShapeNode): def __init__(self, stroke_color='grey', line_width=1, parent=None, *args, **kwargs): #self.stroke_color = stroke_color path=ui.Path.rect(0, 0, rect_w, rect_h) path.line_width = line_width #self.parent = parent super().__init__(path, fill_color='white', stroke_color=stroke_color, parent=parent, *args, **kwargs) class Game(Scene): def setup(self): self.background_color = 'white' grey_rect = Board(stroke_color='grey', line_width=2, parent=self) #rect.line_width = 10 board = Board(stroke_color='purple', line_width=10, parent=self, position=(sw/2, sh/2)) run(Game())
-
@cvp what do you mean super without parenthesis?
About rect - I tried different ways to initiate board and forgot to delete rect.line_width
-
last edited by
-
@cvp what did you change that it works now?
-
For now it’s like this
from scene import * import arrows sw, sh = get_screen_size() rect_w = sw/3 rect_h = sh-100 class Board(ShapeNode): def __init__(self, stroke_color='grey', line_width=1, parent=None, *args, **kwargs): path = ui.Path.rect(0, 0, rect_w, rect_h) path.line_width = line_width super().__init__(path, fill_color='white', stroke_color=stroke_color, parent=parent, *args, **kwargs) if stroke_color == 'grey': print(stroke_color) d = int(rect_w/30) for l in range(int(rect_w/d)): x = l*d path.move_to(x, 0) path.line_to(x, rect_h) class Game(Scene): def setup(self): self.background_color = 'white' grey_rect = Board(line_width=2, parent=self, position=(sw/2, sh/2)) board = Board(stroke_color='purple', line_width=10, parent=self, position=(sw/3, sh/2)) self.add_buttons() def add_buttons(self): ars = arrows.Main() self.present_modal_scene(ars) run(Game())
I use another pos in grey_rect to see that both are drawn, it shouldn’t be in the end like that
But here there’s no vertical grey lines, though they in init. And it prints ‘grey’, so he gets in that for
-
@Karina said:
what did you change that it works now?
Check the differences in positional/keyword parameters in
def __init__(...
-
@Karina said:
But here there’s no vertical grey lines, though they in init.
Put the draw lines before the super().init....
if stroke_color == 'grey': print(stroke_color) d = int(rect_w/30) for l in range(int(rect_w/d)): x = l*d path.move_to(x, 0) path.line_to(x, rect_h) super().__init__(path, fill_color='white', stroke_color=stroke_color, parent=parent, *args, **kwargs)