what I am trying to do is append a rect on each tile I place I have tile map that allows me to place tiles but I want it to place a rect as well with each of the following tiles I am not sure how I would do it but I tried to put my tile images on a class
class block:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.block_types = [pygame.image.load('top_left_outer.png'), pygame.image.load('top_right_outer.png'), pygame.image.load('single_pillar_top.png')]
self.block_types = [pygame.transform.scale(image,(image.get_width()//4,image.get_height()//4)) for image in self.block_types]
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(surface,self.color,self.rect)
then I made a block1 variables that contains my rect I thought this would create 1 block for each of the tile I place but this is happening right no VIDEO < a rect is not appending with the tiles at all
white = (255,255,255)
block1 = block(200,200,50,50,white)
in my main loop this is how I am appending my tiles
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
print('Saving the map...')
with open('save.txt', 'w') as f:
f.write(repr(map))
# this part gets the key event and block type and place it on the map when I click
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
tile_x, tile_y = mouse_x // block1.block_types[0].get_width(), mouse_y // block1.block_types[0].get_height()
if 0 <= tile_x < len(map[0]) and 0 <= tile_y < len(map):
if block01:
map[tile_y][tile_x] = 0 # Block of block_types[0]
if block2:
map[tile_y][tile_x] = 1 # Block of block_types[0]
if block3:
map[tile_y][tile_x] = 2 # Block of block_types[0]
# this part will append tiles for me
for y in range(len(map)):
for x in range(len(map[y])):
if map[y][x] != -1:
surface.blit(block1.block_types[map[y][x]], (x * block1.block_types[0].get_width(), y * block1.block_types[0].get_height()))
TO sum it up what I am trying to say is how would I append a rect for each tile I place because later I will be using collisions with those rectangles my full code
import pygame, os
pygame.init()
surface = pygame.display.set_mode((800,700))
class block:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.block_types = [pygame.image.load('top_left_outer.png'), pygame.image.load('top_right_outer.png'), pygame.image.load('single_pillar_top.png')]
self.block_types = [pygame.transform.scale(image,(image.get_width()//4,image.get_height()//4)) for image in self.block_types]
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(surface,self.color,self.rect)
white = (255,255,255)
block1 = block(200,200,50,50,white)
map = [[-1 for x in range(40)] for y in range(40)]
if os.path.exists('save.txt'):
with open('save.txt') as f:
map = eval(f.read())
else:
map = [
[-1, -1, -1, -1],
[2, 2, -1, 2],
[1, 1, 2, 1],
[0, 0, 1, 0]
]
block01 = False
block2 = False
block3 = False
fps = 60
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
print('Saving the map...')
with open('save.txt', 'w') as f:
f.write(repr(map))
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
tile_x, tile_y = mouse_x // block1.block_types[0].get_width(), mouse_y // block1.block_types[0].get_height()
if 0 <= tile_x < len(map[0]) and 0 <= tile_y < len(map):
if block01:
map[tile_y][tile_x] = 0 # Block of block_types[0]
if block2:
map[tile_y][tile_x] = 1 # Block of block_types[0]
if block3:
map[tile_y][tile_x] = 2 # Block of block_types[0]
#---------------------------------------------------------------
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
block01 = True
#----------------------------------------------------
surface.fill((0, 0, 0))
block1.draw()
for y in range(len(map)):
for x in range(len(map[y])):
if map[y][x] != -1:
surface.blit(block1.block_types[map[y][x]], (x * block1.block_types[0].get_width(), y * block1.block_types[0].get_height()))
pygame.display.flip()
clock.tick(fps)
pygame.quit()