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 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) 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 tick(): if g2d.key_pressed("ArrowUp"): turtle.go_up(True) elif g2d.key_released("ArrowUp"): turtle.go_up(False) if g2d.key_pressed("ArrowRight"): turtle.go_right(True) elif g2d.key_released("ArrowRight"): turtle.go_right(False) if g2d.key_pressed("ArrowDown"): turtle.go_down(True) elif g2d.key_released("ArrowDown"): turtle.go_down(False) if g2d.key_pressed("ArrowLeft"): turtle.go_left(True) elif g2d.key_released("ArrowLeft"): turtle.go_left(False) arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): sym, pos = a.symbol(), a.position() g2d.draw_image_clip(sprites, a.symbol(), a.position())
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()