Jacob_DiDiodato

Tile Editor using PyGame

Feb 23rd, 2017
339
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. #! python2.7
  2. """
  3. A Tile Editor for (mostly) RPGs
  4. #tileEditor.py
  5. Last edit was: Sunday, December 11, 2016
  6. Jacob DiDiodato
  7. """
  8.  
  9. import pygame
  10.  
  11. pygame.init()
  12.  
  13. extension = ".dat"
  14. currentArea = raw_input("Enter the area (of the game world) that you want to edit (in format areaX-areaY)")
  15.  
  16. #To erase the map
  17. map = open(currentArea + extension, "w")
  18.  
  19. keyDown = False
  20.  
  21. #the current height and width of the screen
  22. width, height = 640,480
  23. size = 840, 480
  24. tileSize = 32
  25.  
  26. LEFTMOUSEBUTTON = 0
  27. MIDDLEMOUSE = 1
  28. RIGHTMOUSEBUTTON = 2
  29.  
  30. gameTiles = 30 + 1
  31. totalSprite = 10
  32.  
  33. currentTile = 0
  34. currentSprite = 0
  35.  
  36. #lots of color constants! Yay!
  37. black = (0,0,0)
  38. red = (255,0,0)
  39. green = (0,255,0)
  40. blue = (0,0,255)
  41. grey = (211,211,211)
  42.  
  43. tileMap = []
  44.  
  45. #Fill the map with zeroes
  46. for i in range(height//tileSize):
  47.     tileMap.insert(i, [0]*(width//tileSize))
  48.  
  49. #Show a diagnostic of the map
  50. print tileMap
  51. print "The map is:", len(tileMap), "by", len(tileMap[0])
  52.  
  53. myFont = pygame.font.Font("./wonder.TTF", 20)
  54. clock = pygame.time.Clock()
  55.  
  56. screen = pygame.display.set_mode(size)
  57.  
  58. while True:
  59.    
  60.     #Allow you to quit if you press the "X" button
  61.     for event in pygame.event.get():   
  62.         if event.type == pygame.QUIT:
  63.             #print tileMap #for diagnostic purposes to see the tilemap
  64.             mapString = ""
  65.             #Make a string that we eventually write to the file
  66.             for i in range(len(tileMap)):
  67.                 for j in range(len(tileMap[i])):
  68.                     mapString += str(tileMap[i][j])
  69.                     if (j == len(tileMap[i]) - 1):
  70.                         mapString += "\n"
  71.                     else:
  72.                         mapString += ","
  73.             map.write(mapString)
  74.             raise SystemExit
  75.            
  76.         #The functionality to place blocks
  77.         if event.type == pygame.KEYDOWN and keyDown == False:
  78.             #To handle switching the different tiles
  79.             if event.key == pygame.K_q:
  80.                 currentTile -= 1
  81.                 if (currentTile < 0):
  82.                     currentTile = gameTiles - 1
  83.             elif event.key == pygame.K_e:
  84.                 currentTile += 1
  85.                 if (currentTile > gameTiles - 1):
  86.                     currentTile = 0
  87.             elif event.key == pygame.K_a:
  88.                 currentSprite -= 1
  89.                 if (currentSprite < 0):
  90.                     currentSprite = totalSprite - 1
  91.             elif event.key == pygame.K_d:
  92.                 currentSprite += 1
  93.                 if (currentSprite > totalSprite - 1):
  94.                     currentSprite = 0
  95.         elif event.type == pygame.KEYUP and keyDown == True:
  96.             keyDown == False
  97.        
  98.     #Draw the currently selected image on the right of the screen
  99.     pygame.draw.rect(screen, grey, (width, 0, size[0], size[1]))
  100.     myImage = pygame.image.load("./tiles/"+str(currentTile)+".png")
  101.     screen.blit(myImage, (720, 100))
  102.     tiles = myFont.render("Tiles", 0, green)
  103.    
  104.     screen.blit(tiles, (685, 50))
  105.    
  106.     sprites = myFont.render("Sprites", 0, green)
  107.    
  108.     screen.blit(sprites, (670, 250))
  109.            
  110.     #Checks for mouse press
  111.     if (pygame.mouse.get_pressed()[LEFTMOUSEBUTTON] or pygame.mouse.get_pressed()[RIGHTMOUSEBUTTON] or pygame.mouse.get_pressed()[MIDDLEMOUSE]) and (pygame.mouse.get_pos()[0] < 640):
  112.         #Figure out which tile the mouse is over
  113.         tileX = pygame.mouse.get_pos()[0] // tileSize
  114.         tileY = pygame.mouse.get_pos()[1] // tileSize
  115.         #Left mouse click for visual tiles
  116.         if pygame.mouse.get_pressed()[LEFTMOUSEBUTTON]:
  117.             pygame.draw.rect(screen, red, (tileX*tileSize, tileY*tileSize, tileSize,tileSize))
  118.             screen.blit(myImage, (tileX*tileSize, tileY*tileSize))
  119.             tileMap[tileY][tileX] = currentTile
  120.         #Right mouse click for sprites?
  121.         elif pygame.mouse.get_pressed()[RIGHTMOUSEBUTTON]:
  122.             pygame.draw.rect(screen, black, (tileX*tileSize, tileY*tileSize, tileSize,tileSize))
  123.             tileMap[tileY][tileX] = 0
  124.         #Akin to Minecraft's pick block for the tiles
  125.         else:
  126.             currentTile = tileMap[tileY][tileX]
  127.        
  128.     pygame.display.flip()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment