minecraft.py

Created by squarepoint

Created on September 25, 2022

4.79 KB

Technically a working minecraft with watef physics, but it often crashes :/ someone pls help me


from ion import *
from kandinsky import *
from time import sleep
from random import randint
world=[]
world2=[]
alrmoved=[]
blocks=[["Blank",color(255,255,255)],["Dirt",color(100,50,50)],["Water",color(100,200,255)],["Grass",color(50,150,50)],["TNT",color(255,50,50)],["Stone",color(100,100,100)],["Wood",color(200,80,100)],["Seed",color(0,255,0)],["Fire",color(255,100,0)],["Lava",color(255,50,0)]]
data=[[-79,-80,-81,-38,-39,-40,-41,-42,-2,-1,0,1,2,38,39,40,41,42,79,80,81],[0,-40,-80],[-118,-119,-120,-121,-122,-158,-159,-160,-161,-162,-199,-200,-201],[-39,-40,-41,-1,1,39,40,41]]
def replace(pos,item):
  world2=[]
  world.insert(pos,item)
  for i in range(len(world)-pos-2):
    world2.append(world[len(world)-1])
    world.pop()
  world.pop()
  for i in range(len(world2)):
    world.append(world2[len(world2)-1-i])
  draw_string(str(len(world))+" "+str(len(world2))+" "+str(len(alrmoved)),200,50)

def init(a):
  for i in range(920):
    if a==0:world.append(0)
    else:
      if i<40:world.append(0)
      elif world[i-40]==3 and randint(1,10)==1:
        replace(i-80,7)
        world.append(1)
      elif world[i-40]==5 or (world[i-40]==1 and randint(1,3)==1):world.append(5)
      elif world[i-40]==3 or world[i-40]==1:world.append(1)
      elif i>randint(479,719):world.append(3)
      else:world.append(0)
      fill_rect(i%40*8,int(i/40)*8,8,8,blocks[world[i]][1])

def moveblock(i,a):
  j=i+a
  fill_rect(j%40*8,int(j/40)*8,8,8,blocks[world[i]][1])
  replace(j,world[i])
  replace(i,0)
  fill_rect(i%40*8,int(i/40)*8,8,8,color(255,255,255))
  alrmoved.append(j)

a=input("Write 1 for a generated terrain\n")
init(int(a))

x=0
y=0
selected=1
fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
tick=0
sleep(0.5)
while True:
  draw_string(str(len(world))+" "+str(len(world2))+" "+str(len(alrmoved)),50,50)
  alrmoved=[]
  for i in range(920):
    if world[i]==7:
      for j in range(3):
        replace(i+data[1][j],6)
        fill_rect((i+data[1][j])%40*8,int((i+data[1][j])/40)*8,8,8,color(200,80,100))
      for j in range(13):
        replace(i+data[2][j],3)
        fill_rect((i+data[2][j])%40*8,int((i+data[2][j])/40)*8,8,8,color(50,150,50))
    if world[i]==4:
      for j in range(21):
        replace(i+data[0][j],0)
        fill_rect((i+data[0][j])%40*8,int((i+data[0][j])/40)*8,8,8,color(255,255,255))
    if world[i]==8 and not i in alrmoved:
      for j in range(8):
        if world[i+data[3][j]]==3 or world[i+data[3][j]]==6:
          replace(i+data[3][j],8)
          fill_rect((i+data[3][j])%40*8,int((i+data[3][j])/40)*8,8,8,color(255,100,0))
      if randint(1,5)==1:
        replace(i,0)
        fill_rect(i%40*8,int(i/40)*8,8,8,color(255,255,255))
      elif world[i-40]==0 and world[i-39]==0 and world[i-41]==0:
        a=randint(1,3)
        if a==1:moveblock(i,-39)
        elif a==2:moveblock(i,-41)
        else:moveblock(i,-40)
      elif world[i-40]==0:
        moveblock(i,-40)
      elif world[i-39]==0:
        moveblock(i,-39)
      elif world[i-41]==0:
        moveblock(i,-41)
    if i+40<920:
      if (world[i]==2 or world[i]==9) and not i in alrmoved:
        if world[i+40]==0:
          moveblock(i,40)
        elif world[i+39]==0 and world[i+41]==0:
          if randint(1,2)==1:moveblock(i,39)
          else:moveblock(i,41)
        elif world[i+39]==0:
          moveblock(i,39)
        elif world[i+41]==0:
          moveblock(i,41)
        elif world[i-1]==0 and world[i+1]==0:
          if randint(1,2)==1:moveblock(i,-1)
          else:moveblock(i,1)
        elif world[i-1]==0:
          moveblock(i,-1)
        elif world[i+1]==0:
          moveblock(i,1)
  fill_rect(0,184,320,38,color(150,150,150))
  draw_string("Selected block: "+blocks[selected][0],100,200,color(255,255,255),color(150,150,150))
  tick+=1
  draw_string("Tick: "+str(tick),0,200,color(255,255,255),color(150,150,150))
  fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
  if keydown(KEY_FOUR)and selected>0:
    selected-=1
    sleep(0.1)
  elif keydown(KEY_SIX)and selected<len(blocks)-1:
    selected+=1
    sleep(0.1)
  if keydown(KEY_LEFT)and x>0:
    fill_rect(x*8,y*8,8,8,blocks[world[(y*40)+x]][1])
    x-=1
    fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
    sleep(0.05)
  elif keydown(KEY_RIGHT)and x<39:
    fill_rect(x*8,y*8,8,8,blocks[world[(y*40)+x]][1])
    x+=1
    fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
    sleep(0.05)
  if keydown(KEY_UP)and y>0:
    fill_rect(x*8,y*8,8,8,blocks[world[(y*40)+x]][1])
    y-=1
    fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
    sleep(0.05)
  elif keydown(KEY_DOWN)and y<22:
    fill_rect(x*8,y*8,8,8,blocks[world[(y*40)+x]][1])
    y+=1
    fill_rect(x*8+3,y*8+3,2,2,color(255,0,0))
    sleep(0.05)
  if keydown(KEY_OK):
    world2=[]
    for i in range(920):
      if i==(y*40)+x:
        world2.append(selected)
      else:
        world2.append(world[i])
    fill_rect(x*8,y*8,8,8,blocks[selected][1])
    world=world2

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.