Exemple #1
0
def keydown(key):
    x, y = g2d.mouse_position()
    g2d.set_color((randrange(256), randrange(256), randrange(256)))
    if x <= 25 and y <= 25 and g2d.confirm("Exit?"):
        g2d.close_canvas()
    else:
        g2d.fill_circle((x, y), 25)
Exemple #2
0
def tick():
    global i
    x = int(300 + i * math.cos(i * math.pi / 32))
    y = int(300 + i * math.sin(i * math.pi / 32))
    g2d.clear_canvas()
    g2d.set_color((255 - i, 0, i))
    g2d.fill_circle((x, y), i // 2)
    i = (i + 1) % n
Exemple #3
0
def update():
    global i
    x = int(300 + i * math.cos(i * math.pi / 32))
    y = int(300 + i * math.sin(i * math.pi / 32))
    g2d.clear_canvas()
    g2d.set_color((255 - i, 0, i))
    g2d.fill_circle((x, y), i // 2)
    i = (i + 1) % n
def update():
    global raggio
    x = int(centro + raggio * math.cos(raggio * math.pi / 32))
    y = int(centro + raggio * math.sin(raggio * math.pi / 32))
    g2d.clear_canvas()
    g2d.set_color((255 - raggio, 0, raggio))
    g2d.fill_circle((x, y), int(raggio / 2))
    raggio = (raggio + 1) % n
Exemple #5
0
def tick():
    if g2d.key_pressed("LeftButton"):
        x, y = g2d.mouse_position()
        g2d.set_color((randrange(256), randrange(256), randrange(256)))
        if x <= 25 and y <= 25 and g2d.confirm("Exit?"):
            g2d.close_canvas()
        else:
            g2d.fill_circle((x, y), 25)
Exemple #6
0
def update():
    global balls
    g2d.clear_canvas()  # Clear background

    for b in balls:
        g2d.set_color((b.color()))
        g2d.fill_circle((b.val()), 20)  # Draw foreground
        b.move()
def update():
    g2d.clear_canvas()  # BG
    b1.move()
    b2.move()
    g2d.set_color((0, 0, 255))
    g2d.fill_rect(b1.position())  # FG
    g2d.set_color((0, 255, 0))
    g2d.fill_rect(b2.position())  # FG
Exemple #8
0
def infinite_circles(x: int, y: int, w: int, h: int, c1: (int, int, int),
                     c2: (int, int, int)):
    w2, h2 = w // 2, h // 2
    if h2 < 1:
        return
    g2d.set_color(c1)
    g2d.fill_circle((x + w2, y + h2), h2)

    infinite_circles(x, y, w, h2, c2, c1)
    infinite_circles(x, y + h2, w, h2, c2, c1)
def infinite_circles(x: int, y: int, w: int, h: int,
                     c1: (int, int, int), c2: (int, int, int)):
    w2, h2 = w // 2, h // 2
    if h2 < 1:
        return
    g2d.set_color(c1)
    g2d.fill_circle((x + w2, y + h2), h2)

    infinite_circles(x, y, w, h2, c2, c1)
    infinite_circles(x, y + h2, w, h2, c2, c1)
Exemple #10
0
def tick():
    center_x, center_y = canvas_w // 2, canvas_h // 2
    g2d.clear_canvas()
    g2d.set_color((255, 255, 0))
    g2d.fill_circle((center_x, center_y), 30)
    for p in planets:
        p.move()
        x, y = p.pos()
        radius = p.diameter() // 2
        g2d.set_color(p.color())
        g2d.fill_circle((center_x + x, center_y + y), radius)
def draw_frame():
    center_x, center_y = canvas_w // 2, canvas_h // 2
    g2d.clear_canvas()
    g2d.set_color((255, 255, 0))
    g2d.fill_circle((center_x, center_y), 30)
    for p in planets:
        p.move()
        x, y = p.pos()
        radius = p.diameter() // 2
        g2d.set_color(p.color())
        g2d.fill_circle((center_x + x, center_y + y), radius)
Exemple #12
0
def update():
    global b, b1
    g2d.clear_canvas()  # Clear background

    g2d.set_color((b.color()))
    g2d.fill_circle((b.val()), 20)  # Draw foreground

    g2d.set_color((b1.color()))
    g2d.fill_circle((b1.val()), 20)

    b.move()
    b1.move()
    def update_buttons(self):
        global solved

        g2d.clear_canvas()
        g2d.set_color((0, 0, 0))
        cols, rows = self._game.cols(), self._game.rows()
        for y in range(1, rows):
            g2d.draw_line((0, y * H), (cols * W, y * H))
        for x in range(1, cols):
            g2d.draw_line((x * W, 0), (x * W, rows * H))
        for y in range(rows):
            for x in range(cols):
                value = self._game.value_at(x, y)

                if '#' not in value:
                    center = x * W + W // 2, y * H + H // 2

                    if '!' in value:
                        g2d.set_color((0, 0, 200))
                        g2d.fill_circle(center, 14)
                        g2d.set_color((255, 255, 255))
                        g2d.fill_circle(center, 12)
                        g2d.set_color((0, 0, 0))
                    g2d.draw_text_centered(value[:-1], center, H // 2)
                else:
                    g2d.fill_rect((x * W, y * H, W, H))
        g2d.update_canvas()

        if self._game.finished() and not solved:
            g2d.alert(self._game.message())
            g2d.close_canvas()
Exemple #14
0
 def update_buttons(self):
     g2d.clear_canvas()
     g2d.set_color((0, 0, 0))
     cols, rows = self._game.cols(), self._game.rows()
     for y in range(1, rows):
         g2d.draw_line((0, y * H), (cols * W, y * H))
     for x in range(1, cols):
         g2d.draw_line((x * W, 0), (x * W, rows * H))
     for y in range(rows):
         for x in range(cols):
             g2d.set_color((0,0,0))
             value = self._game.value_at(x, y)
             if self._game.checkblack(x,y):
                 g2d.fill_rect((x * W, y * H, W, H))
             center = x * W + W//2, y * H + H // 2
             if self._game.checkcircle(x,y):
                 g2d.fill_circle((center), W // 2)
                 g2d.set_color((255,255,255))
                 g2d.fill_circle((center), W // 2 - 1)
             g2d.set_color((0,0,0))
             g2d.draw_text_centered(value, center, H//2)
     g2d.update_canvas()
     if self._game.finished():
         g2d.alert(self._game.message())
         g2d.close_canvas()
Exemple #15
0
 def update_buttons(self):
     g2d.clear_canvas()
     g2d.set_color((0, 0, 0))
     rows, cols = self._game.rows(), self._game.cols()
     for y in range(1, rows):
         g2d.draw_line((0, y * H), (cols * W, y * H))
     for x in range(1, cols):
         g2d.draw_line((x * W, 0), (x * W, rows * H))
     for y in range(rows):
         for x in range(cols):
             value = self._game.value_at(x, y)
             center = x * W + W//2, y * H + H//2
             g2d.draw_text_centered(value, center, H//2)
     g2d.update_canvas()
     if self._game.finished():
         g2d.alert(self._game.message())
         g2d.close_canvas()
Exemple #16
0
 def update_buttons(self):
     g2d.clear_canvas()
     g2d.set_color((0, 0, 0))
     rows, cols = self._game.rows(), self._game.cols()
     for y in range(1, rows):
         g2d.draw_line((0, y * H), (cols * W, y * H))
     for x in range(1, cols):
         g2d.draw_line((x * W, 0), (x * W, rows * H))
     for y in range(rows):
         for x in range(cols):
             value = self._game.value_at(x, y)
             center = x * W + W // 2, y * H + H // 2
             g2d.draw_text_centered(value, center, H // 2)
     g2d.update_canvas()
     if self._game.finished():
         g2d.alert(self._game.message())
         g2d.close_canvas()
    def draw(self):
        g2d.clear_canvas()
        for a in self._arena.actors():
            if type(a) in self._game.bg_list:
                g2d.draw_image_clip(self.background, a.symbol(), a.position())
                g2d.draw_image_clip(self.background, a.symbol(),
                                    a.second_position())
            else:
                g2d.draw_image_clip(self.sprites, a.symbol(), a.position())
            if a.get_state() == 'delete':
                self._arena.remove(a)

        g2d.set_color((255, 255, 255))
        g2d.draw_text('Remaining time: ' + str(self._game.remaining_time()),
                      (10, 10), 20)
        g2d.draw_text('Score: ' + str(self._arena.get_score()),
                      (ARENA_W / 2, 10), 20)
        g2d.draw_text('Lives: ' + str(hero_lives + 1), (ARENA_W - 80, 10), 20)
Exemple #18
0
#!/usr/bin/env python3
'''
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

g2d.init_canvas((600, 400))  # width, height

g2d.set_color((255, 255, 0))  # red + green = yellow
g2d.fill_rect((150, 100, 250, 200))  # left, top, width, height

g2d.set_color((0, 255, 0))
g2d.draw_line((150, 100), (400, 300))  # point1, point2

g2d.set_color((0, 0, 255))
g2d.fill_circle((400, 300), 20)  # center, radius

g2d.set_color((255, 0, 0))
g2d.draw_text("Hello", (150, 100), 40)  # text, left-top, font-size

g2d.main_loop()  # manage the window/canvas
Exemple #19
0
#!/usr/bin/env python3
'''
@author Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d
from random import randrange

W, H = 320, 240
SIDE = 100
g2d.init_canvas((W, H))

n = int(g2d.prompt("n? "))  # like input
dx, dc = 0, 0
if n > 1:
    dx = (W - SIDE) / (n - 1)
    dy = (H - SIDE) / (n - 1)
    dc = 255.0 / (n - 1)

i = 0
while i < n:
    g2d.set_color((i * dc, 0, 0))
    g2d.fill_rect((i * dx, i * dy, SIDE, SIDE))
    i += 1

g2d.main_loop()
def update():
    g2d.clear_canvas()  # BG
    b1.move()
    g2d.set_color(random_color())
    g2d.fill_circle(b1.center(), int(b1.wide() / 2))
Exemple #21
0
def update():
    """
    Funzione principale di update
    """
    GUI.changestate()  # Aggiorna l'interfaccia
    g2d.clear_canvas()
    g2d.set_color((0, 0, 0))
    if GUI._gamerunning:  # Solo se in partita
        # --------Disegno linee------- #
        for n in range(0, ActualGame._difficulty + 1):
            drawline(ActualGame._origin, ActualGame._boxsize,
                     ActualGame._difficulty, n)
        # --------Disegno caselle------- #
        for box in ActualGame._boxes:
            if ActualGame._statematrix[box._row][
                    box._column] == 2:  # Stato cerchiato
                g2d.fill_circle(
                    (box._x + ActualGame._boxsize / 2,
                     box._y + ActualGame._boxsize / 2), ActualGame._boxsize /
                    2)  # Disegna un cerchio nero che riempie la casella
                g2d.set_color((255, 255, 255))
                g2d.fill_circle(
                    (box._x + ActualGame._boxsize / 2,
                     box._y + ActualGame._boxsize / 2),
                    ActualGame._boxsize / 2 - 2
                )  # Disegna un cerchio bianco più piccolo per dare l'illusione di contorno
                g2d.set_color(
                    (0, 0, 0))  # Reimposta a nero per disegnare il resto
            if ActualGame._statematrix[box._row][box._column] != 1:
                if len(str(box._number)) == 1:
                    g2d.draw_text(
                        str(box._number), (box._x + ActualGame._boxsize // 5,
                                           box._y + ActualGame._boxsize // 6),
                        ActualGame._boxsize / 4 *
                        3)  # Disegna il testo in modo più o meno centrato
                else:
                    g2d.draw_text(
                        str(box._number), (box._x + ActualGame._boxsize // 16,
                                           box._y + ActualGame._boxsize // 6),
                        ActualGame._boxsize / 4 *
                        3)  # Disegna il testo in modo più o meno centrato
            else:
                g2d.fill_rect(
                    (box._x, box._y, ActualGame._boxsize, ActualGame._boxsize
                     ))  # Se lo stato è 1, disegna invece un rettangolo
            # --------Controllo selezione caselle------- #
            box._selected = False  # Resetta lo stato di selezione
            if checkmousepos(
                    box._x, box._y, ActualGame._boxsize, ActualGame._boxsize
            ):  # Se il mouse si trova all'interno di una casella, marcala come selezionata
                box._selected = True
    # --------Disegno interfaccia------- #
    for elem in GUI._elements:
        if checkmousepos(
                elem._x, elem._y, elem._w, elem._h
        ) and elem._selectable:  # Se il mouse si trova nella bounding box del pulsante, marcalo come selezionato
            elem._selected = True
        if elem._selected:
            g2d.set_color(
                (255, 0, 0))  # I pulsanti selezionati sono disegnati in rosso
        else:
            g2d.set_color((0, 0, 0))  # Gli altri in nero
        if elem._cat == 0:
            g2d.draw_text(elem._text, (elem._x, elem._y),
                          elem._h)  # Se è del testo
        else:
            g2d.draw_image((elem._x, elem._y),
                           (elem._w, elem._h))  # Se è un'immagine
    # --------Controllo pulsanti------- #
    checkbuttons()
Exemple #22
0
 (usare math.sin e math.cos per determinare le posizioni in cui disegnare)
'''

import g2d
from random import randint
from math import sin, cos, pi

dim = 600  # dimensione canvas
colore = 0, 0, 0  # colore prime tacche
centro = dim / 2  # centro dell'orologio
r = 100  # raggio interno tacche
l1 = 10  # lunghezza prime tacche
l2 = 5  # lunghezza tacche minuti

g2d.init_canvas((dim, dim))
g2d.set_color(colore)
angolo = 0
while angolo < 360:
    # coordinate
    x = centro + r * sin(angolo * pi / 180)
    y = centro + r * cos(angolo * pi / 180)
    x2 = centro + (r + l1) * sin(angolo * pi / 180)
    y2 = centro + (r + l1) * cos(angolo * pi / 180)
    # disegno
    g2d.draw_line((x, y), (x2, y2))
    angolo = angolo + 30

colore = 255, 0, 0  # colore seconde tacche
g2d.set_color(colore)
angolo = 0
while angolo < 360:
Exemple #23
0
def update():
    g2d.clear_canvas()
    a.move()
    g2d.set_color(a.color())
    g2d.fill_circle(a.center(), a.radius())
Exemple #24
0
#!/usr/bin/env python3
'''
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

SIDE = 600
g2d.init_canvas((SIDE, SIDE))

n = int(g2d.prompt("Circles? "))

center = SIDE // 2, SIDE // 2
delta_radius = SIDE * 0.5 / n
delta_color = 0
if n > 1:
    delta_color = 255.0 / (n - 1)

for i in range(n):
    radius = int(SIDE // 2 - i * delta_radius)
    g2d.set_color((int(255.0 - i * delta_color), 0, 0))
    g2d.fill_circle(center, radius)

g2d.main_loop()
Exemple #25
0
def tick():
    g2d.clear_canvas()  # BG
    for b in balls:
        b.move()
        g2d.set_color(b.color())
        g2d.fill_rect(b.position())  # FG
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import sys; sys.path.append('../examples/')
import g2d

##with open("_palette.txt", "w") as new_file:
##    print('''180 120 60\n120 180 60\n120 60 180''', file=new_file);

palette = []
with open("_palette.txt", "r") as palette_file:
    for line in palette_file:
        if len(line) > 0:
            vals = line.split()
            color = (int(vals[0]), int(vals[1]), int(vals[2]))
            palette.append(color)

side = 400.0
g2d.init_canvas((int(side), int(side)))

n = int(g2d.prompt("squares? "))
delta = side / n

for i in range(n):
    g2d.set_color(palette[i % len(palette)])
    g2d.fill_rect((0, 0, int(side), int(side)))
    side -= delta

g2d.main_loop()
Exemple #27
0
#!/usr/bin/env python3
'''
@author Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html

Quadrati casuali
- Chiedere all'utente un numero n
- Disegnare n quadrati
-- Tutti con lato di 100 pixel
-- Ciascuno in posizione casuale
-- Ciascuno con un colore casuale
'''

import g2d
from random import randrange

W, H = 640, 480
SIDE = 100
g2d.init_canvas((W, H))

n = int(g2d.prompt("n? "))
i = 0
while i < n:
    color = randrange(255), randrange(255), randrange(255)
    rect = randrange(W - SIDE), randrange(H - SIDE), SIDE, SIDE
    g2d.set_color(color)
    g2d.fill_rect(rect)
    i += 1

g2d.main_loop()
Exemple #28
0
#!/usr/bin/env python3
'''
@author Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

g2d.init_canvas((400, 400))

i = 0
while i < 10:
    x = i * 25
    y = i * 25
    red = i * 25
    g2d.set_color((red, 0, 0))
    g2d.fill_rect((x, y, 100, 100))
    i += 1

g2d.main_loop()
Exemple #29
0
  chiedere all'utente una lista di valori positivi
  la lista termina quando l'utente inserisce il valore 0
  mostrare un istogramma
  larghezza di ciascuna barra proporzionale
  al valore corrispondente
  la barra più lunga occupa tutto lo spazio disponibile
'''

import g2d

val = []
v = int(input("valore (0 fine) "))
while v > 0:
    val.append(v)
    v = int(input("valore (0 fine) "))

w = 600
h = 400
n = len(val)  #numero rettangoli
hr = h // n  #altezza rettangolo
vm = max(val)  #valore massimo nella lista
pix = w // vm  #pixel corrispondenti a unità
g2d.init_canvas((w, h))
g2d.set_color((0, 0, 255))
y = 0  #posizione prima barra
for v in val:
    g2d.fill_rect((0, y, pix * v, hr - 1))
    y += hr

g2d.main_loop()
Exemple #30
0
import g2d

g2d.init_canvas((600, 600))

count = 0
i = int(input("Quanti cerchi vuoi disegnare?"))
r = 255 / i

while count < i:
    g2d.set_color((255 - count * r, 0, 0))
    g2d.fill_circle((300, 300), 200 - count * 10)
    count = count + 1

g2d.main_loop()
Exemple #31
0
def main():
    g2d.init_canvas((263, 263))
    g2d.set_color((127, 0, 0))
    sierpinski((10, 10, 243, 243))
    g2d.main_loop()
Exemple #32
0
import g2d
from random import randrange

side = 480
g2d.init_canvas((side, side))
n = int(g2d.prompt("n?"))

d = side // n
r = d // 2

for y in range(1, n + 1):
    for x in range(1, y + 1):
        g2d.set_color((randrange(256), randrange(256), randrange(256)))
        g2d.fill_circle((x * d - r, y * d - r), r)

g2d.main_loop()
def main():
	g2d.init_canvas((ARENA_W, ARENA_H))
	g2d.set_color((0, 0, 255))
	g2d.main_loop(update)  
Exemple #34
0
import g2d

size = 500
g2d.init_canvas((size, size))

n = int((g2d.prompt("n?")))
dsize = size // n
dcolor = 255
if n > 1:
    dcolor = 255 // (n - 1)
for i in range(n):
    g2d.set_color((0, i * dcolor, 0))
    g2d.fill_rect((0, 0, size - i * dsize, size - i * dsize))

g2d.main_loop()
Exemple #35
0
#!/usr/bin/env python3
'''
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

WIDTH, HEIGHT = 600, 400
g2d.init_canvas((WIDTH, HEIGHT))

cols = int(g2d.prompt("Cols? "))
rows = int(g2d.prompt("Rows? "))

w, h = WIDTH / cols, HEIGHT / rows
delta_blue, delta_green = 0, 0
if cols > 1:
    delta_blue = 255.0 / (cols-1)
if rows > 1:
    delta_green = 255.0 / (rows-1)

for y in range(rows):
    for x in range(cols):
        g2d.set_color((0, int(delta_green*y), int(delta_blue*x)))
        g2d.fill_rect((int(w*x), int(h*y), int(w-1), int(h-1)))

g2d.main_loop()
Exemple #36
0
#!/usr/bin/env python3
'''
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

RADIUS, radius, red = 300, 300, 255
g2d.init_canvas((RADIUS * 2, RADIUS * 2))

n = int(g2d.prompt("Circles? "))
for i in range(n):
    g2d.set_color((red, 0, 0))
    g2d.fill_circle((RADIUS, RADIUS), radius)
    if n > 1:
        radius -= RADIUS // n
        red -= 255 // (n-1)

g2d.main_loop()
Exemple #37
0
'''
 @author Alberto Ferrari - https://albertoferrari.github.io/
 @license This software is free - http://www.gnu.org/licenses/gpl.html
 
 disegna n cerchi concentrici con raggio decrescente
 fa variare il colore dei cerchi dal rosso del livello più esterno
 fino al nero del livello più interno
'''
import g2d

DIMCANVAS = 600  # dimensione canvas
g2d.init_canvas((DIMCANVAS, DIMCANVAS))

CENTRO = (DIMCANVAS // 2, DIMCANVAS // 2)  # centro dei cerchi
raggio = DIMCANVAS // 2  # raggio iniziale
rosso = 255  # rosso iniziale

n = int(g2d.prompt('numero cerchi: '))
d_raggio = raggio // (n + 1)  # delta raggio
d_rosso = rosso // (n - 1)  # delta rosso

for i in range(n):
    # print(rosso,raggio)					# debug -> colore e raggio
    g2d.set_color((rosso, 0, 0))
    g2d.fill_circle(CENTRO, raggio)  # disegna cerchio
    rosso -= d_rosso  # nuovo raggio
    raggio -= d_raggio  # nuovo colore

g2d.main_loop()
Exemple #38
0
def tick():
    g2d.clear_canvas()
    a.move()
    g2d.set_color(a.color())
    g2d.fill_circle(a.center(), a.radius())
from random import randint
import g2d

g2d.init_canvas((600, 600))

count = 0
x = 200

while x > 10:
    r = randint(0, 256)
    b = randint(0, 256)
    g = randint(0, 256)

    g2d.set_color((r, g, b))
    g2d.fill_circle((300, 300), x)

    count = count + 1
    x = 200 - count * randint(0, x - 1)

g2d.main_loop()
Exemple #40
0
def main():
    g2d.init_canvas((263, 263))
    g2d.set_color((127, 0, 0))
    sierpinski((10, 10, 243, 243))
    g2d.main_loop()
import g2d

radius = 300
g2d.init_canvas((radius * 2, radius * 2))

n = int(g2d.prompt("Circles? "))
i = n
while i > 0:
    r = i * radius / n
    c = 0
    if n > 1:
        c = (i-1) * 255 / (n-1)
    g2d.set_color((c, 0, 0))
    g2d.fill_circle((radius, radius), r)
    i -= 1

g2d.main_loop()


Exemple #42
0
#!/usr/bin/env python3
'''
@author Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d
from math import pi, sin, cos

R = 200

g2d.init_canvas((2*R, 2*R))
g2d.set_color((0, 0, 0))
i, angle, r = 0, 0.0, R - 20
while i < 12:
    angle = i * 2 * pi / 12
    pt1 = R + int(r*cos(angle)), R + int(r*sin(angle))
    pt2 = R + int(R*cos(angle)), R + int(R*sin(angle))
    g2d.draw_line(pt1, pt2)
    i += 1

i, angle, r = 0, 0.0, R - 5
while i < 60:
    angle = i * 2 * pi / 60
    pt1 = R + int(r*cos(angle)), R + int(r*sin(angle))
    pt2 = R + int(R*cos(angle)), R + int(R*sin(angle))
    g2d.draw_line(pt1, pt2)
    i += 1

g2d.main_loop()
Exemple #43
0
import g2d
from random import randint

a = int(input("numero righe?"))
x = 40
y = 40
g2d.init_canvas((600, 600))
for i in range(1, a + 1):

    for k in range(1, i + 1):
        # Blue circle, center=(400, 300), radius=20
        g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256)))
        g2d.fill_circle((x, y), 20)  # circle
        x += 80
    y += 80
    x = 40
g2d.main_loop()
'''
l'esercizio è lo stesso del 2.3 applicato ai cerchi, ad ogni incremento di riga aumento la y
per ogni cerchio che devo disegnare per riga aumento la x
poi alla fine la riporto al valore iniziale per riallinearsi a sinistra
'''
    return x + w / 2, y + h / 2


def htree(rect: (int, int, int, int), level: int):
    x, y, w, h = rect
    if level == 0 or w < 3 or h < 3:  #terminazione
        return
    if level % 2 == 0:  #livello pari dimezzo larghezza
        rect1 = x, y, w / 2, h  #primo rettangolo sn
        rect2 = x + w / 2, y, w / 2, h  #secondo rettangolo ds
        #print(level,'sn',rect1,'ds',rect2)	#debug
    else:  #livello dispari dimezzo altezza
        rect1 = x, y, w, h / 2  #alto
        rect2 = x, y + h / 2, w, h / 2  #basso
        #print(level,'up',rect1,'down',rect2)	#debug

    g2d.draw_line(center(rect1), center(rect2))
    htree(rect1, level - 1)
    htree(rect2, level - 1)


if __name__ == '__main__':
    level = int(input('level? '))  ## -1 = infinite
    side = 600

    g2d.init_canvas((side, side))
    g2d.set_color((0, 0, 0))
    htree((0, 0, side, side), level)

    g2d.main_loop()
Exemple #45
0
            self._dx = 0
            self._dy = -10

        if i == 40:  # angolo in alto a sinistra
            self._dx = 10
            self._dy = 0

    def val(self):  # mi restituisce le coordinate del centro
        return self._x, self._y


def update():
    global b, i
    i += 1
    g2d.clear_canvas()  # Clear background
    g2d.fill_circle(
        (b.val()),
        20)  # chiamo il metodo di b per avere le coordinate del centro
    b.move(i)
    if i == 40:
        i = 0


A = 600
B = 500
i = 0
g2d.init_canvas((A, B))
b = TurningBall(300, 200, A, B)
g2d.set_color((255, 0, 0))
g2d.main_loop(update, 30)