diff options
author | Ignacio Rodriguez <ignacio@sugarlabs.org> | 2013-12-13 01:01:48 (GMT) |
---|---|---|
committer | Ignacio Rodriguez <ignacio@sugarlabs.org> | 2013-12-13 01:02:43 (GMT) |
commit | e2b3a76e706a58c4590eb9b3a6ed222703026d96 (patch) | |
tree | 8cb96bb388ecb594af6e4b314cdbb9f8c0db9579 | |
parent | 8c849b54fd06592b7a581fc086fc6fff0f70b8b7 (diff) |
Pep8 fixes
Remove unused modules
Elegant syntax with pep8
-rw-r--r-- | activity.py | 1 | ||||
-rw-r--r-- | helpers.py | 36 | ||||
-rw-r--r-- | physics.py | 30 | ||||
-rwxr-xr-x | setup.py | 1 | ||||
-rw-r--r-- | tools.py | 26 |
5 files changed, 49 insertions, 45 deletions
diff --git a/activity.py b/activity.py index 95c6544..0d905ba 100644 --- a/activity.py +++ b/activity.py @@ -24,7 +24,6 @@ import os import csv import tempfile import json -from StringIO import StringIO from gettext import gettext as _ import logging @@ -23,11 +23,13 @@ #================================================================== import math + def distance(pt1, pt2): """Distance calculator, pt1 and pt2 are ordred pairs. """ return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) + def getAngle(pt1, pt2): """Returns angle between line segment pt1 -> pt2 and x axis, from -pi to pi. """ @@ -35,14 +37,18 @@ def getAngle(pt1, pt2): ycomp = pt1[1] - pt2[1] return math.atan2(ycomp, xcomp) + def constructTriangleFromLine(p1, p2): - """Returns list of ordered pairs describing equilteral triangle around segment pt1 --> pt2. + """ + Returns list of ordered pairs describing equilteral triangle around + segment pt1 --> pt2. """ halfHeightVector = (0.57735 * (p2[1] - p1[1]), 0.57735 * (p2[0] - p1[0])) p3 = (p1[0] + halfHeightVector[0], p1[1] - halfHeightVector[1]) p4 = (p1[0] - halfHeightVector[0], p1[1] + halfHeightVector[1]) return [p2, p3, p4] + def polyArea(vertices): """Returns the area of a polygon. """ @@ -56,10 +62,10 @@ def polyArea(vertices): q += 1 return A / 2.0 - + def insideTriangle(pt, triangle): """Returns true if pt is in triangle. - + Some polygon magic, thanks to John W. Ratcliff on www.flipcode.com """ ax = triangle[2][0] - triangle[1][0] @@ -77,9 +83,10 @@ def insideTriangle(pt, triangle): aCROSSbp = ax * bpy - ay * bpx cCROSSap = cx * apy - cy * apx - bCROSScp = bx * cpy - by * cpx + bCROSScp = bx * cpy - by * cpx return aCROSSbp >= 0.0 and bCROSScp >= 0.0 and cCROSSap >= 0.0 + def polySnip(vertices, u, v, w, n): EPSILON = 0.0000000001 @@ -92,7 +99,7 @@ def polySnip(vertices, u, v, w, n): Cx = vertices[w][0] Cy = vertices[w][1] - if EPSILON > (((Bx-Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax))): + if EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax))): return False for p in range(0, n): @@ -112,7 +119,8 @@ def decomposePoly(vertices): vertices = list(vertices) n = len(vertices) result = [] - if(n < 3): return [] # not a poly! + if(n < 3): + return [] # not a poly! # Force counter-clockwise polygon if 0 >= polyArea(vertices): @@ -120,20 +128,23 @@ def decomposePoly(vertices): # Remove nv-2 vertices, creating 1 triangle every time nv = n - count = 2 * nv # error detection + count = 2 * nv # error detection v = nv - 1 while nv > 2: count -= 1 if 0 >= count: - return [] # Error -- probably bad polygon + return [] # Error -- probably bad polygon # Three consecutive vertices - u = v - if nv <= u: u = 0 # previous + u = v + if nv <= u: + u = 0 # previous v = u + 1 - if nv <= v: v = 0 # new v + if nv <= v: + v = 0 # new v w = v + 1 - if nv <= w: w = 0 # next + if nv <= w: + w = 0 # next if(polySnip(vertices, u, v, w, nv)): @@ -147,6 +158,7 @@ def decomposePoly(vertices): count = 2 * nv return result + def tuple_to_int(tuple_input): """Cast tuple values to ints to avoid gtk+ and pygame's dislike of floats. """ @@ -28,19 +28,15 @@ Code: git://git.sugarlabs.org/physics/mainline.git import os import sys -import math -import logging import pygame from pygame.locals import * from pygame.color import * -import sugargame from gi.repository import Gtk from gi.repository import Gdk sys.path.append("lib/") -import pkg_resources # If your architecture is different, comment these lines and install # the modules in your system. sys.path.append("lib/Box2D-2.0.2b1-py2.5-linux-i686.egg") @@ -139,8 +135,8 @@ class PhysicsGame: # Drive motors if self.world.run_physics: for body in self.world.world.GetBodyList(): - if type(body.userData) == type({}): - if body.userData.has_key('track_index'): + if isinstance(body.userData, dict): + if 'track_index' in body.userData: trackdex = body.userData['track_index'] def to_screen(pos): @@ -163,17 +159,17 @@ class PhysicsGame: except IndexError: self.full_pos_list.append([posx, posy]) - if body.userData.has_key('rollMotor'): - diff = body.userData['rollMotor'] \ - ['targetVelocity'] \ + if 'rollMotor' in body.userData: + diff = body.userData['rollMotor'][ + 'targetVelocity'] \ - body.GetAngularVelocity() - body.ApplyTorque(body.userData['rollMotor'] \ - ['strength'] * diff \ - * body.getMassData().I) + body.ApplyTorque(body.userData['rollMotor'][ + 'strength'] * diff + * body.getMassData().I) # Update & Draw World self.world.update() - self.screen.fill((255, 255, 255)) # 255 for white + self.screen.fill((255, 255, 255)) # 255 for white self.world.draw() # Draw output from tools @@ -188,17 +184,17 @@ class PhysicsGame: pygame.display.flip() # Stay < 30 FPS to help keep the rest of the platform responsive - self.clock.tick(30) # Originally 50 + self.clock.tick(30) # Originally 50 def setTool(self, tool): self.currentTool.cancel() self.currentTool = self.toolList[tool] + def main(activity): - game = PhysicsGame(activity) + game = PhysicsGame(activity) return game # Make sure that main get's called if __name__ == '__main__': - main() - + main()
\ No newline at end of file @@ -1,4 +1,3 @@ #!/usr/bin/env python from sugar3.activity import bundlebuilder bundlebuilder.start() - @@ -24,10 +24,7 @@ # By Alex Levenson #================================================================== import pygame -import sugargame -from pygame.locals import * from helpers import * -from inspect import getmro from gettext import gettext as _ from sugar3.graphics.alert import ConfirmationAlert from gi.repository import Gtk @@ -68,7 +65,7 @@ class Tool(object): self.game.in_focus = True elif event.action == "focus_out": self.game.in_focus = False - elif self.game.toolList.has_key(event.action): + elif event.action in self.game.toolList: self.game.setTool(event.action) elif event.type == MOUSEBUTTONDOWN and event.button == 1: self.game.canvas.grab_focus() @@ -95,7 +92,7 @@ class Tool(object): color = 0 for i in range(0, len(pos_list), 2): posx = int(pos_list[i]) - posy = int(pos_list[i+1]) + posy = int(pos_list[i + 1]) pygame.draw.circle(surface, color, (posx, posy), 2) def cancel(self): @@ -130,7 +127,7 @@ class CircleTool(Tool): def draw(self): Tool.draw(self) # Draw a circle from pt1 to mouse - if self.pt1 != None: + if self.pt1 is not None: delta = distance(self.pt1, tuple_to_int(pygame.mouse.get_pos())) if delta > 0: @@ -164,7 +161,7 @@ class BoxTool(Tool): if event.button == 1: self.pt1 = tuple_to_int(event.pos) elif event.type == MOUSEBUTTONUP: - if event.button == 1 and self.pt1 != None: + if event.button == 1 and self.pt1 is not None: mouse_x_y = tuple_to_int(event.pos) if mouse_x_y[0] == self.pt1[0] and mouse_x_y[1] == self.pt1[1]: self.rect = pygame.Rect(self.pt1, @@ -182,7 +179,7 @@ class BoxTool(Tool): def draw(self): Tool.draw(self) # Draw a box from pt1 to mouse - if self.pt1 != None: + if self.pt1 is not None: mouse_x_y = tuple_to_int(pygame.mouse.get_pos()) if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]: self.width = mouse_x_y[0] - self.pt1[0] @@ -216,7 +213,7 @@ class TriangleTool(Tool): if event.button == 1: self.pt1 = tuple_to_int(event.pos) elif event.type == MOUSEBUTTONUP: - if event.button == 1 and self.pt1 != None: + if event.button == 1 and self.pt1 is not None: mouse_x_y = tuple_to_int(event.pos) if mouse_x_y[0] == self.pt1[0] and mouse_x_y[1] == self.pt1[1]: self.pt1 = [mouse_x_y[0] - self.line_delta[0], @@ -251,14 +248,14 @@ class TriangleTool(Tool): def draw(self): Tool.draw(self) # Draw a triangle from pt1 to mouse - if self.pt1 != None: + if self.pt1 is not None: mouse_x_y = tuple_to_int(pygame.mouse.get_pos()) if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]: self.vertices = constructTriangleFromLine(self.pt1, mouse_x_y) self.line_delta = [mouse_x_y[0] - self.pt1[0], mouse_x_y[1] - self.pt1[1]] pygame.draw.polygon(self.game.screen, (100, 180, 255), - self.vertices, 3) + self.vertices, 3) pygame.draw.line(self.game.screen, (100, 180, 255), self.pt1, mouse_x_y, 1) @@ -311,7 +308,7 @@ class PolygonTool(Tool): return if distance(tuple_to_int(event.pos), self.vertices[0]) < 15 \ and self.safe: - self.vertices.append(self.vertices[0]) # Connect polygon + self.vertices.append(self.vertices[0]) # Connect polygon self.game.world.add.complexPoly(self.vertices, dynamic=True, density=1.0, @@ -566,7 +563,7 @@ class RollTool(Tool): if event.button == 1: self.jb1pos = tuple_to_int(event.pos) self.jb1 = self.game.world.get_bodies_at_pos(self.jb1pos) - if self.jb1 and type(self.jb1[0].userData) == type({}): + if self.jb1 and isinstance(self.jb1[0].userData, dict): self.jb1[0].userData['rollMotor'] = {} self.jb1[0].userData['rollMotor']['targetVelocity'] = -10 self.jb1[0].userData['rollMotor']['strength'] = 40 @@ -590,7 +587,8 @@ class DestroyTool(Tool): def handleToolEvent(self, event): Tool.handleToolEvent(self, event) if pygame.mouse.get_pressed()[0]: - if not self.vertices: self.vertices = [] + if not self.vertices: + self.vertices = [] self.vertices.append(tuple_to_int(event.pos)) if len(self.vertices) > 10: self.vertices.pop(0) |