Jacob_DiDiodato icon

Tile Editor using PyGame

Jacob_DiDiodato | PRO | 02/24/17 01:11:42 AM UTC | 0 ⭐ | 290 👁️ | Never ⏰ | []
Python |

3.6 KB

|

None

|

0 👍

/

0 👎

#! python2.7
"""
A Tile Editor for (mostly) RPGs
#tileEditor.py
Last edit was: Sunday, December 11, 2016
Jacob DiDiodato
"""
 
import pygame
 
pygame.init()
 
extension = ".dat"
currentArea = raw_input("Enter the area (of the game world) that you want to edit (in format areaX-areaY)")
 
#To erase the map
map = open(currentArea + extension, "w")
 
keyDown = False
 
#the current height and width of the screen
width, height = 640,480
size = 840, 480
tileSize = 32
 
LEFTMOUSEBUTTON = 0
MIDDLEMOUSE = 1
RIGHTMOUSEBUTTON = 2
 
gameTiles = 30 + 1
totalSprite = 10
 
currentTile = 0
currentSprite = 0
 
#lots of color constants! Yay!
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
grey = (211,211,211)
 
tileMap = []
 
#Fill the map with zeroes
for i in range(height//tileSize):
    tileMap.insert(i, [0]*(width//tileSize))
 
#Show a diagnostic of the map
print tileMap
print "The map is:", len(tileMap), "by", len(tileMap[0])
 
myFont = pygame.font.Font("./wonder.TTF", 20)
clock = pygame.time.Clock()
 
screen = pygame.display.set_mode(size)
 
while True:
    
    #Allow you to quit if you press the "X" button
    for event in pygame.event.get():    
        if event.type == pygame.QUIT:
            #print tileMap #for diagnostic purposes to see the tilemap
            mapString = ""
            #Make a string that we eventually write to the file
            for i in range(len(tileMap)):
                for j in range(len(tileMap[i])):
                    mapString += str(tileMap[i][j])
                    if (j == len(tileMap[i]) - 1):
                        mapString += "\n"
                    else:
                        mapString += ","
            map.write(mapString)
            raise SystemExit
            
        #The functionality to place blocks
        if event.type == pygame.KEYDOWN and keyDown == False:
            #To handle switching the different tiles
            if event.key == pygame.K_q:
                currentTile -= 1
                if (currentTile < 0):
                    currentTile = gameTiles - 1
            elif event.key == pygame.K_e:
                currentTile += 1
                if (currentTile > gameTiles - 1):
                    currentTile = 0
            elif event.key == pygame.K_a:
                currentSprite -= 1
                if (currentSprite < 0):
                    currentSprite = totalSprite - 1
            elif event.key == pygame.K_d:
                currentSprite += 1
                if (currentSprite > totalSprite - 1):
                    currentSprite = 0
        elif event.type == pygame.KEYUP and keyDown == True:
            keyDown == False
        
    #Draw the currently selected image on the right of the screen
    pygame.draw.rect(screen, grey, (width, 0, size[0], size[1]))
    myImage = pygame.image.load("./tiles/"+str(currentTile)+".png")
    screen.blit(myImage, (720, 100))
    tiles = myFont.render("Tiles", 0, green)
    
    screen.blit(tiles, (685, 50))
    
    sprites = myFont.render("Sprites", 0, green)
    
    screen.blit(sprites, (670, 250))
            
    #Checks for mouse press
    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):
        #Figure out which tile the mouse is over
        tileX = pygame.mouse.get_pos()[0] // tileSize
        tileY = pygame.mouse.get_pos()[1] // tileSize
        #Left mouse click for visual tiles
        if pygame.mouse.get_pressed()[LEFTMOUSEBUTTON]:
            pygame.draw.rect(screen, red, (tileX*tileSize, tileY*tileSize, tileSize,tileSize))
            screen.blit(myImage, (tileX*tileSize, tileY*tileSize))
            tileMap[tileY][tileX] = currentTile
        #Right mouse click for sprites?
        elif pygame.mouse.get_pressed()[RIGHTMOUSEBUTTON]:
            pygame.draw.rect(screen, black, (tileX*tileSize, tileY*tileSize, tileSize,tileSize))
            tileMap[tileY][tileX] = 0
        #Akin to Minecraft's pick block for the tiles
        else:
            currentTile = tileMap[tileY][tileX]
        
    pygame.display.flip()

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎