Advertisement
RedstoneHair

level designer

Mar 5th, 2021 (edited)
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.86 KB | None | 0 0
  1. import pygameextra as pe
  2. import pyperclip
  3.  
  4. pe.init()
  5. pe.display.make((500,500), "lvl designer 1.0",1)
  6. pe.E_intro.run()
  7. pe.settings.UPDATE_AUTO = False
  8. s = 20
  9. hold = False
  10. holdS = (0,0)
  11. level = []
  12. motionX = 0
  13. motionY = 0
  14. LP = (0,0)
  15. menu = False
  16. menuPos = (0,0)
  17. menuClose = False
  18. high = False
  19. selected = None
  20. touchMenu = False
  21. def CloseMenu():
  22.   global selected, menu, menuClose, touchMenu
  23.   menuClose = True
  24.   selected = None
  25.   menu = False
  26.   touchMenu = False
  27. def erase():
  28.   global level
  29.   level = []
  30.   CloseMenu()
  31. def Print():
  32.   global level
  33.   print("\nlevel was copied to clipboard")
  34.   text = ""
  35.   start = 100
  36.   end = 0
  37.   for x in level:
  38.     if x[0][0] < start:
  39.       start = x[0][0]-1
  40.     if x[0][0]+x[0][2] > end:
  41.       end = x[0][0]+x[0][2]
  42.     if x[1] == pe.color.red:
  43.       color = "pe.color.red"
  44.     elif x[1] == pe.color.green:
  45.       color = "pe.color.green"
  46.     elif x[1] == pe.color.yellow:
  47.       color = "pe.color.yellow"
  48.     rect = "("+str(x[0][0])+", "+str(x[0][1])+", "+str(x[0][2])+", "+str(x[0][3])+")"
  49.     text = text + "\n" + "["+rect+", "+color+", "+str(x[2])+"],"
  50.   startT = "[("+str(start)+", 40, 1, 50), pe.color.black, 0],"
  51.   endT = "[("+str(end)+", 40, 1, 50), pe.color.black, 0],"
  52.   pyperclip.copy(startT+"\n"+endT+"\n"+text)
  53.   print("Level Start: "+str(start)+" blocks\n"+"Level End: "+str(end*s)+" px")
  54.   CloseMenu()
  55.  
  56. def eraseBlock():
  57.   global selected, menu, menuClose, touchMenu
  58.   del level[selected]
  59.   selected = None
  60.   menu = False
  61.   menuClose = True
  62.   touchMenu = False
  63. def setC(id):
  64.   global selected
  65.   level[selected][2] = id
  66.   if id == 0:
  67.     color = pe.color.red
  68.   elif id == 1:
  69.     color = pe.color.green
  70.   elif id == 2:
  71.     color = pe.color.yellow
  72.   level[selected][1] = color
  73.  
  74. while True:
  75.   pe.fill.full(pe.color.white)
  76.   for pe.event.c in pe.event.get():
  77.     pe.event.quitcheckauto()
  78.     if pe.event.key_DOWN(100):  # RIGHT ARROW KEY CODE
  79.       motionX = 1
  80.     elif pe.event.key_DOWN(97):  # LEFT ARROW KEY CODE
  81.       motionX = -1
  82.     elif pe.event.key_UP(100) or pe.event.key_UP(97):  # IF BOTH KEYS ARE NOT PRESSED
  83.       motionX = 0
  84.  
  85.     if pe.event.key_DOWN(115):  # RIGHT ARROW KEY CODE
  86.       motionY = 1
  87.     elif pe.event.key_DOWN(119):  # LEFT ARROW KEY CODE
  88.       motionY = -1
  89.     elif pe.event.key_UP(115) or pe.event.key_UP(119):  # IF BOTH KEYS ARE NOT PRESSED
  90.       motionY = 0
  91.   LP = (LP[0] + motionX * s/4 * -1, LP[1] + motionY * s/4 * -1)  # move the camera
  92.   a = (round((LP[0] - s / 2) / s) * s)
  93.   b = (round((LP[1] - s) / s) * s) + s
  94.   pe.Layer[0][1] = (a,b)
  95.  
  96.   mp = pe.mouse.pos()
  97.   a = (round((mp[0] - s / 2) / s) * s)
  98.   b = (round((mp[1] - s) / s) * s) + s
  99.   if pe.mouse.clicked()[0] and not hold and not menu and not menuClose and not high and not touchMenu:
  100.     hold = True
  101.     holdS = (a,b)
  102.   elif pe.mouse.clicked()[2] and not hold and not high and not touchMenu:
  103.     menu = True
  104.     menuPos = (a,b)
  105.   elif hold:
  106.     if not pe.mouse.clicked()[0]:
  107.       hold = False
  108.       rect = (rect[0]-pe.Layer[0][1][0],rect[1]-pe.Layer[0][1][1],rect[2],rect[3])
  109.       realRect = (rect[0]/s,(500 - rect[1])/s,rect[2]/s,rect[3]/s)
  110.       level.append([realRect,pe.color.red,0])
  111.     else:
  112.       if a-holdS[0]+s > 0 and b-holdS[1]+s > 0:
  113.         rect = (holdS[0],holdS[1],a-holdS[0]+s,b-holdS[1]+s)
  114.         pe.draw.rect(pe.color.pink, (holdS[0],holdS[1],a-holdS[0]+s,b-holdS[1]+s),0,layer=1)
  115.       elif a-holdS[0]+s <= 0 and b-holdS[1]+s <= 0:
  116.         rect = (a, b, holdS[0]-a+s, holdS[1]-b+s)
  117.         pe.draw.rect(pe.color.pink, (a, b, holdS[0]-a+s, holdS[1]-b+s), 0,layer=1)
  118.       elif a-holdS[0]+s <= 0:
  119.         rect = (a, holdS[1],holdS[0]-a+s , b-holdS[1]+s)
  120.         pe.draw.rect(pe.color.pink, (a, holdS[1],holdS[0]-a+s , b-holdS[1]+s), 0,layer=1)
  121.       elif b-holdS[1]+s <= 0:
  122.         rect = (holdS[0], b, a-holdS[0]+s, holdS[1]-b+s)
  123.         pe.draw.rect(pe.color.pink, (holdS[0], b, a-holdS[0]+s, holdS[1]-b+s), 0,layer=1)
  124.   if not pe.mouse.clicked()[0] and not pe.mouse.clicked()[2]:
  125.     high = False
  126.   if selected != None:
  127.     block = level[selected]
  128.     if not pe.pygame.Rect(( (block[0][0] * s)+pe.Layer[0][1][0], (500 - block[0][1] * s)+pe.Layer[0][1][1], block[0][2] * s, block[0][3] * s)).colliderect(pe.pygame.Rect(a,b,s,s)) and not touchMenu:
  129.       selected = None
  130.   i = 0
  131.   for block in level:  # do this for all the level blocks
  132.     lvlrect = (block[0][0] * s, 500 - block[0][1] * s, block[0][2] * s, block[0][3] * s)
  133.     pe.draw.rect(block[1], lvlrect, 0)  # draw the block
  134.     r1 = pe.pygame.Rect(( (block[0][0] * s)+pe.Layer[0][1][0], (500 - block[0][1] * s)+pe.Layer[0][1][1], block[0][2] * s, block[0][3] * s))
  135.     r2 = pe.pygame.Rect(a,b,s,s)
  136.     if r1.colliderect(r2) and not menu and not menuClose:
  137.       pe.draw.rect(pe.color.black,lvlrect,5)
  138.       high = True
  139.       if pe.mouse.clicked()[2]:
  140.         selected = i
  141.         menuPos = (a,b)
  142.     i += 1
  143.   if menu:
  144.     p = menuPos
  145.  
  146.     pe.button.rect((p[0]+s,p[1],s,s),(255,0,0),(255,150,150), action=CloseMenu,layer=1)
  147.     pe.draw.line(pe.color.black, (p[0]+s,p[1]),(p[0]+s*2-2,p[1]+s-2),2,layer=1)
  148.     pe.draw.line(pe.color.black, (p[0]+s*2-2,p[1]),(p[0]+s,p[1]+s-2),2,layer=1)
  149.  
  150.     pe.button.rect((p[0],p[1],s,s),(0,255,255),(100,255,255),action=erase,layer=1)
  151.     pe.draw.line(pe.color.red,(p[0],p[1]+s/2),(p[0]+s,p[1]+s/2),5,layer=1)
  152.  
  153.     pe.button.rect((p[0] - s, p[1], s, s), (0, 255, 0), (150, 255, 150), action=Print, layer=1)
  154.   elif selected != None:
  155.     p = menuPos
  156.     menuRect = pe.pygame.Rect(p[0] - s, p[1]-s,s*3,s*3)
  157.     touchMenu = menuRect.colliderect(pe.pygame.Rect(a,b,s,s))
  158.     pe.draw.rect(pe.color.black, (p[0] - s-s/4, p[1] - s-s/4, s * 3.5, s * 3.5), 0, layer=1)
  159.     pe.draw.rect(pe.color.white,(p[0] - s, p[1]-s,s*3,s*3),0,layer=1)
  160.  
  161.     pe.button.rect((p[0] + s, p[1], s, s), (255, 0, 0), (255, 150, 150), action=CloseMenu, layer=1)
  162.     pe.draw.line(pe.color.black, (p[0] + s, p[1]), (p[0] + s * 2 - 2, p[1] + s - 2), 2, layer=1)
  163.     pe.draw.line(pe.color.black, (p[0] + s * 2 - 2, p[1]), (p[0] + s, p[1] + s - 2), 2, layer=1)
  164.  
  165.     pe.button.rect((p[0], p[1], s, s), (0, 255, 255), (100, 255, 255), action=eraseBlock, layer=1)
  166.     pe.draw.line(pe.color.red, (p[0], p[1] + s / 2), (p[0] + s, p[1] + s / 2), 5, layer=1)
  167.  
  168.     pe.button.rect((p[0] - s, p[1]+s, s, s), (255, 255, 0), (255, 255, 100), action=setC, data=2, layer=1)
  169.     pe.button.rect((p[0] - s, p[1], s, s), (0, 255, 0), (150, 255, 150), action=setC, data=1, layer=1)
  170.     pe.button.rect((p[0] - s, p[1]-s, s, s), (255, 0, 0), (255, 150, 150), action=setC, data=0, layer=1)
  171.  
  172.  
  173.   if menuClose:
  174.     menu = False
  175.     selected = None
  176.     if not pe.mouse.clicked()[0]:
  177.       menuClose = False
  178.  
  179.   if not menu and not high and not touchMenu:
  180.     pe.draw.rect(pe.color.black, (a, b, s, s), 2,layer=1)
  181.   pe.draw.line(pe.color.black, (0,0),(0,500+s),1)
  182.   pe.draw.line(pe.color.black, (-s,500),(500,500),1)
  183.   pe.display.update()
  184.   pe.time.tick(500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement