Added two functions to draw rectangles and to remove rectangles, moved the logic for creating and removing as well as altered it

This commit is contained in:
DulcisL committed 2025-10-15 10:09:17 -04:00
1 parent 358dd1f532
commit bec941d624
1 file changed
+54 -51
+54 -51
View File
@@ -1,12 +1,35 @@
import pygame import pygame
#Functions
def drawRects(rects):
for rectangle in rects:
#(Surface, color [A], rect, width)
pygame.draw.rect(screen, (255, 255, 255, 1), rectangle, width=0)
#update screen
pygame.display.update(rectangle)
print(f"I have displayed the rectangle")
return 1
def removeRect(rects, pos):
print(f"I am removing rectangle at {pos}")
for rectangle in rects:
if rectangle.collidepoint(pos):
rects.remove(rectangle)
print(f"I have removed the rectangle")
#update screen
pygame.display.update(rectangle)
return 1
return 0
# Initializations # Initializations
pygame.init() pygame.init()
screen = pygame.display.set_mode((1920, 1080)) screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock() clock = pygame.time.Clock()
running = True running = True
rectPos = [] rects = []
remPos = []
screen.fill((0, 0, 0)) # Clear screen with black
#Game begins #Game begins
while running: while running:
@@ -14,31 +37,49 @@ while running:
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
screen.fill((0, 0, 0)) # Clear screen with black
pygame.display.flip() # Update the display pygame.display.flip() # Update the display
#clock.tick(60) # Maintain 60 FPS clock.tick(30) # Maintain 30 FPS
#Intro Screen #Intro Screen
#Clear screen #Clear screen
screen.fill((0, 0, 0))
pygame.display.flip() pygame.display.flip()
#Draw grid #Draw grid
for x in range(0, 1920, 100): for x in range(0, screen.get_width(), 100):
pygame.draw.line(screen, (0, 0, 60), (x, 0), (x, 1080)) #(surface, color, start_pos, end_pos)
pygame.draw.line(screen, (0, 0, 60), (x, 0), (x, screen.get_width()))
for y in range (0, screen.get_height(), 100):
pygame.draw.line(screen, (0, 0, 60), (0, y), (screen.get_width(), y))
#Get mouse input #Get mouse input
mouseX = pygame.mouse.get_pos()[0] - 10 mousePos = (pygame.mouse.get_pos()[0] - 10, pygame.mouse.get_pos()[1] - 10)
mouseY = pygame.mouse.get_pos()[1] - 10
mouseInput = pygame.mouse.get_pressed() mouseInput = pygame.mouse.get_pressed()
if mouseInput[0]: # Left mouse button make a white box if mouseInput[0]: # Left mouse button make a white box
#store the position for to draw the rectangles #Snap position to grid
rectPos.append([mouseX, mouseY]) x,y = mousePos
if x % 10 == 0 and y % 10 == 0:
break
if x % 10 != 0:
x += 10 - (x % 10)
if y % 10 != 0:
y += 10 - (y % 10)
#Draw rect at grid location
newRect = pygame.Rect(x, y, 10, 10)
if len(rects) == 0:
rects.append(newRect)
if len(rects) != 0:
for rectangle in rects:
if rectangle.colliderect(newRect) == False:
rects.append(newRect)
drawRects(rects)
if mouseInput[2]: # Right mouse button get rid of white box if mouseInput[2]: # Right mouse button get rid of white box
#store the position to remove rectangles #remove rect
remPos.append([mouseX, mouseY]) removeRect(rects, mousePos)
#Get keyboard input #Get keyboard input
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
@@ -55,44 +96,6 @@ while running:
if keys[pygame.K_d]: # Move screen right when 'd' is pressed if keys[pygame.K_d]: # Move screen right when 'd' is pressed
screen.scroll(dx=100)() screen.scroll(dx=100)()
#Drawing / Removing
if len(rectPos) > 1:
#Draw rectangles
i = 0
while i < len(rectPos):
pygame.draw.rect(screen, (255, 255, 255), (rectPos[i][0], rectPos[i][1], 10, 10), width = 0)
i += 1
if len(remPos) > 1:
#remove the rect
#Check rectPos for a matching x,y
i = 0
while i < len(rectPos):
posx = rectPos[i][0]
remx = remPos[0][0]
up = True
#if match
if remx == posx:
#remove the rectangle
pygame.draw.rect(screen, (0, 0, 0), (rectPos[i][0], rectPos[i][1], 10, 10), width = 0)
#Delete both entries
rectPos.remove(i)
remPos.pop()
if remPos[0][0] != posx:
if up == True:
remx += 1
if up == False:
remx -= 1
#Check if difference is greater than 10
if (remx - remPos[0][0]) > 10 or (remx - remPos[0][0]) < -10:
if up == True:
#Skip area already searched
remx - 10
if up == False:
#Skip area already searched
remx + 10
up = not(up)
#Game Logic #Game Logic