Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, random
- from pygame.locals import *
- def doRectsOverlap(rect1, rect2):
- for a, b in [(rect1, rect2)]:
- # Check if a's corners are inside b
- if ((isPointInsideRect(a.left, a.top, b)) or
- (isPointInsideRect(a.left, a.bottom, b)) or
- (isPointInsideRect(a.right, a.top, b)) or
- (isPointInsideRect(a.right, a.bottom, b))):
- return True
- return False
- def isPointInsideRect(x, y, rect):
- if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
- return True
- else:
- return False
- # set up pygame
- pygame.init()
- # set up the window
- WINDOWWIDTH = 600
- WINDOWHEIGHT = 600
- windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
- pygame.display.set_caption('Rectangles')
- # set up the colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- from random import choice
- foo = [BLACK, RED, GREEN, BLUE]
- # draw the background
- windowSurface.fill(WHITE)
- print('Please enter a number:')
- number = input()
- x = 0
- array = []
- for i in array:
- while int(number) > x:
- x = x+1
- x1 = random.randint(1, 400)
- y1 = random.randint(1, 400)
- x2 = random.randint(1, 400)
- y2 = random.randint(1, 400)
- x3 = random.randint(1, 400)
- y3 = random.randint(1, 400)
- x4 = random.randint(1, 400)
- y4 = random.randint(1, 400)
- box = pygame.draw.rect(windowSurface,random.choice(foo), (x1, y1, x2, y2))
- if doRectsOverlap(box, box) == False:
- box
- else:
- x = x-1
- # draw the window onto the screen
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement