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(): g2d.clear_canvas() if random.randrange(50) == 0: Bullet(arena, random.randrange(arena.size()[0])) arena.move_all() for a in arena.actors(): g2d.fill_rect(a.position())
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(): g2d.clear_canvas() if random.randrange(50) == 0: Bullet(arena, random.randrange(arena.size()[0])) arena.move_all() for a in arena.actors(): g2d.fill_rect(a.position())
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
def tick(): arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if isinstance(a, Wall): g2d.fill_rect(a.position()) else: g2d.draw_image_clip(sprites, a.symbol(), a.position())
def update(): arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if isinstance(a, Wall): g2d.fill_rect(a.position()) else: g2d.draw_image_clip(sprites, a.symbol(), a.position())
def tick(): if g2d.key_pressed("1"): b1.start() if g2d.key_pressed("2"): b2.start() g2d.clear_canvas() # BG b1.move() b2.move() g2d.fill_rect(b1.position()) # FG g2d.fill_rect(b2.position()) # FG
def sierpinski(rect: (int, int, int, int)): x, y, w, h = rect w3, h3 = w // 3, h // 3 if w3 < 1 or h3 < 1: return for row in range(3): for col in range(3): rect3 = x + col * w3, y + row * h3, w3, h3 if row == 1 and col == 1: g2d.fill_rect(rect3) else: sierpinski(rect3)
def tick(): arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if isinstance(a, Background): ax, ay, aw, ah = a.position() g2d.draw_image_clip(bg, a.symbol(), (ax, ay, aw, ah)) g2d.draw_image_clip(bg, a.symbol(), (ax + aw, ay, aw, ah)) elif a.symbol() != (0, 0, 0, 0): g2d.draw_image_clip(sprites, a.symbol(), a.position()) else: g2d.fill_rect(a.position())
def tick(): g2d.clear_canvas() # BG if g2d.key_pressed("w"): b1.go_up() if g2d.key_pressed("a"): b1.go_left() if g2d.key_pressed("s"): b1.go_down() if g2d.key_pressed("d"): b1.go_right() b1.move() g2d.fill_rect(b1.position()) # FG
def tick(): if g2d.key_pressed("ArrowUp"): frog.jump_up() elif g2d.key_pressed("ArrowRight"): frog.jump_right() elif g2d.key_pressed("ArrowDown"): frog.jump_down() elif g2d.key_pressed("ArrowLeft"): frog.jump_left() g2d.clear_canvas() arena.move_all() for a in arena.actors(): g2d.fill_rect(a.position())
def sierpinski(x0, y0, w0, h0, livello): w1 = w0 // 3 #larghezza quadrato da disegnare h1 = h0 // 3 #altezza quadrato da disegnare #print(x0, y0, w0, h0, livello) #debug if livello == 0 or w1 == 0 or h1 == 0: #terminazione return for y in range(3): for x in range(3): x1 = x0 + x * w1 #calcolo posizione y1 = y0 + y * h1 if x == 1 and y == 1: #disegno quadrato centrale g2d.fill_rect((x1, y1, w1, h1)) else: sierpinski(x1, y1, w1, h1, livello - 1) #altri quadrati
def update(): terrenoval = B - 100 global contatore, contatore2, contatore3 if g2d.key_pressed("ArrowUp"): turtle.go_up() elif g2d.key_pressed("ArrowRight"): turtle.go_right() elif g2d.key_pressed("ArrowDown"): turtle.go_down() elif g2d.key_pressed("ArrowLeft"): turtle.go_left() elif ( g2d.key_released("ArrowRight") or g2d.key_released("ArrowDown") or g2d.key_released("ArrowLeft")): turtle.stay() arena.move_all() g2d.clear_canvas() # -------------------------------------Sfondo----------------------------------------------------------- '''Disegno 2 volte l'immagine, di cui una volta fuori dallo schermo, quando il decremento è uguale al totale del canvas lo ripristino in modo che il movimento sembri continuo''' g2d.draw_image_clip(image, (0, 0, 512, 128), (0, 0, A, B)) g2d.draw_image_clip(image, (0, 258, 512, 128), (0 - contatore3 // 2, (B - 75) // 3, A, B - (B - 75) // 3)) g2d.draw_image_clip(image, (0, 258, 512, 128), (A - contatore3 // 2, (B - 75) // 3, A, B - (B - 75) // 3)) g2d.draw_image_clip(image, (0, 386, 512, 128), (0 - contatore, terrenoval - 90, A, B - (terrenoval - 90))) g2d.draw_image_clip(image, (0, 386, 512, 128), (A - contatore, terrenoval - 90, A, B - (terrenoval - 90))) g2d.draw_image_clip(image, (0, 513, 512, 128), (0 - contatore2 * 2, terrenoval, A, 100)) g2d.draw_image_clip(image, (0, 513, 512, 128), (A - contatore2 * 2, terrenoval, A, 100)) if contatore == A: contatore = 0 if contatore2 == A // 2: contatore2 = 0 if contatore3 == A * 2: contatore3 = 0 for a in arena.actors(): if a.symbol() != (0, 0, 0, 0): g2d.draw_image_clip(sprites, a.symbol(), a.position()) else: g2d.fill_rect(a.position()) contatore += 1 contatore2 += 1 contatore3 += 1
def tick(): if g2d.key_pressed("ArrowUp"): hero.jump() elif g2d.key_pressed("ArrowRight"): hero.go_right() elif g2d.key_pressed("ArrowLeft"): hero.go_left() elif (g2d.key_released("ArrowLeft") or g2d.key_released("ArrowRight")): hero.stay() arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if a.symbol() != (0, 0, 0, 0): g2d.draw_image_clip(sprites, a.symbol(), a.position()) else: g2d.fill_rect(a.position())
def tick(): if g2d.key_pressed("Spacebar"): mario.jump() elif g2d.key_pressed("ArrowLeft"): mario.go_left() elif g2d.key_pressed("ArrowRight"): mario.go_right() elif (g2d.key_released("ArrowLeft") or g2d.key_released("ArrowRight")): mario.stay() arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if isinstance(a, Wall): g2d.fill_rect(a.position()) else: g2d.draw_image_clip(sprites, a.symbol(), a.position())
def tick(): global view_x, view_y arena_w, arena_h = arena.size() if g2d.key_pressed("ArrowUp"): view_y = max(view_y - 10, 0) elif g2d.key_pressed("ArrowRight"): view_x = min(view_x + 10, arena_w - view_w) elif g2d.key_pressed("ArrowDown"): view_y = min(view_y + 10, arena_h - view_h) elif g2d.key_pressed("ArrowLeft"): view_x = max(view_x - 10, 0) g2d.draw_image_clip(background, (view_x, view_y, view_w, view_h), (0, 0, view_w, view_h)) # BG arena.move_all() for a in arena.actors(): x, y, w, h = a.position() g2d.fill_rect((x - view_x, y - view_y, w, h)) # FG
def main(): W, H = 600, 400 g2d.init_canvas((W, H)) values = [] max_val = 0 val = float(g2d.prompt("Val? ")) while val > 0: values.append(val) if val > max_val: max_val = val val = float(g2d.prompt("Val? ")) if len(values) > 0: for i, v in enumerate(values): rect = (0, i * H / len(values), v * W / max_val, (H / len(values)) - 1) g2d.fill_rect(rect) g2d.main_loop()
def update(): if g2d.key_pressed("ArrowUp"): turtle.go_up() elif g2d.key_pressed("ArrowRight"): turtle.go_right() elif g2d.key_pressed("ArrowDown"): turtle.go_down() elif g2d.key_pressed("ArrowLeft"): turtle.go_left() elif (g2d.key_released("ArrowRight") or g2d.key_released("ArrowDown") or g2d.key_released("ArrowLeft")): turtle.stay() arena.move_all() g2d.clear_canvas() for a in arena.actors(): if a.symbol() != (0, 0, 0, 0): print(a.symbol()) g2d.draw_image_clip(sprites, a.symbol(), a.position()) else: g2d.fill_rect(a.position())
def tick(self): self.handle_keyboard() arena = self._game.arena() arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): if a.symbol() != (0, 0, 0, 0): g2d.draw_image_clip(self._sprites, a.symbol(), a.position()) else: g2d.fill_rect(a.position()) lives = "Lives: " + str(self._game.hero().lives()) toplay = "Time: " + str(self._game.remaining_time()) g2d.draw_text(lives + " " + toplay, (0, 0), 24) if self._game.game_over(): g2d.alert("Game over") g2d.close_canvas() elif self._game.game_won(): g2d.alert("Game won") g2d.close_canvas()
#!/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()
def tick(): g2d.clear_canvas() # BG b1.move() b2.move() g2d.fill_rect(b1.position()) # FG g2d.fill_rect(b2.position()) # FG
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()
griglia di colori mostra una griglia di rettangoli in orizzontale aumenta gradatamentela componente di blu in verticale aumentare gradatamente la componente di verde ''' import g2d LAR, ALT = 600, 400 # dimensione canvas g2d.init_canvas((LAR, ALT)) colonne = int(g2d.prompt('numero colonne: ')) righe = int(g2d.prompt('numero righe: ')) w = LAR / colonne # larghezza rettangolo h = ALT / righe # altezza rettangolo d_blue, d_green = 0, 0 if colonne > 1: d_blue = 255.0 / (colonne - 1) # delta blue if righe > 1: d_green = 255.0 / (righe - 1) # delta green for r in range(righe): for c in range(colonne): colore = 0, int(d_green * r), int(d_blue * c) g2d.set_color(colore) rettangolo = int(w * c), int(h * r), int(w - 1), int(h - 1) g2d.fill_rect(rettangolo) g2d.main_loop()
def tick(): if g2d.key_pressed("Enter"): b.uturn() g2d.clear_canvas() b.move() g2d.fill_rect(b.position())
def update(): g2d.clear_canvas() # BG b1.move() b2.move() g2d.fill_rect(b1.position()) # FG g2d.fill_rect(b2.position()) # FG
#!/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
def update(): g2d.clear_canvas() # BG for b in balls: b.move() g2d.fill_rect(b.position()) # FG
def tick(): g2d.clear_canvas() for a in actors: a.move() g2d.fill_rect(a.position())
def update(): g2d.clear_canvas() b.move() g2d.fill_rect(b.position())
import g2d g2d.init_canvas((300, 300)) for i in range(5): ## range(0, 5) x = i * 40 y = x red = i * 60 g2d.set_color((red, 0, 0)) g2d.fill_rect((x, y, 140, 140)) 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 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()
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()
- chiede all'utente un numero n - disegna n quadrati - tutti con lato di 100 pixel - ciascuno in posizione casuale - ciascuno con un colore casuale ''' import g2d from random import randint dim = 600 # dimensione canvas lato = 100 # lato del quadrato g2d.init_canvas((dim, dim)) #n = int(input("numero quadrati: ")) n = int(g2d.prompt("numero quadrati: ")) i = 0 while i < n: # coordinate x = randint(0, dim - lato) y = randint(0, dim - lato) # colore colore = randint(0, 255), randint(0, 255), randint(0, 255) # disegno g2d.set_color(colore) g2d.fill_rect((x, y, lato, lato)) i += 1 g2d.main_loop()
def tick(): g2d.clear_canvas() a.move() g2d.fill_rect(a.position())
def tick(): g2d.clear_canvas() # BG for b in balls: b.move() g2d.fill_rect(b.position()) # FG
def update(): g2d.clear_canvas() for a in aliens: a.move() g2d.fill_rect(a.position())
#!/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()
@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()
#!/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()
def tick(): g2d.clear_canvas() for b in balls: b.move() g2d.set_color(b.color()) g2d.fill_rect(b.position())
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()