예제 #1
0
   def renderMap(self):
      (mouseX,mouseY) = pygame.mouse.get_pos()
      (xdiff,ydiff) = (mouseX%32,mouseY%32)
   
      for event in pygame.event.get():
         
         if     event.type == MOUSEBUTTONUP:
            if self.currentGUI != None:
               self.currentGUI.readEvent(event)
         
         elif   event.type == KEYDOWN:
            if self.currentGUI != None:
               self.currentGUI.readEvent(event)
         
         elif   event.type == MOUSEBUTTONDOWN:
            if self.currentGUI != None:
               self.currentGUI.readEvent(event)
            
            # if the main menu is used
            if (mouseX >= self.mainMenu.x and mouseX <= self.mainMenu.x + self.mainMenu.width and
                mouseY >= self.mainMenu.y and mouseY <= self.mainMenu.y + self.mainMenu.height
               ):
               if self.mainMenu.isOpen:
               
                  btn = self.mainMenu.findPressed(mouseX,mouseY)
               
                  if btn != None:
                     
                     if btn.label == 'exit':
                        self.exit()
                     elif btn.label == 'portfolio':
                        from view.ingamegui import PortfolioGUI
                        self.currentGUI   = PortfolioGUI()
                        self.currentState = self.STATE_GUI_PORTFOLIO
                     elif btn.label == 'opponents':
                        from view.ingamegui import OpponentsGUI
                        self.currentGUI = OpponentsGUI()
                        self.currentState = self.STATE_GUI_OPPONENTS
                     elif btn.label == 'market':
                        from view.ingamegui import MarketGUI
                        self.currentGUI = MarketGUI()
                        self.currentState = self.STATE_GUI_MARKET
                  
                  self.mainMenu.close()
               else:
                  self.mainMenu.open()
            
            elif (self.currentState == self.STATE_GUI_PORTFOLIO or
                  self.currentState == self.STATE_GUI_MARKET    or
                  self.currentState == self.STATE_GUI_OPPONENTS
                 ):
                 
                 btn = self.currentGUI.findPressed(mouseX,mouseY)
               
                 if btn != None:
                    if btn.label == 'closeGUI':
                        self.currentGUI   = None
                        self.currentState = self.STATE_GAME_MODE
            
            elif self.currentMap.blockingLayer.content2D[(mouseY+self.camera[1])/32][(mouseX+self.camera[0])/32] == None:
               mc = self.currentMap.mainChar
               mc.startpoint = ((mc.x)/32,(mc.y)/32)
               mc.endpoint   = (pygame.mouse.get_pos()[0]/32+self.camera[0]/32,pygame.mouse.get_pos()[1] / 32+self.camera[1]/32)
               mc.pathlines = findPath(mc.startpoint,mc.endpoint,(self.currentMap.width,self.currentMap.height),self.currentMap.aStarMap)
               mc.setMovingPositions(mc.pathlines)
      
         elif event.type == QUIT:
            self.exit()

      self.camera[0] = self.currentMap.mainChar.x + 16     - (self.widthInTiles / 2)  * 32 - 32
      self.camera[1] = self.currentMap.mainChar.y + 33 +16 - (self.heightInTiles / 2) * 32 - 64

      if self.camera[0] < 0:
         self.camera[0] = 0
   
      if self.camera[1] < 0:
         self.camera[1] = 0
   
      if self.camera[0] > (self.currentMap.width * 32) - (self.widthInTiles) * 32:
         self.camera[0] = (self.currentMap.width * 32) - (self.widthInTiles) * 32
   
      if self.camera[1] > (self.currentMap.height * 32) - (self.heightInTiles) * 32:
         self.camera[1] = (self.currentMap.height * 32) - (self.heightInTiles) * 32
   
      self.renderer.set_camera_position(self.camera[0],self.camera[1],'topleft')

      self.screen.fill((0, 0, 0))
      
      for sprite_layer in self.currentMap.spriteLayers:
         if sprite_layer.is_object_group:
            continue
         else:
            if sprite_layer.layer_idx != 2:
               self.renderer.render_layer(self.screen, sprite_layer)

      mouseColor = None
      if self.currentMap.blockingLayer.content2D[(mouseY-ydiff+self.camera[1])/32][(mouseX-xdiff+self.camera[0])/32] == None:
         mouseColor = (0,255,255)
      else:
         mouseColor = (255,0,0)
      
      if self.currentGUI == None:
         s = pygame.Surface((32,32))
         s.set_alpha(80)
         s.fill(mouseColor)
         self.screen.blit(s, (mouseX-xdiff,mouseY-ydiff))
      
      # draw characters
      for ch in self.currentMap.characters:
         
         chpos = ch.getOffset()
         
         #character clipping!
         if (ch.x >= self.camera[0] - self.charClippingOffset and
             ch.x <= self.camera[0] + self.widthInTiles  * 32 + self.charClippingOffset and
             ch.y >= self.camera[1] - self.charClippingOffset and
             ch.y <= self.camera[1] + self.heightInTiles * 32 + self.charClippingOffset):
            
            img = loadImage(self.charSheet,
                            chpos[0],
                            chpos[1]
                           )
            charPos = (ch.x-self.camera[0]-16, ch.y-33-self.camera[1])
            self.screen.blit(img,charPos)
            
            # draw the nametag background
            self.screen.blit(ch.nametagImage,(charPos[0]-32,charPos[1]-46))
            
            # draw the texts describing name and capital
            font  = pygame.font.SysFont('monospace',10)
            (nameLabelW,nameLabelH) = font.size(ch.name)
            nameLabel = font.render(ch.name, 1, (34,34,34))
            self.screen.blit(nameLabel,(charPos[0]-32+(97/2)-(nameLabelW/2),charPos[1]-43))
            
            cashLabel = font.render('$'+str(ch.tempTotalCapital), 1, (211,211,211))
            (cashLabelW,cashLabelH) = font.size('$'+str(ch.tempTotalCapital))
            self.screen.blit(cashLabel,(charPos[0]-32+(97/2)-(cashLabelW/2),charPos[1]-23))
            
            # walk somewhere?
            if ch != self.currentMap.mainChar:
               if applyChance(5):
                  if ch.movingPositions == []:
                     ch.startpoint = ((ch.x)/32,(ch.y)/32)
                     ch.endpoint   = (ch.startpoint[0]+random.randint(0,10)-random.randint(0,10),ch.startpoint[1]+random.randint(0,10)-random.randint(0,10))
                     if (ch.endpoint[0] < self.currentMap.width and
                        ch.endpoint[1] < self.currentMap.height and
                        ch.endpoint[0] > 0 and ch.endpoint[1] > 0):
                     # if the endpoint is a valid point, let's go there!
                        if self.currentMap.blockingLayer.content2D[ch.endpoint[1]][ch.endpoint[0]] == None:
                           ch.pathlines = findPath(ch.startpoint,ch.endpoint,(self.currentMap.width,self.currentMap.height),self.currentMap.aStarMap)
                           ch.setMovingPositions(ch.pathlines)
               
      # time to teleport?
      if (self.currentMap.mainChar.x/32,self.currentMap.mainChar.y/32) in self.currentMap.teleportTiles:
   
         if self.currentMap.tag == 'town':
            self.mainCharPosBackup = (self.currentMap.mainChar.x,self.currentMap.mainChar.y-(self.currentMap.mainChar.y%32)+32+16)
            self.startpoint = self.mainCharPosBackup
            self.currentMap.destroy()
            self.currentMap = HouseMap()
            from gamemodels.models import Investor
            self.currentMap.injectMainCharacter(Investor.objects.get(type='player'))
         else:
            self.currentMap.destroy()
            self.currentMap = TownMap()
            from gamemodels.models import Investor
            self.currentMap.injectCharacters(Investor.objects.all())
            self.currentMap.mainChar.x = self.mainCharPosBackup[0]
            self.currentMap.mainChar.y = self.mainCharPosBackup[1]
      
         self.screen.fill((0, 0, 0))
         pygame.time.delay(200)
      
      # Draw the main menu
      self.screen.blit(self.mainMenu.image, (10, 10))
      
      if self.currentGUI != None:
      # Draw the main menu
         self.screen.blit(self.startScreen.image, (167, 8))
         self.screen.blit(self.currentGUI.image, (self.currentGUI.x, self.currentGUI.y))
         self.currentGUI.drawExtra(self.screen)
def handleHour(mainRef):
   
   comps = Company.objects.all()

   # For each company, find the latest price and 
   # calculate a new one
   for comp in comps:
      
      # find the last time that was entered
      lastTime = comp.priceHistory.all().aggregate(Max('time'))
      
      # find the price associated with that time
      lastEntry = comp.priceHistory.get(time=lastTime['time__max'])
      
      # If the company's stock price is 0
      # the company is in bancruptcy
      if lastEntry.price > 0:
      
         newEntry = Stock()
         newEntry.time = lastEntry.time + 1
         newEntry.price = calcNewPrice(lastEntry.price)
         newEntry.save()
      
         comp.priceHistory.add(newEntry)
         comp.save()
      
   print 'new prices calculated'
   
   investors = Investor.objects.all()
   
   # For each investor, except the player, 
   # make some random buys and sells
   for investor in investors:
      
      # don't do this for the player
      if investor.type == 'bot':
      
         # iterate through all companies, and it's a 20% chance
         # each investor will buy each stock
         for comp in comps:
            if applyChance(2):
               success = buyStock(investor,comp,100)
      
         # iterate through the portfolio, and apply a 50% chance of
         # selling each company
         portfolio = getCurrentPortfolio(investor)
         for entry in portfolio:
            if applyChance(20):
               
               # get the corresponding company to sell
               comp = Company.objects.get(name=entry[0])
               
               # then delegate the object modification
               # to sellStock
               sellStock(investor,comp,entry[1])
   
   # get the current cash amount of the investor
   if mainRef.currentMap.characters != None:
      
      for ch in mainRef.currentMap.characters:
      
         inv           = Investor.objects.get(name=ch.name)
         investorStock = calcCurrentPortfolioWorth(inv)
         totalCapital  = investorStock + inv.cash
      
         ch.setTempTotalCapital(totalCapital)