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()
def draw(self): g2d.clear_canvas() for a in self._arena.actors(): if type(a) in self._game.bg_list: g2d.draw_image_clip(self.background, a.symbol(), a.position()) g2d.draw_image_clip(self.background, a.symbol(), a.second_position()) else: g2d.draw_image_clip(self.sprites, a.symbol(), a.position()) if a.get_state() == 'delete': self._arena.remove(a) g2d.set_color((255, 255, 255)) g2d.draw_text('Remaining time: ' + str(self._game.remaining_time()), (10, 10), 20) g2d.draw_text('Score: ' + str(self._arena.get_score()), (ARENA_W / 2, 10), 20) g2d.draw_text('Lives: ' + str(hero_lives + 1), (ARENA_W - 80, 10), 20)
def update(): global playgame if (frog[0].getdeath() == 3): pygame.mixer.music.pause() g2d.fill_canvas((0, 0, 0)) g2d.draw_image(gameover, (20, 10)) g2d.draw_text_centered(("SCORE:" + str(score.count(1) * 1000)), (255, 255, 255), (300, 400), 45) elif (playgame == True): x, y, w, h = frog[0].position() if ((x > 43 and x < 85) and (y > 45 and y < 70) and score[0] != 1): score[0] = 1 frog[0].setposition(0) pygame.mixer.music.play() elif ((x > 175 and x < 205) and (y > 45 and y < 70) and score[1] != 1): score[1] = 1 frog[0].setposition(0) pygame.mixer.music.play() elif ((x > 305 and x < 335) and (y > 45 and y < 70) and score[2] != 1): score[2] = 1 frog[0].setposition(0) pygame.mixer.music.play() elif ((x > 435 and x < 475) and (y > 45 and y < 70) and score[3] != 1): score[3] = 1 frog[0].setposition(0) pygame.mixer.music.play() elif ((x > 560 and x < 590) and (y > 45 and y < 70) and score[4] != 1): score[4] = 1 frog[0].setposition(0) pygame.mixer.music.play() if (score.count(1) == 5): pygame.mixer.music.pause() g2d.fill_canvas((0, 0, 0)) g2d.draw_image(win, (20, 10)) g2d.draw_text_centered(("SCORE:" + str(score.count(1) * 1000)), (255, 255, 255), (300, 400), 45) else: frog[0].setcollideraft() frog[0].setcollidefiume() frog[0].setcollideturtle() frog[0].setcollidecrocodile() arena.move_all() g2d.fill_canvas((255, 255, 255)) g2d.draw_image(background, (0, 0)) g2d.draw_text(("SCORE:" + str(score.count(1) * 1000)), (255, 0, 100), (0, 0), 25) g2d.draw_text(("DEATHS FROG:" + str(frog[0].getdeath())), (255, 0, 100), (150, 0), 25) g2d.draw_text(("LIFE FROG:"), (255, 0, 100), (0, 465), 25) for i in range(0, 3 - frog[0].getdeath()): g2d.draw_image_clip(sprite, (100 + i * 18, 465, 18, 15), (14, 369, 18, 15)) for i in range(0, 15): veicoli[i].move() for i in range(0, 12): raft[i].move() for i in range(0, 12): turtle[i].move() for i in range(0, 4): crocodile[i].move() frog[0].move() for a in arena.actors(): if a != fiume and a != land[0] and a != land[1] and a != land[ 2] and a != land[3] and a != land[4] and a != land[ 5] and a != land[6]: g2d.draw_image_clip(sprite, a.position(), a.symbol()) else: g2d.fill_canvas((0, 0, 0)) g2d.draw_image(logo, (20, 10)) g2d.draw_text_centered(("PRESS ANY KEY"), (255, 255, 255), (300, 240), 25)
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 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