How can I make a map editor?

2024/10/9 20:29:54

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.

enter image description here

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()
Answer

You failed to continue generalizing the tile properties. You don't make a different class for each position, size, and color; why do it for the image? Just make that another instance parameter:

class cloud:def __init__(self, x, y, height, width, color, image):...self.image = pygame.image.load(image).convert_alpha()...cloud_list = {"top-left"  : cloud(200, 200, 50,  50, white,  "top_left_outer.png"),"top-right" : cloud(400, 200, 50,  50, yellow, "top_rght_outer.png"),"var2"      : cloud(300, 300, 50, 100, purple, "top_variation_02.png"),...
}

This gives you a dict of tiles, indexed by a readable name.

Does that get you moving?

https://en.xdnf.cn/q/118539.html

Related Q&A

Counting total number of unique characters for Python string

For my question above, Im terribly stuck. So far, the code I have come up with is:def count_bases():get_user_input()amountA=get_user_input.count(A)if amountA == 0:print("wrong")else:print (&q…

adding a newly created and uploaded package to pycharm

I created a package (thompcoUtils) on test.pypi.org and pypi.org https://pypi.org/project/thompcoUtils/ and https://test.pypi.org/project/thompcoUtils/ show the package is installed in both the test an…

Using builtin name as local variable but also as builtin [duplicate]

This question already has answers here:UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)(14 answers)Closed 1 year ago.I have the following f…

How to print the results of a SQLite query in python?

Im trying to print the results of this SQLite query to check whether it has stored the data within the database. At the moment it just prints None. Is there a way to open the database in a program like…

python sort strings with leading numbers alphabetically

I have a list of filenames, each of them beginning with a leading number:10_file 11_file 1_file 20_file 21_file 2_file ...I need to put it in this order:1_file 10_file 11_file 2_file 21_file 22_file ..…

Javascript is not recognizing a Flask variable

Im passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isnt recognizing it. The …

Float sum broken? [duplicate]

This question already has answers here:Is floating-point math broken?(36 answers)Closed 9 years ago.print(0.1 + 0.2 == 0.3)returnsFalseWhy?

SyntaxError: EOL while scanning string literal -Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Formatting a return value from a serial device

I am reading a value from a device over serial, and the return value has the format: [Theoretical position in mm, Encoder position in mm], for example, b\r#-0.001504,-0.001516\n I would like to format …

if Else statement inside for loop is not working [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.am using following code to check certain conditionsfor myarg in my…