I am making a map editor. For 3 tiles I have to make 3 classes:
class cloud:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.image = pygame.image.load("top_left_outer.png").convert_alpha()self.image = pygame.transform.scale(self.image,(self.image.get_width()//4,self.image.get_height()//4))self.rect = pygame.Rect(x,y,height,width)def draw(self):self.rect.topleft = (self.x,self.y)window.blit(self.image,self.rect)class clou:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.image = pygame.image.load("top_variation_02.png").convert_alpha()self.image = pygame.transform.scale(self.image,(self.image.get_width()//4,self.image.get_height()//4))self.rect = pygame.Rect(x,y,height,width)def draw(self):self.rect.topleft = (self.x,self.y)window.blit(self.image,self.rect)class clo:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.image = pygame.image.load("top_right_outer.png").convert_alpha()self.image = pygame.transform.scale(self.image,(self.image.get_width()//4,self.image.get_height()//4))self.rect = pygame.Rect(x,y,height,width)def draw(self):self.rect.topleft = (self.x,self.y)window.blit(self.image,self.rect)
I want an easier way. How do I use my mouse to switch between tiles and place and save them?
clouds = []
clos = []
clay = []
platformGroup = pygame.sprite.Group
level = [" "," "," c d d d o "," "," "," c d d d o "," "," "," "," "," c d d d o "," "," "," "," c d d d d d d d d d d d d d o"," "," "," "," "," "," "," "," "," "," "," "]
for iy, row in enumerate(level):for ix, col in enumerate(row):if col == "c":new_platforms = cloud(ix*9.9, iy*45, 120,20,(23, 32, 42))clouds.append(new_platforms)for iy, row in enumerate(level):for ix, col in enumerate(row):if col == "d":new_platforms = clou(ix*9.9, iy*45, 120,20,(23, 32, 42))clay.append(new_platforms)for iy, row in enumerate(level):for ix, col in enumerate(row):if col == "o":new_platforms = clo(ix*9.9, iy*45, 120,20,(23, 32, 42))clos.append(new_platforms)
Full code.
The code displays 1 image:
import pygame
pygame.init()window = pygame.display.set_mode((800,700))
pygame.display.set_caption("Level Editor")class cloud:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.image = pygame.image.load("top_left_outer.png").convert_alpha()self.image = pygame.transform.scale(self.image,(self.image.get_width()//4,self.image.get_height()//4))self.rect = pygame.Rect(x,y,height,width)def draw(self):self.rect.topleft = (self.x,self.y)window.blit(self.image,self.rect)white = 155,155,155
cloud1 = cloud(200,200,50,50,white)
fps = 60
clock = pygame.time.Clock()def redraw():cloud1.draw()run = True
while run:for event in pygame.event.get():if event.type == pygame.QUIT:run = Falseredraw()pygame.display.update()pygame.quit()