Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame,sys
- from pygame.locals import *
- from pygame import gfxdraw
- from random import randint
- import math
- pygame.init()
- options = input("Default Settings> (y/n):")
- class wld():
- def __init__(self,options):
- self.clock = pygame.time.Clock()
- self.FPS = 60
- self.bls = []
- if options == "n":
- self.x = int(input("X Resolution:"))
- self.y = int(input("Y Resolution:"))
- self.g = float(input("Acceleration due to gravity:"))
- self.r = int(input("Particle radius:"))
- self.c = input("Colour according to rms or component velocity? (rms/com):")
- self.f = abs(int(input("Drag (Higher = Less Drag):")))
- else:
- self.x,self.y,self.g,self.r,self.f,self.c = 500,500,9.81,4,500,"rms"
- self.num = int(input("How Many Balls? (0 for random):"))
- if (self.num == 0):
- self.num = randint(1,1000)
- if self.f <= 1 and self.f >=0:
- self.f = 1
- self.screen = pygame.display.set_mode((self.x,self.y))
- class Balls():
- def __init__(self):
- self.radius = world.r
- self.ydisplacement = (world.y/2)
- self.xdisplacement = (world.x/2)
- self.yvelocity = randint(int(-world.y/3),int(world.y/3))
- self.xvelocity = randint(int(-world.x/3),int(world.x/3))
- self.xlist = []
- self.ylist = []
- self.time = 0
- self.falltime = 0
- self.bouncetime = 0
- 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
- def friction(self):
- if not self.xvelocity == 0 and self.xvelocity >= 0:
- self.xvelocity = self.xvelocity - self.xvelocity/world.f
- elif not self.xvelocity == 0 and self.xvelocity <= 0:
- self.xvelocity = self.xvelocity - self.xvelocity/world.f
- if self.xvelocity >= -0.2 and self.xvelocity <= 0.2:
- self.xvelocity = 0
- def calc_ybounce(self):
- if self.ydisplacement >= world.y - self.radius:
- self.yvelocity = (-1) * self.yvelocity
- self.ydisplacement = world.y - self.radius
- self.bouncetime = 0
- if self.ydisplacement <= 0+self.radius:
- self.yvelocity = (-1) * self.yvelocity
- self.ydisplacement = 0+self.radius
- self.bouncetime =0
- 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
- def draw(self):
- self.col = int((((self.xvelocity**2)+(self.yvelocity**2))/2)**0.5)
- if world.c == "com":
- pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(self.col/2,abs(int(self.yvelocity)),abs(int(self.xvelocity))))
- pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(self.col/2,abs(int(self.yvelocity)),abs(int(self.xvelocity))))
- else:
- pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2))
- pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2))
- def tick(self):
- self.get_motion()
- self.friction()
- self.calc_ybounce()
- self.calc_xbounce()
- self.draw()
- world = wld(options)
- for i in range(0,world.num):
- world.bls.append(Balls())
- while True:
- world.screen.fill((200,200,200))
- tick = world.clock.tick(world.FPS)
- for i in range(0,world.num):
- world.bls[i].tick()
- pygame.display.update()
- 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 and len(world.bls) <= 500:
- if world.graphing:
- world.graphing = False
- else:
- world.graphing = True
Advertisement
Add Comment
Please, Sign In to add comment