Advertisement
Guest User

Curtis Brownian Motion (WILL NEED PYGAME)

a guest
Oct 9th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Define some colors
  5. BLACK = (0, 0, 0)
  6. WHITE = (255, 255, 255)
  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)
  9.  
  10. # Defines variables for all particles
  11. posx = [10,10,25,500] # origonal X axis
  12. posy = [10,460,20,20] # origonal Y axis
  13. speedx = [0,0,1,1] # speed for the X axis
  14. speedy = [1,1,0,0] # speed for the Y axis
  15. sizexy = [20,20,20,20] # size of each particle
  16.  
  17.  
  18. # the block of code before is used to randomly generate information for starting positions (this would be used for the final sim)
  19. """
  20. for i in range(2):
  21. posx.append(random.randint(10,690))
  22. posy.append(random.randint(10,490))
  23. speedx.append(1)
  24. speedy.append(1)
  25. sizexy.append(20)
  26. """
  27.  
  28. pygame.init()
  29.  
  30. # Set the width and height of the screen [width, height]
  31. size = (700, 500)
  32. screen = pygame.display.set_mode(size)
  33.  
  34. pygame.display.set_caption("Brownian Motion") #sets the name of the window
  35.  
  36. # This makes the program shutdown when the close button is clicked
  37. done = False
  38.  
  39. # Used to manage how fast the screen updates
  40. clock = pygame.time.Clock()
  41.  
  42. # -------- Main Program Loop -----------
  43. while not done:
  44. # --- Main event loop
  45. for event in pygame.event.get():
  46. if event.type == pygame.QUIT:
  47. done = True
  48.  
  49. # --- Game logic should go here
  50.  
  51. #this changes the position of the box every tick by its speed
  52. for i in range(len(posx)):
  53. i -= 1
  54. posx[i] = posx[i] + speedx[i]
  55. posy[i] = posy[i] + speedy[i]
  56. i += 1
  57.  
  58. #this is collision detection
  59. #this block is to bounce on the y axis, it checks if the two particles are on the same axis, then if they are close enough to be considered touching, if they are it reverses the y speed
  60. for i in range(len(posx)):
  61. for j in range(len(posx)):
  62. if i != j:
  63. if posx[i] == posx[j]:
  64. if posy[i] - posy[j] < sizexy[i] and posy[i] - posy[j] > -1* sizexy[1] :
  65. speedy[i] *= -1
  66. #this does the same as the last block but checks if in the same y axis and then changes the x axis if too close
  67. for i in range(len(posx)):
  68. for j in range(len(posx)):
  69. if i != j:
  70. if posy[i] == posy[j]:
  71. if posx[i] - posx[j] < sizexy[i] and posx[i] - posx[j] > -1* sizexy[1] :
  72. speedx[i] *= -1
  73.  
  74.  
  75.  
  76. #this detects if the box is at a wall, and if it is it reverses its movement along that axis to bounce
  77. for i in range(len(posy)):
  78. i-=1
  79. if posy[i] > 500 - sizexy[i]:
  80. speedy[i] = speedy[i] * -1
  81. if posy[i] < 0:
  82. speedy[i] = speedy[i] * -1
  83.  
  84. if posx[i] > 700 - sizexy[i]:
  85. speedx[i] = speedx[i] * -1
  86. if posx[i] < 0:
  87. speedx[i] = speedx[i] * -1
  88. i+=1
  89.  
  90.  
  91. # --- Drawing code should go here
  92.  
  93. # First, clear the screen to black. Don't put other drawing commands
  94. # above this, or they will be erased with this command.
  95. screen.fill(BLACK) #background colour
  96.  
  97. pygame.draw.rect(screen, WHITE, [posx[0], posy[0], sizexy[0],sizexy[0]])
  98. pygame.draw.rect(screen, RED, [posx[1], posy[1], sizexy[1],sizexy[1]])
  99. pygame.draw.rect(screen, GREEN, [posx[2], posy[2], sizexy[2],sizexy[2]]) #this draws each rectangle a different color and uses each particles statistics on where it should be
  100. pygame.draw.rect(screen, RED, [posx[3], posy[3], sizexy[3],sizexy[3]])
  101.  
  102. #the below block is used to automatically go through all the lists and make that many squares in the correct positions, useful for masses of particles, but they are all the same color so you cant tell if collision is working
  103. """for i in range(len(posx)):
  104. i -=1
  105. pygame.draw.rect(screen, WHITE, [posx[i], posy[i], sizexy[i],sizexy[i]]) # this draws a rectangle on the screen, in white with the position of posx,posy and with sizex and sizy for its size
  106. i+=1
  107. """
  108. # --- Go ahead and update the screen with what we've drawn.
  109. pygame.display.flip() # this is nessacary for computor reasons reasons
  110.  
  111. # --- Limit to 60 frames per second
  112. clock.tick(60)
  113.  
  114. # Close the window and quit.
  115. # If you forget this line, the program will 'hang'
  116. # on exit if running from IDLE.
  117. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement