Forum Archive

Multiple boards on one screen

sodoku

How do I add another 3 by 3 box’s so I have 2 3by3 separate boxes on one screen
,Without making the m and n 3 by 6

import scene, ui


class Block(scene.ShapeNode):
    def __init__(self, x, y, w, h, fill_color="black", parent=None):
        path = ui.Path.rect(0, 0, w, h)
        self.label = None
        self.root = parent
        super(Block, self).__init__(
            path=path, fill_color=fill_color, position=(x, y), parent=parent
        )

    def touch_began(self, touch):
        if not self.label:
            self.label = scene.LabelNode(
                "♛", font=("Helvetica", 40), color="green", parent=self.root
            )
            self.add_child(self.label)


class MyScene(scene.Scene):
    def setup(self):
        self.background_color = "gray"
        colorlist = ["black", "lightyellow"]
        m, n = 3, 3
        w, h = 64, 64
        start_x, start_y = self.size[0] / 2 - n / 2 * w, self.size[0] / 2 - m / 2 * h
        self.grid = {}
        for i in range(m):
            for j in range(n):
                x, y = start_x + i * w, start_y + j * h
                self.grid[i, j] = Block(
                    x, y, w, h, fill_color=colorlist[(i + j) % 2], parent=self
                )

    def touch_began(self, touch):
        for i, j in self.grid:
            if touch.location in (self.grid[i, j].frame):
                self.grid[i, j].touch_began(touch)
                return


scene.run(MyScene(), show_fps=True)
cvp

@sodoku something like

import scene, ui


class Block(scene.ShapeNode):
    def __init__(self, x, y, w, h, fill_color='black', parent=None):
        path = ui.Path.rect(0, 0, w, h)
        self.label = None
        self.root = parent
        super(Block, self).__init__(path=path,
        fill_color=fill_color,
        position=(x, y),
        parent=parent)

    def touch_began(self, touch):
        if not self.label:
            self.label = scene.LabelNode('♛',
            font=('Helvetica', 40),
            color='green', parent=self.root)
            self.add_child(self.label)


class MyScene (scene.Scene):
    def setup(self):
      self.background_color = 'gray'
      colorlist = ['black', 'lightyellow']
      self.grid = {}
      for grid in range(2):
        m, n = 3,3
        w, h = 64, 64
        start_x, start_y = self.size[0]/2-n/2*w, (grid+1)*self.size[1]/3 - m/2*h
        #self.grid = {}
        for i in range(m):
            for j in range(n):
                x, y = start_x+i*w, start_y+j*h
                self.grid[grid,i,j] = Block(x, y, w, h,
                fill_color=colorlist[(i+j)%2],
                parent=self)

    def touch_began(self, touch):
        for grid,i, j in self.grid:
            if (touch.location in (self.grid[grid,i, j].frame)):
                self.grid[grid,i,j].touch_began(touch)
                return


scene.run(MyScene(), show_fps=True) 

Note also that you did use [0] instead of [1] in start_y...

sodoku

Wow amazing thanks a lot for the help

sodoku

@cvp

I have another question if it’s possible to add two more 3by3 box’s beside them so I have four quadrants???

cvp

@sodoku you can change nx,ny as you want but be careful: as w,h are fixed, you could reach the screen size

import scene, ui


class Block(scene.ShapeNode):
    def __init__(self, x, y, w, h, fill_color='black', parent=None):
        path = ui.Path.rect(0, 0, w, h)
        self.label = None
        self.root = parent
        super(Block, self).__init__(path=path,
        fill_color=fill_color,
        position=(x, y),
        parent=parent)

    def touch_began(self, touch):
        if not self.label:
            self.label = scene.LabelNode('♛',
            font=('Helvetica', 40),
            color='green', parent=self.root)
            self.add_child(self.label)


class MyScene (scene.Scene):
    def setup(self):
      self.background_color = 'gray'
      colorlist = ['black', 'lightyellow']
      self.grid = {}
      m, n = 3,3
      w, h = 64, 64
      nx = 2
      ny = 2
      dx = (self.size[0]-nx*m*w)/(nx+1)
      dy = (self.size[1]-ny*n*h)/(ny+1)
      for grid_x in range(nx):
        start_x = (grid_x+1)*dx + grid_x*m*w
        for grid_y in range(ny):
          start_y = (grid_y+1)*dy + grid_y*n*h
          for i in range(m):
            x = start_x+i*w + w/2       # block position is center
            for j in range(n):
              y = start_y+j*h + h/2
              self.grid[grid_x,grid_y,i,j] = Block(x, y, w, h,
              fill_color=colorlist[(i+j)%2],
              parent=self)

    def touch_began(self, touch):
        for grid_x,grid_y,i, j in self.grid:
            if (touch.location in (self.grid[grid_x,grid_y,i, j].frame)):
                self.grid[grid_x,grid_y,i,j].touch_began(touch)
                return


scene.run(MyScene(), show_fps=True) 
sodoku

Fantastic, thanks your amazing, I wish I was as knowledgeable at python (Pythonista) as you

cvp

@sodoku Believe me, my knowledge of Python is not so big as you think. Often, this kind of code is more a problem of logic, not of programming language. And, my age has the only advantage of giving a longer experience. 😀 but also 😢