Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- import pygame,sys
- from pygame.locals import *
- from pygame import gfxdraw
- from random import randint as randint
- import math
- #Initialization of pygame and variables
- pygame.init()
- xres = int(input("X Resolution:"))
- yres = int(input("Y Resolution:"))
- g = float(input("Acceleration due to gravity:"))
- r = int(input("Particle Radius:"))
- #Class containing world settings and info
- class wld():
- def __init__(self,x,y,g,r):
- self.x = x
- self.y = y
- self.num = int(input("How Many Balls? (0 for random):"))
- if (self.num == 0):
- self.num = randint(1,1000)
- self.screen = pygame.display.set_mode((x,y))
- self.clock = pygame.time.Clock()
- self.FPS = 60
- self.g = g
- self.bls = []
- self.graphing = False
- #Class for particles
- class Balls():
- #Initialization of particle variables
- def __init__(self):
- self.radius = r
- self.ydisplacement = (world.y/2)
- self.xdisplacement = (world.x/2)
- self.yvelocity = randint(0,50)
- self.xvelocity = randint(-150,150)
- self.xlist = []
- self.ylist = []
- self.time = 0
- self.falltime = 0
- self.bouncetime = 0
- #Calculating Motion of particles
- def get_motion(self):
- self.time = self.time + 0.001
- self.falltime = self.falltime + 0.001
- self.bouncetime = self.bouncetime + 0.001
- self.yvelocity = self.yvelocity + 2 * world.g * self.falltime
- self.ydisplacement = self.ydisplacement + self.yvelocity * self.falltime + 0.5 * world.g * self.falltime
- self.xdisplacement = self.xdisplacement + self.xvelocity * self.bouncetime
- if self.xvelocity >= -0.2 and self.xvelocity <= 0.2:
- self.xvelocity = 0
- #Calculating bounces along Y axis
- def calc_ybounce(self):
- if self.ydisplacement >= world.y - self.radius:
- self.yvelocity = (-1) * self.yvelocity
- self.ydisplacement = world.y - 1 - self.radius
- if not self.xvelocity == 0 and self.xvelocity >= 0:
- self.xvelocity = self.xvelocity - self.xvelocity/25
- elif not self.xvelocity == 0 and self.xvelocity <= 0:
- self.xvelocity = self.xvelocity - self.xvelocity/25
- if self.ydisplacement <= 0+self.radius:
- self.yvelocity = (-1) * self.yvelocity
- self.ydisplacement = 0+self.radius
- #Calculating bounces along X axis
- def calc_xbounce(self):
- if self.xdisplacement >= world.x - self.radius:
- self.xvelocity = (-1) * self.xvelocity
- self.xdisplacement = world.x - self.radius
- if self.xdisplacement <= 0 + self.radius:
- self.xvelocity = (-1) * self.xvelocity
- self.xdisplacement = 0 + self.radius
- #Collect particle metrics for plotting
- def tracking(self):
- self.xlist.append(self.xdisplacement + self.radius*2)
- self.ylist.append(self.ydisplacement + self.radius*2)
- if (len(self.xlist)>=2 and world.graphing):
- for x in range(0,len(self.xlist)-2):
- pygame.draw.line(world.screen,(0,255,30),(self.xlist[x],self.ylist[x]),(self.xlist[x+1],self.ylist[x+1]),1)
- #Drawing Particles
- def draw(self):
- pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(30,0,0))
- pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(100,0,0))
- #Tick event to call other class functions in sequence
- def tick(self):
- self.get_motion()
- self.calc_ybounce()
- self.calc_xbounce()
- self.tracking()
- self.draw()
- #Call world and ball classes to initialize and store particles in table.
- world = wld(xres,yres,g,r)
- for i in range(0,world.num):
- world.bls.append(Balls())
- #Main Loop
- while True:
- #Clock and background
- tick = world.clock.tick(world.FPS)
- world.screen.fill((200,200,200))
- #Quit handler
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- if world.graphing:
- world.graphing = False
- else:
- world.graphing = True
- #Tick for all particles and update display
- for i in range(0,world.num):
- world.bls[i].tick()
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment