Advertisement
Guest User

Rectangles

a guest
Jan 20th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import pygame, sys, random
  2. from pygame.locals import *
  3.  
  4. def doRectsOverlap(rect1, rect2):
  5.     for a, b in [(rect1, rect2)]:
  6.         # Check if a's corners are inside b
  7.         if ((isPointInsideRect(a.left, a.top, b)) or
  8.             (isPointInsideRect(a.left, a.bottom, b)) or
  9.             (isPointInsideRect(a.right, a.top, b)) or
  10.             (isPointInsideRect(a.right, a.bottom, b))):
  11.             return True
  12.  
  13.     return False
  14.  
  15. def isPointInsideRect(x, y, rect):
  16.     if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
  17.         return True
  18.     else:
  19.         return False
  20.  
  21. # set up pygame
  22. pygame.init()
  23.  
  24. # set up the window
  25. WINDOWWIDTH = 600
  26. WINDOWHEIGHT = 600
  27. windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
  28. pygame.display.set_caption('Rectangles')
  29.  
  30. # set up the colors
  31. BLACK = (0, 0, 0)
  32. WHITE = (255, 255, 255)
  33. RED = (255, 0, 0)
  34. GREEN = (0, 255, 0)
  35. BLUE = (0, 0, 255)
  36.  
  37. from random import choice
  38. foo = [BLACK, RED, GREEN, BLUE]
  39.  
  40. # draw the background
  41. windowSurface.fill(WHITE)
  42.  
  43. print('Please enter a number:')
  44. number = input()
  45. x = 0
  46. array = []
  47. for i in array:
  48.    
  49. while int(number) > x:
  50.     x = x+1
  51.     x1 = random.randint(1, 400)
  52.     y1 = random.randint(1, 400)
  53.     x2 = random.randint(1, 400)
  54.     y2 = random.randint(1, 400)
  55.     x3 = random.randint(1, 400)
  56.     y3 = random.randint(1, 400)
  57.     x4 = random.randint(1, 400)
  58.     y4 = random.randint(1, 400)
  59.     box = pygame.draw.rect(windowSurface,random.choice(foo), (x1, y1, x2, y2))
  60.     if doRectsOverlap(box, box) == False:
  61.         box
  62.     else:
  63.         x = x-1
  64.  
  65. # draw the window onto the screen
  66. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement