polytopia.py

Created by stimorol

Created on February 01, 2025

14.2 KB

Jeu de stratégie impliquant des technologies a débloquer, des ennemis a combattre sur des territoires diversifiés (inspiré du jeu polytopia) !


from kandinsky import *
from random import *
from time import *
from ion import *

# SCRIPT MADE BY ARMAND JAMET
# COOL STRATEGY GAME :D
# STARTED ??.??.22
# v.1.2 12.10

wood=(255,200,100)
brown=(180,110,60)
yellow=(250,230,40)
gray=(240,240,240)
keyDeplacement=[KEY_VAR,KEY_TOOLBOX,KEY_BACKSPACE,KEY_IMAGINARY,KEY_POWER,KEY_PI,KEY_SQRT,KEY_SQUARE,KEY_COMMA,KEY_OK,KEY_HOME,KEY_ONOFF,KEY_LN]
dammage=[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,15]
life=[1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,15]
colors=[yellow,'blue','green','gray',brown]

def kd(key):
  if keydown(key):
    while keydown(key):return True
  return False

def draw_menu(choices,title,col,s):
  fill_rect(0,0,322,222,col[1])
  draw_string(title,20,20,col[0],col[1])
  for i in range(len(choices)):draw_string(choices[i],160-int(len(choices[i])/2)*8,100+25*i,col[2] if i==s else col[0],col[1])

def menu(choices,title,col=['black',(240,240,240),'red']):
  select=0
  draw_menu(choices,title,col,select)
  while not keydown(KEY_OK):
    for i in range(0,4):
      if keydown(i):
        select+=([0,-1,1,0][i])
        select%=len(choices)
        draw_menu(choices,title,col,select)
        sleep(0.1)
  return select

def reloadStats(player,stars,starsPerTurn,ennemyType,phase):
  fill_rect(0,0,322,10,'black')
  draw_string("U:"+str(player[0])+"/"+str(player[1])+" E:"+str(dammage[ennemyType])+"/"+str(life[ennemyType]),0,0,'white','black',10)
  draw_string(str(stars)+"*",280,0,'white','black',1)
  draw_string(str(starsPerTurn)+"*/TURN",215,0,'white','black',1)
  draw_string(phase,125,0,'white','black',1)

def draw_bg(ennemy):
  yEnnemys=[]
  xEnnemys=[]
  fill_rect(0,0,322,222,'orange')
  draw_line(0,221,322,221,'black')
  for i in range(32):draw_line(i*10,0,i*10,222,'black')
  for i in range(2,23):draw_line(0,i*10,322,i*10,'black')
  for i in colors:
    case=0
    while case<127:
      x,y=(randint(0,31)*10)+1,(randint(2,21)*10)+1
      if get_pixel(x,y)==(248,132,24):
        fill_rect(x,y,9,9,i)
        case+=1
        stop=False
        while not stop:
          if random()>0.50:x+=randint(-1,1)*10
          else:y+=randint(-1,1)*10
          if x>=311 or x<=1 or y>=211 or y<=21:stop=True
          if get_pixel(x,y)==(248,132,24):
            fill_rect(x,y,9,9,i)
            case+=1
          if case>=128:stop=True
  for i in range(ennemy):
    xEnnemys.append(randint(1,30))
    yEnnemys.append(randint(3,20))
    fill_rect((xEnnemys[i]*10)+2,(yEnnemys[i]*10)+2,7,7,'red')
  return xEnnemys,yEnnemys

def turn(xSprite,ySprite,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity,colorPlayer='black'):
  newx=newy=stop=0
  while not stop:
    for i in keyDeplacement:
      if keydown(i):
        stop=True
        while keydown(i):pass
        key=keyDeplacement.index(i)
        for j in range(8):
          if i==keyDeplacement[0:8][j]:
            newy,newx=[-1,-1,-1,0,0,1,1,1][j],[-1,0,1,-1,1,-1,0,1][j]
        if i==KEY_ONOFF:print(ennemy,",",fullUpgrades,",",xCity,",",yCity,",",stars)
        elif i==KEY_LN:
          if fullUpgrades[2][3]:
            fill_rect(xSprite*10+1,ySprite*10+1,9,9,wood)
            stars-=1
            fill_rect(xSprite*10+2,ySprite*10+2,7,7,colorPlayer)
        elif i==KEY_HOME:
           if get_pixel(xSprite*10+1,ySprite*10+1)==(248,132,24):return spawnTown(xSprite,ySprite,xCity,yCity,stars,colorSprite)
        elif i==KEY_COMMA:
          stop=False
          while not stop:
            draw_string(" SURE ? ",125,0,'white','black',1)
            if kd(KEY_OK):
              upgrade(fullUpgrades,ennemy,stars,xCity,yCity,colorSprite)
              return xSprite,ySprite,stars
            elif kd(KEY_COMMA):
              draw_string("DEPLACEMENT",125,0,'white','black',1)
              return turn(xSprite-newx,ySprite-newy,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
        elif i==KEY_OK:
          if get_pixel(xSprite*10+1,ySprite*10+1)==(80,192,0):
            if fullUpgrades[3][3] and (xCity[0].count(xSprite)==0 and yCity[0].count(ySprite)==0):
              for i in range(3):xCity[0].append(0)
              draw_string("+1*",245,0,'green','black',1)
              sleep(1)
          elif get_pixel(xSprite*10+1,ySprite*10+1)==(176,108,56):
            if fullUpgrades[3][2]:
              stars+=3
              fill_rect(xSprite*10+1,ySprite*10+1,9,9,'green')
              draw_string("+3*",295,0,'green','black',1)
              sleep(1)
          elif get_pixel(xSprite*10+1,ySprite*10+1)==(0,0,248):
            if fullUpgrades[3][0]:
              stars+=2
              draw_string("+2*",295,0,'green','black',1)
              sleep(1)
          elif get_pixel(xSprite*10+1,ySprite*10+1)==(160,164,160):
            if fullUpgrades[3][1]:
              stars+=1
              draw_string("+3*",295,0,'green','black',1)
              sleep(1)
          else:return turn(xSprite,ySprite,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
        xSprite+=newx
        ySprite+=newy
        if xSprite>32 or xSprite<0 or ySprite>21 or ySprite<2:
          draw_string("Err: BORDER ",125,0,"red",'black',1)
          sleep(1)
          draw_string("DEPLACEMENT ",125,0,'white','black',1)
          return turn(xSprite-newx,ySprite-newy,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
        else:
          if get_pixel(xSprite*10+1,ySprite*10+1)==(160,164,160):
            if not fullUpgrades[2][2]:
              draw_string(" Err: MOUNT ",125,0,'red','black',1)
              sleep(1)
              draw_string("DEPLACEMENT  ",125,0,'white','black',1)
              return turn(xSprite-newx,ySprite-newy,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
          elif get_pixel(xSprite*10+1,ySprite*10+1) == (0,0,248):
            if not fullUpgrades[2][1]:
              draw_string(" Err: SEA ",125,0,'red','black',1)
              sleep(1)
              draw_string("DEPLACEMENT",125,0,'white','black',1)
              return turn(xSprite-newx,ySprite-newy,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
          fill_rect((xSprite-newx)*10+1,(ySprite-newy)*10+1,9,9,get_pixel((xSprite-newx)*10+1,(ySprite-newy)*10+1))
          fill_rect(xSprite*10+2,ySprite*10+2,7,7,colorSprite)
          if get_pixel(xSprite*10+1,ySprite*10+1)==(80,192,0):
            if fullUpgrades[2][0]:
              return turn(xSprite,ySprite,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
          elif get_pixel(xSprite*10+1,ySprite*10+1)==(248,200,96):return turn(xSprite,ySprite,colorSprite,fullUpgrades,ennemy,stars,xCity,yCity)
        return xSprite,ySprite,stars

def attack(player,xPlayer,yPlayer,xEnnemys,yEnnemys,ennemyType,ennemy,xCity,yCity,stars):
  i,rep=0,len(xEnnemys)
  if player[2]!=0:porte=int(player[2]/2)
  else:porte=1
  while i<rep:
    if xPlayer-porte<=xEnnemys[i]<=xPlayer+porte and yPlayer-porte<=yEnnemys[i]<=yPlayer+porte:
      stop=False
      reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"ATTACK")
      while not stop:
        if kd(KEY_OK):
          stop=True
          if player[0]>=life[ennemyType]:
            reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"ELIMINE")
            for t in range(3):
              for l in ['red',choice([choice(colors),'orange'])]:
                fill_rect(xEnnemys[i]*10+1,yEnnemys[i]*10+1,9,9,l)
                sleep(0.4)
            xEnnemys.remove(xEnnemys[i])
            yEnnemys.remove(yEnnemys[i])
            stars+=3
            rep-=1
          else:
            reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"TROP FAIBLE")
            stop,rep=True,0
            sleep(1)
        elif kd(KEY_BACKSPACE):return stars,ennemy,xEnnemys,yEnnemys
    else:i+=1
  return stars,ennemy,xEnnemys,yEnnemys

def spawnTown(xSprite,ySprite,xCity,yCity,stars,colorSprite):
  if len(xCity)<=9:
    xCity.append(xSprite)
    yCity.append(ySprite)
    fill_rect(xSprite*10+1,ySprite*10+1,9,9,'white')
    fill_rect(xSprite*10+2,ySprite*10+2,7,7,colorSprite)
    draw_string("+1*",245,0,'green','black',1)
    sleep(1)
  else:
    draw_string("Nb. VILLES MAX.(9)",125,0,'white','black')
    sleep(1)
  return xCity,yCity,stars

def upgrade2(upgrade,choices,cost,increasePerUpgrade,increaseCost,stars):
  select=menu([choices[0]+cost[0],choices[1]+cost[1],choices[2]+cost[2],choices[3]+cost[3]],choices[4])
  restart=False
  coins=stars
  for i in range(0,4):
    if select==i:
      if not upgrade[i]:
        upgrade[i]=1
        if i==3:coins-=int((i+10)*(increasePerUpgrade*increaseCost))
        else:coins-=int((i+2)*(increasePerUpgrade*increaseCost))
      else:
        restart=True
        draw_string("TECHOLOGIE DEJA DEBLOQUEE",60,20,'red',gray)
  return upgrade,coins,restart

def upgrade(fullUpgrade,ennemy,stars,xCity,yCity,colorPlayer,increasePerUpgrade=1.75):
  coins,stop,restart,cost,cost2,increaseCost=stars,True,False,[],[],0
  for i in range(3):
    increaseCost+=fullUpgrade[i].count(1)
    increaseCost+=fullUpgrade[1][i]
  increaseCost+=fullUpgrade[1][3]
  for i in range(3):
    if fullUpgrade[0][i]==0:cost2.append(": 10*")
    else:cost2.append("")
  select=menu(["COMBAT"+cost2[0],"DEPLACEMENT"+cost2[1],"RESSOURCES"+cost2[2],"RETOUR: 10*"],"AMELIORATIONS")
  if select==0:increaseCost=1
  for i in range(4):
    if i!=3:cost.append(": "+str(int((i+2)*(increasePerUpgrade*increaseCost)))+" *")
    else:cost.append(": "+str(int((i+10)*(increasePerUpgrade*increaseCost)))+" *")
  if select==3:poly(ennemy,fullUpgrade,xCity,yCity,stars-10,colorPlayer)
  elif fullUpgrade[0][select]!=0:
    choices=[["ARMURERIE","AFFUTAGE","ARC","ENGIN DE SIEGE","COMBAT"],["EQUITATION","NAVIGATION","ALPINISME","ROUTES","DEPLACEMENT"],["PECHE","MINAGE","DEBOISEMENT","AGRICULTURE","RESSOURCES"]]
    if select==0:
      choices=choices[0]
      select=menu([choices[0]+cost[0],choices[1]+cost[1],choices[2]+cost[2],choices[3]+cost[3]],choices[4])
      for i in range(0,4):
        if select==i:
          fullUpgrade[1][i]+=1
          if i==3:coins-=int((i+10)*(increasePerUpgrade*increaseCost))
          else:coins-=int((i+2)*(increasePerUpgrade*increaseCost))
    elif select==1:fullUpgrade[2],coins,restart=upgrade2(fullUpgrade[2],choices[1],cost,increasePerUpgrade,increaseCost,stars)
    elif select==2:fullUpgrade[3],coins,restart=upgrade2(fullUpgrade[3],choices[2],cost,increasePerUpgrade,increaseCost,stars)
    if restart==True or coins<0:
      if coins<0:
        draw_string("CREDITS INSUFFISANTS",85,20,'red',gray)
      sleep(1)
      upgrade(fullUpgrade,ennemy,stars,xCity,yCity,colorPlayer)
    else:poly(ennemy+1,fullUpgrade,xCity,yCity,coins,colorPlayer)
  elif (fullUpgrade[0]==[0,0,0] or stars-10>=0):
    if fullUpgrade[0]!=[0,0,0]:stars-=10
    fullUpgrade[0][select]=1
    draw_string("TEHNOLOGIE ACHETEE",85,20,'red',gray)
    sleep(1)
    upgrade(fullUpgrade,ennemy,stars,xCity,yCity,colorPlayer)
  else:
    draw_string("CREDITS INSUFFISANTS",85,10,'red',gray)
    sleep(1)
    upgrade(fullUpgrade,ennemy,stars,xCity,yCity,colorPlayer)

def botTurn(player,xPlayer,yPlayer,xEnnemy,yEnnemy,ennemyType,colorPlayer,ennemy):
  for i in range(len(xEnnemy)):
    if xPlayer-12<xEnnemy[i]<xPlayer+12 and yPlayer-12<yEnnemy[i]<xPlayer+12:
      if xEnnemy[i]>xPlayer:botx=-1
      elif xEnnemy[i]<xPlayer:botx=1
      else:botx=randint(-1,1)
      if yEnnemy[i]>yPlayer:boty=-1
      elif yEnnemy[i]<yPlayer:boty=1
      else:boty=randint(-1,1)
      if life[ennemyType]<player[0]:botx,boty=-botx,-boty
    else:
      botx=randint(-1,1)
      boty=randint(-1,1)
    fill_rect(xEnnemy[i]*10+1,yEnnemy[i]*10+1,9,9,get_pixel(xEnnemy[i]*10+1,yEnnemy[i]*10+1))
    xEnnemy[i]+=botx
    yEnnemy[i]+=boty
    if xEnnemy.count(xEnnemy[i])>1 and yEnnemy.count(xEnnemy[i])>1 or xEnnemy[i]>=31 or xEnnemy[i]<=0 or yEnnemy[i]>=21 or yEnnemy[i]<=2:
      xEnnemy[i]-=2*botx
      yEnnemy[i]-=2*boty
    fill_rect(xEnnemy[i]*10+2,yEnnemy[i]*10+2,7,7,'red')
#    sleep(0.2)
    if xPlayer-1<=xEnnemy[i]<=xPlayer+1 and yPlayer-1<=yEnnemy[i]<=yPlayer+1:
      if dammage[ennemyType]>=player[1]:
        for i in range(5):
          for s in [colorPlayer,'red']:
            fill_rect(xPlayer*10+2,yPlayer*10+2,7,7,s)
            sleep(0.3)
        return True
        break
  return False

def playerTurn(player,xPlayer,yPlayer,xEnnemys,yEnnemys,ennemyType,colorPlayer,fullUpgrades,ennemy,stars,xCity,yCity):
  reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"DEPLACEMENT")
  return1,return2,stars=turn(xPlayer,yPlayer,colorPlayer,fullUpgrades,ennemy,stars,xCity,yCity)
  if isinstance(return1,int):xPlayer,yPlayer=return1,return2
  else:xCity,yCity=return1,return2
  reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"ATTACK")
  stars,ennemy,xEnnemys,yEnnemys=attack(player,xPlayer,yPlayer,xEnnemys,yEnnemys,ennemyType,ennemy,xCity,yCity,stars)
  stars+=int((len(xCity)+len(xCity[0])-1)/3)
  return xPlayer,yPlayer,stars,ennemy,xEnnemys,yEnnemys

def poly(ennemy,fullUpgrade,xCity,yCity,stars=0,colorPlayer='black'):
  xEnnemys,yEnnemys=draw_bg(ennemy)
  ennemyType=ennemy-5
  xCity[0]=[]
  fill_rect(0,0,322,21,'black')
  for i in range(len(xCity)-1):
    fill_rect(xCity[i+1]*10+1,yCity[i+1]*10+1,9,9,'white')
  xPlayer=randint(1,31)
  yPlayer=randint(2,21)
  Player=[]
  Player.append(fullUpgrade[1][1]+1*fullUpgrade[1][3]+1)
  Player.append(fullUpgrade[1][0]+1*fullUpgrade[1][3]+1)
  Player.append(fullUpgrade[1][2]+fullUpgrade[1][3])
  player=Player
  fill_rect((xPlayer*10)+2,(yPlayer*10)+2,7,7,colorPlayer)
  end=False
  while not end:
    xPlayer,yPlayer,stars,ennemy,xEnnemys,yEnnemys=playerTurn(player,xPlayer,yPlayer,xEnnemys,yEnnemys,ennemyType,colorPlayer,fullUpgrade,ennemy,stars,xCity,yCity)
    if xCity.count(xPlayer)>0 and yCity.count(yPlayer)>0:defense=2
    else:defense=0
    player=[]
    for i in range(2):
      player.append(Player[i]+defense)
    player.append(Player[2])
    reloadStats(player,stars,int((len(xCity)+len(xCity[0])-1)/3),ennemyType,"TOUR ENNEMI")
    end=botTurn(player,xPlayer,yPlayer,xEnnemys,yEnnemys,ennemyType,colorPlayer,ennemy)
  for i in range(750):
    fill_rect(randint(0,32)*10,randint(2,22)*10,10,10,"black")
    sleep(0.001)
  draw_string("GAME OVER",120,0,"red","black")
  draw_string("STATS",128,50,'white','black')
  draw_string(" - Etoiles:"+str(stars)+"\n - Villes: "+str(len(xCity)-1)+"\n - Technologies debloquees: "+str((4-fullUpgrades[1].count(0))+fullUpgrades[2].count(1)+fullUpgrades[3].count(1))+"/12",0,98,'white','black',1)

fullUpgrades=[[0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

poly(5,fullUpgrades,[[]],[[]],0)

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.