def __init__(self, taille=15, taille_carre=30, color_carre=(255, 255, 255), nb_apple=5): """create the game Args: taille (int, optional): taille de la fenetre (en nombre de carre). Defaults to 20. taille_carre (int, optional): taille du carré (en px). Defaults to 30. color_carre (tuple, optional): color . Defaults to (255,255,255) white. nb_apple (int, optional): [nombre de pomme sur l'ecran]. Defaults to 5. """ self.taille = taille self.taille_carre = taille_carre self.color_carre = color_carre self.colors = { "white": (255, 255, 255), "green": (142, 254, 29), "red": (221, 15, 5) } self.wind = pygame.display.set_mode( (self.taille * self.taille_carre + 4, self.taille * self.taille_carre + 4)) self.create_zone() self.array = self.create_grid_array() self.serpent = Serpent(self) self.serpent.spawn() self.restart = False self.point = 0 for i in range(nb_apple): self.create_apple()
def finalBattle(): if 'finalbattle' in savedata: pass # make the river passable else: savedata['finalbattle'] = 'True' p = system.engine.player def walkUp(): p.move(dir.UP, 96) p.anim = 'walk' for n in range(96): yield None p.state = walkUp() for n in range(96): system.engine.tick() system.engine.draw() ika.Video.ShowPage() ika.Input.Update() def noOp(): while True: yield None p.anim = 'stand' p.state = noOp() for n in range(256): # teh earthquake ika.Map.xwin += ika.Random(-4, 5) ika.Map.ywin += ika.Random(-4, 5) system.engine.tick() system.engine.draw() ika.Video.ShowPage() ika.Input.Update() s = Serpent(ika.Entity(25 * 16, 24 * 16, p.layer, 'serpent.ika-sprite')) s.anim = 'appear' system.engine.addEntity(s) for n in range(19, 32): # close off the way back ika.Map.SetTile(n, 38, p.layer, 26) ika.Map.SetTile(n, 39, p.layer, 32) ika.Map.SetObs(n, 38, p.layer, True) system.engine.tick() system.engine.draw() ika.Video.ShowPage() ika.Input.Update() p.state = p.defaultState() s.state = s.roarState() system.engine.synchTime() system.engine.mapThings.append(DeathListener(s))
def __init__(self): self.score = 0 self.interval = self.INITIAL_INTERVAL self.direction = self.INITIAL_DIRECTION self.snake = Serpent(self.INITIAL_DIRECTION, self.INITIAL_SNAKE_LENGTH) self.pomme = Pomme() self.tk = Tk() self.can = Canvas(self.tk, width=500, height=500, bg="black") self.can.pack()
class Game: INITIAL_DIRECTION = (0, -1) INITIAL_SNAKE_LENGTH = 3 INITIAL_INTERVAL = 1000 def __init__(self): self.score = 0 self.interval = self.INITIAL_INTERVAL self.direction = self.INITIAL_DIRECTION self.snake = Serpent(self.INITIAL_DIRECTION, self.INITIAL_SNAKE_LENGTH) self.pomme = Pomme() self.tk = Tk() self.can = Canvas(self.tk, width=500, height=500, bg="black") self.can.pack() def compute_next_frame(self): self.can.delete("all") mange_pomme = self.snake.avance(self.direction, self.pomme) if self.snake.tete_in_queue() or self.snake.hors_zone(): askokcancel("Game Over") exit(0) self.snake.show(self.can) if mange_pomme: self.score += 1 if self.interval > 100: self.interval -= 10 self.pomme.change() self.pomme.show(self.can) self.tk.after(self.interval, self.compute_next_frame) def right(self, event): if self.direction[0] != -1: self.direction = [1, 0] def left(self, event): if self.direction[0] != 1: self.direction = [-1, 0] def up(self, event): if self.direction[1] != 1: self.direction = [0, -1] def down(self, event): if self.direction[1] != -1: self.direction = [0, 1] def run(self): self.tk.bind("<d>", self.right) self.tk.bind("<q>", self.left) self.tk.bind("<z>", self.up) self.tk.bind("<s>", self.down) self.compute_next_frame() self.tk.mainloop()
class Game: def __init__(self, taille=15, taille_carre=30, color_carre=(255, 255, 255), nb_apple=5): """create the game Args: taille (int, optional): taille de la fenetre (en nombre de carre). Defaults to 20. taille_carre (int, optional): taille du carré (en px). Defaults to 30. color_carre (tuple, optional): color . Defaults to (255,255,255) white. nb_apple (int, optional): [nombre de pomme sur l'ecran]. Defaults to 5. """ self.taille = taille self.taille_carre = taille_carre self.color_carre = color_carre self.colors = { "white": (255, 255, 255), "green": (142, 254, 29), "red": (221, 15, 5) } self.wind = pygame.display.set_mode( (self.taille * self.taille_carre + 4, self.taille * self.taille_carre + 4)) self.create_zone() self.array = self.create_grid_array() self.serpent = Serpent(self) self.serpent.spawn() self.restart = False self.point = 0 for i in range(nb_apple): self.create_apple() def create_zone(self): #self.wind.fill((255,255,255)) for x in range(4, self.taille * self.taille_carre + 4, self.taille_carre): for y in range(4, self.taille * self.taille_carre + 4, self.taille_carre): Rect = pygame.Rect( (x, y), (self.taille_carre - 4, self.taille_carre - 4)) pygame.draw.rect(self.wind, self.color_carre, Rect) # print("rect created") def change_square_color(self, x, y, color): x *= self.taille_carre x += 4 y *= self.taille_carre y += 4 color = self.colors[color] Rect = pygame.Rect((x, y), (self.taille_carre - 4, self.taille_carre - 4)) pygame.draw.rect(self.wind, color, Rect) def create_grid_array(self): array = np.tile("white", [self.taille, self.taille]) return array def create_apple(self): rand_x = random.randint(0, self.taille - 1) rand_y = random.randint(0, self.taille - 1) if self.array[rand_x, rand_y] == "white": self.array[rand_x, rand_y] = "red" self.change_square_color(rand_x, rand_y, "red") else: self.create_apple() #print (array) def game_restart(self): self.restart = True
def __init__(self): """Construction de la fenêtre principale""" Tk.__init__(self) self.title('Snake Byte') self.geometry("+100+100") self.configure(bg='light grey') self.resizable(width=0, height=0) # Fenètre non redimensionnable manuellement [self.largeur, self.son] = initLargeur() self.hauteur = 3 * self.largeur // 4 self.croque = 0 self.fini = 1 #--- Polices de caractères self.helv18 = tkFont.Font(family='Helvetica', size=18) self.helv18b = tkFont.Font(family='Helvetica', size=18, weight='bold') self.helv24 = tkFont.Font(family='Helvetica', size=24) self.helv24b = tkFont.Font(family='Helvetica', size=24, weight='bold') self.helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold') #--- Intialisation des variables Tkinter self.score = StringVar() self.hiScore = StringVar() self.largstr = StringVar() self.largstr.set(str(self.largeur)) self.hiScore.set(self.lectureHiScore(self.largeur)) self.terrain = Canvas(self) self.terrain.configure(width=self.largeur * 20, height=self.hauteur * 20, bg="green") self.terrain.grid(row=10, columnspan=5, padx=5, pady=5) Label(self, text='Snake Byte', font=self.helv36, bg='green', fg="#cc9f26").grid(row=9, column=2, padx=25, pady=5) Label(self, text='Score :', font=self.helv18).grid(row=9, column=0, padx=5, pady=5, sticky=E) Label(self, textvariable=self.score, font=self.helv18b, bg='light grey').grid(row=9, column=1, padx=5, pady=5, sticky=W) Label(self, text='Hi Score :', font=self.helv18).grid(row=9, column=3, padx=5, pady=5, sticky=E) Label(self, textvariable=self.hiScore, font=self.helv18b, bg='dark grey', fg='white').grid(row=9, column=4, padx=5, pady=5, sticky=W) Label(self, text='Largeur :', font=self.helv18, bg='light grey').grid(row=11, column=0, padx=5, pady=5, sticky=E) self.afficheLargeur = Label(self, textvariable=self.largstr, font=self.helv18b, bg='light grey', fg='black').grid(row=11, column=1, padx=5, pady=5, sticky=W) Label( self, text= 'Démarrer : [Espace] - Diriger le serpent : [<-] [->]\nPréférences :[P] - Quitter : [esc]', bg='dark grey', fg='white').grid(row=11, column=2, columnspan=3, pady=5) self.serpent = Serpent(self.terrain, self.largeur, self.hauteur) self.pomme = Pomme(self.terrain) self.bind("<Left>", self.serpent.tourneGauche) self.bind("<Right>", self.serpent.tourneDroite) self.bind("<space>", self.depart) self.bind("<Escape>", self.quitter) self.bind("<p>", self.preferences) self.bind("<P>", self.preferences) self.mainloop()
class SnakeByte( Tk ): #la classe SnakeByte dérive de la classe Tk de tkinter et hérite de toutes ses méthodes def __init__(self): """Construction de la fenêtre principale""" Tk.__init__(self) self.title('Snake Byte') self.geometry("+100+100") self.configure(bg='light grey') self.resizable(width=0, height=0) # Fenètre non redimensionnable manuellement [self.largeur, self.son] = initLargeur() self.hauteur = 3 * self.largeur // 4 self.croque = 0 self.fini = 1 #--- Polices de caractères self.helv18 = tkFont.Font(family='Helvetica', size=18) self.helv18b = tkFont.Font(family='Helvetica', size=18, weight='bold') self.helv24 = tkFont.Font(family='Helvetica', size=24) self.helv24b = tkFont.Font(family='Helvetica', size=24, weight='bold') self.helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold') #--- Intialisation des variables Tkinter self.score = StringVar() self.hiScore = StringVar() self.largstr = StringVar() self.largstr.set(str(self.largeur)) self.hiScore.set(self.lectureHiScore(self.largeur)) self.terrain = Canvas(self) self.terrain.configure(width=self.largeur * 20, height=self.hauteur * 20, bg="green") self.terrain.grid(row=10, columnspan=5, padx=5, pady=5) Label(self, text='Snake Byte', font=self.helv36, bg='green', fg="#cc9f26").grid(row=9, column=2, padx=25, pady=5) Label(self, text='Score :', font=self.helv18).grid(row=9, column=0, padx=5, pady=5, sticky=E) Label(self, textvariable=self.score, font=self.helv18b, bg='light grey').grid(row=9, column=1, padx=5, pady=5, sticky=W) Label(self, text='Hi Score :', font=self.helv18).grid(row=9, column=3, padx=5, pady=5, sticky=E) Label(self, textvariable=self.hiScore, font=self.helv18b, bg='dark grey', fg='white').grid(row=9, column=4, padx=5, pady=5, sticky=W) Label(self, text='Largeur :', font=self.helv18, bg='light grey').grid(row=11, column=0, padx=5, pady=5, sticky=E) self.afficheLargeur = Label(self, textvariable=self.largstr, font=self.helv18b, bg='light grey', fg='black').grid(row=11, column=1, padx=5, pady=5, sticky=W) Label( self, text= 'Démarrer : [Espace] - Diriger le serpent : [<-] [->]\nPréférences :[P] - Quitter : [esc]', bg='dark grey', fg='white').grid(row=11, column=2, columnspan=3, pady=5) self.serpent = Serpent(self.terrain, self.largeur, self.hauteur) self.pomme = Pomme(self.terrain) self.bind("<Left>", self.serpent.tourneGauche) self.bind("<Right>", self.serpent.tourneDroite) self.bind("<space>", self.depart) self.bind("<Escape>", self.quitter) self.bind("<p>", self.preferences) self.bind("<P>", self.preferences) self.mainloop() def depart(self, event): "Initialisation et lancement du jeu" if self.fini: # Réinitialisation des paramètres du jeu self.score.set('0') try: self.terrain.delete(self.gameOver) except: None self.fini = 0 [self.largeur, self.son] = initLargeur() self.hauteur = 3 * self.largeur // 4 self.terrain.configure(width=self.largeur * 20, height=self.hauteur * 20, bg="green") self.largstr.set(str(self.largeur)) self.hiScore.set(self.lectureHiScore(self.largeur)) #self.serpent.initialisation() self.serpent.__init__(self.terrain, self.largeur, self.hauteur) try: self.quitterPrefs() except: None test = 0 while test == 0: test = 1 x = randint(1, self.largeur) y = randint(1, self.hauteur) #print(str(x)+" "+str(y)) for piece in self.serpent.corps: if x == piece[0] and y == piece[1]: test = 0 self.pomme.deplacePomme(x, y) self.deplacement() # Lancement de l'animation def fin(self): "Terminer le jeu" if self.son == 'On': self.bell() self.gameOver = self.terrain.create_text(20 * self.largeur // 2, 20 * self.hauteur // 2, text='Game Over', font=self.helv36, fill='white') fichier = open( 'hiscore', 'w') # ouverture d'un fichier vierge d'écriture des high scores fichier.write(str(self.scores)) fichier.close() self.ecritureHiScore(self.largeur) def quitter(self, event): "Quitter le jeu" self.quit() def deplacement(self): "Modification du serpent" if not self.fini: self.fini = self.serpent.deplacement(self.croque) if (self.serpent.corps[0][0], self.serpent.corps[0][1]) == (self.pomme.x, self.pomme.y): if self.son == 'On': self.bell() self.croque = 1 self.score.set(str(eval(self.score.get() + "+1"))) if eval(self.score.get()) > eval(self.hiScore.get()): self.hiScore.set(self.score.get()) test = 0 while test == 0: test = 1 x = randint(1, self.largeur) y = randint(1, self.hauteur) #print(str(x)+" "+str(y)) for piece in self.serpent.corps: if x == piece[0] and y == piece[1]: test = 0 self.pomme.deplacePomme(x, y) else: self.croque = 0 self.after(75, self.deplacement) else: self.fin() def lectureHiScore(self, largeur): self.scores = {} try: fichier = open( 'hiscore', 'r' ) # ouverture du fichier de stockage des high score en lecture self.scores = eval(fichier.read()) fichier.close() hiscore = str(self.scores[largeur]) except: hiscore = "0" return hiscore def ecritureHiScore(self, largeur): self.scores[largeur] = int(self.hiScore.get()) fichier = open('hiscore', 'w') fichier.write(str(self.scores)) fichier.close() def preferences(self, event): try: self.prefs.destroy() except: None self.prefs = Preferences(self) def quitterPrefs(self): self.prefs.destroy()
if __name__ == "__main__": # NE PAS SUPPRIMER CETTE LIGNE # Votre programme principal ne sera pas évalué. # Utilisez-le pour tester votre programme en faisant # les appels de votre choix. # Respectez bien ce niveau d'identation. print("Debut du prog. principal") #[Instanciation et attributs] chat = Animal(3.3, 37) #print(chat._animal_size) print("chat") chat.getPoidsTaille() #[Héritage et Polymorphisme] vipere = Serpent(0.3, 58) print("vipere") vipere.getPoidsTaille() vipere.se_deplacer() aigle = Oiseau(3.45, 78, 450) print("oiseau") aigle.getPoidsTaille() aigle.se_deplacer() #[Super fonction] print("Altitude maximal", aigle.oiseau_alt, "m") #[Encapsulation] chien = Animal(5.6, 37) #chien = Animal(-5.6,37)