Ejemplo n.º 1
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):
                value = self._game.value_at(x, y)
                if value == "":
                    g2d.set_color((204, 204, 204))
                elif value == "W":
                    g2d.set_color((255, 255, 255))
                elif value == "B":
                    g2d.set_color((0, 0, 0))
                g2d.fill_rect((x * W + 1, y * H + 1, W - 1, H - 1))
        g2d.update_canvas()

        if self._game.finished():
            g2d.alert(self._game.message())
            g2d.close_canvas()
def sierp(x, y, w, h):
    if w < 1 and h < 1:
        return

    w2, h2 = w / 2, h / 2
    g2d.fill_rect((x + w2, y, w2, h2))

    sierp(x, y, w2, h2)
    sierp(x, y + h2, w2, h2)
    sierp(x + w2, y, w2, h2)
def main():
    width, height = 512, 512
    g2d.init_canvas((width, height))

    g2d.set_color((0, 0, 0))  #sfondo nero
    g2d.fill_rect((0, 0, width, height))
    g2d.set_color((255, 255, 255))

    sierp(0, 0, width, height)
    g2d.main_loop()
def sierpinski(x,y, w, h):
    w3,h3=w//3, h//3

    if w3<1 or h3<1:
        return
    for row in range (3):
        for col in range (3):
            x3,y3=x+col*w3, y+row*w3
            if row==1 and col==1:
                g2d.fill_rect((x+col*w3, y+row*w3, w3, h3))
            else:
                sierpinski(x3, y3, w3, h3)
def triangle(x, y, w, h, level):
    # caso base
    if level == 0 or w < 5 or h < 5:
        return

    w2 = w / 2
    h2 = h / 2

    # coloro solo il secondo quadrante
    g2d.fill_rect((x + w2, y, w2, h2))

    # applicazione della ricorsione su ogni quandrante rimanente
    triangle(x, y, w2, h2, level - 1)
    triangle(x, y + h2, w2, h2, level - 1)
    triangle(x + w2, y + h2, w2, h2, level - 1)
Ejemplo n.º 6
0
 def fill(self):
     g2d.set_color((99, 92, 92))
     g2d.fill_rect((self._x, self._y, self._w, self._h))
    def tick(self):
        global CONT_STEP, DECISION, DIRECTION

        # controlli da tastiera separati per ogni giocatore e con gestione della direzione per la bolla. A destra ←→↑↓ ; a sinistra WASD
        if g2d.key_pressed("ArrowUp"):
            self._game.hero1().go_up()
        elif g2d.key_pressed("ArrowRight"):
            self._game.hero1().go_right()
            DIRECTION = 1
        elif g2d.key_pressed("ArrowLeft"):
            self._game.hero1().go_left()
            DIRECTION = 0
        elif (g2d.key_released("ArrowLeft") or g2d.key_released("ArrowRight")):
            self._game.hero1().stay()
        elif g2d.key_pressed("ArrowDown"):
            dragon_x, dragon_y, dragon_w, dragon_h = self._game.hero1(
            ).position()
            if DIRECTION == 0:
                dragon_dimension = dragon_x - dragon_w
                speed = -SPEED
            elif DIRECTION == 1:
                dragon_dimension = dragon_x + dragon_w
                speed = SPEED
            self._game.bubble().append(
                Bubble(self._game.arena(), (dragon_dimension, dragon_y),
                       speed))

        if g2d.key_pressed('w'):
            self._game.hero().go_up()
        elif g2d.key_pressed("d"):
            self._game.hero().go_right()
            DIRECTION = 1
        elif g2d.key_pressed("a"):
            self._game.hero().go_left()
            DIRECTION = 0
        elif (g2d.key_released("a") or g2d.key_released("d")):
            self._game.hero().stay()
        elif g2d.key_pressed("s"):
            dragon_x, dragon_y, dragon_w, dragon_h = self._game.hero(
            ).position()
            if DIRECTION == 0:
                dragon_dimension = dragon_x - dragon_w
                speed = -SPEED
            elif DIRECTION == 1:
                dragon_dimension = dragon_x + dragon_w
                speed = SPEED
            self._game.bubble().append(
                Bubble(self._game.arena(), (dragon_dimension, dragon_y),
                       speed))

        # decisione del movimento per il nemico aggiornando il numero di passi con il tick
        CONT_STEP += 1
        if CONT_STEP == STEP_DIR_FRAME:
            for enemy in self._game.enemy():
                DECISION = randint(1, 3)
                enemy.decision(DECISION)
            CONT_STEP = 0
        else:
            for enemy in self._game.enemy():
                enemy.decision(DECISION)

        # controllo se il giocatore è vivo e nel caso lo rimuov0 e lo riaggiungo nel punto di origine
        if self._game.hero().lives() == 0:
            self._game.arena().remove(self._game.hero())
            g2d.alert("Sei stato eliminato!")
            self._game.hero().restore()

        if self._game.hero1().lives() == 0:
            self._game.arena().remove(self._game.hero1())
            g2d.alert("Sei stato eliminato!")
            self._game.hero1().restore()

        self._game.arena().move_all()  # Game logic

        # disegno degli elementi grafici
        g2d.clear_canvas()

        for i in self._game.platform():
            i.fill()

        for a in self._game.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())

        # gestione delle vite, dei punti e del tempo nell'interfaccia
        lives_player = "Player1 Lives: " + str(self._game.hero().lives())
        lives_player1 = "Player2 Lives: " + str(self._game.hero1().lives())
        points = "Points: " + str(self._game.hero().points() +
                                  self._game.hero1().points())
        toplay = "Time: " + str(self._game.remaining_time())

        g2d.draw_text(lives_player + "    " + toplay + "    " + points, (0, 0),
                      24)
        g2d.draw_text(lives_player1, (0, 24), 24)

        # creazione degli alert per il termine del gioco
        if self._game.game_over():
            g2d.alert("Game over")
            g2d.close_canvas()

        elif self._game.game_won():
            g2d.alert("Game won")
            g2d.close_canvas()
import g2d_pyg as g2d


def triangle(x, y, w, h, level):
    # caso base
    if level == 0 or w < 5 or h < 5:
        return

    w2 = w / 2
    h2 = h / 2

    # coloro solo il secondo quadrante
    g2d.fill_rect((x + w2, y, w2, h2))

    # applicazione della ricorsione su ogni quandrante rimanente
    triangle(x, y, w2, h2, level - 1)
    triangle(x, y + h2, w2, h2, level - 1)
    triangle(x + w2, y + h2, w2, h2, level - 1)


w, h = 512, 512  #uso una potenza di 2 per non avere degli errori grafici
level = 5
g2d.init_canvas((w, h))

g2d.set_color((0, 0, 0))
g2d.fill_rect((0, 0, w, h))

g2d.set_color((255, 255, 255))
triangle(0, 0, w, h, level)

g2d.main_loop()