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)
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()
def update(): global dx, dy, x, y g2d.clear_canvas() # Clear background g2d.fill_circle((x, y), 20) # Draw foreground x += dx y += dy if x >= A - distance: # angolo in alto a destra dx = 0 dy = 5 if y >= A - distance: # angolo in basso a destra dx = -5 dy = 0 ''' nei primi due if posso anche inserire gli and come negli ultimi due ottenendo lo stesso risultato. Non l'ho messo semplicemente per ottimizzare il codice ''' if x <= 0 + distance and y >= A - distance: # angolo in basso a sinistra dx = 0 dy = -5 if y <= 0 + distance and x <= 0 + distance: # angolo in alto a sinistra dx = 5 dy = 0
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()
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
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
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(): 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 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)
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
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)
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)
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 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()
import g2d from random import randint a = int(input("numero righe?")) x = 300 y = 40 g2d.init_canvas((600, 600)) for i in range(1, a + 1): #in che riga siamo? if i % 2 != 0: # riga dispari g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) # disegno il cerchio centrale g2d.fill_circle((x, y), 20) # circle for u in range(1, (i // 2) + 1): #ad ogni iterazione disegno un cerchio traslato di 80 a destra e uno di 80 a sinistra (per questo dimezzo i) g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x + 80 * u, y), 20) # circle g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x - 80 * u, y), 20) # circle else: #riga pari x1 = x + 40 x2 = x - 40 for u in range(0, i // 2): #disegno un cerchio a destra e uno a sinistra traslati di 80 g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x2 - 80 * u, y), 20) # circle g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x1 + 80 * u, y), 20) # circle y += 80 x = 300 g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x, y), 20) g2d.main_loop() '''
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()
import g2d from random import randrange side = 480 g2d.init_canvas((side, side)) n = int(g2d.prompt("n?")) d = side // (n + 1) r = d // 2 start = side // 2 for y in range(1, n + 1): for x in range(y): g2d.set_color((randrange(256), randrange(256), randrange(256))) g2d.fill_circle((start + x * d, y * d - r), r) start -= r g2d.set_color((randrange(256), randrange(256), randrange(256))) g2d.fill_circle((side // 2, side - r), r) 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))
#!/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
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()
r = 200 radious.append(r) red.append(random.randint(0, 255)) green.append(random.randint(0, 255)) blue.append(random.randint(0, 255)) while (True): decrement = random.randint(0, r - 1) new_r = r - decrement if (new_r < 10): break else: r = new_r radious.append(r) red.append(random.randint(0, 255)) green.append(random.randint(0, 255)) blue.append(random.randint(0, 255)) print("Raggi estratti : ", radious) x_total = y_total = 500 center = x_total / 2 g2d.init_canvas((x_total, y_total)) # width, height for i in range(len(radious)): g2d.set_color((red[i], green[i], blue[i])) g2d.fill_circle((center, center), radious[i]) g2d.main_loop()
#!/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()
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()
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 '''
def tick(): g2d.clear_canvas() a.move() g2d.set_color(a.color()) g2d.fill_circle(a.center(), a.radius())
import g2d from random import randint AREA_X = AREA_Y = 600 g2d.init_canvas((AREA_X, AREA_Y)) n = int(input("Quante file vuoi? ")) raggio = AREA_X / ((n * 2)) for i in range(0, n): for z in range(0, i): r = randint(0, 256) g = randint(0, 256) b = randint(0, 256) xc = (((n - i) * raggio) + (z * 2 * raggio)) + raggio yc = (raggio + (raggio * i * 2)) - (raggio * 2) g2d.set_color((r, g, b)) g2d.fill_circle((xc, yc), raggio) g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((AREA_X / 2, AREA_Y - raggio), raggio) g2d.main_loop()
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()
#!/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()
def update(): g2d.clear_canvas() # BG b1.move() g2d.fill_circle(b1.center(), int(b1.wide() / 2))
def update(): g2d.clear_canvas() a.move() g2d.set_color(a.color()) g2d.fill_circle(a.center(), a.radius())
# CERCHI CONCENTRICI GRADUALI - EXERCISE 1.4 import g2d N_circle = int(input("Digita il numero di cerchi che vuoi disegnare ")) R_step = float(input("Digita lo step che vuoi applicare sul raggio ")) C_step = float(input("Digita lo step che vuoi applicare sulle tonalità di rosso ")) R_0 = 3*R_step x_total = y_total = (R_0 + R_step * (N_circle + 1)) * 2 center = x_total/2 g2d.init_canvas((x_total, y_total)) # width, height for i in range(N_circle): index_inverted = N_circle - i - 1 R = R_0 + index_inverted*R_step red = 0 + index_inverted*C_step g2d.set_color((red,0,0)) g2d.fill_circle((center, center), R) g2d.main_loop() # manage the window/canvas2
import g2d from random import randint g2d.init_canvas((600, 600)) r = 20 x = r y = r n = 11 while n > 10: n = int(input("Che numero desideri? ")) for i in range(1, n + 1): for z in range(1, i): g2d.set_color((randint(0, 256), randint(0, 256), randint(0, 256))) g2d.fill_circle((x * z * 2, y * i * 2), r) g2d.main_loop()
''' @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()
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()
def update(): global b g2d.clear_canvas() # Clear background g2d.fill_circle((b.val()), 20) # Draw foreground b.move()