Advertisement
Guest User

Random rect

a guest
Nov 22nd, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import random, math
  2. import pygame, sys
  3. from pygame.locals import *
  4. from pygame import gfxdraw
  5.  
  6. pygame.init()
  7.  
  8. screen_width = 1000
  9. screen_height = 800
  10.  
  11. rect_x = 500
  12. rect_y = 250
  13. rect_width = 100
  14. rect_height = 75
  15.  
  16. rand_max = (screen_width * screen_height) - (rect_width * rect_height)
  17. rect_start = (screen_width * rect_y) + rect_x
  18. rect_end = (screen_width * (rect_y + rect_height)) + rect_x + rect_width
  19.  
  20. def rand_point():
  21. rand_raw = random.randint(0, rand_max-1)
  22. if rand_raw > rect_end:
  23. rand_raw += rect_width * rect_height
  24. elif rand_raw > rect_start:
  25. rows_add = rand_raw // screen_width
  26. col = rand_raw % screen_width
  27. if col >= rect_x:
  28. rand_raw += rect_width
  29. rand_raw += rows_add * screen_width
  30. x = rand_raw % screen_width
  31. y = rand_raw // screen_width
  32. return (x, y)
  33.  
  34.  
  35. window = pygame.display.set_mode((screen_width, screen_height))
  36. window.fill((0, 0, 0))
  37.  
  38. while True:
  39. x, y = rand_point()
  40. print x, y
  41.  
  42. gfxdraw.pixel(window, x, y, (255, 0, 0))
  43.  
  44. for event in pygame.event.get():
  45. if event.type == QUIT:
  46. pygame.quit()
  47. sys.exit()
  48.  
  49. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement