So.. the things is:
I've created a small 'level editor', just like that, for fun and the experience.
Anyway, I've finished it, but couldn't find a way to make the player be able to interact with the objects that are placed by the user, as if I check with colliderect it doesn't work because the variable must be a rect style object! So my only solution is to check whether the colors overlap.
You'll understand everything once you see the code:
import pygame
import time
size = 30
playing = False
pozx = 0
pozy = 0
color = 'red'
cube_list_red = []
cube_list_green = []
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw_red(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
def draw_green(self):
pygame.draw.rect(screen, (0, 255, 0), self.square)
cube = Cube()
drawing_cube = False
drawing_cube2 = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if not playing:
cube = Cube()
cube.update()
if color == 'red':
cube_list_red.append(cube)
elif color == 'green':
cube_list_green.append(cube)
drawing_cube = True
key = pygame.key.get_pressed()
if key[pygame.K_c]:
if not playing:
if color == 'red':
color = 'green'
time.sleep(0.5)
print(color)
else:
color = 'red'
time.sleep(0.5)
print(color)
elif key[pygame.K_r]:
cube_list_red.clear()
cube_list_green.clear()
elif key[pygame.K_u]:
if not playing:
if color == 'red':
if len(cube_list_red) > 0:
cube_list_red.pop()
time.sleep(0.5)
elif color == 'green':
if len(cube_list_green) > 0:
cube_list_green.pop()
time.sleep(0.5)
elif key[pygame.K_p]:
playing = True
elif key[pygame.K_9]:
playing = False
pozx = 0
pozy = 0
if key[pygame.K_w] == True:
if pozy >= 0 and playing != False:
pozy = pozy - 0.40
if key[pygame.K_a] == True:
if pozx >= 0 and playing != False:
pozx = pozx - 0.40
if key[pygame.K_s] == True:
if pozy <= 500 - size and playing != False:
pozy = pozy + 0.40
if key[pygame.K_d] == True and playing != False:
if pozx <= 800 - size:
pozx = pozx + 0.40
screen.fill((0, 0, 255))
player = pygame.Rect(pozx, pozy, size, size)
if drawing_cube:
for cube in cube_list_red:
cube.draw_red()
for cube in cube_list_green:
cube.draw_green()
if playing:
pygame.draw.rect(screen, (255, 255, 0), player)
pygame.display.flip()
pygame.quit()
What I have tried:
I've tried to do player.colliderect(cube) and a lot of other for loop things with the cube_list_red and cube_list_green variables