diff options
-rw-r--r-- | physics.py | 92 |
1 files changed, 55 insertions, 37 deletions
@@ -19,8 +19,9 @@ Code: http://git.sugarlabs.org/physics License: GPLv3 http://gplv3.fsf.org/ """ -import sys -import math +# Many unused imports, should investigate why +#import sys +#import math import pygame from pygame.locals import * @@ -28,10 +29,10 @@ from pygame.color import * import olpcgames import elements -from elements import Elements +#from elements import Elements import tools from bridge import Bridge -from helpers import * +#from helpers import * from gettext import gettext as _ @@ -40,7 +41,7 @@ class PhysicsGame: This class is the actual game that runs. It sets up the resources needed to run and has the loop that handles drawing updates to the screen. """ - def __init__(self,screen): + def __init__(self, screen): """ The constructor for the class. """ @@ -50,15 +51,17 @@ class PhysicsGame: # get everything set up self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 24) # font object - self.canvas = olpcgames.ACTIVITY.canvas - self.joystickobject = None - self.debug = True + self.canvas = olpcgames.ACTIVITY.canvas # canvas object + self.joystickobject = None # TODO Figure out why this exists + self.debug = True # TODO Figure out what this does. + + self.running = False # create the name --> instance map for components - self.toolList = {} - for c in tools.allTools: - self.toolList[c.name] = c(self) - self.currentTool = self.toolList[tools.allTools[0].name] + self.toollist = {} + for tool in tools.ALLTOOLS: + self.toollist[tool.name] = tool(self) + self.currenttool = self.toollist[tools.ALLTOOLS[0].name] # set up the world (instance of Elements) self.world = elements.Elements(self.screen.get_size()) @@ -72,19 +75,21 @@ class PhysicsGame: self.bridge.create_world() def run(self): + """This method starts the simulation and provides the loop mechanism. + """ self.running = True - t = pygame.time.get_ticks() + ticks = pygame.time.get_ticks() while self.running: - if (pygame.time.get_ticks() - t) > 1500: + if (pygame.time.get_ticks() - ticks) > 1500: # bridge.create_train(self) - t = pygame.time.get_ticks() + ticks = pygame.time.get_ticks() for event in pygame.event.get(): # Handles events like button clicks - self.currentTool.handleEvents(event) + self.currenttool.handleEvents(event) # Clear Display - self.screen.fill((80,160,240)) #255 for white + self.screen.fill((80, 160, 240)) #255 for white # Update & Draw World self.world.update() @@ -96,26 +101,34 @@ class PhysicsGame: self.bridge.for_each_frame() # draw output from tools - self.currentTool.draw() + self.currenttool.draw() #Print all the text on the screen - # The underscore function will do translations to native languages if available. - text = self.font.render(_("Total Cost: %d") % self.bridge.cost, True, (0,0,0)) - textpos = text.get_rect(left=100,top=7) - self.screen.blit(text,textpos) + # The underscore function will do translations + # to native languages if available. + text = self.font.render(_("Total Cost: %d") % self.bridge.cost, + True, (0, 0, 0)) + textpos = text.get_rect(left=100, top=7) + self.screen.blit(text, textpos) ratio = self.bridge.stress*100/self.bridge.capacity - text = self.font.render(_("Stress: %d%%") % ratio, True, (0,0,0)) - textpos = text.get_rect(left=100,top=25) - self.screen.blit(text,textpos) + text = self.font.render(_("Stress: %d%%") % ratio, True, (0, 0, 0)) + textpos = text.get_rect(left=100, top=25) + self.screen.blit(text, textpos) if self.bridge.train_off_screen: - text = self.font.render(_("Train fell off the screen, press R to try again!"), True, (0,0,0)) + text = self.font.render( + _("Train fell off the screen, press R to try again!"), + True, (0, 0, 0)) elif self.bridge.level_completed: - text = self.font.render(_("Level completed, well done!! Press T to send another train."), True, (0,0,0)) + text = self.font.render( + _("Level completed, well done!! Press T to send another train."), + True, (0, 0, 0)) else: - text = self.font.render(_("Press the Spacebar to start/pause."), True, (0,0,0)) - textpos = text.get_rect(left=100,top=43) - self.screen.blit(text,textpos) + text = self.font.render( + _("Press the Spacebar to start/pause."), + True, (0, 0, 0)) + textpos = text.get_rect(left=100, top=43) + self.screen.blit(text, textpos) # Flip Display pygame.display.flip() @@ -123,19 +136,25 @@ class PhysicsGame: # Try to stay at 30 FPS self.clock.tick(30) # originally 50 - def setTool(self,tool): - self.currentTool.cancel() - self.currentTool = self.toolList[tool] + def settool(self, tool): + """Deactivates the previously selected tool, and then sets + the currenttool to be the specified tool. + """ + self.currenttool.cancel() + self.currenttool = self.toolList[tool] # main() is a function, by itself. It is not in a class. def main(): + """Runs the activity if the file is ran independently. + See longer comment below. + """ toolbarheight = 75 # number of pixels tabheight = 45 # number of pixels pygame.init() pygame.display.init() - x,y = pygame.display.list_modes()[0] # function returns an array + xsize, ysize = pygame.display.list_modes()[0] # function returns an array # where the first element [0] is probably the largest resolution. - screen = pygame.display.set_mode((x,y-toolbarheight-tabheight)) + screen = pygame.display.set_mode((xsize, ysize-toolbarheight-tabheight)) # screen uses the full width, x # set the screen height less the space needed for the toolbar and tab. @@ -144,9 +163,8 @@ def main(): game = PhysicsGame(screen) # start the main loop - # This is where the game actually runs. This method is defined in this file. + # This is where the game actually runs. This method is defined in this file. game.run() - # end of main() # make sure that main() get's called |