minecraft_2d.py

Created by marius-layan

Created on April 27, 2025

6.21 KB


from random import *
from kandinsky import *
from ion import *
from time import *
def texture(x,y,n):
  if n=="grass":
    fill_rect(x*16,y*16,16,6,'green')
    fill_rect(x*16,y*16+6,16,10,'brown')
  if n=="dirt":
    fill_rect(x*16,y*16,16,16,'brown')
  if n=="stone":
    fill_rect(x*16,y*16,16,16,'gray')
  if n=="wood":
      fill_rect(x*16,y*16,4,16,(113,93,66))
      fill_rect(x*16+4,y*16,4,16,'brown')
      fill_rect(x*16+8,y*16,4,16,(113,93,66))
      fill_rect(x*16+12,y*16,4,16,'brown')
def choice_h():
  for i in range(40):
    if len(h)<2:
      h.append(h[-1]+choice([-1,0,0,1]))
    elif h[-1]+1==h[-2]:
      h.append(h[-1]+choice([0,0,-1,-1,-2]))
    elif h[-1]==h[-2]+1:
      h.append(h[-1]+choice([0,0,1,1,2]))
    elif h[-1]+2==h[-2]:
      h.append(h[-1]+choice([-1,-2]))
    elif h[-1]==h[-2]+2:
      h.append(h[-1]+choice([1,2]))
    else:
      h.append(h[-1]+choice([-1,0,0,1]))
def block():
  for j in range(15):
    for i in range(len(h)):
      if j==0:l.append([i,h[i]+j,"grass"])
      elif j<4:l.append([i,h[i]+j,"dirt"])
      else:l.append([i,h[i]+j,"stone"])
def arbres(n):
  global n1
  n1=(l[-1][0]//n)
  draw_string(str(n1),0,0)
  for i in range(n1):
    for j in l:
      if j[2]=="grass" and j[0]%n1==0:
        l.append([j[0],j[1]-1,"wood"])
        l.append([j[0],j[1]-2,"wood"])
        l.append([j[0],j[1]-3,"wood"])
def new_chunck():
  choice_h();block()
def hotebar():
  fill_rect(60,198,200,24,(100,100,100))
  fill_rect(62,192,196,4,(80,80,0))
  for i in range(9):
    fill_rect(i*22+62,200,20,20,(140,140,140))
  for i in range(life):
    fill_rect(i*8+64,184,6,6,'red')
  for i in range(food):
    fill_rect(i*8+178,184,6,6,(230,168,84))
  for i in range(len(inv)):
    if inv[i][0]=="dirt":
      if inv[i][1]<10:draw_string(str(str(inv[i][1])+" "),i*22+62,201,'white','brown')
      else:draw_string(str(inv[i][1]),i*22+62,201,'white','brown')
    if inv[i][0]=="grass":
      if inv[i][1]<10:draw_string(str(str(inv[i][1])+" "),i*22+62,201,'white','green')
      else:draw_string(str(inv[i][1]),i*22+62,201,'white','green')
    if inv[i][0]=="wood":
      if inv[i][1]<10:draw_string(str(str(inv[i][1])+" "),i*22+62,201,'white',(113,93,66))
      else:draw_string(str(inv[i][1]),i*22+62,201,'white',(113,93,66))
def update_terrain():
  fill_rect(0,0,320,222,(200,200,255))
  for i in l:
    if -1<i[0]<20 and -1<i[1]<14:texture(i[0],i[1],i[2])
  hotebar()
def player():
  fall();fill_rect(x*16+4,y*16,8,32,'blue');fill_rect(x*16,y*16+8,16,12,'blue')
def no_player():
  fill_rect(x*16,y*16,16,32,get_pixel(x*16,y*16))
def dead():
  global test,x,y,life,food
  test=[l[10][0],l[10][1]]
  for i in l:i[0]+=10-test[0]
  for i in l:i[1]+=10-test[1]
  x,y,life,food=10,l[10][1]-2,10,10;update_terrain();test=False
def fall():
  global x,y,life
  test=False
  while test==False:
    for i in l:
      if i[0]==x and i[1]==y+2 and i[2]!="":test=True;break
    if test==False:
      if y==8:
        for i in l:i[1]-=1
        update_terrain()
      else:y+=1
    l1=[]
    for i in l:l1.append(i[1])
    if max(l1)<0:
      life-=1
      if life==0:dead()
def select():
  fill_rect(x1*16,y1*16,16,1,'white')
  fill_rect(x1*16,y1*16,1,16,'white')
  fill_rect(x1*16,y1*16+15,16,1,'white')
  fill_rect(x1*16+15,y1*16,1,16,'white')
h,l,life,food,inv=[randint(11,12)],[],10,10,[]
choice_h();block()#;arbres(randint(6,12))
while True:
  if l[10][1]<8:
    for i in range(len(l)):l[i][1]+=1
  elif l[10][1]>8:
    for i in range(len(l)):l[i][1]-=1
  else:break
update_terrain();x,y,m=10,l[10][1]-2,monotonic();player()
while True:
  if keydown(KEY_RIGHT):
    no_player()
    test=[True,True,True,True]
    for i in range(len(l)):
      if l[i][0]==x+1 and l[i][1]==y+1 and l[i][2]!="":test[0]=False
      if l[i][0]==x+1 and l[i][1]==y and l[i][2]!="":test[1]=False
      if l[i][0]==x+1 and l[i][1]==y-1 and l[i][2]!="":test[2]=False
      if l[i][0]==x and l[i][1]==y-1 and l[i][2]!="":test[3]=False
    if test[0] and test[1]:x+=1
    if test[0]==False and test[1] and test[2] and test[3]:
      x+=1
      if y!=2:y-=1
      else:
        for i in l:i[1]+=1
        update_terrain()
    if x==16:
      x-=1
      for i in l:i[0]-=1
      update_terrain()
      #new_chunck()
    player()
    while keydown(KEY_RIGHT) and monotonic()-m<0.3:continue
  if keydown(KEY_LEFT):
    no_player()
    test=[True,True,True,True]
    for i in range(len(l)):
      if l[i][0]==x-1 and l[i][1]==y+1 and l[i][2]!="":test[0]=False
      if l[i][0]==x-1 and l[i][1]==y and l[i][2]!="":test[1]=False
      if l[i][0]==x-1 and l[i][1]==y-1 and l[i][2]!="":test[2]=False
      if l[i][0]==x and l[i][1]==y-1 and l[i][2]!="":test[3]=False
    if test[0] and test[1]:x-=1
    if test[0]==False and test[1] and test[2] and test[3]:
      x-=1
      if y!=2:y-=1
      else:
        for i in l:i[1]+=1
        update_terrain()
    if x==2:
      x+=1
      for i in l:i[0]+=1
      update_terrain()
      #new_chunck()
    player()
    while keydown(KEY_LEFT) and monotonic()-m<0.3:continue
  if keydown(KEY_OK):
    x1,y1=x,y+2
    select()
    while keydown(KEY_OK):continue
    while True:
      if keydown(KEY_LEFT) and x1>x-2:
        x1-=1
        update_terrain();player();select()
        while keydown(KEY_LEFT):continue
      if keydown(KEY_RIGHT) and x1<x+2:
        x1+=1
        update_terrain();player();select()
        while keydown(KEY_RIGHT):continue
      if keydown(KEY_UP) and y+3>y1>y-1:
        y1-=1
        update_terrain();player();select()
        while keydown(KEY_UP):continue
      if keydown(KEY_DOWN) and y+2>y1>y-2:
        y1+=1
        update_terrain();player();select()
        while keydown(KEY_DOWN):continue
      if keydown(KEY_OK):
        for i in range(len(l)):
          if l[i][0]==x1 and l[i][1]==y1 and l[i][2]!="":
            for j in range(1,4):
              fill_rect(x1*16+8-j*2,y1*16+8-j*2,j*4,j*4,'white')
              if l[i][2] in ("grass","dirt"):sleep(0.3)
              elif l[i][2]=="wood":sleep(0.7)
              else:sleep(1)
            del l[i]
            in_inv=False
            for j in inv:
              if j[0]==l[i][2] and j[1]!=64:
                j[1]+=1
                in_inv=True
                break
            if in_inv==False:inv.append([l[i][2],1])
            break
        break
    update_terrain();player()
  m=monotonic()

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.