Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import os
- import sys
- pygame.init()
- pygame.mixer.init()
- motion = "stop"
- collisionTolerance = 10
- boxCollisionRect = None
- blockTime = 0
- lastBlockX = 0
- lastBlockY = 0
- screen = pygame.display.set_mode((512, 512))
- pygame.display.set_caption('Sandbox Mode - Endlace')
- clock = pygame.time.Clock()
- allSprites = pygame.sprite.Group()
- ### Настраиваем директории ###
- rootPath = os.path.dirname(__file__)
- resourcesPath = os.path.join(rootPath, "resources")
- texturesPath = os.path.join(resourcesPath, "textures")
- playerPath = os.path.join(texturesPath, "player")
- skinsPath = os.path.join(playerPath, "skins")
- defaultIdle = os.path.join(skinsPath, "default-idle")
- defaultRun = os.path.join(skinsPath, "default-run")
- ### Настраиваем пути текстур ###
- ground512 = pygame.image.load(os.path.join(texturesPath, "ground512x512.png"))
- box = pygame.image.load(os.path.join(texturesPath, "normalbox.png"))
- box = pygame.transform.scale(box, (64, 64))
- playerShadow = pygame.image.load(os.path.join(playerPath, "shadow.png"))
- playerShadow = pygame.transform.scale(playerShadow, (36,64))
- boxSprite = pygame.sprite.Sprite()
- boxSprite.image = box
- boxSprite.rect = box.get_rect()
- ### Настраиваем шрифт ###
- font1 = pygame.font.Font('font.ttf', 36)
- grid1 = pygame.Rect((0,0,64,64))
- fakeScreen = screen.copy()
- testSurface = pygame.Surface((512,512))
- screenScrollX = 0
- screenScrollY = 0
- frameCount = 0
- mapList = [[[0,0,0,0,0,0,0,0], #layer 1
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0]],
- [[0,0,0,0,0,0,0,0], #layer 2
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0]],
- [[0,0,0,0,0,0,0,0], #layer 3
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0]]]
- ### Классы ###
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load(os.path.join(defaultIdle, "frame1.png"))
- self.image = pygame.transform.scale(self.image, (36, 64))
- self.rect = self.image.get_rect()
- self.rect.center = (128,128)
- def moveRight(self):
- global screenScrollX, boxCollisionRect
- collisionTolerance = 80
- try:
- if not self.rect.right > 512:
- if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.left - player.rect.right) > collisionTolerance: # and abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance :
- self.rect.x += 4
- elif abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance:
- self.rect.x += 4
- elif abs(boxCollisionRect.bottom - player.rect.top) > collisionTolerance:
- self.rect.x += 4
- except:
- self.rect.x += 4
- def moveLeft(self):
- global screenScrollX, boxCollisionRect
- collisionTolerance = 80
- try:
- if not self.rect.left < 0:
- if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.right - player.rect.left) > collisionTolerance:
- self.rect.x -= 4
- elif abs(boxCollisionRect.top - player.rect.bottom) > collisionTolerance:
- self.rect.x -= 4
- elif abs(boxCollisionRect.bottom - player.rect.top) > collisionTolerance:
- self.rect.x -= 4
- except:
- self.rect.x -= 4
- def moveUp(self):
- global screenScrollY, boxCollisionRect
- collisionTolerance = 20
- try:
- if not self.rect.top < 0:
- if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.top - player.rect.bottom) < collisionTolerance:
- self.rect.y -= 4
- elif abs(boxCollisionRect.left - player.rect.right) < collisionTolerance:
- self.rect.y -= 4
- elif abs(boxCollisionRect.right - player.rect.left) < collisionTolerance:
- self.rect.y -= 4
- except:
- self.rect.y -= 4
- def moveDown(self):
- global screenScrollY, boxCollisionRect
- collisionTolerance = 20
- try:
- if not self.rect.bottom > 512:
- if not player.rect.colliderect(boxCollisionRect) or abs(boxCollisionRect.bottom - player.rect.top) < collisionTolerance:
- self.rect.y += 4
- elif abs(boxCollisionRect.left - player.rect.right) < collisionTolerance:
- self.rect.y += 4
- elif abs(boxCollisionRect.right - player.rect.left) < collisionTolerance:
- self.rect.y += 4
- except:
- self.rect.y += 4
- def moveLeftDown(self):
- global screenScrollX, screenScrollY
- if not self.rect.bottom > 512:
- if not self.rect.left < 0:
- self.rect.y += 4
- # screenScrollY -= 8
- self.rect.x -= 4
- # screenScrollX += 8
- def moveLeftUp(self):
- global screenScrollX, screenScrollY
- if not self.rect.top < 0:
- if not self.rect.left < 0:
- self.rect.y -= 4
- # screenScrollY += 8
- self.rect.x -= 4
- # screenScrollX += 8
- def moveRightDown(self):
- global screenScrollX, screenScrollY
- if not self.rect.bottom > 512:
- if not self.rect.right > 512:
- self.rect.y += 4
- # screenScrollY -= 8
- self.rect.x += 4
- # screenScrollX -= 8
- def moveRightUp(self):
- global screenScrollX, screenScrollY
- self.rect.y -= 4
- # screenScrollY += 8
- self.rect.x += 4
- # screenScrollX -= 8
- player = Player()
- allSprites.add(player)
- def mainMenu():
- while True:
- clock.tick(60)
- screen.fill((0,0,0))
- posX, posY = pygame.mouse.get_pos()
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- pygame.display.flip()
- def createBlock(blockX, blockY):
- global boxSprite, blockTime, lastBlockX, lastBlockY
- posX, posY = pygame.mouse.get_pos()
- boxSprite = pygame.sprite.Sprite()
- boxSprite.image = box
- boxSprite.rect = box.get_rect()
- print(int(posX/64))
- print(int(posY/64))
- clickX = int(posX/64)
- clickY = int(posY/64)
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- if clickX == lastBlockX and clickY == lastBlockY:
- if blockTime == 0:
- if mapList[0][clickX][clickY] == 1:
- mapList[1][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64-28
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- print(boxSprite.rect)
- blockTime += 1
- allSprites.add(boxSprite)
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- elif blockTime == 1:
- if mapList[0][clickX][clickY] == 1:
- mapList[1][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64-28
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- print(boxSprite.rect)
- blockTime += 1
- allSprites.add(boxSprite)
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- elif blockTime == 2:
- if mapList[1][clickX][clickY] == 1:
- mapList[2][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64-56
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- print(boxSprite.rect)
- allSprites.add(boxSprite)
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- elif blockTime == 3:
- if mapList[2][clickX][clickY] == 1:
- mapList[3][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64-84
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- print(boxSprite.rect)
- allSprites.add(boxSprite)
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- else:
- if mapList[0][clickX][clickY] == 1:
- mapList[1][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64-28
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 48, 64)
- print(boxSprite.rect)
- allSprites.add(boxSprite)
- blockTime += 1
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- else:
- mapList[0][clickX][clickY] = 1
- boxSprite.rect.x = (posX//64)*64
- boxSprite.rect.y = (posY//64)*64
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 48, 64)
- print(boxSprite.rect)
- allSprites.add(boxSprite)
- blockTime = 0
- lastBlockX = clickX
- lastBlockY = clickY
- print(lastBlockX, lastBlockY)
- def sandbox():
- global motion
- global frameCount, boxCollisionRect, blockTime
- while True:
- clock.tick(60)
- screen.fill((0,0,0))
- posX, posY = pygame.mouse.get_pos()
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYUP:
- motion = "stop"
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- createBlock(posX, posY)
- global boxCollisionRect
- boxCollisionRect = pygame.Rect(boxSprite.rect.x, boxSprite.rect.y, 64, 64)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT] or keys[pygame.K_a]:
- motion = "left"
- if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
- motion = "right"
- if keys[pygame.K_UP] or keys[pygame.K_w]:
- motion = "up"
- if keys[pygame.K_DOWN] or keys[pygame.K_s]:
- motion = "down"
- if keys[pygame.K_LEFT] and keys[K_UP]:
- motion = "leftup"
- if keys[pygame.K_LEFT] and keys[K_DOWN]:
- motion = "leftdown"
- if keys[pygame.K_RIGHT] and keys[K_UP]:
- motion = "rightup"
- if keys[pygame.K_RIGHT] and keys[K_DOWN]:
- motion = "rightdown"
- if motion == "left":
- player.moveLeft()
- elif motion == "right":
- player.moveRight()
- elif motion == "up":
- player.moveUp()
- elif motion == "down":
- player.moveDown()
- if motion == "leftup":
- player.moveLeftUp()
- if motion == "leftdown":
- player.moveLeftDown()
- if motion == "rightup":
- player.moveRightUp()
- if motion == "rightdown":
- player.moveRightDown()
- if motion == "stop":
- pass
- fakeScreen.blit(ground512, (0,0))
- fakeScreen.blit(playerShadow, (player.rect.x, player.rect.y))
- allSprites.update()
- allSprites.draw(fakeScreen)
- text2 = font1.render(str(int(clock.get_fps())), False,
- (255, 255, 255))
- fakeScreen.blit(text2, (0,0))
- screen.blit(pygame.transform.scale(fakeScreen, (512, 512)), (screenScrollX,screenScrollY))
- pygame.display.flip()
- sandbox()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement