Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- # Define some colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- # Defines variables for all particles
- posx = [10,10,25,500] # origonal X axis
- posy = [10,460,20,20] # origonal Y axis
- speedx = [0,0,1,1] # speed for the X axis
- speedy = [1,1,0,0] # speed for the Y axis
- sizexy = [20,20,20,20] # size of each particle
- # the block of code before is used to randomly generate information for starting positions (this would be used for the final sim)
- """
- for i in range(2):
- posx.append(random.randint(10,690))
- posy.append(random.randint(10,490))
- speedx.append(1)
- speedy.append(1)
- sizexy.append(20)
- """
- pygame.init()
- # Set the width and height of the screen [width, height]
- size = (700, 500)
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Brownian Motion") #sets the name of the window
- # This makes the program shutdown when the close button is clicked
- done = False
- # Used to manage how fast the screen updates
- clock = pygame.time.Clock()
- # -------- Main Program Loop -----------
- while not done:
- # --- Main event loop
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- # --- Game logic should go here
- #this changes the position of the box every tick by its speed
- for i in range(len(posx)):
- i -= 1
- posx[i] = posx[i] + speedx[i]
- posy[i] = posy[i] + speedy[i]
- i += 1
- #this is collision detection
- #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
- for i in range(len(posx)):
- for j in range(len(posx)):
- if i != j:
- if posx[i] == posx[j]:
- if posy[i] - posy[j] < sizexy[i] and posy[i] - posy[j] > -1* sizexy[1] :
- speedy[i] *= -1
- #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
- for i in range(len(posx)):
- for j in range(len(posx)):
- if i != j:
- if posy[i] == posy[j]:
- if posx[i] - posx[j] < sizexy[i] and posx[i] - posx[j] > -1* sizexy[1] :
- speedx[i] *= -1
- #this detects if the box is at a wall, and if it is it reverses its movement along that axis to bounce
- for i in range(len(posy)):
- i-=1
- if posy[i] > 500 - sizexy[i]:
- speedy[i] = speedy[i] * -1
- if posy[i] < 0:
- speedy[i] = speedy[i] * -1
- if posx[i] > 700 - sizexy[i]:
- speedx[i] = speedx[i] * -1
- if posx[i] < 0:
- speedx[i] = speedx[i] * -1
- i+=1
- # --- Drawing code should go here
- # First, clear the screen to black. Don't put other drawing commands
- # above this, or they will be erased with this command.
- screen.fill(BLACK) #background colour
- pygame.draw.rect(screen, WHITE, [posx[0], posy[0], sizexy[0],sizexy[0]])
- pygame.draw.rect(screen, RED, [posx[1], posy[1], sizexy[1],sizexy[1]])
- 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
- pygame.draw.rect(screen, RED, [posx[3], posy[3], sizexy[3],sizexy[3]])
- #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
- """for i in range(len(posx)):
- i -=1
- 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
- i+=1
- """
- # --- Go ahead and update the screen with what we've drawn.
- pygame.display.flip() # this is nessacary for computor reasons reasons
- # --- Limit to 60 frames per second
- clock.tick(60)
- # Close the window and quit.
- # If you forget this line, the program will 'hang'
- # on exit if running from IDLE.
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement