monopoly3.py

Created by elnix91

Created on September 28, 2025

9.71 KB

debug version monopoly, feel free to finish it; the last version is available here


from kandinsky import fill_rect as rect,draw_string as txt,set_pixel as pix
from random import randint,choice
from ion import keydown as k
import time

SKY=(0,0,0)
TXT=(255,)*3
IMP=(0,0,255)
SEL=(255,255,0)
PC=[(200,0,200),(0,255,255),(255,0,255),(255,200,0),(255,0,0),(255,255,0),(0,150,0),(0,0,255)]

savefile="monopoly.sav"

# Types of cases:
# 0:proprety (22)
# 1:gain/pay money (3)
# 2:train-station (4)
# 3:service (2)
# 4:luck (3)
# 5:box (3)
# 6:jail (1)
# 7:go to jail (1)
# 8:free parc (1)

CASES=[
# ["name",type,id/amount,group-if-propriety,houses]
["Depart",1,20000],
["Propriety1",0,0,0,0],
["Community chest",5],
["Propriety2",0,1,0,0],
["Taxe 1",1,-20000],
#["Train station 1",2,0],
["Train station 1",0,22],
["Propriety3",0,2,1,0],
["Chance",4],
["Propriety4",0,3,1,0],
["Propriety5",0,4,1,0],
["Jail",6],
["Propriety6",0,5,2,0],
#["Electricity",3,1],
["Electricity",0,26],
["Propriety7",0,6,2,0],
["Propriety8",0,7,2,0],
#["Train station 2",2,1],
["Train station 2",0,23],
["Propriety9",0,8,3,0],
["Community chest",5],
["Propriety10",0,9,3,0],
["Propriety11",0,10,3,0],
["Free park",8],
["Propriety12",0,11,4,0],
["Chance",4],
["Propriety13",0,12,4,0],
["Propriety14",0,13,4,0],
#["Train station 3",2,2],
["Train station 3",0,24],
["Propriety15",0,14,5,0],
["Propriety16",0,15,5,0],
#["Water",3,0],
["Water",0,27],
["Propriety17",0,16,5,0],
["Go to jail",7],
["Propriety18",0,17,6,0],
["Propriety19",0,18,6,0],
["Community chest",5],
["Propriety20",0,19,6,0],
#["Train station 4",2,3],
["Train station 4",0,25],
["Chance",4],
["Propriety21",0,20,7,0],
["Taxe 2",1,-10000],
["Propriety22",0,21,7,0],
]

try:
  import os
  OS=True
except:OS=False

clean=lambda:rect(0,0,320,222,SKY)
txtc=lambda t,y,fc,bc:txt(t,160-len(t)*5,y,fc,bc)
s=lambda:k(0)|k(1)|k(2)|k(3)
v=lambda:k(4)|k(52)

def popup(x,y,w,h,xso,yso,c,title,opt,frame=True,padding=3,back=False):
  if frame:
    rect(x,y,w,h,c[-1])
    rect(x+padding,y+padding,w-2*padding,h-2*padding,SKY)
    y+=2
  else:rect(x,y,w,h,SKY)
  txtc(title,y+2,c[0],SKY)
  for i,o in enumerate(opt):
    txt(o,xso,yso+20*i,c[1],SKY)
  first=True
  r=0
  lastAct=-1
  while v():1
  while not v():
    if s()|first:
      lastAct=r
      r=(r-k(1)+k(2))%len(opt)
      #if r!=lastAct:
      rect(xso-20,yso+20*lastAct,10,18,SKY)
      txt("",xso-20,yso+20*r,SEL,SKY)
      if first:first=False
      while s():1
    elif k(17)&back:return -1
  return r

def switch(x,y,on):
  rect(x,y,17,17,(200,)*3)
  rect(x+1,y+1,15,15,SKY)
  if on:c=(0,255,0)
  else:c=(100,)*3
  rect(x+3,y+3,11,11,c)

def kinput(x,y,maxLenght=4,onlynumbers=True,ct=TXT):
  letters={29: 'l', 30: 'm', 31: 'n', 32: 'o', 33: 'p', 34: 'q', 36: 'r', 37: 's', 38: 't', 39: 'u', 40: 'v', 42: 'w', 43: 'x', 44: 'y', 45: 'z', 18: 'a', 19: 'b', 20: 'c', 21: 'd', 22: 'e', 23: 'f', 24: 'g', 25: 'h', 26: 'i', 27: 'j', 28: 'k'}
  numbers={30: '7', 31: '8', 32: '9', 42: '1', 43: '2', 44: '3', 36: '4', 37: '5', 38: '6', 48: '0'}
  Input=""
  rect(x,y+17,10*maxLenght,1,IMP)
  while v():1
  while not v():
    for i in range(17,49):
      if k(i):
        if onlynumbers:
          if(i in numbers.keys())&(len(Input)<maxLenght):
            Input+=numbers[i]
        else:
          if(i in letters.keys())&(len(Input)<maxLenght):
            Input+=letters[i]
        if (i==17)&(len(Input)>0):
          Input=Input[:-1]
          rect(x+len(Input)*10,y,10,18,SKY)
        rect(x,y+17,10*maxLenght,1,IMP)
        txt(Input,x,y,ct,SKY)
        while k(i):1
  return Input

def draw_case(x,y,id,rot=0):
  t=CASES[id][1]
  rect(x,y,20,20,TXT)
  rect(x+1,y+1,18,18,SKY)
  if t==0:
    if 21<CASES[id][2]<26:
      rect(x+2,y+2,16,16,(50,)*3)
      rect(x+4,y+4,12,12,SKY)
      rect(x+6,y+6,8,8,(50,)*3)
    elif CASES[id][2]>25:
      if CASES[id][2]==0:c=(0,0,200)
      else:c=SEL
      txt("S",x+5,y+1,c,SKY)

    else:
      xs1,ys1,w1,h1,xs2,ys2,w2,h2=x+1,y+1,18,5,x+1,y+7,18,12
      if rot==1:xs1,w1,h1,ys2,w2,h2=x+14,5,18,y+1,12,18
      elif rot==2:ys1,ys2=y+14,y+1
      elif rot==3:w1,h1,xs2,ys2,w2,h2=5,18,x+7,y+1,12,18
      rect(xs1,ys1,w1,h1,PC[CASES[id][3]])
      for i in range(N):
        if CASES[id][2] in A[i]["terrains"]:
          rect(xs2,ys2,w2,h2,PC[i])

      if 0<A[-2][CASES[id][2]]<5:
        xi,yi=0,0
        if w1>h1:xi=4
        else:yi=4
        for i in range(A[-2][CASES[id][2]]):
          rect(xs1+1+xi*i,ys1+1+yi*i,3,3,SKY)
      elif A[-2][CASES[id][2]]==5:
        rect(xs1+1,ys1+1,3,3,TXT)

  elif t==1:
    if CASES[id][2]>0:c=(0,200,0)
    else:c=(200,0,0)
    rect(x+2,y+2,16,16,c)
#  elif t==2:
#    rect(x+2,y+2,16,16,(50,)*3)
#    rect(x+4,y+4,12,12,SKY)
#    rect(x+6,y+6,8,8,(50,)*3)
#  elif t==3:
#    if CASES[id][2]==0:c=(0,0,200)
#    else:c=SEL
#    txt("S",x+5,y+1,c,SKY)
  elif 3<t<6:
    if t==4:c=(0,0,255)
    else:c=(255,0,0)
    txt("C",x+5,y+1,c,SKY)
  elif t==6:
    rect(x+1,y+1,18,18,(255,100,0))
    rect(x+1,y+7,12,12,(70,)*3)
  elif t==7:rect(x+3,y+3,14,14,(255,100,0))
  elif t==8:
    rect(x+2,y+2,16,16,(0,255,0))
    rect(x+4,y+4,12,12,(0,180,0))
  np=[]
  for i in range(N):
    if A[i]["case"]==id:np.append(i)
  if len(np)>0:
    rect(x+5,y+7,10,6,SKY)
    for _ in range(2):
      for __ in range(4):
        if _*4+__ in np:rect(x+6+__*2,y+8+_*2,2,2,PC[_*4+__])

def menu():
  global A
  clean()
  c=0
  if OS:
    if savefile in os.listdir():
      txt("Savefile found!",85,30,IMP,SKY)
      txt("Do you want to",90,70,(255,150,0),SKY)
      txt("continue the game?",70,100,(255,150,0),SKY)
      txt("YES   NOO",120,150,(0,255,255))
      first=True
      while v():1
      while not v():
        if s()|first:
          c=(c-k(0)+k(3))%2
          rect(110+60*(1-c),150,10,18,SKY)
          rect(150+60*(1-c),150,10,18,SKY)
          txt("[",110+60*c,150,TXT)
          txt("]",150+60*c,150,TXT)
          if first:first=False
          while s():1
      if c==0:
        with open(savefile,"r")as f:
          A=eval(f.read())
          if"code"in A[0].keys():Password=True
          else:Password=False
        return len(A)-3,Password,True
  params=[0,False,False]
  act=0
  first=True
  lastAct=-1
  pressS,pressV=0,0
  clean()
  txt("MONOPOLY",120,20,IMP,SKY)
  txt("Number of players:   <   >",30,70,TXT,SKY)
  txt("Password protection",30,110,TXT,SKY)
  txt("VALIDATE",120,190,(0,255,0),SKY)
  switch(260,110,0)
  if OS:
    txt("Enable saving",30,150,TXT,SKY)
    switch(260,150,0)
  while v():1
  while 1:
    if not s():pressS=True
    if not v():pressV=True
    if (s()&pressS)|first:
      act=(act-k(1)+k(2))%4
      if not OS:
        if(act==2):act=4-lastAct
      if lastAct!=act:
        rect(10,70+40*lastAct,10,17,SKY)
        txt("",10,70+40*act,(255,255,0),SKY)
      if (act==0)|first:
        params[0]=(params[0]-k(0)+k(3))%7
        txt(str(params[0]+2),260,70,IMP,SKY)
      lastAct=act
      pressS=False
      if first:first=False
    if (v()&pressV):
      if 0<act<3:
        params[act]=not params[act]
        switch(260,110+40*(act-1),params[act])
      if act==3:
        N=params[0]+2
        A=[{"name":"","money":150000,"terrains":[],"hyp":[],"case":0} for i in range(N)]
        for i in range(N):
          clean()
          txt("Player "+str(i+1),30,20,PC[i],SKY)
          txt(", enter your name:",110,20,TXT,SKY)
          A[i]["name"]=kinput(110,70,10,False)
          if params[1]:
            txt("Please set a up a pin code:",25,110,TXT,SKY)
            while 1:
              code=kinput(140,140)
              if len(code)<4:txt("Too short!",110,170,(255,0,0),SKY)
              else:
                A[i]["code"]=code
                break
        A.append([i for i in range(22)])
        A.append([0 for i in range(22)])
        A.append(0)
        return N,params[1],params[2]
      pressV=False

def draw_terrain():
  id=0
  for i in range(10):
    draw_case(1+i*20,1,id,0)
    id+=1
  for i in range(10):
    draw_case(201,1+i*20,id,1)
    id+=1
  for i in range(10):
    draw_case(201-i*20,201,id,2)
    id+=1
  for i in range(10):
    draw_case(1,201-i*20,id,3)
    id+=1
  txt("Total in",70,50,IMP,SKY)
  txt("Free park:",60,70,IMP,SKY)
  txt(str(A[-1]),110-len(str(A[-1]))*5,90,IMP,SKY)

#-1:cancel,0:fine,1:one lost, 2:game ends
def pay(p,r,amount,middle=False,forced=False):
  if A[p]["money"]>=amount:
    A[p]["money"]-=amount
    if middle:A[-1]+=amount
    if r!=-1:
      A[r]["money"]+=amount
  else:
    while A[p]["money"]<amount:
      opt=[]
      houses={}
      for i in A[p]["terrains"]:
          houses[i]=A[-2][i]
      if sum(houses.values())>0:
        opt.append("Sell houses")
      if 0 in houses.values():opt.append("Mordgage")
      if len(opt)==0:
        if lose(p):return 1
        else:return 2
      code=popup(10,40,300,122,40,90,[(255,0,0),IMP],"Warning! Not enough money!",opt,back=forced)
      if code==0:
        txtc("Select what to sell:")
        act=0
        first=True
        while v():1
        clean()
        while 1:
          htcbs=[i for i,j in houses.items() if j>0]]
          if s()|first:
            act=(act-k(0)+k(1))%len(htcbs)
            txtc("< "+CASES[htcbs[act]][0]+" >",10,)
  return 0

def lose(p):
  global N
  N-=1
#  for i in A[p]["terrains"]:
#    A[-3].append(i)
  for i in A[p]["hyp"]:
    A[-3].append(i)
  rect(20,20,280,182,PC[p])
  rect(23,23,274,176,SKY)
  if N>1:
    txtc(A[p]["name"]+" lose!",70,PC[p])
    txtc("BUT THE GAME CONTINUE!",110,TXT,SKY)
  del A[p]
  if N==1:
    txtc("Congrat to "+A[0]["name"],70,PC[p],SKY)
    txtc("For winning this game!",110,TXT,SKY)
    return False
  else:return True

def game():
  global N
  N,Password,Save=menu()
  T=randint(0,N-1)
  clean()
  
#  A[0]["terrains"].append(0)
#  A[0]["terrains"].append(1)
#  for i in range(22):
#    A[-2][i]=randint(1,5)
  
  draw_terrain()
#  print(pay(0,1,9899999))
#  lose(1)
  while 1:
    
    if Save:
      with open(savefile,"w")as f:
        f.write(str(A))
    T=(T+1)%N
  remove(savefile)

while 1:game()

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.