monopoly2.py

Created by elnix91

Created on September 28, 2025

5.99 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)
PC=[(200,255,0),(0,255,255),(255,0,150),(255,0,0),(255,255,0),(0,255,50),(0,0,255)]


#(255,0,0),(0,255,0),(0,0,255),(255,0,255),(255,255,0),(0,255,255),(255,0,150),(255,)*3]
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,40000],
["Propriety1",0,0,0,0],
["Propriety2",0,1,0,0],
["Taxe 1",1,-40000],
["Luck",4],
["Train station 1",2,0],
["Propriety3",0,2,1,0],
["Propriety4",0,3,1,0],
["Box",4],
["Propriety5",0,4,1,0],
["Jail",6],
["Propriety6",0,5,2,0],
["Water service",3],
["Propriety7",0,6,2,0],
["Propriety8",0,7,2,0],
["Train station 2",2,1],
["Propriety9",0,8,3,0],
["Propriety10",0,9,3,0],
["Luck",4],
["Propriety11",0,10,3,0],
["Free park",8],
["Propriety12",0,11,4,0],
["Propriety13",0,12,4,0],
["Box",5],
["Propriety14",0,13,4,0],
["Train station 3",2,2],
["Propriety15",0,14,5,0],
["Electricity",6],
["Propriety16",0,15,5,0],
["Propriety17",0,16,5,0],
["Go to jail",7],
["Propriety18",0,17,6,0],
["Luck",4],
["Propriety19",0,18,6,0],
["Propriety20",0,19,6,0],
["Train station 4",2,3],
["Box",5],
["Propriety21",0,20,7,0],
["Taxe 2",1,-20000],
["Propriety22",0,21,7,0],
]

BankProp=[i for i in range(22)]


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

clean=lambda:rect(0,0,320,222,SKY)
s=lambda:k(0)|k(1)|k(2)|k(3)
v=lambda:k(4)|k(52)

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][2]
  if t==0:
    rect(x,y,20,20,TXT)
    rect(x+1,y+1,18,18,SKY)
    xs,ys,w,h=x+1,y+1,18,5
#      rect(x+1,y+1,18,5,PC[CASES[id][3]])
    if rot==1:xs,w,h=x+14,5,18
#      rect(x+14,y+1,5,18,PC[CASES[id][3]])
    elif rot==2:ys=y+14
#      rect(x+1,y+14,18,5,PC[CASES[id][3]])
    elif rot==3:w,h=5,18
    rect(xs,ys,w,h,PC[CASES[id][3]])
    if 0<A[-2][id]<5:
      xi,yi=0,0
      if w>h:xi=4
      else:yi=4
      for i in range(A[-2][id]):rect(xs+xi//4+xi*i,ys+yi//4+yi*i,3,3,SKY)
    elif A[-2][id]==5:rect(xs,ys,3,3,TXT)
#  elif t==1:


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),Password,True
  params=[0,False,False]
  act=0
  first=True
  lastAct=-1
  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":[],"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():
  


def game():
  N,Password,Save=menu()
  T=randint(0,N-1)
  clean()

  print(A)
  print(A[-2])
  draw_case(10,10,0,1)
  draw_case(10,40,1,1)
  draw_case(10,70,2,2)
  draw_case(10,100,3,3)
  while 1:
    
    if Save:
      with open(savefile,"w")as f:
        f.write(str(A))
    T=(T+1)%N




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.