SHOW:
|
|
- or go back to the newest paste.
| 1 | - | #Libraries |
| 1 | + | |
| 2 | from pygame.locals import * | |
| 3 | from pygame import gfxdraw | |
| 4 | from random import randint | |
| 5 | - | from random import randint as randint |
| 5 | + | import math |
| 6 | - | import math |
| 6 | + | |
| 7 | - | |
| 7 | + | |
| 8 | - | #Initialization of pygame and variables |
| 8 | + | options = input("Default Settings> (y/n):")
|
| 9 | ||
| 10 | - | xres = int(input("X Resolution:"))
|
| 10 | + | |
| 11 | - | yres = int(input("Y Resolution:"))
|
| 11 | + | def __init__(self,options): |
| 12 | - | g = float(input("Acceleration due to gravity:"))
|
| 12 | + | |
| 13 | - | r = int(input("Particle Radius:"))
|
| 13 | + | |
| 14 | self.bls = [] | |
| 15 | - | #Class containing world settings and info |
| 15 | + | if options == "n": |
| 16 | self.x = int(input("X Resolution:"))
| |
| 17 | - | def __init__(self,x,y,g,r): |
| 17 | + | self.y = int(input("Y Resolution:"))
|
| 18 | - | self.x = x |
| 18 | + | self.g = float(input("Acceleration due to gravity:"))
|
| 19 | - | self.y = y |
| 19 | + | self.r = int(input("Particle radius:"))
|
| 20 | - | self.num = int(input("How Many Balls? (0 for random):"))
|
| 20 | + | self.c = input("Colour according to rms or component velocity? (rms/com):")
|
| 21 | self.f = abs(int(input("Drag (Higher = Less Drag):")))
| |
| 22 | - | self.num = randint(1,1000) |
| 22 | + | else: |
| 23 | - | self.screen = pygame.display.set_mode((x,y)) |
| 23 | + | self.x,self.y,self.g,self.r,self.f,self.c = 500,500,9.81,4,500,"rms" |
| 24 | self.num = int(input("How Many Balls? (0 for random):"))
| |
| 25 | if (self.num == 0): | |
| 26 | - | self.g = g |
| 26 | + | self.num = randint(1,1000) |
| 27 | - | self.bls = [] |
| 27 | + | if self.f <= 1 and self.f >=0: |
| 28 | - | self.graphing = False |
| 28 | + | self.f = 1 |
| 29 | self.screen = pygame.display.set_mode((self.x,self.y)) | |
| 30 | - | #Class for particles |
| 30 | + | |
| 31 | ||
| 32 | - | #Initialization of particle variables |
| 32 | + | |
| 33 | def __init__(self): | |
| 34 | - | self.radius = r |
| 34 | + | self.radius = world.r |
| 35 | self.ydisplacement = (world.y/2) | |
| 36 | self.xdisplacement = (world.x/2) | |
| 37 | - | self.yvelocity = randint(0,50) |
| 37 | + | self.yvelocity = randint(int(-world.y/3),int(world.y/3)) |
| 38 | - | self.xvelocity = randint(-150,150) |
| 38 | + | self.xvelocity = randint(int(-world.x/3),int(world.x/3)) |
| 39 | self.xlist = [] | |
| 40 | self.ylist = [] | |
| 41 | self.time = 0 | |
| 42 | self.falltime = 0 | |
| 43 | self.bouncetime = 0 | |
| 44 | ||
| 45 | - | #Calculating Motion of particles |
| 45 | + | |
| 46 | self.time = self.time + 0.001 | |
| 47 | self.falltime = self.falltime + 0.001 | |
| 48 | self.bouncetime = self.bouncetime + 0.001 | |
| 49 | self.yvelocity = self.yvelocity + 2 * world.g * self.falltime | |
| 50 | self.ydisplacement = self.ydisplacement + self.yvelocity * self.falltime + 0.5 * world.g * self.falltime | |
| 51 | self.xdisplacement = self.xdisplacement + self.xvelocity * self.bouncetime | |
| 52 | ||
| 53 | def friction(self): | |
| 54 | - | self.xvelocity = 0 |
| 54 | + | if not self.xvelocity == 0 and self.xvelocity >= 0: |
| 55 | self.xvelocity = self.xvelocity - self.xvelocity/world.f | |
| 56 | - | #Calculating bounces along Y axis |
| 56 | + | elif not self.xvelocity == 0 and self.xvelocity <= 0: |
| 57 | self.xvelocity = self.xvelocity - self.xvelocity/world.f | |
| 58 | if self.xvelocity >= -0.2 and self.xvelocity <= 0.2: | |
| 59 | self.xvelocity = 0 | |
| 60 | - | self.ydisplacement = world.y - 1 - self.radius |
| 60 | + | |
| 61 | - | if not self.xvelocity == 0 and self.xvelocity >= 0: |
| 61 | + | |
| 62 | - | self.xvelocity = self.xvelocity - self.xvelocity/25 |
| 62 | + | |
| 63 | - | elif not self.xvelocity == 0 and self.xvelocity <= 0: |
| 63 | + | |
| 64 | - | self.xvelocity = self.xvelocity - self.xvelocity/25 |
| 64 | + | self.ydisplacement = world.y - self.radius |
| 65 | self.bouncetime = 0 | |
| 66 | ||
| 67 | if self.ydisplacement <= 0+self.radius: | |
| 68 | self.yvelocity = (-1) * self.yvelocity | |
| 69 | - | #Calculating bounces along X axis |
| 69 | + | |
| 70 | self.bouncetime =0 | |
| 71 | ||
| 72 | def calc_xbounce(self): | |
| 73 | - | self.xdisplacement = world.x - self.radius |
| 73 | + | |
| 74 | self.xvelocity = (-1) * self.xvelocity | |
| 75 | self.xdisplacement = world.x - self.radius | |
| 76 | ||
| 77 | if self.xdisplacement <= 0 + self.radius: | |
| 78 | - | #Collect particle metrics for plotting |
| 78 | + | |
| 79 | - | def tracking(self): |
| 79 | + | |
| 80 | - | self.xlist.append(self.xdisplacement + self.radius*2) |
| 80 | + | |
| 81 | - | self.ylist.append(self.ydisplacement + self.radius*2) |
| 81 | + | |
| 82 | - | if (len(self.xlist)>=2 and world.graphing): |
| 82 | + | self.col = int((((self.xvelocity**2)+(self.yvelocity**2))/2)**0.5) |
| 83 | - | for x in range(0,len(self.xlist)-2): |
| 83 | + | if world.c == "com": |
| 84 | - | pygame.draw.line(world.screen,(0,255,30),(self.xlist[x],self.ylist[x]),(self.xlist[x+1],self.ylist[x+1]),1) |
| 84 | + | 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)))) |
| 85 | pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(self.col/2,abs(int(self.yvelocity)),abs(int(self.xvelocity)))) | |
| 86 | - | #Drawing Particles |
| 86 | + | else: |
| 87 | pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2)) | |
| 88 | - | pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(30,0,0)) |
| 88 | + | pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2)) |
| 89 | - | pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(100,0,0)) |
| 89 | + | |
| 90 | - | |
| 90 | + | |
| 91 | - | #Tick event to call other class functions in sequence |
| 91 | + | |
| 92 | self.friction() | |
| 93 | self.calc_ybounce() | |
| 94 | self.calc_xbounce() | |
| 95 | self.draw() | |
| 96 | - | self.tracking() |
| 96 | + | |
| 97 | world = wld(options) | |
| 98 | for i in range(0,world.num): | |
| 99 | - | #Call world and ball classes to initialize and store particles in table. |
| 99 | + | |
| 100 | - | world = wld(xres,yres,g,r) |
| 100 | + | |
| 101 | while True: | |
| 102 | world.screen.fill((200,200,200)) | |
| 103 | tick = world.clock.tick(world.FPS) | |
| 104 | - | #Main Loop |
| 104 | + | |
| 105 | world.bls[i].tick() | |
| 106 | - | #Clock and background |
| 106 | + | pygame.display.update() |
| 107 | ||
| 108 | for event in pygame.event.get(): | |
| 109 | - | #Quit handler |
| 109 | + | |
| 110 | pygame.quit() | |
| 111 | sys.exit() | |
| 112 | if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and len(world.bls) <= 500: | |
| 113 | if world.graphing: | |
| 114 | - | if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: |
| 114 | + | |
| 115 | else: | |
| 116 | world.graphing = True |