power.py

Created by elnix91

Created on September 28, 2025

9.05 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
# Menu:choosing number of players #
def menu():
  nbPlayers,first=0,1
  rect(0,0,320,222,SKY)
  ds("POWER !",125,20,(255,0,0),SKY)
  ds("Number of players:",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:
      if first:first=0
      nbPlayers=(nbPlayers+V)%3
      ds(str(nbPlayers+2),155,120,TXT,SKY)
      while k(1)or k(2):1
  return nbPlayers+2
# Drawing cell in [x,y] #
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+2])
          break
      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+2])
  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)
# Drawing arrow in bitmap
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)
# ACTIONS FUNCTION
# Move function
def move(unit,player,x,y,X,Y):1
def generate(unit,player):1
def fuse(unit,player):1
# Variables
SKY       = (255,255,255)
TXT       = (0,0,0)
nbPlayers = menu()
FLAG      = "001110000000000001122100000000001122221100000001122222211000001122222222100001122222211000001122221100000001122110000000001111000000000001100000000000001100000000000001100000000000111111000000000111111000000000000000000000000"
UNITS     = [["Soldat",1,1,2],["tank",2,4,5],["Avion",3,3,3],["bateau",2,10,10],["regiment",2,10,20],["char",4,40,40],["chasseur",5,30,30],["cuirasse",3,50,50],["Missile",0,100],["drapeau"]]
ARROW     = "000000110000000000011110000000001111111111111111111111111111001111111111111000011110000000000000110000000"
FIELD     = "100010001022204440022204440022204440100010001055503330055503330055503330100010001"
COLORS    = [(0,0,255),(255,255,100),(255,0,0),(0,255,0),(0,255,255),(255,255,0)]
FIELDS    = [[[[]for i in range(nbPlayers)]for X in range(9)]for Y in range(9)]
TYPES     = ["Sea","Island","P1","P2","P3","P4"]
POWER     = [0 for i in range(nbPlayers)]
ACTIONS   = ["MOVE","GENERATE","FUSE"]
BASES     = [[0,0],[8,8],[0,8],[8,0]]
X         = 4
Y         = 4
tour      = 0
# Adding units gnd flags to FIELDS
for i in range(4):
  for j in range(2):
    FIELDS[0][0][0].append(i)
for i in range(4):
  for j in range(2):
    FIELDS[8][8][1].append(i)
if nbPlayers>2:
  for i in range(4):
    for j in range(2):
      FIELDS[0][8][2].append(i)
if nbPlayers>3:
  for i in range(4):
    for j in range(2):
      FIELDS[8][0][3].append(i)
# Tests for testing
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)
# ULTIMATE MAIN LOOP
while 1:
  tour,earn,earned,move=(tour+1)%nbPlayers,0,0,-1
# Loop of writting objects
  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:
          earned+=len(FIELDS[y][x][tour])
    if earn==0:POWER[tour]+=earned
    ds("Round: ",230,0,TXT,SKY)
    ds("P"+str(tour+1),300,0,TXT,COLORS[tour+2])
    ds("POWER:",240,40,TXT,(255,255,0))
    ds(str(POWER[tour]),271-len(str(POWER[tour]))*5,60,(255,0,0),SKY)
    ds("+"+str(earned),264-len(str(earned))*5,80,(0,255,0),SKY)
    first,earn,brek,brak=1,1,0,0
# Loop of tests of keydown
    while k(4):1
    while 1:
      if k(1):V=-1
      elif k(2):V=1
      else:V=0
      if k(0):H=-1
      elif k(3):H=1
      else:H=0
      if k(4):O=1
      else:O=0
      if k(17):C=1
      else:C=0
      if k(52):E=1
      else:E=0
# Validation bloc
      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):
          if k(0):H=-1
          elif k(3):H=1
          else:H=0
          if H!=0 or second:
            if second:second=0
            rect(105+choice*60,111,10,18,SKY)
            rect(145+choice*60,111,10,18,SKY)
            choice=(choice+H)%2
            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
# Selection bloc
      if H!=0 or V!=0 or C or first:
        vmove,first=0,0
        draw_cell(X,Y,2)
        rect(225,100,80,40,SKY)
        if move>-1:
          ds("Select a",225,120,(0,0,255),SKY)
          ds("cell:",225,140,(0,0,255),SKY)
          ds("[OK]",280,140,(255,0,0),SKY)
          if C:
            move=-1
            break
          elif 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
        else:
          rect(225,120,100,60,SKY)
          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])]+"    ",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
# Action bloc
      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[tour+2])
        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)
        # Calculating possibles actions
        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)
        # Choosing an action
        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)
              move=0
              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[units_cell[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[units_cell[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
# Mooving bloc
      if O and move>-1:
        allow=0
        if units_cell[move]in[0,1,4,5]:
          if FIELD[Y*9+X]!="0":allow=1
        elif units_cell[move]in[2,6]:
          if FIELD[Y*9+X]!="0":allow=1
        elif units_cell[move]in[3,7]:
          if [Y,X]not in[[2,2],[6,2],[2,6],[6,6]]: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.