def main(): win = ps.Window('Boring Objects') count = 0 data = [] h = hpy() for i in range(5): r = ps.Rect(count/10, count/10, 40, 40, (1,1,1,.5)) win.add(r) print h.heap() count += 1; win.refresh() print h.heap() #data.append(win.s_since_refresh()) return data
import pyshiva as ps from math import sin w = ps.Window(title="Follow Me!") for circle_idx in range(25): rgba = (abs(sin(circle_idx)), 0, abs(sin(circle_idx)), 0.5) w.add(ps.Circle(x=0, y=0, radius=circle_idx, color=rgba)) while w.is_open(): t = w.s_since_refresh() x, y = ps.get_mouse_pos() for i, e in enumerate(w): dx = x - e.x dy = y - e.y e.x += dx * (t * i * 0.3) e.y += dy * (t * i * 0.3) w.refresh()
# # Color Change Demo # # Import the python module import pyshiva as ps import math, random # Create a window with the title "Color Grid" w = ps.Window(title="Color Change Demo") all_rects = list() # Creates two color objects colorA = ps.Color(1, 0, 0, .5) colorB = ps.Color(0, 1, 1, .5) #creates a 10 by 10 grid of squares, choosing from the list of colors side_length = 50 for x in range(10): for y in range(10): if (y + x) % 2 == 0: r = ps.Rect(x * (side_length + 1), y * (side_length + 1), side_length, side_length, colorA) else: r = ps.Rect(x * (side_length + 1), y * (side_length + 1), side_length, side_length, colorB) w.add(r) # Add the rectangles to the window... all_rects.append(r) # and keep track of them with a list #changes which color each square is based on the time. This means swapping out colors, not creating new colors.
import random import time try: import pyshiva as ps except ImportError as e: print("""For now, this program must be run from the same directory in which pyshiva.so is located! - Julian""") raise e if __name__ == "__main__": width = 1680 direction = 1 speed = 1000000 win = ps.Window("My First pyShiva Program", width=500, height=500) last_time = time.clock() while win.is_open(): elapsed = time.clock() - last_time """ win.x += elapsed*speed*direction if win.x + win.width >= width or win.x <= 0: direction *= -1 """ win.refresh() last_time = time.clock()
''' Fractal Demo. ''' import pyshiva as ps from math import sin, cos w = ps.Window(title="Fractals!") def create_circles(iterations, x, y, radius): w.add( ps.Circle(x, y, radius, color=(abs(sin(radius)), 0, abs(cos(radius)), 0.2))) if iterations > 0: iterations -= 1 factor = 2.4 create_circles(iterations, x, y + radius / factor, radius / factor) create_circles(iterations, x, y - radius / factor, radius / factor) create_circles(iterations, x + radius / factor, y, radius / factor) create_circles(iterations, x - radius / factor, y, radius / factor) create_circles(5, w.width / 2, w.height / 2, w.height / 2) while w.is_open(): w.refresh()
# # Colorful Rose Curves Demo # # Import the python module import pyshiva as ps import math, random w = ps.Window(title = "Colorful Rose Curves") # Create 1000 circles with different colors for i in range(1000): r = random.random() a = abs(math.cos(i))*0.5 radius = abs(math.sin(i))*25 c = ps.Circle(x = 0, y = 0, radius = radius, color = (r,abs(math.sin(i)),1,0.1)) w.add(c) # Add the circles to the window. k = 0.25 # k is the type of rose curve while w.is_open(): x,y = ps.get_mouse_pos() t = w.s_since_open()*2 # Use a scaled time since program start as the parametric time value radius = abs(math.sin(w.s_since_open())) if radius < 0.01: # Every time the curve collapses... k = random.random() # Randomize the k value to change the type of the curve # Place every circle along a rose curve, offset by its index for (i,c) in enumerate(w): ran = random.random() c.x = radius*math.cos(k*(t+i))*math.sin(t+i)*w.width/2+w.width/2 c.y = radius*math.sin(k*(t+i))*math.sin(t+i)*w.height/2+w.height/2
# # Resize Rectangle Demo # # Import the python module import pyshiva as ps import math, random # Create a window with the title "Rose Curves" w = ps.Window(title="Rose Curves") all_rects = list() # Create 1000 squares with different colors for i in range(1): #print "Initialized" c = random.random() a = abs(math.cos(i)) * 0.5 side_length = 1 * 50 r = ps.Rect(0, 0, side_length, side_length, (c, abs(math.sin(i)), 1, 1)) w.add(r) # Add the rectangles to the window... all_rects.append(r) # and keep track of them with a list k = 0.25 while w.is_open(): t = w.s_since_open() for (i, r) in enumerate(all_rects): if (int(t) % 2): r.x = w.width / 2 r.y = w.height / 2 r.width = float(100.0)
x = max(0, min(x, self.window.width)) y = max(0, min(y, self.window.height)) self.x = (x - self.window.width / 2) * ( math.pow(math.sin(elapsed * 2 * math.pi / 20), 48) + 1) + self.window.width / 2 self.y = (y - self.window.height / 2) * ( math.pow(math.sin(elapsed * 2 * math.pi / 20), 48) + 1) + self.window.height / 2 #self. im = Image.open("downey.jpg") im.load() im.thumbnail((sizex, sizey)) jigglers = [] sizex, sizey = im.size win = ps.Window("THE JPEGGLER", width=sizex * 10, height=sizey * 10) for x in range(sizex): for y in range(sizey): jigglers.append(Jiggler(win, x, sizey - y - 1, im.getpixel((x, y)))) win.add(jigglers[-1]) print 'READY!' print win.width, win.height while win.is_open(): for j in jigglers: j.jiggle() win.refresh()
elif self.x + delta_x >= self.window.width - self.width: self.dir_x = -1 self.x = self.window.width - self.width else: self.x += delta_x if self.y + delta_y < 0: self.dir_y = 1 self.y = 0 if self.y + delta_y >= self.window.height - self.height: self.dir_y = -1 self.y = self.window.height - self.height else: self.y += delta_y w = ps.Window() paddle = Paddle(w.height / 2, w) enemy_paddle = Enemy(w.height / 2, w) ball = Ball(w) colliders = list() colliders.append(paddle) colliders.append(enemy_paddle) while w.is_open(): t = w.s_since_refresh() paddle.simulate(t) enemy_paddle.simulate(t, ball.y) ball.simulate(t, colliders) w.refresh()
import pyshiva as ps w = ps.Window("Path Test") p = ps.Path(100, 100, color=(1, 0, 0, 1)) p.add_line_to(0, 100) p.add_line_to(100, 100) p.close_path() w.add(p) p = ps.Path(200, 100, stroke_thickness=3) p.add_quad_to(0, 100, 100, 100) p.close_path() w.add(p) while w.is_open(): w.refresh()
# # Color Grid Demo # # Import the python module import pyshiva as ps import math, random # Create a window with the title "Color Grid" w = ps.Window(title = "Color Grid", width=500, height=500) all_rects = list() all_colors = list() # Creats a list of 10 colors, ranging from blue to red for i in range(10): testColor = ps.Color(1-.1*i,0,.1*i,1) all_colors.append(testColor) #creates a 10 by 10 grid of squares, choosing from the list of colors for x in range(10): for y in range(10): side_length = 50 r = ps.Rect(x*(side_length+1),y*(side_length+1),side_length,side_length,all_colors[abs(x-y)]) w.add(r) # Add the rectangles to the window... all_rects.append(r) # and keep track of them with a list #changes which color each square is based on the time. This means swapping out colors, not creating new colors. oldt = 0 while w.is_open(): t = w.s_since_open()*10 for x in range(10):
elif self.x + delta_x >= self.window.width - self.width: self.dir_x = -1 self.x = self.window.width - self.width else: self.x += delta_x if self.y + delta_y < 0: self.dir_y = 1 self.y = 0 if self.y + delta_y >= self.window.height - self.height: self.dir_y = -1 self.y = self.window.height - self.height else: self.y += delta_y w = ps.Window(b"Many Objects Demo!") bouncers = list() ''' for _ in range(1000): b = Bouncer(w) bouncers.append(b) w.add(b) ''' cycles = 0 while w.is_open(): cycles += 1 if cycles % 2 == 0: b = Bouncer(w) bouncers.append(b)
import pyshiva as ps w = ps.Window(b"Groups, Yo!") g = ps.Group(10, 10) g2 = ps.Group(10, 10) r1 = ps.Rect(0, 0, 100, 100, (1, 0, 0)) r2 = ps.Rect(25, 25, 50, 50, (0, 0, 1)) g.add(r1) g.add(r2) r3 = ps.Rect(30, 30, 10, 10, (0, 1, 0)) r4 = ps.Rect(50, 50, 10, 10, (0, 1, 0)) g2.add(r3) g2.add(r4) g.add(g2) w.add(g) while w.is_open(): x, y = ps.get_mouse_pos() g.x = x g.y = y w.refresh()