def main(): global dx, dy g2d.init_canvas((ARENA_W, ARENA_H)) dx = int(g2d.prompt("dx?")) dy = int(g2d.prompt("dy?")) g2d.handle_events(update, keydn, None) g2d.main_loop(5) # Call update 5 times/second
def main(): global arena arena = Arena((480, 360)) Vehicle(arena, (40, 40), 5) Vehicle(arena, (80, 80), -5) g2d.init_canvas(arena.size()) g2d.main_loop(tick)
def main(): global b1, b2 b1 = Ball(40, 80) b2 = Ball(80, 40) g2d.init_canvas((ARENA_W, ARENA_H)) g2d.handle_events(update) g2d.main_loop()
def main(): global arena arena = Arena((320, 240)) Alien(arena, (40, 40)) Alien(arena, (80, 80)) g2d.init_canvas(arena.size()) g2d.main_loop(tick)
def main(): g2d.init_canvas((A, B)) global players, rover2 players = g2d.confirm("2 Giocatori?") print(players) if players: rover2 = Rover(arena, 200, B - 100) g2d.main_loop(update)
def main(): global b1, b2, img b1 = FrogBall(40, 80) b2 = FrogBall(80, 40) g2d.init_canvas((ARENA_W, ARENA_H)) img = g2d.load_image("ball.png") g2d.handle_events(update, keydn, None) g2d.main_loop(5)
def main(): global arena arena = Arena(320, 240) Alien(arena, 40, 40) Alien(arena, 80, 80) g2d.init_canvas(arena.size()) g2d.handle_events(update) g2d.main_loop()
def main(): global arena, turtle, sprites arena = Arena(320, 240) turtle = Turtle(arena, 80, 80) g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.main_loop(tick)
def main(): global arena, sprites arena = Arena(320, 240) Ball(arena, 40, 80) Ball(arena, 85, 40) Wall(arena, 115, 80, 100, 20) g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.main_loop(tick)
def main(): global planets, canvas_w, canvas_h planets = [] # ex.: try to use a list comprehension for i in range(5): p = Planet(100 + i * 40) planets.append(p) canvas_w, canvas_h = 600, 600 g2d.init_canvas((canvas_w, canvas_h)) g2d.main_loop(tick)
def main(): global arena, sprites arena = Arena(320, 240) Ball(arena, 40, 80) Ball(arena, 85, 40) Wall(arena, 115, 80, 100, 20) g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.handle_events(update) g2d.main_loop()
def main(): global arena, turtle, sprites arena = Arena(320, 240) turtle = Turtle(arena, 80, 80) g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.handle_events(update, keydown, keyup) g2d.main_loop()
def main(): global planets, canvas_w, canvas_h planets = [] # ex.: try to use a list comprehension for i in range(5): p = Planet(100 + i * 40) planets.append(p) canvas_w, canvas_h = 600, 600 g2d.init_canvas((canvas_w, canvas_h)) g2d.handle_events(draw_frame) g2d.main_loop()
def main(): global arena, view_x, view_y, view_w, view_h, background arena = Arena(500, 250) FallingBall(arena, 40, 80) FallingBall(arena, 80, 40) Plane(arena, 60, 60) view_x, view_y, view_w, view_h = 0, 0, 300, 200 g2d.init_canvas((view_w, view_h)) background = g2d.load_image("https://raw.githubusercontent.com/tomamic/fondinfo/gh-pages/images/oop/viewport.png") g2d.main_loop(tick)
def reset(self): self.sprites = g2d.load_image( "https://tomamic.github.io/images/sprites/moon-patrol.png") self.background = g2d.load_image( "https://tomamic.github.io/images/sprites/moon-patrol-bg.png") #self.game_over = g2d.load_image("moon-patrol-game-over.png") Not Working self._arena = self._game.arena() self._hero = self._game.hero() self._is_game_starting = True g2d.init_canvas(self._arena.size()) if self._settings['level'] == 'level_1': g2d.main_loop(self.level_1_tick) else: g2d.main_loop(self.level_2_tick)
def __init__(self): self._game = BubbleBobbleGame() self._total_levels = self._game.total_levels() self._sprites = g2d.load_image("bubble-bobble.png") self._background = g2d.load_image("bubble-bobble-maps.png") self._menu = g2d.load_image("BB_main_menu.png") self._end = g2d.load_image("BB_end.png") self._ready = g2d.load_image("BB_ready_not-ready.png") self._x_y_background = [(0, 0), (0, 0), (512, 0), (0, 0), (0, 0)] self._player1_ready, self._player2_ready = False, False self._time_of_win, self._time_of_lost = 0, 0 self._current_level = 0 self._numbers_scores1 = [] self._numbers_scores2 = [] self._x1, self._x2 = 2, 450 self._win, self._lost = True, True g2d.init_canvas(self._game.arena().size()) g2d.main_loop(self.tick)
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()
if self._y < 0 + distance: self._dy = -self._dy def val(self): return self._x, self._y def color(self): return self._r, self._green, self._b 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() A = 500 B = 500 g2d.init_canvas((A, B)) b = FallingBall( 20, 20, A) # l'esercizio sta tutto qui, nel creare due oggetti invece che uno b1 = FallingBall(300, 300, A) balls = [b, b1] g2d.main_loop(update, 30)
def main(): global b b = Ball(40, 40) g2d.init_canvas((W, H)) g2d.handle_events(update) 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 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()
#!/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()
def main(): g2d.init_canvas((263, 263)) g2d.set_color((127, 0, 0)) sierpinski((10, 10, 243, 243)) 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 main(): global a a = Spiral() g2d.init_canvas((ARENA_W, ARENA_H)) g2d.handle_events(update) 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 sys; sys.path.append("../examples") import g2d 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) g2d.init_canvas((600, 600)) infinite_circles(10, 10, 580, 580, (0, 0, 255), (255, 255, 0)) g2d.main_loop()
def main(): global sprites g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.handle_events(update, keydown, keyup) 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 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 gui_play(game: BoardGame): g2d.init_canvas((game.cols() * W, game.rows() * H)) ui = BoardGameGui(game) g2d.handle_events(None, ui.mousedown, ui.mouseup) g2d.main_loop()
''' import g2d def center(rect: (int, int, int, int)) -> (int, int): x, y, w, h = rect 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: return if level % 2 == 0: rect1 = x, y, w / 2, h rect2 = x + w / 2, y, w / 2, h else: rect1 = x, y, w, h / 2 rect2 = x, y + h / 2, w, h / 2 g2d.draw_line(center(rect1), center(rect2)) htree(rect1, level - 1) htree(rect2, level - 1) level = int(input('level? ')) ## -1 = infinite side = 600 g2d.init_canvas((side, side)) htree((0, 0, side, side), level) g2d.main_loop()
if isinstance(a, Wall): g2d.fill_rect(a.position()) else: g2d.draw_image_clip(sprites, a.symbol(), a.position()) def keydown(code): print("#"+code+"#") if code == "Space": mario.jump() elif code == "ArrowLeft": mario.go_left() elif code == "ArrowRight": mario.go_right() def keyup(code): if code in ("ArrowLeft", "ArrowRight"): mario.stay() arena = Arena(320, 240) mario = Mario(arena, 80, 80) CrazyGoomba(arena, 180, 80) CrazyGoomba(arena, 150, 80) Wall(arena, 200, 80, 80, 20) Wall(arena, 120, 160, 80, 20) Wall(arena, 0, 220, 320, 20) g2d.init_canvas(arena.size()) sprites = g2d.load_image("sprites.png") g2d.handle_events(update, keydown, keyup) g2d.main_loop()
def main(): size = arena.size() g2d.init_canvas((size.x, size.y)) g2d.main_loop(tick)
@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()
def main(): g2d.init_canvas((320, 240)) g2d.main_loop(tick)
def main(): g2d.init_canvas((ARENA_W, ARENA_H)) g2d.main_loop(update, 1000 // 30) # Millis
def main(): g2d.init_canvas((ARENA_W, ARENA_H)) g2d.handle_events(update) # Set update to be called... g2d.main_loop() # ... at 30 fps
import g2d from random import randrange 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) g2d.init_canvas((640, 480)) g2d.handle_events(None, keydown, None) g2d.main_loop()
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()
#!/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 main(): global b1, b2 b1 = Ball((40, 80)) b2 = Ball((80, 40)) g2d.init_canvas((ARENA_W, ARENA_H)) g2d.main_loop(tick)
def __init__(self, g: BoardGame): self._game = g self._downtime = 0 g2d.init_canvas((g.cols() * W, g.rows() * H)) self.update_buttons() g2d.handle_mouse(self.mousedown, self.mouseup)
#!/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()
def main(): g2d.init_canvas((ARENA_W, ARENA_H)) g2d.main_loop(tick) # call tick 30 times/second
miglioramento: disegnare anche le tacche dei minuti, più piccole (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
#!/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()
#!/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 p2_oop_alien import Alien def update(): g2d.clear_canvas() for a in aliens: a.move() g2d.fill_rect(a.position()) aliens = [Alien(40, 40), Alien(80, 80), Alien(120, 40)] g2d.init_canvas((320, 240)) g2d.handle_events(update) 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 from random import randrange 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) g2d.init_canvas((480, 360)) g2d.main_loop(tick)
#!/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 sys sys.path.append("../examples") import g2d 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) g2d.init_canvas((600, 600)) infinite_circles(10, 10, 580, 580, (0, 0, 255), (255, 255, 0)) g2d.main_loop()
def main(): global balls balls = [Ball((40, 80)), Ball((80, 40)), Ball((120, 120))] g2d.init_canvas((ARENA_W, ARENA_H)) g2d.main_loop(tick)
def main(): g2d.init_canvas((ARENA_W, ARENA_H)) g2d.main_loop(tick, 5)
#!/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 main(): g2d.init_canvas((600, 600)) g2d.handle_events(update) g2d.main_loop()
def main(): g2d.init_canvas(vals(arena.size())) g2d.handle_events(update, keydown, keyup) g2d.main_loop()
def main(): global b b = Vehicle((40, 40), 5) g2d.init_canvas((W, H)) g2d.main_loop(tick)
def main(): global balls balls = [Ball(40, 80), Ball(80, 40), Ball(120, 120)] g2d.init_canvas((ARENA_W, ARENA_H)) g2d.handle_events(update) g2d.main_loop()
def main(): global a a = Alien((40, 40)) g2d.init_canvas((480, 360)) g2d.main_loop(tick, 10)
def main(): g2d.init_canvas(arena.size()) g2d.main_loop(tick)
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()
def main(): g2d.init_canvas((320, 240)) g2d.handle_events(update) g2d.main_loop()