from math import factorial import sys, pygame, random pygame.init() mouse = 0 temp = pygame.display.list_modes(0) size = width, height = temp[0] cSpeed = [random.choice([-1, 1]), random.choice([-1, 1]), random.choice([-1, 1])] black = 0, 0, 0 ####red = 255, 0 , 0#### delta = 0.01 lineColor = [random.randrange(1,254), random.randrange(1,254), random.randrange(1,254)] if len(sys.argv) == 3: numCurves = int(sys.argv[1]) if numCurves < 1: numCurves = 1 numPoints = 3 * numCurves + 1 nm1 = numPoints - 2 n = 3 speed = [random.choice([-1, 1]) * int(sys.argv[2]), random.choice([-1, 1]) * int(sys.argv[2])] i = 1 while i < numPoints: speed += [random.choice([-1, 1]) * int(sys.argv[2]), random.choice([-1, 1]) * int(sys.argv[2])] i += 1 else: print("Bounces a bezier curve with a given number of points.") print("") print("Usage:") print(" bez.py numCurves speed") print("") print(" numCurves Specifies how many bezier curves to use in the loop") print(" -Value must be at least 1") print(" speed Specifies the speed at which bezier bounces around the screen") print(" -Value must be an integer greater than 0") sys.exit() p = [0, 0] * numPoints def midPoints(): if numCurves > 1: p[0] = (p[2] + p[nm1 * 2]) / 2 p[1] = (p[3] + p[nm1 * 2 + 1]) / 2 i = 3 while i < numPoints - 1: p[2 * i] = (p[2 * (i - 1)] + p[2 * (i + 1)]) / 2 p[2 * i + 1] = (p[2 * (i - 1) + 1] + p[2 * (i + 1) + 1]) / 2 i += 3 p[2 * i] = p[0] p[2 * i + 1] = p[1] def Bezier(): c = 0 while c < numCurves: """#### i = 0 while i < numPoints - 1: pygame.draw.line(screen, red, (p[2*i],p[2*i+1]), (p[2*i+2],p[2*i+3]), i+1) i+=1 i = 0 ####""" mu = delta pA = [p[6 * c], p[6 * c + 1]] while mu <= 1.001: i = 0 node = 3 * c pB = [0, 0] while node <= ((3 * c) + 3): pB[0] += p[2 * node] * pow(mu, i) * pow(1 - mu, n - i) * (factorial(n) / (factorial(i) * factorial(n - i))) pB[1] += p[2 * node + 1] * pow(mu, i) * pow(1 - mu, n - i) * (factorial(n) / (factorial(i) * factorial(n - i))) i += 1 node += 1 pygame.draw.line(screen, lineColor, pA, pB, 1) pA = pB mu += delta c += 1 i = 0 while i < numPoints: p[2 * i] = random.randrange(1, width - 1) p[2 * i + 1] = random.randrange(1, height - 1) i += 1 midPoints() screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN) pygame.mouse.set_visible(0) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mouse.set_visible(1) sys.exit() elif event.type == pygame.MOUSEBUTTONUP: pygame.mouse.set_visible(1) sys.exit() elif event.type == pygame.MOUSEMOTION: mouse += 1 if mouse == 5: pygame.mouse.set_visible(1) sys.exit() cColor = random.randrange(0,4) if cColor == 1: lineColor[0] += cSpeed[0] if lineColor[0] < 1 or lineColor[0] > 254: cSpeed[0] = -cSpeed[0] if cColor == 2: lineColor[1] += cSpeed[1] if lineColor[1] < 1 or lineColor[1] > 254: cSpeed[1] = -cSpeed[1] if cColor == 3: lineColor[2] += cSpeed[2] if lineColor[2] < 1 or lineColor[2] > 254: cSpeed[2] = -cSpeed[2] i = 0 while i < numPoints: p[2 * i] += speed[2 * i] if p[2 * i] < 1 or p[2 * i] > width - 1: speed[2 * i] = -speed[2 * i] p[2 * i + 1] += speed[2 * i + 1] if p[2 * i + 1] < 1 or p[2 * i + 1] > height - 1: speed[2 * i + 1] = -speed[2 * i + 1] i += 1 midPoints() screen.fill(black) Bezier() pygame.display.flip()