Le jeu de dés que j’ai découvert sur AC Valhalla.
""" Orlog - AC Valhalla """ from random import randint from kandinsky import fill_rect, draw_string from time import sleep from ion import keydown dices = (0,0,11,20,30,41,0,0,11,21,30,40,0,0,10,21,31,40,0,0,10,20,31,41,0,0,11,20,31,40,0,0,10,21,30,41) graphics = ((3,5,10,20,104,112,240,192),(15,5,7,9,16,224,96,32),(24,126,255,153,255,153,219,90),(60,66,165,153,153,165,66,60),(4,118,15,20,38,66,70,60),(60,66,153,161,173,153,66,60)) try: from kandinsky import get_keys os_color = (192,53,53) except: os_color = (255,183,52) class Player: def __init__(self,gods,x,y,rev): """ Start of game """ self.life = 15 self.token = 0 self.remaining = [0,1,2,3,4,5] self.rolling = 0 self.dices = [] self.first = 0 self.x = x self.y = y self.rev = rev self.god_A,self.god_B,self.god_C = gods def roll(self): global dices x = self.x+174 y = self.y+20+39*(not self.rev) j = 0 self.rolling += 1 draw = [(0,0) for _ in range(len(self.remaining))] for i in self.remaining: to_draw = dices[randint(i * 6, i * 6 + 5)] draw[j] = (to_draw,i) if to_draw % 10: fill_rect(j*21+x, y, 20, 20, os_color) fill_rect(j*21+x+1, y+1, 18, 18, (255,)*3) pixel_art(graphics[to_draw // 10], j*21+x+2, y+2) j += 1 index = 0 changed = 1 select = [0 for _ in range(len(self.remaining))] while self.rolling != 3: if changed: fill_rect(self.x+174,self.y+84-39*self.rev,126,5,(255,)*3) j = 0 for i in select: if i: fill_rect(self.x+177+21*j, self.y+85-39*self.rev,16,4,os_color) j += 1 fill_rect(self.x+176+21*index, self.y+84-39*self.rev,18,5,os_color) changed = 0 sleep(0.2) if keydown(0): index = (index - 1) % len(self.remaining) changed = 1 if keydown(3): index = (index + 1) % len(self.remaining) changed = 1 if keydown(4): select[index] = 1 - select[index] changed = 1 if keydown(6): break for i in range(len(self.remaining)): if select[i] or self.rolling == 3: self.remaining.remove(draw[i][1]) self.dices += [draw[i][0]] self.rolling %= 3 fill_rect(x,y,126,30,(255,)*3) return self.draw_dices() def draw_dices(self): j = 0 x = self.x+20 y = self.y+20+39*(not self.rev) for i in self.dices: if i % 10: fill_rect(j*21+x, y, 20, 20, os_color) fill_rect(j*21+x+1, y+1, 18, 18, (255,)*3) pixel_art(graphics[i // 10], j*21+x+2, y+2) j += 1 def clean_board(self, l=126): x = self.x+20 y = self.y+20+39*(not self.rev) fill_rect(x,y,l,21,(255,)*3) def resolve(self, to_blit, turn): x = self.x+20 y = self.y+20+39*(not self.rev) to_count = ((0, 2), (1, 3), (2, 0), (3, 1), (4, -1), (-1, 4)) decalage = 0 for k in range(6): added = 0 for i in range(6): if self.dices[i] // 10 == to_count[k][turn]: if self.dices[i] % 10: fill_rect(int(decalage*21)+x, y, 20, 20, os_color) fill_rect(int(decalage*21)+x+1, y+1, 18, 18, (255,)*3) pixel_art(graphics[self.dices[i] // 10], int(decalage*21)+x+2, y+2) decalage += 1 added += 1 decalage += max(to_blit[k]) - added + 0.5 def draw_life(self): x = self.x+20+220*(not self.rev) y = self.y+10+44*self.rev for i in range(15): fill_rect(x+12*(i%5)+2,y+12*((self.rev and 3-i//5 or i//5)-1)+1,8,10,i<self.life and (180,255,160) or (255,)*3) fill_rect(x+12*(i%5)+1,y+12*((self.rev and 3-i//5 or i//5)-1)+2,10,8,i<self.life and (180,255,160) or (255,)*3) def draw_token(self,turn): x = self.x+149 y = self.y+10+44*self.rev pixel_art(graphics[5],x+1,y,turn and os_color or (0,)*3) fill_rect(x,y+25,20,10,(255,)*3) draw_string(str(self.token), x + 4*(self.token<10), y+25) def add_token(self): for i in self.dices: if i%10: self.token += 1 def __isub__(self, other): x = self.x+20+220*(not self.rev) y = self.y+10+44*self.rev for i in range(15): if self.life-other<=i<self.life: fill_rect(x+12*(i%5)+2,y+12*((self.rev and 3-i//5 or i//5)-1)+1,8,10,(255,0,0)) fill_rect(x+12*(i%5)+1,y+12*((self.rev and 3-i//5 or i//5)-1)+2,10,8,(255,0,0)) self.life -= other sleep(0.5) self.draw_life() return self def pixel_art(t,x,y,color=(0,)*3): for i in range(8): for j in range(8): fill_rect(x+j*2, y+i*2, 2, 2, (t[i] >> (7-j)) & 1 and color or (255,)*3) def pre(): draw_string("Orlog", 135, 20, os_color) draw_string("Le but : tuer l'adversaire",30,50) draw_string("Pour attaquer : ou", 30, 70) pixel_art(graphics[0], 185, 70) pixel_art(graphics[1], 240, 70) draw_string("Pour défendre :",30,90) draw_string("bloque", 210, 90) draw_string("bloque", 210, 110) pixel_art(graphics[2], 185, 90) pixel_art(graphics[3], 185, 110) pixel_art(graphics[0], 275, 90) pixel_art(graphics[1], 275, 110) draw_string("Un dé donne 1",30,130) draw_string("entouré", 90,130,os_color) pixel_art(graphics[5],245,130,os_color) draw_string(" permet de voler",30,150) pixel_art(graphics[4],30,150) pixel_art(graphics[5],210,150,os_color) draw_string(" permet d'invoquer les",30,170) draw_string("dieux et leurs capacités",30,190) pixel_art(graphics[5],30,170,os_color) while not keydown(4): pass fill_rect(0,0,320,222,(255,)*3) def main(): players = [Player((0,0,0), 0, 10, 0), Player((0,0,0), 0, 110, 1)] turn = randint(0,1) players[turn].first = 1 for k in (0,1): players[k].draw_life() while players[0].life > 0 and players[1].life > 0: players[turn].draw_token(1) players[1-turn].draw_token(0) for i in range(3): players[turn].roll() players[1-turn].roll() for k in (0,1): players[k].clean_board() to_blit = [[],[],[],[],[],[]] to_count = ((0, 2), (1, 3), (2, 0), (3, 1), (4, -1), (-1, 4)) player_o = list(map(lambda x: x//10,players[turn].dices)) player_t = list(map(lambda x: x//10,players[1-turn].dices)) for i in range(6): to_blit[i] = [player_o.count(to_count[i][0]), player_t.count(to_count[i][1])] players[turn].resolve(to_blit, 0) players[1-turn].resolve(to_blit, 1) """ LES DIEUX """ for k in (0,1): players[k].add_token() players[turn].draw_token(1) players[1-turn].draw_token(0) decalage = 0 for k in (0,1): if to_blit[k][0] - to_blit[k][1] > 0: players[1-turn] -= to_blit[k][0] - to_blit[k][1] decalage += max(to_blit[k]) + 0.5 for i in (0,1): players[i].clean_board(int(decalage*21)) sleep(1.2) if players[1-turn].life <= 0: break for k in (2,3): if to_blit[k][1] - to_blit[k][0] > 0: players[turn] -= to_blit[k][1] - to_blit[k][0] decalage += max(to_blit[k]) + 0.5 for i in (0,1): players[i].clean_board(int(decalage*21)) sleep(1.2) if players[turn].life <= 0: break for k in (4,5): players[(turn,1-turn)[k%2]].token += min(max(to_blit[k]), players[(1-turn,turn)[k%2]].token) players[(1-turn,turn)[k%2]].token -= min(max(to_blit[k]), players[(1-turn,turn)[k%2]].token) players[turn].draw_token(1) players[1-turn].draw_token(0) decalage += max(to_blit[k]) + 0.5 for i in (0,1): players[i].clean_board(int(decalage*21)) sleep(1.2) for k in (0,1): players[k].dices = [] players[k].remaining = [0,1,2,3,4,5] turn = 1 - turn pre() main()