Changeset 209


Ignore:
Timestamp:
05/23/2012 03:53:50 PM (3 years ago)
Author:
andefodahl
Message:

ughhhhhhhhhhhhhhhh

Location:
traintris
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • traintris/constants.py

    r199 r209  
    138138            self.herbivoreList = ["Deer", "Sheep", "Rabbit", "Squirrel", "Cow", "Elephant", "Giraffe", "Rats"]
    139139           
    140 class GraphicsConstants:
    141     '''Contains constants used by graphics'''
     140class Constants:
     141    '''Contains constants usable by all other classes'''
    142142    def __init__(self):
    143143        self.NEUTRAL, self.HOVER, self.DEPRESSED = range(3)
    144144        self.buttonModes = ["neutral", "hover", "depressed"]
     145        self.SUMMER, self.WINTER = range(2)
     146        self.CATEGORY, self.ORGANISM, self.MENU, self.DIALOG = range(4)
    145147       
    146148class gameNumbers:
     
    191193        self.numbers = numbers
    192194
    193 GRAPHICS = GraphicsConstants()
     195CONSTANTS = Constants()
  • traintris/gameboardview.py

    r200 r209  
    8585        self.tileSet = self.graphics.makeGroundTiles('ground')
    8686
    87         self.dark = 0
     87        self.season = CONSTANTS.SUMMER
    8888        self.daysTillChange = 20
    8989       
     
    116116                        "To skip the tutorial press SKIP, " +
    117117                        "to continue hit NEXT", True, True)
    118        
    119         self.addButton("hi", "testbutton", (100,100), (100,100))
    120118       
    121119        self.draw()
     
    195193    def draw(self):
    196194        self.screen.fill((155,187,88))
    197         self.graphics.drawBackground(self.dark)
     195        self.graphics.drawBackground(self.season)
    198196        self.graphics.drawBoard(self.level[0], self.tileSet, self.screen)
    199197        self.graphics.drawObjectsAndMobs(self.level[1], self.spriteTileSet, [],
    200198                           self.screen, 0)
    201199        self.highLighter()
    202         self.panelDraw()
     200        self.graphics.drawBackground(self.season)
     201        self.graphics.drawInfoBox()
     202        self.graphics.drawMenuBox()
     203        self.graphics.drawBottomBar(self.daysTillChange, self.season)
     204        # self.panelDraw()
     205        self.drawCategoryButtons()
    203206        for button in self.buttonList:
    204207            self.graphics.drawButton(button.images, button.mode, button.pos)
     
    208211   
    209212    ############################################################################
    210     ########################## Button stuff ####################################
     213    ################################ Button stuff ##############################
    211214    ############################################################################   
    212215   
    213     def addButton(self, text, imageName, size, pos):
    214         self.buttonList.append(Button(text, imageName, size, pos))
     216    def addButton(self, text, imageName, size, pos, type):
     217        self.buttonList.append(Button(text, imageName, size, pos, type))
    215218       
    216219    def removeButton(self, imageName):
     
    221224    def drawCategoryButtons(self):
    222225        buttonsize = (self.screenwidth / 4)
    223         self.addButton( "Producers", "plantButton", (buttonsize, 90), (0,0))
    224         self.addButton( "Herbivores", "herbButton", (buttonsize, 90), (buttonsize, 0))
    225         self.addButton( "Carnivores", "carniButton", (buttonsize, 90), (2*buttonsize, 0))
    226         self.addButton( "Humans", "omniButton", (buttonsize, 90), (3*buttonsize, 0))
     226        self.addButton( "Producers", "topbutton", (buttonsize, 90), (0,0), CONSTANTS.CATEGORY)
     227        self.addButton( "Herbivores", "topbutton", (buttonsize, 90), (buttonsize, 0), CONSTANTS.CATEGORY)
     228        self.addButton( "Carnivores", "topbutton", (buttonsize, 90), (2*buttonsize, 0), CONSTANTS.CATEGORY)
     229        self.addButton( "Humans", "topbutton", (buttonsize, 90), (3*buttonsize, 0), CONSTANTS.CATEGORY)
    227230       
    228231    def drawOrganismBar(self, animalList):
    229232        buttonsize = (self.screenwidth / len(animalList))
    230233        for x in range(len(animalList)):
    231             self.addButton( animalList[x], animalList[x], (buttonsize, 90), ((x+1)*buttonsize, 0))
     234            self.addButton( animalList[x], animalList[x]+".jpg", (buttonsize, 90), ((x+1)*buttonsize, 0), CONSTANTS.ORGANISM)
    232235   
    233236    ############################################################################
     
    505508            self.y = event.y
    506509            for button in self.buttonList:
    507                 if button.mode != GRAPHICS.DEPRESSED:
     510                if button.mode != CONSTANTS.DEPRESSED:
    508511                    if self.x > button.pos[0] and self.x < button.pos[0]+button.size[0] \
    509512                    and self.y > button.pos[1] and self.y < button.pos[1]+button.size[1]:
    510                         button.mode = GRAPHICS.HOVER
     513                        button.mode = CONSTANTS.HOVER
    511514                    else:
    512                         button.mode = GRAPHICS.NEUTRAL
     515                        button.mode = CONSTANTS.NEUTRAL
    513516            self.draw()
    514517            pygame.display.update()
     
    522525                    and self.y > button.pos[1] and self.y < button.pos[1]+button.size[1]:
    523526                        self.lastButtonClicked = button
    524                         button.mode = GRAPHICS.DEPRESSED
     527                        button.mode = CONSTANTS.DEPRESSED
    525528            if event.type == 'up':
    526529                for button in self.buttonList:
    527                     button.mode = GRAPHICS.NEUTRAL
     530                    button.mode = CONSTANTS.NEUTRAL
    528531                    if self.x > button.pos[0] and self.x < button.pos[0]+button.size[0] \
    529532                    and self.y > button.pos[1] and self.y < button.pos[1]+button.size[1]:
    530533                        if button == self.lastButtonClicked:
    531534                            self.lastButtonUsed = button
    532                         button.mode = GRAPHICS.HOVER
     535                        button.mode = CONSTANTS.HOVER
    533536            self.draw()
    534537                       
     
    564567
    565568        if isinstance(event, SeasonChangeEvent):
    566             self.dark = 1 - self.dark
     569            if self.season == CONSTANTS.SUMMER:
     570                self.season = CONSTANTS.WINTER
     571            else:
     572                self.season = CONSTANTS.SUMMER
    567573
    568574        if isinstance(event, GameOver):
     
    599605           
    600606class Button:
    601     def __init__(self, text, imageName, size, pos):
    602         self.mode = GRAPHICS.NEUTRAL
     607    def __init__(self, text, imageName, size, pos, type):
     608        self.mode = CONSTANTS.NEUTRAL
    603609        self.pos = pos
    604610        self.imageName = imageName
    605611        self.size = size
     612        self.type = type
    606613        imageList = []
    607         for mode in GRAPHICS.buttonModes:
     614        for mode in CONSTANTS.buttonModes:
    608615            imageList.append(imageName + "_" + mode + ".jpg")
    609616        self.images = imageList       
  • traintris/graphics.py

    r200 r209  
    11import pygame
    2 from constants import GRAPHICS
     2from constants import CONSTANTS
    33import os.path
    44
     
    106106    #------------------------------------------------------------------
    107107    # render background
    108     def drawBackground(self, dark):
     108    def drawBackground(self, season):
    109109        #draws summer or winter background accordingly
    110110        return
    111111
    112     def drawInfoBox(self, name, description, diet, rang, predators, color):
     112    def drawInfoBox(self, name=None, description=None, diet=None, rang=None, predators=None, color=None):
    113113        boxsize = (self.screenwidth / 5)
    114114        ystart = self.screenheight - boxsize
    115         self.drawImage("infobox", (0, ystart))
    116         if rang == "" and predators == "":
    117             self.drawText(name, (20, ystart + 20), (boxsize - 40), color, 10)
    118             self.drawText(description, (20, ystart + 98), (boxsize - 40), color, 10)
    119             self.drawText(diet, (20, ystart + 176), (boxsize - 40), color, 10)
    120         else:
    121             self.drawText(name, (20, ystart + 20), (boxsize - 40), color, 10)
    122             self.drawText(description, (20, ystart + 67), (boxsize - 40), color, 10)
    123             self.drawText(diet, (20, ystart + 114), (boxsize - 40), color, 10)
    124             self.drawText(rang, (20, ystart + 161), (boxsize - 40), color, 10)
    125             self.drawText(predators, (20, ystart + 208), (boxsize - 40), color, 10)
     115        self.drawImage("infobox.jpg", (0, ystart))
     116        if name:
     117            if rang == "" and predators == "":
     118                self.drawText(name, (20, ystart + 20), color, 10)
     119                self.drawText(description, (20, ystart + 98), color, 10)
     120                self.drawText(diet, (20, ystart + 176), color, 10)
     121            else:
     122                self.drawText(name, (20, ystart + 20), color, 10)
     123                self.drawText(description, (20, ystart + 67), color, 10)
     124                self.drawText(diet, (20, ystart + 114), color, 10)
     125                self.drawText(rang, (20, ystart + 161), color, 10)
     126                self.drawText(predators, (20, ystart + 208), color, 10)
    126127           
    127128    def drawDialogBox(self, string1, string2, color):
    128         self.drawImage("dialogbox", ((self.screenwidth / 2) - 150, (self.screenheight / 2) - 100))
    129         self.drawText(string1, ((self.screenwidth / 2) - 130, (self.screenheight / 2) - 80), 260, color, 40)
    130         self.drawText(string2, ((self.screenwidth / 2) -130, (self.screenheight / 2) + 20), 260, color, 40)
     129        self.drawImage("dialogbox.jpg", ((self.screenwidth / 2) - 150, (self.screenheight / 2) - 100))
     130        self.drawText(string1, ((self.screenwidth / 2) - 130, (self.screenheight / 2) - 80), color, 40)
     131        self.drawText(string2, ((self.screenwidth / 2) -130, (self.screenheight / 2) + 20), color, 40)
    131132   
    132133    def drawMenuBox(self):
     
    134135        xstart = self.screenwidth - boxsize
    135136        ystart = self.screenheight - boxsize
    136         self.drawImage("menubox", (xstart, ystart))
    137        
    138     def drawBottomBar(self, daysRemaining, season, animal, diet, rang):
     137        self.drawImage("menubox.jpg", (xstart, ystart))
     138       
     139    def drawBottomBar(self, daysRemaining, season, animal=None, diet=None, rang=None):
    139140        boxsize = (3/5)*self.screenwidth
    140141        xstart = self.screenwidth / 5
    141142        ystart = self.screenheight - 90
    142143        midbox = (1/2)*self.screenwidth
    143         self.drawImage("bottomBar", (xstart, ystart))
    144         self.drawImage(season, (xstart, ystart))
    145         self.drawImage(animal, (midbox, ystart))
    146         self.drawText(daysRemaining, (xstart + 100, ystart + 20), (mixbox - 110), self.black, 20)
    147         self.drawText(diet, (midbox + 100, ystart + 20), 190, self.black, 20)
    148         self.drawText(rang, (mixbox + 300, ystart + 20), 90, self.black, 20)
    149    
    150     # def drawText(self, text, pos, width, color, size)
     144        self.drawImage("bottomBar.jpg", (xstart, ystart))
     145        #### uncomment when we have season icons
     146        if season == CONSTANTS.SUMMER:
     147            # self.drawImage(summerIcon, (xstart, ystart))
     148            self.drawText("Days until winter: "+str(daysRemaining), (xstart + 100, ystart + 20), self.black, 20)
     149        else:
     150            # self.drawImage(winterIcon, (xstart, ystart))
     151            self.drawText("Days until summer: "+str(daysRemaining), (xstart + 100, ystart + 20), self.black, 20)
     152        if animal:
     153            self.drawImage(animal, (midbox, ystart))
     154            self.drawText(diet, (midbox + 100, ystart + 20), self.black, 20)
     155            self.drawText(rang, (midbox + 300, ystart + 20), self.black, 20)
     156   
     157    # def drawText(self, text, pos, color, size)
     158    def drawText(self, text, pos, color=pygame.Color("black"), textSize=20,
     159             drawpos="left"):
     160        """parses a string for certain indicators and draws it"""
     161        x = pos[0]
     162        y = pos[1]
     163        if text != "":
     164            textLines = [] #Will contain different lines
     165            line = ""
     166            while text != "":
     167                if text[0] == "\n":
     168                    text = text[1:]
     169                    textLines.append(line)
     170                    line = ""
     171                else:
     172                    line += text[0]
     173                    text = text[1:]
     174
     175            textLines.append(line)
     176            prevLineHeight = 0 #Vertical shift for sequential lines
     177            for line in textLines:
     178                lineChunks = [] #Will contain "chunks" of individual lines
     179                lineChunk = ""
     180                textColor = color
     181                currentFont = self.font
     182                while line != "":
     183                        lineChunk += line[0]
     184                        line = line[1:]
     185                lineChunks.append(self.font.render(lineChunk, 1, textColor))
     186                y += prevLineHeight
     187                prevLineChunkWidth = 0 #Horizontal shift for individual line chunks
     188                originalx = x #Reset x after drawing so that next line is flush
     189                for lineChunk in lineChunks:
     190                    textRect = lineChunk.get_rect()
     191                    x += prevLineChunkWidth
     192                    prevLineChunkWidth = textRect.width
     193                    prevLineHeight = textRect.height
     194                    if drawpos == "right":
     195                        x += -textRect.right
     196                    elif drawpos == "center":
     197                        x += -textRect.centerx
     198                        y += -textRect.centery
     199                    self.screen.blit(lineChunk, (x,y))
     200                x = originalx
    151201   
    152202    #-------------------------------------------------------------------------------------------------
     
    154204    def drawButton(self, images, mode, pos):
    155205   
    156         if mode == GRAPHICS.NEUTRAL:
    157             image = images[GRAPHICS.NEUTRAL]
    158         elif mode == GRAPHICS.HOVER:
    159             image = images[GRAPHICS.HOVER]
     206        if mode == CONSTANTS.NEUTRAL:
     207            image = images[CONSTANTS.NEUTRAL]
     208        elif mode == CONSTANTS.HOVER:
     209            image = images[CONSTANTS.HOVER]
    160210        else:
    161             image = images[GRAPHICS.DEPRESSED]
     211            image = images[CONSTANTS.DEPRESSED]
    162212               
    163213        self.drawImage(image, pos)
  • traintris/main.py

    r198 r209  
    3838    evManager = eventmanager.EventManager()
    3939    pygame.display.init()
    40     screen = pygame.display.set_mode((800, 600), HWSURFACE)
     40    screen = pygame.display.set_mode((1366, 768), HWSURFACE)
    4141   
    4242    pygame.font.init()
Note: See TracChangeset for help on using the changeset viewer.