From ef3edc36c1a1c06f19823bc7b99a880c48675dfa Mon Sep 17 00:00:00 2001 From: Anish Mangal Date: Tue, 22 Feb 2011 22:56:40 +0000 Subject: pep8 and pylint fixes for examples --- (limited to 'data') diff --git a/data/graphics/bounce b/data/graphics/bounce index 6667b14..375d530 100644 --- a/data/graphics/bounce +++ b/data/graphics/bounce @@ -1,10 +1,12 @@ # bounce: move some text around the screen -import pippy, pygame, sys +import pippy +import pygame +import sys from pygame.locals import * # the text to bounce around the screen -msg = "Hello!" +msg = 'Hello!' # the size of the text, in pixels fsize = 36 @@ -41,46 +43,46 @@ text = font.render(msg, True, (10, 10, 10)) textRect = text.get_rect() # start at the top left -textRect.left = 0; -textRect.top = 0; +textRect.left = 0 +textRect.top = 0 while pippy.pygame.next_frame(): - # every time we move the text, check for quit or keydown events and exit - for event in pygame.event.get(): - if event.type == QUIT: - sys.exit() - - elif event.type == KEYDOWN: - sys.exit() - - # fill the screen with almost white - screen.fill((250, 250, 250)) - - # draw the text - screen.blit(text, textRect) - - # update the display - pygame.display.flip() - - # move the text - # - # Rect.move returns a new Rect while - # Rect.move_ip moves in place, so we'll use - # the latter - textRect.move_ip(mvect) - - # bounce off edges - if textRect.left < 0 : - textRect.left = 0 - mvect[0] = -1 * mvect[0] - elif textRect.right >= size[0] : - textRect.right = size[0] - 1 - mvect[0] = -1 * mvect[0] - - if textRect.top < 0 : - textRect.top = 0 - mvect[1] = -1 * mvect[1] - elif textRect.bottom >= size[1] : - textRect.bottom = size[1] - 1 - mvect[1] = -1 * mvect[1] + # every time we move the text, check for quit or keydown events and exit + for event in pygame.event.get(): + if event.type == QUIT: + sys.exit() + + elif event.type == KEYDOWN: + sys.exit() + + # fill the screen with almost white + screen.fill((250, 250, 250)) + + # draw the text + screen.blit(text, textRect) + + # update the display + pygame.display.flip() + + # move the text + # + # Rect.move returns a new Rect while + # Rect.move_ip moves in place, so we'll use + # the latter + textRect.move_ip(mvect) + + # bounce off edges + if textRect.left < 0: + textRect.left = 0 + mvect[0] = -1 * mvect[0] + elif textRect.right >= size[0]: + textRect.right = size[0] - 1 + mvect[0] = -1 * mvect[0] + + if textRect.top < 0: + textRect.top = 0 + mvect[1] = -1 * mvect[1] + elif textRect.bottom >= size[1]: + textRect.bottom = size[1] - 1 + mvect[1] = -1 * mvect[1] diff --git a/data/graphics/camera b/data/graphics/camera index 85dc606..f0eb35f 100644 --- a/data/graphics/camera +++ b/data/graphics/camera @@ -1,6 +1,10 @@ # camera: take a picture, animate it on screen -import gst, pippy, pygame, sys, time +import gst +import pippy +import pygame +import sys +import time # grey background bgcolor = (128, 128, 128) @@ -26,32 +30,34 @@ time.sleep(1) pipeline.set_state(gst.STATE_NULL) # load in the grabbed camera frame -image = pygame.image.load("/tmp/pippypic.jpg") +image = pygame.image.load('/tmp/pippypic.jpg') angle = 0.0 scale = 2.0 while pippy.pygame.next_frame(): - # every time we animate, check for quit or keydown events and exit - for event in pygame.event.get(): - if event.type == pygame.QUIT: sys.exit() - elif event.type == pygame.KEYDOWN: sys.exit() - - # rotate and scale the image - newImage = pygame.transform.rotozoom(image, angle, scale) - newImageRect = newImage.get_rect() - newImageRect.centerx = screen.get_rect().centerx - newImageRect.centery = screen.get_rect().centery - - # display the rotated and scaled image - screen.fill(bgcolor) - screen.blit(newImage, newImageRect) - pygame.display.flip() - - # choose a new rotation angle and scale - angle = angle + 5.0 - scale = scale * 0.95 - - # finish once the scale becomes very very small - if scale < 0.001: - break + # every time we animate, check for quit or keydown events and exit + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + elif event.type == pygame.KEYDOWN: + sys.exit() + + # rotate and scale the image + newImage = pygame.transform.rotozoom(image, angle, scale) + newImageRect = newImage.get_rect() + newImageRect.centerx = screen.get_rect().centerx + newImageRect.centery = screen.get_rect().centery + + # display the rotated and scaled image + screen.fill(bgcolor) + screen.blit(newImage, newImageRect) + pygame.display.flip() + + # choose a new rotation angle and scale + angle = angle + 5.0 + scale = scale * 0.95 + + # finish once the scale becomes very very small + if scale < 0.001: + break diff --git a/data/graphics/jump b/data/graphics/jump index 7786187..7e4d433 100644 --- a/data/graphics/jump +++ b/data/graphics/jump @@ -1,32 +1,32 @@ import pippy -for i in xrange(0,50): +for i in xrange(0, 50): pippy.console.clear() if i < 25: - pippy.console.red() + pippy.console.red() else: - pippy.console.blue() + pippy.console.blue() # Note that we have to escape backslashes - print "\\o/" - print "_|_" - print " " + print '\\o/' + print '_|_' + print ' ' pippy.wait() pippy.console.clear() - print "_o_" - print " | " - print "/ \\" + print '_o_' + print ' | ' + print '/ \\' pippy.wait() pippy.console.clear() - print " o " - print "/|\\" - print "| |" + print ' o ' + print '/|\\' + print '| |' pippy.wait() pippy.console.clear() - print "_o_" - print " | " - print "/ \\" + print '_o_' + print ' | ' + print '/ \\' pippy.wait() diff --git a/data/graphics/life b/data/graphics/life index 7ddb4c2..902f345 100644 --- a/data/graphics/life +++ b/data/graphics/life @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- # This is the game life http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life -import os, time, random - -# -# we need a function to load cells in the neighborhood -# +import os +import time +import random def LoadCells(rows, cols): + """ We need a function to load cells in the neighborhood """ grid = [] col = [0] * cols # first we load an empty grid @@ -21,54 +20,49 @@ def LoadCells(rows, cols): grid[x][y] = cell return grid -# -# here we draw the grid -# - def DrawGrid(grid): + """ Here we draw the grid """ rows = len(grid) cols = len(grid[1]) for x in range(rows): for y in range(cols): if grid[x][y] != 1: - print ".", + print '.', else: - print "o", - print "\n", - -# -# count neighbors arround a single cell -# + print 'o', + print '\n', def CountNeighbors(grid, x, y): + """ Count neighbors arround a single cell""" + neighbors = 0 rows = len(grid) cols = len(grid[1]) - if x < rows-1 and grid[x+1][y] == 1: + if x < (rows - 1) and grid[x + 1][y] == 1: neighbors += 1 - if x > 0 and grid[x-1][y] == 1: + if x > 0 and grid[x - 1][y] == 1: neighbors += 1 - if y < cols-1 and grid[x][y+1] == 1: + if y < (cols - 1) and grid[x][y + 1] == 1: neighbors += 1 - if y > 0 and grid[x][y-1] == 1: + if y > 0 and grid[x][y - 1] == 1: neighbors += 1 - if x < rows-1 and y < cols-1 and grid[x+1][y+1] == 1: + if x < (rows - 1) and y < (cols - 1) and grid[x + 1][y + 1] == 1: neighbors += 1 - if x > 0 and y > 0 and grid[x-1][y-1] == 1: + if x > 0 and y > 0 and grid[x - 1][y - 1] == 1: neighbors += 1 - if x > 0 and y < cols-1 and grid[x-1][y+1] == 1: + if x > 0 and y < (cols - 1) and grid[x - 1][y + 1] == 1: neighbors += 1 - if x < rows-1 and y > 0 and grid[x+1][y-1] == 1: + if x < (rows - 1) and y > 0 and grid[x + 1][y - 1] == 1: neighbors += 1 return neighbors -# here we define a single iteration -# if we have between 3 and 6 neighbors the single cell lives -# in other case the cell dies - def Iteration(grid): + """ here we define a single iteration + if we have between 3 and 6 neighbors the single cell lives + in other case the cell dies + """ rows = len(grid) cols = len(grid[1]) neighbors = 0 @@ -82,21 +76,18 @@ def Iteration(grid): if neighbors == 3: grid[x][y] = 1 -# -# iterate n pulses and draws the result of each one -# - def Iterator(rows, cols, pulses): + """ Iterate n pulses and draws the result of each one """ pulse = 1 grid = LoadCells(rows, cols) while pulse <= pulses: os.system('clear') - print "Pulse: ", pulse + print 'Pulse: ', pulse Iteration(grid) DrawGrid(grid) pulse += 1 time.sleep(0.2) -number = input("Please input the number of rows and cols (unique number):") -pulses = input("Please input the number of pulses:") +number = input('Please input the number of rows and cols (unique number):') +pulses = input('Please input the number of pulses:') Iterator(number, number, pulses) diff --git a/data/graphics/lines b/data/graphics/lines index bb67a34..5699719 100644 --- a/data/graphics/lines +++ b/data/graphics/lines @@ -1,6 +1,8 @@ # lines: make lots of lines on the screen -import pippy, pygame, sys +import pippy +import pygame +import sys from pygame.locals import * from random import * @@ -18,84 +20,84 @@ screen = pygame.display.set_mode() size = width, height = screen.get_size() # start the screen all black -screen.fill((0,0,0)) +screen.fill((0, 0, 0)) # we need starting endpoints for the line and seed motion vectors -start=[randint(0,size[0]), randint(0,size[1])] -end =[randint(0,size[0]), randint(0,size[1])] +start = [randint(0, size[0]), randint(0, size[1])] +end = [randint(0, size[0]), randint(0, size[1])] # randomize the motion, 1..3 in each direction, positive or negative, but # never 0 -mvect_start = [choice((-1,1)) * randint(1,3), choice((-1,1)) * randint(1,3)] -mvect_end = [choice((-1,1)) * randint(1,3), choice((-1,1)) * randint(1,3)] +mvect_start = [choice((-1, 1)) * randint(1, 3), choice((-1, 1)) * randint(1, 3)] +mvect_end = [choice((-1, 1)) * randint(1, 3), choice((-1, 1)) * randint(1, 3)] # start with a random color and color direction -color = [randint(0,255), randint(0,255), randint(0,255)] -direction = [choice((-1,1)), choice((-1,1)), choice((-1,1))] +color = [randint(0, 255), randint(0, 255), randint(0, 255)] +direction = [choice((-1, 1)), choice((-1, 1)), choice((-1, 1))] while pippy.pygame.next_frame(): - for event in pygame.event.get(): - if event.type == QUIT: - sys.exit() - - elif event.type == KEYDOWN: - sys.exit() - - # draw the line using the current values and width=3 - pygame.draw.line(screen, color, start, end, 3) - - # update the display - pygame.display.flip() - - # update the end points and the color - for i in range(2): - start[i] = start[i] + mvect_start[i] - end[i] = end[i] + mvect_end[i] - - for i in range(3): - color[i] = color[i] + direction[i] - - # check if anything has gone out of range and - # if so, bring back to edge and reverse the - # corresponding motion vector - if start[0] < 0 : - start[0] = 0 - mvect_start[0] = -1 * mvect_start[0] - elif start[0] >= size[0] : - start[0] = size[0] - 1 - mvect_start[0] = -1 * mvect_start[0] - - if start[1] < 0 : - start[1] = 0 - mvect_start[1] = -1 * mvect_start[1] - elif start[1] >= size[1] : - start[1] = size[1] - 1 - mvect_start[1] = -1 * mvect_start[1] - - if end[0] < 0 : - end[0] = 0 - mvect_end[0] = -1 * mvect_end[0] - elif end[0] >= size[0] : - end[0] = size[0] - 1 - mvect_end[0] = -1 * mvect_end[0] - - if end[1] < 0 : - end[1] = 0 - mvect_end[1] = -1 * mvect_end[1] - elif end[1] >= size[1] : - end[1] = size[1] - 1 - mvect_end[1] = -1 * mvect_end[1] - - for i in range(3) : - if color[i] < 0: - color[i] = 0 - direction[i] = direction[i] * -1 - elif color[i] >= 255 : - color[i] = 255 - direction[i] = direction[i] * -1 - - # randomly change the color directon on occasion - if randint(0,511) == 128: - for i in range(3): - direction[i] = choice((-1,1)) + for event in pygame.event.get(): + if event.type == QUIT: + sys.exit() + + elif event.type == KEYDOWN: + sys.exit() + + # draw the line using the current values and width=3 + pygame.draw.line(screen, color, start, end, 3) + + # update the display + pygame.display.flip() + + # update the end points and the color + for i in range(2): + start[i] = start[i] + mvect_start[i] + end[i] = end[i] + mvect_end[i] + + for i in range(3): + color[i] = color[i] + direction[i] + + # check if anything has gone out of range and + # if so, bring back to edge and reverse the + # corresponding motion vector + if start[0] < 0: + start[0] = 0 + mvect_start[0] = (-1) * mvect_start[0] + elif start[0] >= size[0]: + start[0] = size[0] - 1 + mvect_start[0] = (-1) * mvect_start[0] + + if start[1] < 0: + start[1] = 0 + mvect_start[1] = (-1) * mvect_start[1] + elif start[1] >= size[1]: + start[1] = size[1] - 1 + mvect_start[1] = (-1) * mvect_start[1] + + if end[0] < 0: + end[0] = 0 + mvect_end[0] = (-1) * mvect_end[0] + elif end[0] >= size[0]: + end[0] = size[0] - 1 + mvect_end[0] = (-1) * mvect_end[0] + + if end[1] < 0: + end[1] = 0 + mvect_end[1] = (-1) * mvect_end[1] + elif end[1] >= size[1]: + end[1] = size[1] - 1 + mvect_end[1] = (-1) * mvect_end[1] + + for i in range(3): + if color[i] < 0: + color[i] = 0 + direction[i] = direction[i] * (-1) + elif color[i] >= 255: + color[i] = 255 + direction[i] = direction[i] * (-1) + + # randomly change the color directon on occasion + if randint(0, 511) == 128: + for i in range(3): + direction[i] = choice((-1, 1)) diff --git a/data/graphics/physics b/data/graphics/physics index fbc336c..3d57a06 100644 --- a/data/graphics/physics +++ b/data/graphics/physics @@ -1,6 +1,9 @@ # physics -import pippy, pygame, sys, math +import pippy +import pygame +import sys +import math from pygame.locals import * from pippy import physics @@ -14,13 +17,13 @@ world.renderer.set_surface(screen) # set up initial physics objects world.add.ground() -world.add.ball((600,0), 50) -world.add.rect((500,0), 25, 300, dynamic=True, density=1.0, restitution=0.16, friction=0.5) +world.add.ball((600, 0), 50) +world.add.rect((500, 0), 25, 300, dynamic=True, density=1.0, restitution=0.16, friction=0.5) # add 20 more balls balls = 0 while(balls < 20): - world.add.ball((balls*5+200, balls*5), 50) + world.add.ball(((balls * 5) + 200, balls * 5), 50) balls += 1 # begin physics simulation diff --git a/data/graphics/pong b/data/graphics/pong index b94aeb9..8182861 100644 --- a/data/graphics/pong +++ b/data/graphics/pong @@ -5,7 +5,9 @@ # on the XO, the escape key is the top lefthand key, # circle with an x in it. -import pippy, pygame, sys +import pippy +import pygame +import sys from pygame.locals import * from random import * @@ -42,7 +44,7 @@ ball_radius = 25 # game constants fsize = 48 -msg = "Press 'g' to start game" +msg = 'Press \'g\' to start game' font = pygame.font.Font(None, fsize) text = font.render(msg, True, (250, 250, 250)) @@ -52,101 +54,101 @@ textRect.centery = screen.get_rect().centery while pippy.pygame.next_frame(): - # display msg - screen.fill(bgcolor) - screen.blit(text, textRect) - pygame.display.flip() + # display msg + screen.fill(bgcolor) + screen.blit(text, textRect) + pygame.display.flip() - # chill until a key is pressed - for idle_event in pygame.event.get(): - if idle_event.type == QUIT: - sys.exit() + # chill until a key is pressed + for idle_event in pygame.event.get(): + if idle_event.type == QUIT: + sys.exit() - if idle_event.type == KEYDOWN: - if idle_event.key == K_ESCAPE: - sys.exit() - - if idle_event.key == 103: # g key - - # play a game! - - # start the paddle in the center - paddle_location = height / 2 - - # number of balls to a game - balls = 4 - - while balls > 0: - - ball_position = [ball_radius, ball_radius] - ball_mvect = [randint(3,5), randint(3,5)] - ball_limit = size - balls = balls - 1 - - while ball_position[0] + ball_radius < ball_limit[0]: # in play - - for event in pygame.event.get(): - if event.type == QUIT: + if idle_event.type == KEYDOWN: + if idle_event.key == K_ESCAPE: sys.exit() - elif event.type == KEYDOWN: - if event.key == K_ESCAPE: - sys.exit() - elif event.key == 273 \ - or event.key == 265 \ - or event.key == 264: # up - paddle_location = paddle_location - step - elif event.key == 274 \ - or event.key == 259 \ - or event.key == 258: # down - paddle_location = paddle_location + step - - # make sure the paddle is in-bounds - if paddle_location - paddle_radius < 0: - paddle_location = paddle_radius - elif paddle_location + paddle_radius >= height: - paddle_location = height - 1 - paddle_radius - - # clear the screen - screen.fill(bgcolor) - - # draw the paddle on the right side of the screen - pygame.draw.line(screen, - paddle_color, - (width - paddle_width, paddle_location - - paddle_radius), - (width - paddle_width, - paddle_location+paddle_radius), - paddle_width) - - # draw the ball - pygame.draw.circle(screen, ball_color, ball_position, ball_radius) - - # draw the unused balls - for i in range(balls): - pygame.draw.circle(screen, ball_color, - (int(round(30+i*ball_radius*2.4)), 30), + if idle_event.key == 103: # g key + + # play a game! + + # start the paddle in the center + paddle_location = height / 2 + + # number of balls to a game + balls = 4 + + while balls > 0: + + ball_position = [ball_radius, ball_radius] + ball_mvect = [randint(3, 5), randint(3, 5)] + ball_limit = size + balls = balls - 1 + + while ball_position[0] + ball_radius < ball_limit[0]: # in play + + for event in pygame.event.get(): + if event.type == QUIT: + sys.exit() + + elif event.type == KEYDOWN: + if event.key == K_ESCAPE: + sys.exit() + elif event.key == 273 \ + or event.key == 265 \ + or event.key == 264: # up + paddle_location = paddle_location - step + elif event.key == 274 \ + or event.key == 259 \ + or event.key == 258: # down + paddle_location = paddle_location + step + + # make sure the paddle is in-bounds + if paddle_location - paddle_radius < 0: + paddle_location = paddle_radius + elif paddle_location + paddle_radius >= height: + paddle_location = height - 1 - paddle_radius + + # clear the screen + screen.fill(bgcolor) + + # draw the paddle on the right side of the screen + pygame.draw.line(screen, + paddle_color, + (width - paddle_width, paddle_location - + paddle_radius), + (width - paddle_width, + paddle_location + paddle_radius), + paddle_width) + + # draw the ball + pygame.draw.circle(screen, ball_color, ball_position, ball_radius) + + # draw the unused balls + for i in range(balls): + pygame.draw.circle(screen, ball_color, + (int(round(30 + i * ball_radius * 2.4)), 30), ball_radius) - # update the display - pygame.display.flip() - - # update the ball - for i in range(2): - ball_position[i] = ball_position[i] + ball_mvect[i] - - # bounce on top and left - if ball_position[i] < ball_radius: - ball_position[i] = ball_radius - ball_mvect[i] = -1 * ball_mvect[i] - # bounce on bottom - elif i == 1 \ - and ball_position[i] >= ball_limit[i] - ball_radius: - ball_position[i] = ball_limit[i] - ball_radius - 1 - ball_mvect[i] = -1 * ball_mvect[i] - elif i == 0 \ - and ball_position[i] >= ball_limit[i] - ball_radius - paddle_width \ - and ball_position[1] > paddle_location - paddle_radius \ - and ball_position[1] < paddle_location + paddle_radius : - ball_position[i] = ball_limit[i] - ball_radius - paddle_width - 1 - ball_mvect[i] = -1 * ball_mvect[i] + # update the display + pygame.display.flip() + + # update the ball + for i in range(2): + ball_position[i] = ball_position[i] + ball_mvect[i] + + # bounce on top and left + if ball_position[i] < ball_radius: + ball_position[i] = ball_radius + ball_mvect[i] = -1 * ball_mvect[i] + # bounce on bottom + elif i == 1 \ + and ball_position[i] >= ball_limit[i] - ball_radius: + ball_position[i] = ball_limit[i] - ball_radius - 1 + ball_mvect[i] = -1 * ball_mvect[i] + elif i == 0 \ + and ball_position[i] >= ball_limit[i] - ball_radius - paddle_width \ + and ball_position[1] > paddle_location - paddle_radius \ + and ball_position[1] < paddle_location + paddle_radius: + ball_position[i] = ball_limit[i] - ball_radius - paddle_width - 1 + ball_mvect[i] = (-1) * ball_mvect[i] diff --git a/data/graphics/snow b/data/graphics/snow index efcb28f..af8abf9 100644 --- a/data/graphics/snow +++ b/data/graphics/snow @@ -1,6 +1,8 @@ # snow -import pippy, pygame, sys +import pippy +import pygame +import sys from pygame.locals import * from random import * @@ -27,48 +29,48 @@ sizes = [] nflakes = 1000 while pippy.pygame.next_frame(): - # if we don't have enough flakes, add one - if len(xs) < nflakes: - xs.append(randint(0, width)) - ys.append(0) - dxs.append(randint(-2, 2)) - size = expovariate(1) * 5 - sizes.append(int(size)) - dys.append(size * 2) + # if we don't have enough flakes, add one + if len(xs) < nflakes: + xs.append(randint(0, width)) + ys.append(0) + dxs.append(randint(-2, 2)) + size = expovariate(1) * 5 + sizes.append(int(size)) + dys.append(size * 2) - # clear the screen - screen.fill(bg_color) + # clear the screen + screen.fill(bg_color) - for event in pygame.event.get(): - if event.type == QUIT: - sys.exit() - elif event.type == KEYDOWN: - if event.key == K_ESCAPE: - sys.exit() + for event in pygame.event.get(): + if event.type == QUIT: + sys.exit() + elif event.type == KEYDOWN: + if event.key == K_ESCAPE: + sys.exit() - for x, y, size in zip(xs, ys, sizes): - c = 40 + int(float(y) / height * 215) - pygame.draw.circle( - screen, (c, c, c), (x, y), size) + for x, y, size in zip(xs, ys, sizes): + c = 40 + int(float(y) / height * 215) + pygame.draw.circle( + screen, (c, c, c), (x, y), size) - xs_ = [] - ys_ = [] - dxs_ = [] - dys_ = [] - sizes_ = [] + xs_ = [] + ys_ = [] + dxs_ = [] + dys_ = [] + sizes_ = [] - for x, y, dx, dy, size in zip(xs, ys, dxs, dys, sizes): - if 0 <= x + dx <= width and 0 <= y + dy <= height: - xs_.append(x + dx) - ys_.append(y + int(dy)) - dxs_.append(dx) - dys_.append(dy) - sizes_.append(size) + for x, y, dx, dy, size in zip(xs, ys, dxs, dys, sizes): + if 0 <= x + dx <= width and 0 <= y + dy <= height: + xs_.append(x + dx) + ys_.append(y + int(dy)) + dxs_.append(dx) + dys_.append(dy) + sizes_.append(size) - xs = xs_ - ys = ys_ - dxs = dxs_ - dys = dys_ - sizes = sizes_ + xs = xs_ + ys = ys_ + dxs = dxs_ + dys = dys_ + sizes = sizes_ - pygame.display.flip() + pygame.display.flip() diff --git a/data/graphics/tree b/data/graphics/tree index acbdf78..04996f7 100644 --- a/data/graphics/tree +++ b/data/graphics/tree @@ -1,6 +1,8 @@ # tree -import pippy, pygame, sys +import pippy +import pygame +import sys from pygame.locals import * from random import * import math @@ -27,31 +29,31 @@ min_angle_delta = 0.4 max_angle_delta = 0.5 # start the screen all black -bgcolor = (0,0,0) +bgcolor = (0, 0, 0) screen.fill(bgcolor) def draw_tree(x, y, length, angle): - x2 = x + length * math.sin(angle) - y2 = y - length * math.cos(angle) - pygame.draw.line(screen, color, (x, y), (x2, y2)) + x2 = x + length * math.sin(angle) + y2 = y - length * math.cos(angle) + pygame.draw.line(screen, color, (x, y), (x2, y2)) - if length > min_length: - # draw left branch - left_angle = angle - \ - uniform(min_angle_delta, max_angle_delta) - left_length = length * \ - uniform(min_factor, max_factor) - draw_tree(x2, y2, left_length, left_angle) - # draw middle branch - middle_length = length * \ - uniform(min_factor, max_factor) - draw_tree(x2, y2, middle_length, angle) - # draw right branch - right_angle = angle + \ - uniform(min_angle_delta, max_angle_delta) - right_length = length * \ - uniform(min_factor, max_factor) - draw_tree(x2, y2, right_length, right_angle) + if length > min_length: + # draw left branch + left_angle = angle - \ + uniform(min_angle_delta, max_angle_delta) + left_length = length * \ + uniform(min_factor, max_factor) + draw_tree(x2, y2, left_length, left_angle) + # draw middle branch + middle_length = length * \ + uniform(min_factor, max_factor) + draw_tree(x2, y2, middle_length, angle) + # draw right branch + right_angle = angle + \ + uniform(min_angle_delta, max_angle_delta) + right_length = length * \ + uniform(min_factor, max_factor) + draw_tree(x2, y2, right_length, right_angle) # clear the screen screen.fill(bgcolor) @@ -63,10 +65,10 @@ pygame.display.flip() # do nothing visible until the escape key is pressed while pippy.pygame.next_frame(): - for event in pygame.event.get(): - if event.type == QUIT: - sys.exit() + for event in pygame.event.get(): + if event.type == QUIT: + sys.exit() - if event.type == KEYDOWN: - if event.key == K_ESCAPE: - sys.exit() + if event.type == KEYDOWN: + if event.key == K_ESCAPE: + sys.exit() diff --git a/data/graphics/xolympics b/data/graphics/xolympics index 525e480..b6ed90b 100644 --- a/data/graphics/xolympics +++ b/data/graphics/xolympics @@ -5,7 +5,10 @@ Use w,a,s,d and up,down,left,right to play. Score by making the newest ball hit your opponent's wall. """ -import pippy, pygame, sys, math +import pippy +import pygame +import sys +import math from pygame.locals import * from pippy import physics from pygame.color import * @@ -42,15 +45,15 @@ class XOlympicsGame: # add the left border and player (red) self.world.set_color((255, 0, 0)) self.world.add.rect((0, -20), 25, self.height, - dynamic=False, density=1.0, - restitution=0.16, friction=0.5) - self.leftplayer = self.world.add.poly(( self.width * 0.25, 81.0 ), [(-109.9405166666667, -64.244016666666653), (110.60718333333335, -63.089316666666605), (-0.66666666666668561, 127.33333333333337)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) + dynamic=False, density=1.0, + restitution=0.16, friction=0.5) + self.leftplayer = self.world.add.poly(( self.width * 0.25, 81.0 ), [(-109.9405166666667, -64.244016666666653), (110.60718333333335, -63.089316666666605), (-0.66666666666668561, 127.33333333333337)], dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) # add the right border and player (blue) self.world.set_color((0, 0, 255)) self.world.add.rect((self.width, -20), 25, self.height, - dynamic=False, density=1.0, - restitution=0.16, friction=0.5) - self.rightplayer = self.world.add.poly(( self.width * 0.75, 81.0 ), [(108.94051666666667, -65.976066666666611), (2.6666666666666288, 127.33333333333337), (-111.60718333333341, -61.357266666666646)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) + dynamic=False, density=1.0, + restitution=0.16, friction=0.5) + self.rightplayer = self.world.add.poly(( self.width * 0.75, 81.0 ), [(108.94051666666667, -65.976066666666611), (2.6666666666666288, 127.33333333333337), (-111.60718333333341, -61.357266666666646)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) # we're getting 2 grounds - grey and green. why? def run(self): @@ -94,39 +97,39 @@ class XOlympicsGame: self.rightDPress = False if self.leftLPress: - self.leftplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed,0), self.leftplayer.GetWorldCenter()) + self.leftplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed, 0), self.leftplayer.GetWorldCenter()) if self.leftRPress: - self.leftplayer.ApplyForce(box2d.b2Vec2(self.forcespeed,0), self.leftplayer.GetWorldCenter()) + self.leftplayer.ApplyForce(box2d.b2Vec2(self.forcespeed, 0), self.leftplayer.GetWorldCenter()) if self.leftJump: if self.leftplayer.GetWorldCenter().y < 0.75: - self.leftplayer.ApplyImpulse(box2d.b2Vec2(0,self.jumpforce), self.leftplayer.GetWorldCenter()) + self.leftplayer.ApplyImpulse(box2d.b2Vec2(0, self.jumpforce), self.leftplayer.GetWorldCenter()) if self.rightLPress: - self.rightplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed,0), self.rightplayer.GetWorldCenter()) + self.rightplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed, 0), self.rightplayer.GetWorldCenter()) if self.rightRPress: - self.rightplayer.ApplyForce(box2d.b2Vec2(self.forcespeed,0), self.rightplayer.GetWorldCenter()) + self.rightplayer.ApplyForce(box2d.b2Vec2(self.forcespeed, 0), self.rightplayer.GetWorldCenter()) if self.rightDPress: - self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,-self.jumpforce), self.rightplayer.GetWorldCenter()) + self.rightplayer.ApplyImpulse(box2d.b2Vec2(0 ,-self.jumpforce), self.rightplayer.GetWorldCenter()) if self.rightJump: if self.rightplayer.GetWorldCenter().y < 0.75: - self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,self.jumpforce), self.rightplayer.GetWorldCenter()) + self.rightplayer.ApplyImpulse(box2d.b2Vec2(0, self.jumpforce), self.rightplayer.GetWorldCenter()) if self.leftDPress: - self.leftplayer.ApplyImpulse(box2d.b2Vec2(0,-self.jumpforce), self.leftplayer.GetWorldCenter()) + self.leftplayer.ApplyImpulse(box2d.b2Vec2(0, -self.jumpforce), self.leftplayer.GetWorldCenter()) # Clear Display if self.ball.GetWorldCenter().x < 1: self.leftscore += 1 print "Goal Blue!", self.leftscore self.world.set_color((0, 0, 255)) - self.ball = self.world.add.ball((self.width/2, 0), 50) + self.ball = self.world.add.ball((self.width / 2, 0), 50) elif self.ball.GetWorldCenter().x > 11: # FIXME: the 11 above works only when display width is # 1200 pixels self.rightscore += 1 print "Goal Red!", self.rightscore self.world.set_color((255, 0, 0)) - self.ball = self.world.add.ball((self.width/2, 0), 50) + self.ball = self.world.add.ball((self.width / 2, 0), 50) - self.screen.fill((255,255,255)) + self.screen.fill((255, 255, 255)) # Update & Draw World self.world.update() self.world.draw() diff --git a/data/math/factorial b/data/math/factorial index 93441b6..bbe5a20 100644 --- a/data/math/factorial +++ b/data/math/factorial @@ -1,34 +1,35 @@ import time -# define a factorial function in recursive flavor def factorial_recursive(number): + """ Define a factorial function in recursive flavor """ result = 1 if number > 0: - result = number * factorial_recursive(number-1) - print "factorizing: ", number, " result: ", result + result = number * factorial_recursive(number - 1) + print 'factorizing: ', number, ' result: ', result return result -# define a factorial function in iterative flavor def factorial_iterative(number): + """ Define a factorial function in iterative flavor """ result = 1 - for i in range(1, number+1): + for i in range(1, number + 1): result = result * i - print "factorizing: ", i, " result: ", result + print 'factorizing: ', i, ' result: ', result return result def calculate(number, type): + """ Calculate factorial using recursive and iterative methods """ start = time.time() if type == 0: - type_s = "recursive" - result = factorial_recursive(number) + type_s = 'recursive' + factorial_recursive(number) else: - type_s = "iterative" - result = factorial_iterative(number) + type_s = 'iterative' + factorial_iterative(number) delta = time.time() - start - print "Type: ", type_s, " in: ", 1/delta + print 'Type: ', type_s, ' in: ', 1 / delta -# ask for a number to factorize -number = input("Please input a number:") -print "Calculating..." +# ask for a number to compute the factorial of +number = input('Please input a number:') +print 'Calculating...' calculate(number, 0) calculate(number, 1) diff --git a/data/math/fibonacci b/data/math/fibonacci index 9bc1acb..6cef209 100644 --- a/data/math/fibonacci +++ b/data/math/fibonacci @@ -1,4 +1,4 @@ a, b = 0, 1 while b < 1001: - print b, - a, b = b, a+b + print b, + a, b = b, a + b diff --git a/data/math/guess b/data/math/guess index 68c0760..14c6539 100644 --- a/data/math/guess +++ b/data/math/guess @@ -1,18 +1,19 @@ import random import pippy -R = random.randrange(1,100) +R = random.randrange(1, 100) -print "Guess a number between 1 and 100!" -N = input("Enter a number: ") -i=1 -while (N!=R): - if N>R: +print 'Guess a number between 1 and 100!' +N = input('Enter a number: ') +i = 1 +while (N != R): + if N > R: pippy.console.red() - print "Too big... try again" + print 'Too big... try again' else: pippy.console.blue() - print "Too small.. try again" + print 'Too small.. try again' pippy.console.black() - N = input("Enter a number: ") - i=i+1 -print "You got it in", i, "tries" + N = input('Enter a number: ') + i = i + 1 + +print 'You got it in', i, 'tries' diff --git a/data/math/pascal b/data/math/pascal index 359acdd..06fab84 100644 --- a/data/math/pascal +++ b/data/math/pascal @@ -1,19 +1,18 @@ # Pascal's triangle lines = 9 - vector = [1] -for i in range(1,lines+1): - vector.insert(0,0) - vector.append(0) +for i in range(1, lines + 1): + vector.insert(0, 0) + vector.append(0) -for i in range(0,lines): - newvector = vector[:] - for j in range(0,len(vector)-1): - if (newvector[j] == 0): - print " ", - else: - print "%2d" % newvector[j], - newvector[j] = vector[j-1] + vector[j+1] - print - vector = newvector[:] +for i in range(0, lines): + newvector = vector[:] + for j in range(0, len(vector) - 1): + if (newvector[j] == 0): + print ' ', + else: + print '%2d' % newvector[j], + newvector[j] = vector[j - 1] + vector[j + 1] + print + vector = newvector[:] diff --git a/data/math/sierpinski b/data/math/sierpinski index 8b5f291..1c0b89e 100644 --- a/data/math/sierpinski +++ b/data/math/sierpinski @@ -1,26 +1,25 @@ # Sierpinski triangles -import sys size = 3 modulus = 2 -lines = modulus**size +lines = modulus ** size vector = [1] -for i in range(1,lines+1): - vector.insert(0,0) - vector.append(0) +for i in range(1, lines + 1): + vector.insert(0, 0) + vector.append(0) -for i in range(0,lines): - newvector = vector[:] - for j in range(0,len(vector)-1): - if (newvector[j] == 0): - print " ", - else: - remainder = newvector[j] % modulus - if (remainder == 0): - print "O", - else: - print ".", - newvector[j] = vector[j-1] + vector[j+1] - print - vector = newvector[:] +for i in range(0, lines): + newvector = vector[:] + for j in range(0, len(vector) - 1): + if (newvector[j] == 0): + print ' ', + else: + remainder = newvector[j] % modulus + if (remainder == 0): + print 'O', + else: + print '.', + newvector[j] = vector[j - 1] + vector[j + 1] + print + vector = newvector[:] diff --git a/data/math/times b/data/math/times index b764ac0..4757a4a 100644 --- a/data/math/times +++ b/data/math/times @@ -1,3 +1,3 @@ -number = input("Which times table? ") -for i in range(1,13): - print i, "x", number, "=", i*number +number = input('Which times table? ') +for i in range(1, 13): + print i, 'x', number, '=', i * number diff --git a/data/python/function b/data/python/function index 18e8e91..2149c67 100644 --- a/data/python/function +++ b/data/python/function @@ -1,4 +1,4 @@ -def square(x): +def square(x): print x * x square(3) diff --git a/data/python/if b/data/python/if index 6bcfbb8..0e4b947 100644 --- a/data/python/if +++ b/data/python/if @@ -1,8 +1,8 @@ -number = input("Enter a number: ") +number = input('Enter a number: ') if number > 5: - print "Greater than 5" + print 'Greater than 5' elif number < 5: - print "Less than 5" + print 'Less than 5' else: - print "Number is 5!" + print 'Number is 5!' diff --git a/data/python/interpreter b/data/python/interpreter index dcc838e..535aaca 100644 --- a/data/python/interpreter +++ b/data/python/interpreter @@ -1,2 +1,2 @@ import code -code.InteractiveConsole().interact(banner="") +code.InteractiveConsole().interact(banner='') diff --git a/data/python/recursion b/data/python/recursion index c34638d..bf8cd53 100644 --- a/data/python/recursion +++ b/data/python/recursion @@ -1,10 +1,10 @@ -def countbackwards(number): - print "I have the number", number +def count_backwards(number): + print 'I have the number', number if number > 0: - print "Calling countbackwards again!" - countbackwards(number-1) + print 'Calling count_backwards again!' + count_backwards(number - 1) else: - print "I am done counting" + print 'I am done counting' -number = input("Enter a number: ") -countbackwards(number) +number = input('Enter a number: ') +count_backwards(number) diff --git a/data/sound/playSine b/data/sound/playSine index 216b4aa..c17e169 100644 --- a/data/sound/playSine +++ b/data/sound/playSine @@ -2,4 +2,3 @@ import pippy pippy.sound.playSine(pitch=1000, amplitude=5000) pippy.sound.audioOut() - diff --git a/data/sound/playWave b/data/sound/playWave index 819ad4f..ee86aa6 100644 --- a/data/sound/playWave +++ b/data/sound/playWave @@ -2,4 +2,3 @@ import pippy pippy.sound.playWave(sound='didjeridu', loop=True, duration=5) pippy.sound.audioOut() - diff --git a/data/sound/sequence b/data/sound/sequence index 3922e7a..06eac36 100644 --- a/data/sound/sequence +++ b/data/sound/sequence @@ -1,10 +1,11 @@ -import pippy, random +import pippy +import random for i in range(25): - pitch = random.randint(500,2000) + pitch = random.randint(500, 2000) amplitude = 5000 duration = 0.1 - starttime = i * 0.1 - pippy.sound.playSine(pitch, amplitude, duration, starttime) -pippy.sound.audioOut() + start_time = i * 0.1 + pippy.sound.playSine(pitch, amplitude, duration, start_time) +pippy.sound.audioOut() diff --git a/data/string/thanks b/data/string/thanks index ddd470a..48d6d95 100644 --- a/data/string/thanks +++ b/data/string/thanks @@ -15,10 +15,11 @@ table = { 'Finance & Legal': 'Eben Moglen, Bruce Parker, William Kolb, John Sare, Sandra Lee, Richard Bernstein, Jaclyn Tsai, Jaime Cheng, Robert Fadel, Charles Kane (Grasshopper), Kathy Paur, Andriani Ferti', 'PR and Media': 'Larry Weber, Jackie Lustig, Jodi Petrie, George Snell, Kyle Austin, Hilary Meserole, Erick A. Betancourt, Michael Borosky, Sylvain Lefebvre, Martin Le Sauteur', 'Directors & Advisors': 'Howard Anderson, Rebecca Allen, Ayo Kusamotu, Jose Maria Aznar, V. Michael Bove, Jr., Rodrigo Mesquita, Seymour Papert, Ted Selker, Ethan Beard (Google); John Roese (Nortel); Dandy Hsu (Quanta); Marcelo Claure (Brightstar); Gary Dillabough (eBay); Gustavo Arenas (AMD); Mike Evans (Red Hat); Ed Horowitz (SES Astra); Jeremy Philips (NewsCorp); Scott Soong (Chi Lin); Sehat Sutardja (Marvell); Joe Jacobson (MIT Media Lab); Steve Kaufman (Riverside); and Tom Meredith (MFI)', - 'Pippy': 'Chris Ball, C. Scott Ananian' + 'Pippy': 'Chris Ball, C. Scott Ananian, James Cameron, Anish Mangal' } -import random, time +import random +import time from pippy.console import * from textwrap import fill -- cgit v0.9.1