Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.38 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. import os
  4. import sys
  5.  
  6. pygame.init()
  7. pygame.mixer.init()
  8.  
  9. motion = "stop"
  10.  
  11. collisionTolerance = 10
  12.  
  13. boxCollisionRect = None
  14.  
  15. blockTime = 0
  16. lastBlockX = 0
  17. lastBlockY = 0
  18.  
  19. screen = pygame.display.set_mode((512, 512))
  20. pygame.display.set_caption('Sandbox Mode - Endlace')
  21.  
  22. clock = pygame.time.Clock()
  23. allSprites = pygame.sprite.Group()
  24.  
  25. ### Настраиваем директории ###
  26. rootPath = os.path.dirname(__file__)
  27. resourcesPath = os.path.join(rootPath, "resources")
  28. texturesPath = os.path.join(resourcesPath, "textures")
  29. playerPath = os.path.join(texturesPath, "player")
  30. skinsPath = os.path.join(playerPath, "skins")
  31. defaultIdle = os.path.join(skinsPath, "default-idle")
  32. defaultRun = os.path.join(skinsPath, "default-run")
  33.  
  34. ### Настраиваем пути текстур ###
  35. ground512 = pygame.image.load(os.path.join(texturesPath, "ground512x512.png"))
  36. box = pygame.image.load(os.path.join(texturesPath, "normalbox.png"))
  37. box = pygame.transform.scale(box, (64, 64))
  38. playerShadow = pygame.image.load(os.path.join(playerPath, "shadow.png"))
  39. playerShadow = pygame.transform.scale(playerShadow, (36,64))
  40. boxSprite = pygame.sprite.Sprite()
  41. boxSprite.image = box
  42. boxSprite.rect = box.get_rect()
  43.  
  44. ### Настраиваем шрифт ###
  45. font1 = pygame.font.Font('font.ttf', 36)
  46.  
  47. grid1 = pygame.Rect((0,0,64,64))
  48. fakeScreen = screen.copy()
  49. testSurface = pygame.Surface((512,512))
  50.  
  51. screenScrollX = 0
  52. screenScrollY = 0
  53.  
  54. frameCount = 0
  55.  
  56. mapList = [[[0,0,0,0,0,0,0,0], #layer 1
  57.             [0,0,0,0,0,0,0,0],
  58.             [0,0,0,0,0,0,0,0],
  59.             [0,0,0,0,0,0,0,0],
  60.             [0,0,0,0,0,0,0,0],
  61.             [0,0,0,0,0,0,0,0],
  62.             [0,0,0,0,0,0,0,0],
  63.             [0,0,0,0,0,0,0,0]],
  64.            [[0,0,0,0,0,0,0,0], #layer 2
  65.             [0,0,0,0,0,0,0,0],
  66.             [0,0,0,0,0,0,0,0],
  67.             [0,0,0,0,0,0,0,0],
  68.             [0,0,0,0,0,0,0,0],
  69.             [0,0,0,0,0,0,0,0],
  70.             [0,0,0,0,0,0,0,0],
  71.             [0,0,0,0,0,0,0,0]],
  72.            [[0,0,0,0,0,0,0,0], #layer 3
  73.             [0,0,0,0,0,0,0,0],
  74.             [0,0,0,0,0,0,0,0],
  75.             [0,0,0,0,0,0,0,0],
  76.             [0,0,0,0,0,0,0,0],
  77.             [0,0,0,0,0,0,0,0],
  78.             [0,0,0,0,0,0,0,0],
  79.             [0,0,0,0,0,0,0,0]]]
  80.  
  81. ### Классы ###
  82. class Player(pygame.sprite.Sprite):
  83.     def __init__(self):
  84.         pygame.sprite.Sprite.__init__(self)
  85.         self.image = pygame.image.load(os.path.join(defaultIdle, "frame1.png"))
  86.         self.image = pygame.transform.scale(self.image, (36, 64))
  87.         self.rect = self.image.get_rect()
  88.         self.rect.center = (128,128)
  89.     def moveRight(self):
  90.         global screenScrollX, boxCollisionRect
  91.         collisionTolerance = 80
  92.         try:
  93.             if not self.rect.right > 512:
  94.                 if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.left - player.rect.right) > collisionTolerance: # and abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance :
  95.                     self.rect.x += 4
  96.                 elif abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance:
  97.                     self.rect.x += 4
  98.                 elif abs(boxCollisionRect.bottom - player.rect.top) > collisionTolerance:
  99.                     self.rect.x += 4
  100.         except:
  101.             self.rect.x += 4
  102.    
  103.     def moveLeft(self):
  104.         global screenScrollX, boxCollisionRect
  105.         collisionTolerance = 80
  106.         try:
  107.             if not self.rect.left < 0:
  108.                 if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.right - player.rect.left) > collisionTolerance:
  109.                     self.rect.x -= 4
  110.                 elif abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance:
  111.                     self.rect.x -= 4
  112.                 elif abs(boxCollisionRect.bottom - player.rect.top) > collisionTolerance:
  113.                     self.rect.x -= 4
  114.         except:
  115.             self.rect.x -= 4
  116.    
  117.     def moveUp(self):
  118.         global screenScrollY, boxCollisionRect
  119.         collisionTolerance = 20
  120.         try:
  121.             if not self.rect.top < 0:
  122.                 if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.top - player.rect.bottom) < collisionTolerance:
  123.                     self.rect.y -= 4
  124.                 elif abs(boxCollisionRect.left - player.rect.right) < collisionTolerance:
  125.                     self.rect.y -= 4
  126.                 elif abs(boxCollisionRect.right - player.rect.left) < collisionTolerance:
  127.                     self.rect.y -= 4
  128.         except:
  129.             self.rect.y -= 4
  130.    
  131.     def moveDown(self):
  132.         global screenScrollY, boxCollisionRect
  133.         collisionTolerance = 20
  134.         try:
  135.             if not self.rect.bottom > 512:
  136.                 if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.bottom - player.rect.top) < collisionTolerance:
  137.                     self.rect.y += 4
  138.                 elif abs(boxCollisionRect.left - player.rect.right) < collisionTolerance:
  139.                     self.rect.y += 4
  140.                 elif abs(boxCollisionRect.right - player.rect.left) < collisionTolerance:
  141.                     self.rect.y += 4
  142.         except:
  143.             self.rect.y += 4
  144.    
  145.     def moveLeftDown(self):
  146.         global screenScrollX, screenScrollY
  147.         if not self.rect.bottom > 512:
  148.             if not self.rect.left < 0:
  149.                 self.rect.y += 4
  150.                 # screenScrollY -= 8
  151.                 self.rect.x -= 4
  152.                 # screenScrollX += 8
  153.  
  154.     def moveLeftUp(self):
  155.         global screenScrollX, screenScrollY
  156.         if not self.rect.top < 0:
  157.             if not self.rect.left < 0:
  158.                 self.rect.y -= 4
  159.                 # screenScrollY += 8
  160.                 self.rect.x -= 4
  161.                 # screenScrollX += 8
  162.  
  163.     def moveRightDown(self):
  164.         global screenScrollX, screenScrollY
  165.         if not self.rect.bottom > 512:
  166.             if not self.rect.right > 512:
  167.                 self.rect.y += 4
  168.                 # screenScrollY -= 8
  169.                 self.rect.x += 4
  170.                 # screenScrollX -= 8
  171.  
  172.     def moveRightUp(self):
  173.         global screenScrollX, screenScrollY
  174.         self.rect.y -= 4
  175.         # screenScrollY += 8
  176.         self.rect.x += 4
  177.         # screenScrollX -= 8
  178.  
  179.  
  180. player = Player()
  181. allSprites.add(player)
  182.  
  183. def mainMenu():
  184.     while True:
  185.         clock.tick(60)
  186.  
  187.         screen.fill((0,0,0))
  188.  
  189.         posX, posY = pygame.mouse.get_pos()
  190.  
  191.         for event in pygame.event.get():
  192.             if event.type == QUIT:
  193.                 pygame.quit()
  194.                 sys.exit()
  195.        
  196.         pygame.display.flip()
  197.  
  198. def createBlock(blockX, blockY):
  199.     global boxSprite, blockTime, lastBlockX, lastBlockY
  200.     posX, posY = pygame.mouse.get_pos()
  201.     boxSprite = pygame.sprite.Sprite()
  202.     boxSprite.image = box
  203.     boxSprite.rect = box.get_rect()
  204.     print(int(posX/64))
  205.     print(int(posY/64))
  206.     clickX = int(posX/64)
  207.     clickY = int(posY/64)
  208.     boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  209.     if clickX == lastBlockX and clickY == lastBlockY:
  210.         if blockTime == 0:
  211.             if mapList[0][clickX][clickY] == 1:
  212.                 mapList[1][clickX][clickY] = 1
  213.                 boxSprite.rect.x = (posX//64)*64
  214.                 boxSprite.rect.y = (posY//64)*64-28
  215.                 boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  216.                 print(boxSprite.rect)
  217.                 blockTime += 1
  218.                 allSprites.add(boxSprite)
  219.                 lastBlockX = clickX
  220.                 lastBlockY = clickY
  221.                 print(lastBlockX, lastBlockY)
  222.         elif blockTime == 1:
  223.             if mapList[0][clickX][clickY] == 1:
  224.                 mapList[1][clickX][clickY] = 1
  225.                 boxSprite.rect.x = (posX//64)*64
  226.                 boxSprite.rect.y = (posY//64)*64-28
  227.                 boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  228.                 print(boxSprite.rect)
  229.                 blockTime += 1
  230.                 allSprites.add(boxSprite)
  231.                 lastBlockX = clickX
  232.                 lastBlockY = clickY
  233.                 print(lastBlockX, lastBlockY)
  234.         elif blockTime == 2:
  235.             if mapList[1][clickX][clickY] == 1:
  236.                 mapList[2][clickX][clickY] = 1
  237.                 boxSprite.rect.x = (posX//64)*64
  238.                 boxSprite.rect.y = (posY//64)*64-56
  239.                 boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  240.                 print(boxSprite.rect)
  241.                 allSprites.add(boxSprite)
  242.                 lastBlockX = clickX
  243.                 lastBlockY = clickY
  244.                 print(lastBlockX, lastBlockY)
  245.         elif blockTime == 3:
  246.             if mapList[2][clickX][clickY] == 1:
  247.                 mapList[3][clickX][clickY] = 1
  248.                 boxSprite.rect.x = (posX//64)*64
  249.                 boxSprite.rect.y = (posY//64)*64-84
  250.                 boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  251.                 print(boxSprite.rect)
  252.                 allSprites.add(boxSprite)
  253.                 lastBlockX = clickX
  254.                 lastBlockY = clickY
  255.                 print(lastBlockX, lastBlockY)
  256.     else:
  257.         if mapList[0][clickX][clickY] == 1:
  258.             mapList[1][clickX][clickY] = 1
  259.             boxSprite.rect.x = (posX//64)*64
  260.             boxSprite.rect.y = (posY//64)*64-28
  261.             boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 48, 64)
  262.             print(boxSprite.rect)
  263.             allSprites.add(boxSprite)
  264.             blockTime += 1
  265.             lastBlockX = clickX
  266.             lastBlockY = clickY
  267.             print(lastBlockX, lastBlockY)
  268.         else:
  269.             mapList[0][clickX][clickY] = 1
  270.             boxSprite.rect.x = (posX//64)*64
  271.             boxSprite.rect.y = (posY//64)*64
  272.             boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 48, 64)
  273.             print(boxSprite.rect)
  274.             allSprites.add(boxSprite)
  275.             blockTime = 0
  276.             lastBlockX = clickX
  277.             lastBlockY = clickY
  278.             print(lastBlockX, lastBlockY)
  279.  
  280. def sandbox():
  281.     global motion
  282.     global frameCount, boxCollisionRect, blockTime
  283.     while True:
  284.         clock.tick(60)
  285.        
  286.         screen.fill((0,0,0))
  287.  
  288.         posX, posY = pygame.mouse.get_pos()
  289.  
  290.         for event in pygame.event.get():
  291.             if event.type == QUIT:
  292.                 pygame.quit()
  293.                 sys.exit()
  294.             elif event.type == pygame.KEYUP:
  295.                 motion = "stop"
  296.             elif event.type == pygame.MOUSEBUTTONDOWN:
  297.                 if event.button == 1:
  298.                     createBlock(posX, posY)
  299.                     global boxCollisionRect
  300.                     boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
  301.         keys = pygame.key.get_pressed()
  302.  
  303.         if keys[pygame.K_LEFT] or keys[pygame.K_a]:
  304.             motion = "left"
  305.         if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
  306.             motion = "right"
  307.         if keys[pygame.K_UP] or keys[pygame.K_w]:
  308.             motion = "up"
  309.         if keys[pygame.K_DOWN] or keys[pygame.K_s]:
  310.             motion = "down"
  311.         if keys[pygame.K_LEFT] and keys[K_UP]:
  312.             motion = "leftup"
  313.         if keys[pygame.K_LEFT] and keys[K_DOWN]:
  314.             motion = "leftdown"
  315.         if keys[pygame.K_RIGHT] and keys[K_UP]:
  316.             motion = "rightup"
  317.         if keys[pygame.K_RIGHT] and keys[K_DOWN]:
  318.             motion = "rightdown"
  319.  
  320.         if motion == "left":
  321.             player.moveLeft()
  322.         elif motion == "right":
  323.             player.moveRight()
  324.         elif motion == "up":
  325.             player.moveUp()
  326.         elif motion == "down":
  327.             player.moveDown()
  328.  
  329.         if motion == "leftup":
  330.             player.moveLeftUp()
  331.         if motion == "leftdown":
  332.             player.moveLeftDown()
  333.         if motion == "rightup":
  334.             player.moveRightUp()
  335.         if motion == "rightdown":  
  336.             player.moveRightDown()
  337.  
  338.         if motion == "stop":
  339.             pass
  340.  
  341.         fakeScreen.blit(ground512, (0,0))
  342.         fakeScreen.blit(playerShadow, (player.rect.x, player.rect.y))
  343.         allSprites.update()
  344.         allSprites.draw(fakeScreen)
  345.         text2 = font1.render(str(int(clock.get_fps())), False,
  346.                   (255, 255, 255))
  347.         fakeScreen.blit(text2, (0,0))
  348.         screen.blit(pygame.transform.scale(fakeScreen, (512, 512)), (screenScrollX,screenScrollY))
  349.         pygame.display.flip()
  350.  
  351. sandbox()
  352.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement