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:
1 parent
358dd1f532
commit
bec941d624
1 file changed
+54
-51
+54
-51
@@ -1,12 +1,35 @@
|
||||
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
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1920, 1080))
|
||||
clock = pygame.time.Clock()
|
||||
running = True
|
||||
rectPos = []
|
||||
remPos = []
|
||||
rects = []
|
||||
|
||||
screen.fill((0, 0, 0)) # Clear screen with black
|
||||
|
||||
#Game begins
|
||||
while running:
|
||||
@@ -14,31 +37,49 @@ while running:
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
screen.fill((0, 0, 0)) # Clear screen with black
|
||||
|
||||
pygame.display.flip() # Update the display
|
||||
#clock.tick(60) # Maintain 60 FPS
|
||||
clock.tick(30) # Maintain 30 FPS
|
||||
#Intro Screen
|
||||
|
||||
#Clear screen
|
||||
screen.fill((0, 0, 0))
|
||||
pygame.display.flip()
|
||||
|
||||
#Draw grid
|
||||
for x in range(0, 1920, 100):
|
||||
pygame.draw.line(screen, (0, 0, 60), (x, 0), (x, 1080))
|
||||
for x in range(0, screen.get_width(), 100):
|
||||
#(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
|
||||
mouseX = pygame.mouse.get_pos()[0] - 10
|
||||
mouseY = pygame.mouse.get_pos()[1] - 10
|
||||
mousePos = (pygame.mouse.get_pos()[0] - 10, pygame.mouse.get_pos()[1] - 10)
|
||||
mouseInput = pygame.mouse.get_pressed()
|
||||
|
||||
if mouseInput[0]: # Left mouse button make a white box
|
||||
#store the position for to draw the rectangles
|
||||
rectPos.append([mouseX, mouseY])
|
||||
#Snap position to grid
|
||||
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
|
||||
#store the position to remove rectangles
|
||||
remPos.append([mouseX, mouseY])
|
||||
#remove rect
|
||||
removeRect(rects, mousePos)
|
||||
|
||||
#Get keyboard input
|
||||
keys = pygame.key.get_pressed()
|
||||
@@ -55,44 +96,6 @@ while running:
|
||||
if keys[pygame.K_d]: # Move screen right when 'd' is pressed
|
||||
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
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user