monopoly.py

Created by elnix91

Created on September 28, 2025

3.96 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
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=[(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"

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 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
        return N,params[1],params[2]
      pressV=False

def game():
  N,Password,Save=menu()
  T=randint(0,N-1)
  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.