#!/usr/bin/python """ This file is part of the 'Bridge' Project Code: http://git.sugarlabs.org/bridge git clone git://git.sugarlabs.org/bridge/mainline.git This file was originally part of the 'Physics' Project Physics is a 2D Physics Playground for Kids (supporting Box2D2) Physics Copyright (C) 2008, Alex Levenson, Brian Jordan Elements Copyright (C) 2008, The Elements Team, Wiki: http://wiki.laptop.org/wiki/Physics IRC: #olpc-physics on irc.freenode.org Code: http://git.sugarlabs.org/physics git clone git://git.sugarlabs.org/physics/mainline.git License: GPLv3 http://gplv3.fsf.org/ """ import sys import math import pygame from pygame.locals import * from pygame.color import * import olpcgames import elements from elements import Elements import tools from bridge import Bridge from helpers import * from gettext import gettext as _ 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): """ The constructor for the class. """ # screen is provided from main() below self.screen = screen # 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 # 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] # set up the world (instance of Elements) self.world = elements.Elements(self.screen.get_size()) self.world.renderer.set_surface(self.screen) # set up static environment #self.world.add.ground() self.world.run_physics = False self.bridge = Bridge(self) self.bridge.create_world() def run(self): self.running = True t = pygame.time.get_ticks() while self.running: if (pygame.time.get_ticks() - t) > 1500: # bridge.create_train(self) t = pygame.time.get_ticks() for event in pygame.event.get(): # Handles events like button clicks self.currentTool.handleEvents(event) # Clear Display self.screen.fill((80,160,240)) #255 for white # Update & Draw World self.world.update() self.world.draw() # This is True if the simulation is active, meaning # the user has requested a train attempt to drive across the bridge. if self.world.run_physics: self.bridge.for_each_frame() # draw output from tools 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) 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) if self.bridge.train_off_screen: 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)) 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) # Flip Display pygame.display.flip() # Try to stay at 30 FPS self.clock.tick(30) # originally 50 def setTool(self,tool): self.currentTool.cancel() self.currentTool = self.toolList[tool] # main() is a function, by itself. It is not in a class. def main(): 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 # where the first element [0] is probably the largest resolution. screen = pygame.display.set_mode((x,y-toolbarheight-tabheight)) # screen uses the full width, x # set the screen height less the space needed for the toolbar and tab. # create an instance of the game # PhysicsGame is a class (see above in this file) game = PhysicsGame(screen) # start the main loop # 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 # This means that the program was run from the command line as # python physics.py or ./physics.py or similar. # if the file was loaded from another .py file using the import keyword # than main() would not be called. # This is a handy way to provide a classes and functions to a larger program # while still performing a smaller action on its own usage. # Also can be handy for testing code. if __name__ == '__main__': main()