power_old7.py

Created by elnix91

Created on September 28, 2025

8.21 KB

debug version of power, the last version is available here


from kandinsky import fill_rect as rect,set_pixel as set,draw_string as ds
from ion import keydown as k
from time import sleep

def menu(nbPlayers=0,first=1):
  rect(0,0,320,222,SKY)
  ds("POWER !",125,20,(255,0,0),SKY)
  ds("Nombre de joueurs:",70,50,(0,0,255),SKY)
  ds("+",155,90,TXT,SKY)
  ds("-",155,150,TXT,SKY)
  while k(4):1
  while not k(4):
    if not k(1)or k(2):V=0
    if k(1):V=-1
    elif k(2):V=1
    if V!=0 or first:
      nbPlayers,first=(nbPlayers+V)%3,0
      ds(str(nbPlayers+2),155,120,TXT,SKY)
      while k(1)or k(2):1
  return nbPlayers+2

def draw_cell(x,y,case=0,first=0):
  if first:
    rect(3+x*24,3+y*24,23,23,COLORS[int(FIELD[y*9+x])])
    for i in range(nbPlayers):
      for l in range(9):
        if l in FIELDS[y][x][i]:
          rect(x*24+8,y*24+8,13,13,TXT)
          rect(x*24+10,y*24+10,9,9,COLORS[i+3])
          break
    for i in range(nbPlayers):
      if 9 in FIELDS[y][x][i]:
        for j in range(15):
          for k in range(15):
            if FLAG[j*15+k]=="1":set(8+x*24+k,9+y*24+j,TXT)
            elif FLAG[j*15+k]=="2":set(8+x*24+k,9+y*24+j,COLORS[i+3])
  if case>0:
    if case==1:c=(255,0,255)
    elif case==2:c=(COLORS[int(FIELD[y*9+x])])
    rect(3+x*24,3+y*24,23,2,c)
    rect(3+x*24,3+y*24,2,23,c)
    rect(26+x*24,26+y*24,-23,-2,c)
    rect(26+x*24,26+y*24,-2,-23,c)
def draw_arrow(x,y,c):
  for i in range(7):
    for j in range(15):
      if ARROW[i*15+j]=="1":set(x+j,y+i,c)
SKY,TXT=(255,255,255),(0,0,0)
COLORS=[(0,0,0),(0,0,255),(255,255,100),(255,0,0),(0,255,0),(0,255,255),(255,255,0)]
FIELD="211121112133315551133315551133315551211121112166614441166614441166614441211121112"
FLAG="001110000000000001122100000000001122221100000001122222211000001122222222100001122222211000001122221100000001122110000000001111000000000001100000000000001100000000000001100000000000111111000000000111111000000000000000000000000"
ARROW="000000110000000000011110000000001111111111111111111111111111001111111111111000011110000000000000110000000"
TYPES=["Sea","Island","P1","P2","P3","P4"]
UNITS=[["Soldat",1,1,2],["Avion",3,3,3],["tank",2,4,5],["bateau",2,10,10],["regiment",2,10,20],["chasseur",5,30,30],["char",4,40,40],["cuirasse",3,50,50],["Missile",0,100],["drapeau"]]
ACTIONS=["MOVE","GENERATE","FUSE"]
BASES=[[0,0],[8,8],[0,8],[8,0]]
X,Y=4,4
nbPlayers=menu()
FIELDS=[[[[]for i in range(nbPlayers)]for X in range(9)]for Y in range(9)]
POWER=[0 for i in range(nbPlayers)]
tour=0
FIELDS[0][0][0].append(9)
FIELDS[8][8][1].append(9)
if nbPlayers>2:FIELDS[0][8][2].append(9)
if nbPlayers>3:FIELDS[8][0][3].append(9)
FIELDS[7][8][0].append(0)
FIELDS[3][5][1].append(5)
FIELDS[3][5][1].append(5)
FIELDS[3][5][1].append(5)
for i in range(9):FIELDS[4][4][1].append(i)
while 1:
  tour,earn,earned,move=(tour+1)%nbPlayers,0,0,-1
  while 1:
    rect(0,0,320,222,SKY)
    rect(0,0,222,222,TXT)
    for y in range(9):
      for x in range(9):
        draw_cell(x,y,0,1)
        if int(FIELD[y*9+x])>2 and int(FIELD[y*9+x])!=tour+3 and earn==0:
          POWER[tour]+=len(FIELDS[y][x][tour])
          earned+=1
    ds("Round: ",230,0,TXT,SKY)
    ds("P"+str(tour+1),300,0,TXT,COLORS[tour+3])
    ds("POWER: "+str(POWER[tour]),240,40,TXT,(255,255,0))
    first,earn,brek,brak=1,1,0,0
    while k(4):1
    while 1:
      H,V,O,E=0,0,0,0
      if k(0):H=-1
      elif k(3):H=1
      elif k(1):V=-1
      elif k(2):V=1
      elif k(4):O=1
      elif k(52):E=1
      if E:
        choice,second=0,1
        rect(100,71,120,70,TXT)
        rect(103,74,114,64,SKY)
        ds("Valider?",120,81,TXT,SKY)
        ds("Oui",115,111,(0,255,0),SKY)
        ds("Non",175,111,(255,0,0),SKY)
        while k(4):1
        while not k(4):
          H=0
          if k(0):H=-1
          elif k(3):H=1
          if H!=0 or second:
            rect(105+choice*60,111,10,18,SKY)
            rect(145+choice*60,111,10,18,SKY)
            choice,second=(choice+H)%2,0
            ds("[",105+choice*60,111,TXT,SKY)
            ds("]",145+choice*60,111,TXT,SKY)
            while k(0) or k(3):1
        if choice==0:brek=1
        break
      if H!=0 or V!=0 or first:
        vmove=0
        first=0
        draw_cell(X,Y,2)
        rect(225,100,80,40,SKY)
        if move>-1:
          ds("Select a",225,100,(0,0,255),SKY)
          ds("cell:",225,120,(0,0,255),SKY)
          if X+H>=0 and X+H<=8:
            if Y+V>=0 and Y+V<=8:
              if max(X+H,cell[0])-min(X+H,cell[0])<=UNITS[units_cell[move]][1]:
                if max(Y+V,cell[1])-min(Y+V,cell[1])<=UNITS[units_cell[move]][1]:
                  vmove=1
#                  if move==0 or move==1 or move==4 or move==5:
#                    if 
#              elif move==2 or move==6:
#              elif move==3 or move==7:
        else:vmove=1
        if vmove==1:X,Y=(X+H)%9,(Y+V)%9
        draw_cell(X,Y,1)
        ds(""+TYPES[int(FIELD[Y*9+X])-1]+"    ",222,182,TXT,SKY)
        ds("["+str(X)+";"+str(Y)+"]",246,202,(255,0,0),SKY)
        while k(0) or k(1) or k(2) or k(3):1
      if O and len(FIELDS[Y][X][tour])>0 and move==-1:
        units,second,action,actions,units_cell,cell=-1,1,0,[],[],[X,Y]
        rect(10,10,300,202,TXT)
        rect(13,13,294,196,SKY)
        ds("CELL:",15,15,TXT,SKY)
        ds("["+str(X)+" "+str(Y)+"]",70,15,(255,0,0),SKY)
        ds("ACTIONS:",180,50,(255,0,0),SKY)
        if 9 in FIELDS[Y][X][tour]:ds("YOUR FLAG",210,15,TXT,COLORS[3+tour])
        for i in range(9):
          if i in FIELDS[Y][X][tour]:
            units+=1
            units_cell.append(i)
            if units==0:ds("UNITS:",15,33,(255,0,0),SKY)
            ds((str(FIELDS[Y][X][tour].count(i))+" "+UNITS[i][0]),15,51+units*17,TXT,SKY)
        for i in range(9):
          if i in FIELDS[Y][X][tour] and 0 not in actions:actions.append(0)
        if BASES[tour][0]==Y and BASES[tour][1]==X:actions.append(1)
        for i in range(len(FIELDS[Y][X][tour])):
          if FIELDS[Y][X][tour].count(FIELDS[Y][X][tour][i])>2 and 2 not in actions:actions.append(2)
        for i in range(len(actions)):ds(ACTIONS[actions[i]],220-len(ACTIONS[actions[i]])*5,80+i*30,TXT,SKY)
        while k(4) or k(17):1
        while 1:
          V,O,C=0,0,0
          if k(1):V=-1
          elif k(2):V=1
          elif k(4):O=1
          elif k(17):C=1
          if V!=0 or second:
            rect(210-len(ACTIONS[actions[action]])*5,80+action*30,10,18,SKY)
            rect(220+len(ACTIONS[actions[action]])*5,80+action*30,10,18,SKY)
            action,second=(action+V)%len(actions),0
            ds("[",210-len(ACTIONS[actions[action]])*5,80+action*30,(255,0,0),SKY)
            ds("]",220+len(ACTIONS[actions[action]])*5,80+action*30,(255,0,0),SKY)
            while k(1) or k(2):1
          elif O or C:
            if O and actions[action]==0:
              second=1
              rect(180,50,90,120,SKY)
              ds("MOVE:",180,35,(255,0,0),SKY)
              ds("[OK]",150,65,(255,0,0),SKY)
              ds("to valide",200,65,TXT,SKY)
              ds("[CLEAR]",140,85,(255,0,0),SKY)
              ds("to quit",220,85,TXT,SKY)
              while k(4):1
              while not k(4):
                V=0
                if k(1):V=-1
                elif k(2):V=1
                if V!=0 or second:
                  draw_arrow(20+len(str(FIELDS[Y][X][tour].count(move))+" "+UNITS[move][0])*10,57+move*17,SKY)
                  move,second=(move+V)%(units+1),0
                  draw_arrow(20+len(str(FIELDS[Y][X][tour].count(move))+" "+UNITS[move][0])*10,57+move*17,(255,0,0))
                  while k(1) or k(2):1
                if k(17):
                  move=-1
                  break
            if O and actions[action]==1:
              generate=1
            if O and actions[action]==2:
              fuse=1
            brak=1
            if brak:break
        if brak:break
      if O and move>-1:
        allow=0
        if units_cell[move]==0 or units_cell[move]==1 or units_cell[move]==4 or units_cell[move]==5 and FIELD[Y*9+X]!=2:allow=1
        elif units_cell[move]==2 or units_cell[move]==6 and FIELD[Y*9+X]==3 or FIELD[Y*9+X]==4 or FIELD[Y*9+X]==5 or FIELD[Y*9+X]==6:allow=1
        elif units_cell[move]==3 or units_cell[move]==7 and FIELD[Y*9+X]==1 or FIELD[Y*9+X]==2:allow=1
        if allow:
          FIELDS[Y][X][tour].append(FIELDS[cell[1]][cell[0]][tour][move])
          FIELDS[cell[1]][cell[0]][tour].pop(move)
          move=-1
          break
    if brek:break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.