-
-
Save krclark/f13999e4783f88f35facf05b0f02543d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, sys | |
import random | |
import copy | |
import pickle | |
from pygame.locals import * | |
pygame.init() | |
GREEN = ( 0, 100, 0) | |
WHITE = (255, 255, 255) | |
BLACK = ( 0, 0, 0) | |
RED = (255, 0 , 0) | |
LIGHTGREEN = (0,255, 0) | |
pygame.display.set_caption('Mellon-Topia') | |
windowW , windowH = 1200 , 700 | |
DISPLAYSURF = pygame.display.set_mode((windowW, windowH), 0, 32) | |
DISPLAYSURF.fill(BLACK) | |
class MellonTopia: | |
# generates a 2D list of with a certain length of columns and rows | |
def mapGenerator(self, mapWidth, mapHeight): | |
return [["empty"] * mapHeight for cell in xrange(mapWidth)] | |
#reversed for sake of mapDraw function | |
#button class which draws a button, stores its location, and determines whether it | |
# is clicked or not | |
class button(object): | |
def __init__(self, name, x0, y0, clicked): | |
self.clicked=clicked | |
self.name=name | |
self.x0=x0 | |
self.y0=y0 | |
self.button=pygame.image.load(self.name + ".bmp") | |
self.x1=x0+self.button.get_rect()[2] | |
self.y1=y0+self.button.get_rect()[3] | |
def draw(self): | |
if not self.clicked: | |
self.button=pygame.image.load(self.name+ ".bmp") | |
DISPLAYSURF.blit(self.button, (self.x0, self.y0)) | |
if self.clicked: | |
self.button=pygame.image.load(self.name+"_clicked"+".bmp") | |
DISPLAYSURF.blit(self.button, (self.x0, self.y0)) | |
#menu object | |
class menu(object): | |
def __init__(self, x, y, select): | |
self.x=x | |
self.y=y | |
self.select=select | |
menu=pygame.image.load("menu.bmp") | |
rect = menu.get_rect() | |
self.x1 = self.x + rect[2] | |
self.y1 = self.y + rect[3]/2 | |
self.y2 = self.y + rect[3] | |
def draw(self): | |
if self.select != None: self.menu=pygame.image.load("menu_"+str(self.select)+".bmp") | |
else: self.menu=pygame.image.load("menu.bmp") | |
DISPLAYSURF.blit(self.menu, (self.x,self.y)) | |
# generates a list of objects for buttons | |
def buttonObjectList(self): | |
buttonContainer=[] | |
for item in xrange(len(self.buttonList)): | |
newButton = self.button(self.buttonList[item], 10, 10*(item+1)+74*item, | |
self.clickedList[item]) | |
buttonContainer+=[newButton] | |
return buttonContainer | |
# fire object | |
class fire(object): | |
def __init__(self, gridx, gridy, x, y): | |
self.images=[] | |
for num in xrange(1, 6): | |
self.images.append(pygame.image.load("fire"+str(num)+".gif")) | |
random.shuffle(self.images) | |
self.index = 0 | |
self.image = self.images[self.index] | |
self.x = x | |
self.y = y | |
self.gridx=gridx | |
self.gridy=gridy | |
self.rect = self.image.get_rect() | |
self.fireTimer = 0 | |
#animates fire | |
def update(self): | |
try: | |
self.index += 1 | |
self.image = self.images[self.index] | |
except: | |
self.index = 0 | |
self.image = self.images[self.index] | |
self.fireTimer += 1 | |
def draw(self): | |
self.image.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(self.image, (self.x-self.rect[2]/2, self.y-self.rect[3])) | |
#people on the campus | |
class student(object): | |
def __init__(self, x, y, speed, student, origin, mapW, mapH, tileW, isClicked): | |
self.isClicked=isClicked | |
self.speedx=speed[0] | |
self.speedy=speed[1] | |
self.img=pygame.image.load(student+".gif") | |
self.rect=self.img.get_rect() | |
self.mapStart=origin | |
self.mapW=mapW | |
self.mapH=mapH | |
self.tileW=tileW | |
self.gridx=x | |
self.gridy=y | |
self.newgridx=x | |
self.newgridy=y | |
self.x=self.mapStart[0]+(self.gridx-self.gridy)*(self.tileW/2)+self.tileW/2 | |
self.y=self.mapStart[1]+(self.gridx+self.gridy)*(self.tileW/4)+self.tileW/4 | |
def draw(self): | |
self.img.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(self.img, (self.x-(self.rect[2]/2), self.y-(self.rect[3]/2))) | |
if self.isClicked or self.isClicked==None: | |
highlight=pygame.image.load("highlight.gif") | |
highlight.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(highlight, (self.x-(self.rect[2]/2), self.y-(self.rect[3]/2))) | |
#sets new location for moving function | |
def changeDirection(self, dx, dy): | |
self.gridx=self.newgridx | |
self.gridy=self.newgridy | |
if self.gridx+dx >= 0 and self.gridx+dx < self.mapW: | |
self.newgridx+=dx | |
else: | |
self.newgridx-=dx | |
if self.gridy+dy >= 0 and self.gridy+dy < self.mapH: | |
self.newgridy+=dy | |
else: | |
self.newgridy-=dy | |
destx=self.mapStart[0]+(self.newgridx-self.newgridy)*(self.tileW/2)+self.tileW/2 | |
desty=self.mapStart[1]+(self.newgridx+self.newgridy)*(self.tileW/4)+self.tileW/4 | |
self.x=self.mapStart[0]+(self.gridx-self.gridy)*(self.tileW/2)+self.tileW/2 | |
self.y=self.mapStart[1]+(self.gridx+self.gridy)*(self.tileW/4)+self.tileW/4 | |
self.speedy=(desty-self.y)/10 | |
self.speedx=(destx-self.x)/10 | |
#moves them from tile to tile | |
def move(self): | |
self.x+=self.speedx | |
self.y+=self.speedy | |
#initializes all the variables | |
def __init__(self): | |
self.tileW=64 | |
self.windowW=windowW | |
self.windowH=windowH | |
self.mapW=self.windowW/self.tileW-5 | |
self.mapH=self.windowH/(self.tileW/2)-5 | |
self.origin=(self.windowW/2, 10) | |
self.mapGrid=self.mapGenerator(self.mapW, self.mapH) | |
self.well, self.pres, self.safe = 0.5, 0.5, 0.5 | |
self.endo, self.divr, self.facp = 0.5, 0.5, 0.5 | |
self.wellTimer, self.presTimer, self.safeTimer=0, 0, 0 | |
self.endoTimer, self.divrTimer, self.facpTimer=0, 0, 0 | |
self.statBarList=[self.well, self.pres, self.endo, | |
self.safe, self.divr, self.facp] | |
self.statBarTimers=[self.wellTimer, self.presTimer, self.endoTimer, | |
self.safeTimer, self.divrTimer, self.facpTimer] | |
self.statBarTitles=["Wellness", "Prestige", "Wealth", "Safety", "Diversity", "Faculty"] | |
self.buttonList=["treeButton","safewalkButton","buildingButton","sidewalkButton", "foodButton"] | |
self.clickedList=[False, False, False, False, False] | |
self.buttonObjectList=self.buttonObjectList() | |
self.font = pygame.font.Font(None, 30) | |
self.tuition = 50000 | |
self.tuitionText = self.font.render("tuition: $"+str(self.tuition), 1, (255, 255, 255)) | |
self.population = 1000 | |
self.populationText = self.font.render("population: " + str(self.population), 1, (255, 255, 255)) | |
self.students=30 | |
self.studentRoster=[] | |
self.studentRosterGenerator(self.students) | |
self.facultyCount=0 | |
self.studentLocation=None | |
self.path=None | |
self.step=0 | |
self.fireList=[] | |
self.isMenu=False | |
self.dropMenu=None | |
self.arrowUpx0=180 | |
self.arrowUpy0=490 | |
self.titleScreen=True | |
self.tag=None | |
self.gameOver=False | |
self.facultyCount = 0 | |
self.studentTag=False | |
self.instructions=False | |
self.unlocked=False | |
self.goldStatueButton = self.button("goldStatueButton", 150, 50, False) | |
self.upArrow=pygame.image.load("arrowup.gif") | |
self.upArrow.set_colorkey((255,255,255)) | |
self.arrowWidth=self.upArrow.get_rect()[2] | |
self.arrowHeight=self.upArrow.get_rect()[3] | |
self.downArrow=pygame.image.load("arrowdown.gif") | |
self.downArrow.set_colorkey((255,255,255)) | |
self.arrowDownx0=200 | |
self.arrowDowny0=490 | |
def on_init(self): | |
pygame.init() | |
self.running = True | |
def distanceList(self, curLoc, dest): | |
xsteps=dest[0]-curLoc[0] | |
ysteps=dest[1]-curLoc[1] | |
dx = -1 if xsteps < 0 else 1 | |
dy = -1 if ysteps < 0 else 1 | |
stepList=[] | |
x=curLoc[0] | |
y=curLoc[1] | |
while curLoc != dest: | |
stepList+=[(dx,0)] | |
x = curLoc[0]+dx if x != dest[0] else curLoc[0] | |
dx = dx if x != dest[0] else 0 | |
stepList+=[(0,dy)] | |
y = curLoc[1]+dy if y != dest[1] else curLoc[1] | |
dy = dy if y != dest[1] else 0 | |
curLoc=(x,y) | |
return stepList | |
def locationSet(self, student, coords): | |
clickX=coords[0] | |
clickY=coords[1] | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
left = self.origin[0]+(x-y)* (self.tileW/2) | |
right = self.origin[0]+(x-y)* (self.tileW/2)+self.tileW | |
cx = self.origin[0]+(x-y)*(self.tileW/2)+self.tileW/2 | |
cy = self.origin[1]+(x+y)*(self.tileW/4)+self.tileW/4 | |
if left <= clickX <= right: | |
if cy <= clickY <= (-0.5*abs(clickX - cx) + cy + self.tileW/4) or (0.5*abs(clickX - cx) + cy - self.tileW/4) <= clickY <= cy: | |
self.studentLocation = tuple([x,y]) | |
self.path = self.distanceList(tuple([student.gridx, student.gridy]), self.studentLocation) | |
def setOnFire(self): | |
fireList=[] | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
left = self.origin[0]+(x-y)* (self.tileW/2) | |
right = self.origin[0]+(x-y)* (self.tileW/2)+self.tileW | |
cx = self.origin[0]+(x-y)*(self.tileW/2)+self.tileW/2 | |
cy = self.origin[1]+(x+y)*(self.tileW/4)+self.tileW/4 | |
if self.mapGrid[x][y] != "empty" and self.mapGrid[x][y] != "sidewalk": | |
fireList.append(self.fire(x, y, cx, cy)) | |
self.fireList=fireList | |
for student in self.studentRoster: | |
speedx = random.choice([-6, 6]) | |
speedy = random.choice([speedx/2, -(speedx/2)]) | |
student.speed=[speedx, speedy] | |
def drawFire(self): | |
for fire in self.fireList: | |
if fire.fireTimer <= 45: | |
fire.draw() | |
def updateFire(self): | |
tempList = copy.deepcopy(self.fireList) | |
for i in xrange(len(self.fireList)): | |
fire = self.fireList[i] | |
if fire.fireTimer <= 45: | |
fire.update() | |
else: | |
self.mapGrid[fire.gridx][fire.gridy] = "rubble" | |
self.isBurnedDown() | |
def deleteFire(self): | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y] == "rubble": | |
for i in xrange(len(self.fireList)): | |
fire=self.fireList[i] | |
if fire.gridx==x and fire.gridy==y: | |
self.fireList.pop(i) | |
break | |
def isBurnedDown(self): | |
buildingCount=0 | |
rubbleCount=0 | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y] == "rubble": | |
rubbleCount+=1 | |
if self.mapGrid[x][y] != "rubble" and self.mapGrid[x][y] != "empty" and self.mapGrid[x][y] != "sidewalk": | |
buildingCount+=1 | |
if rubbleCount > 0 and buildingCount == 0: | |
self.gameOver=True | |
def win(self): | |
count=0 | |
for bar in xrange(len(self.statBarList)): | |
if self.statBarList[bar] > 0.8: | |
count+=1 | |
if count == len(self.statBarList): | |
self.unlocked=True | |
self.buttonObjectList+=[self.goldStatueButton] | |
def on_event(self, event): | |
coords=pygame.mouse.get_pos() | |
if self.isMenu: | |
if self.dropMenu.x <= coords[0] <= self.dropMenu.x1: | |
if self.dropMenu.y <= coords[1] <= self.dropMenu.y1: | |
self.dropMenu.select = "select1" | |
if self.dropMenu.y1 <= coords[1] <= self.dropMenu.y2: | |
self.dropMenu.select = "select2" | |
if event.type == QUIT: | |
self.running = False | |
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[2]==1: | |
coords=pygame.mouse.get_pos() | |
self.dropMenu = self.menu(coords[0], coords[1], None) | |
self.isMenu = True | |
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]==1: | |
coords=pygame.mouse.get_pos() | |
if self.isMenu: | |
if self.dropMenu.x <= coords[0] <= self.dropMenu.x1: | |
if self.dropMenu.y <= coords[1] <= self.dropMenu.y2: | |
if self.dropMenu.select == "select1": | |
self.facultyCount+=1 | |
if self.statBarList[2]-0.05 >= 0: | |
self.statBarList[2] -= 0.05 | |
if self.dropMenu.select == "select2": | |
self.setOnFire() | |
self.isMenu = False | |
self.createBuilding(self.mapW, self.mapH, self.tileW, self.tileW/2, self.origin, coords) | |
self.goldButtonClicked(coords) | |
foundClicked=False | |
foundTag=False | |
for item in self.buttonObjectList: | |
if (item.x0 <= coords[0] <= item.x1) and (item.y0 <= coords[1] <= item.y1): | |
self.clickButtons(coords) | |
self.studentHover() | |
for item in self.buttonObjectList: | |
if item.clicked and not self.isMenu: | |
if item.name == "treeButton": | |
self.tag="tree" | |
foundTag=True | |
if item.name == "safewalkButton": | |
self.tag="safewalk" | |
foundTag=True | |
if item.name == "buildingButton": | |
self.tag="building" | |
foundTag=True | |
if item.name == "foodButton": | |
self.tag="food" | |
foundTag=True | |
if not foundTag: | |
self.tag=None | |
if (self.arrowUpy0) <= coords[1]<= (self.arrowUpy0+self.arrowHeight): | |
if (self.arrowUpx0) <= coords[0] <= (self.arrowUpx0+self.arrowWidth): | |
self.tuition+=5000 | |
self.tuitionText = self.font.render("tuition: $"+str(self.tuition), 1, (255, 255, 255)) | |
DISPLAYSURF.blit(self.tuitionText, (20, 490)) | |
if self.statBarList[4]-0.06 > 0: | |
self.statBarList[4]-=0.06 | |
if (self.arrowDownx0) <=coords[0]<= (self.arrowDownx0+self.arrowWidth): | |
if self.tuition-5000 > 0: | |
self.tuition-=5000 | |
self.tuitionText = self.font.render("tuition: $"+str(self.tuition), 1, (255, 255, 255)) | |
DISPLAYSURF.blit(self.tuitionText, (20, 490)) | |
if self.statBarList[4]<1.0: | |
self.statBarList[4]+=0.06 | |
for student in self.studentRoster: | |
if student.isClicked: | |
self.locationSet(student, coords) | |
foundClicked=True | |
if self.studentLocation != None: | |
student.isClicked = None | |
break | |
for button in self.buttonObjectList: | |
if button.clicked: | |
foundClicked=True | |
if not foundClicked: | |
self.studentClick(coords) | |
if (pygame.key.get_pressed()[pygame.K_SPACE] != 0): | |
if self.titleScreen: | |
self.instructions=True if not self.instructions else False | |
if not self.titleScreen: | |
self.rotateMap() | |
if (pygame.key.get_pressed()[pygame.K_KP_ENTER] != 0): | |
if self.titleScreen: | |
self.titleScreen = False | |
self.instructions = False | |
if (pygame.key.get_pressed()[pygame.K_r] != 0): pass #how to restart game | |
def instructionsDraw(self): | |
if self.instructions: | |
instructions=pygame.image.load("instructions.png") | |
rect=instructions.get_rect() | |
DISPLAYSURF.blit(instructions, (self.windowW/2-rect[2]/2, self.windowH/2-rect[3]/2)) | |
def studentHover(self): | |
coords=pygame.mouse.get_pos() | |
foundClicked=False | |
for student in self.studentRoster: | |
if student.x <= coords[0] <= student.x + student.rect[2]: | |
if student.y <= coords[1] <= student.y + student.rect[3]: | |
self.studentTag=True | |
foundClicked=True | |
if not foundClicked: | |
self.studentTag=False | |
def drawClickMe(self): | |
if self.studentTag: | |
clickme=pygame.image.load("clickme.bmp") | |
DISPLAYSURF.blit(clickme, pygame.mouse.get_pos()) | |
def studentClick(self, coords): | |
clickX = coords[0] | |
clickY = coords[1] | |
for student in self.studentRoster: | |
left = self.origin[0]+(student.gridx-student.gridy)* (self.tileW/2) | |
right = self.origin[0]+(student.gridx-student.gridy)* (self.tileW/2)+self.tileW | |
cx = self.origin[0]+(student.gridx-student.gridy)*(self.tileW/2)+self.tileW/2 | |
cy = self.origin[1]+(student.gridx+student.gridy)*(self.tileW/4)+self.tileW/4 | |
if left <= clickX <= right: | |
if cy <= clickY <= (-0.5*abs(clickX - cx) + cy + self.tileW/4) or (0.5*abs(clickX - cx) + cy - self.tileW/4) <= clickY <= cy: | |
student.x = cx | |
student.y = cy | |
student.isClicked=True | |
break | |
def rotateMap(self): | |
newMapRow=[] | |
newMap=[] | |
for y in xrange(self.mapH): | |
for x in xrange(self.mapW): | |
newMapRow+=[self.mapGrid[x][y]] | |
newMap+=[newMapRow] | |
newMapRow=[] | |
self.mapGrid=newMap | |
temp=self.mapW | |
self.mapW=self.mapH | |
self.mapH=temp | |
for student in self.studentRoster: #issues here? | |
student.gridx, student.gridy = student.gridy, student.gridx | |
student.newgridx, student.newgridy = student.newgridy, student.newgridx | |
def goldButtonClicked(self, coords): | |
clickX=coords[0] | |
clickY=coords[1] | |
if self.goldStatueButton.x0 <= clickX <= self.goldStatueButton.x1: | |
if self.goldStatueButton.y0 <= clickY <= self.goldStatueButton.y1: | |
self.goldStatueButton.clicked=not self.goldStatueButton.clicked | |
#will create students | |
def studentRosterGenerator(self, number): | |
for i in range(number): | |
x = random.randint(1, self.mapW-2) | |
y = random.randint(1, self.mapH-2) | |
student = random.choice(["student1", "student2", "student3", "student4", "student5"]) | |
speedx = random.choice([-2, 2]) | |
speedy = random.choice([speedx/2, -(speedx/2)]) | |
kid = self.student(x, y, [speedx, speedy], student, self.origin, self.mapW, self.mapH, self.tileW, False) | |
self.studentRoster+=[kid] | |
def endGame(self): | |
if self.gameOver: | |
endscreen=pygame.image.load("endscreen.gif") | |
endscreen.set_colorkey((0,0,0)) | |
rect=endscreen.get_rect() | |
DISPLAYSURF.blit(endscreen, (self.windowW/2-(rect[2]/2), self.windowH/2-(rect[3]/2))) | |
def changeStudentsDirection(self): | |
for student in self.studentRoster: | |
dx=random.choice([1, -1, 0, 0]) | |
dy=random.choice([1, -1, 0, 0]) if dx==0 else 0 | |
if student.isClicked == None: | |
try: | |
gridCoord=self.path[self.step] | |
student.changeDirection(gridCoord[0], gridCoord[1]) | |
self.step+=1 | |
except: | |
student.isClicked=False | |
self.studentLocation=None | |
self.path=None | |
self.step=0 | |
elif not student.isClicked: | |
student.changeDirection(dx, dy) | |
def moveStudents(self): | |
for student in self.studentRoster: | |
if not student.isClicked or student.isClicked == None: | |
student.move() | |
def statusBarsDraw(self): | |
for bar in xrange(len(self.statBarList)): | |
pygame.draw.rect(DISPLAYSURF, RED, (960, 420+40*bar, 130, 20)) | |
pygame.draw.rect(DISPLAYSURF, LIGHTGREEN, (960, 420+40*bar, | |
130*min(self.statBarList[bar], 1.0), 20)) | |
barText= self.font.render(self.statBarTitles[bar], 1, (255, 255, 255)) | |
DISPLAYSURF.blit(barText, (800, 420+40*bar,)) | |
def campusDraw(self): | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
gridx = 600+(x-y)*(self.tileW/2) | |
gridy = 10 +(x+y)*(self.tileW/4) | |
construct=self.mapGrid[x][y] | |
if construct == "sidewalk": | |
newBuilding=pygame.image.load("sidewalk.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx-4, gridy-2)) | |
for student in self.studentRoster: | |
if x==student.gridx and y==student.gridy: | |
student.draw() | |
if construct == "tree": | |
newBuilding=pygame.image.load("tree.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx+self.tileW/4, gridy-self.tileW/4)) | |
if construct == "safewalk": | |
newBuilding=pygame.image.load("safewalk.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx+self.tileW/4, gridy-self.tileW/4)) | |
if construct == "building": | |
newBuilding=pygame.image.load("building.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx-4, gridy-92+self.tileW/2)) | |
if construct == "building1": | |
newBuilding=pygame.image.load("building1.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx-4, gridy-118+self.tileW/2)) | |
if construct == "building11": | |
newBuilding=pygame.image.load("building11.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx-4, gridy-138+self.tileW/2)) | |
if construct == "food": | |
newBuilding=pygame.image.load("foodstand1.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx+self.tileW/4, gridy-self.tileW/4)) | |
if construct == "food1": | |
newBuilding=pygame.image.load("foodstand2.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx+self.tileW/4-12, gridy-self.tileW/4+9)) | |
if construct == "rubble": | |
newBuilding=pygame.image.load("rubble.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx-4, gridy-2)) | |
if construct == "goldStatue": | |
newBuilding=pygame.image.load("goldStatue.gif") | |
newBuilding.set_colorkey((255,255,255)) | |
DISPLAYSURF.blit(newBuilding,(gridx+self.tileW/4, gridy-self.tileW/4)) | |
#draws the tuition and arrow keys | |
def tuitionDraw(self): | |
DISPLAYSURF.blit(self.tuitionText, (20, 490)) | |
if self.unlocked: | |
self.goldStatueButton.draw() | |
DISPLAYSURF.blit(self.populationText, (20, 520)) | |
DISPLAYSURF.blit(self.upArrow,(self.arrowUpx0, self.arrowUpy0)) | |
DISPLAYSURF.blit(self.downArrow,(self.arrowDownx0, self.arrowDowny0)) | |
def lowBarCount(self): | |
lowBarCount=0 | |
for bar in self.statBarList: | |
if bar <= 0.15: | |
lowBarCount+=1 | |
return lowBarCount | |
def highBarCount(self): | |
barCount=0 | |
for bar in self.statBarList: | |
if bar >0.8: | |
barCount+=1 | |
return barCount | |
def clues(self, coords): | |
something=False | |
for item in self.buttonObjectList: | |
if (item.x0 <= coords[0] <= item.x1) and (item.y0 <= coords[1] <= item.y1): | |
if item.clicked: | |
displayText=self.font.render("Click the campus to create a " + item.name[:-6], 1, (255, 255, 255)) | |
else: | |
displayText=self.font.render("Click to select a " + item.name[:-6], 1, (255, 255, 255)) | |
something=True | |
for bar in xrange(len(self.statBarList)): | |
if self.lowBarCount() > 3: | |
displayText=self.font.render("Get it together, you're losing your campus", 1, (255, 255, 255)) | |
something=True | |
#run a function that sets every building on fire and speeds up people | |
self.setOnFire() | |
elif self.statBarList[bar] <= 0.15: | |
displayText=self.font.render("Your campus is lacking in " + self.statBarTitles[bar], 1, (255, 255, 255)) | |
something=True | |
elif self.highBarCount() > 4: | |
displayText=self.font.render("Your campus is outstanding!", 1, (255, 255, 255)) | |
something=True | |
elif self.highBarCount() == 7: | |
displayText=self.font.render("Your campus is number one! Congratulations!", 1, (255, 255, 255)) | |
self.unlocked=True | |
something=True | |
for student in self.studentRoster: | |
if student.isClicked: | |
displayText=self.font.render("Select a location for your student to go", 1, (255, 255, 255)) | |
something=True | |
if self.fireList != []: | |
displayText=self.font.render("Oh no! Your campus is on fire!", 1, (255, 255, 255)) | |
something = True | |
if not something: | |
displayText=self.font.render("Click a button to get started!", 1, (255, 255, 255)) | |
DISPLAYSURF.blit(displayText, (100, 0)) | |
def titleScreenDraw(self): | |
screen=pygame.image.load("titlescreen.bmp") | |
screen.set_colorkey((0,0,0)) | |
DISPLAYSURF.blit(screen, (0,0)) | |
def on_loop(self): #on draw | |
DISPLAYSURF.fill(BLACK) | |
if self.titleScreen: | |
self.titleScreenDraw() | |
self.instructionsDraw() | |
else: | |
self.clues(pygame.mouse.get_pos()) | |
self.mapDraw(self.mapW, self.mapH, | |
self.tileW, self.tileW/2, self.origin) | |
self.statusBarsDraw() | |
self.campusDraw() | |
self.drawButtons() | |
self.tuitionDraw() | |
self.drawFire() | |
if self.isMenu: | |
self.dropMenu.draw() | |
if self.tag !=None: | |
tagDraw=pygame.image.load(self.tag+"tag.bmp") | |
DISPLAYSURF.blit(tagDraw, pygame.mouse.get_pos()) | |
self.endGame() | |
self.drawClickMe() | |
# increases safety status bar based on the number of safewalks | |
def safewalkCount(self): | |
safewalkCount=0 | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y]=="safewalk": | |
safewalkCount+=1 | |
return safewalkCount | |
def statBarKeep(self): | |
for bar in self.statBarList: | |
if bar > 1.0: | |
bar = 1.0 | |
if bar < 0.0: | |
bar = 0.0 | |
#returns the number of trees on the campus | |
def treeCount(self): | |
treeCount=0 | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y]=="tree": | |
treeCount+=1 | |
return treeCount | |
#returns the number of buildings + the quality | |
def buildingUpgradeCount(self): | |
buildingCount=0 | |
building1Count=0 | |
building11Count=0 | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y]=="building": | |
buildingCount+=1 | |
if self.mapGrid[x][y]=="building1": | |
building1Count+=1 | |
if self.mapGrid[x][y]=="building11": | |
building11Count+=1 | |
return (buildingCount, building1Count, building11Count) | |
def foodCount(self): | |
foodCount=0 | |
for x in xrange(self.mapW): | |
for y in xrange(self.mapH): | |
if self.mapGrid[x][y][:4]=="food": | |
foodCount+=1 | |
return foodCount | |
def on_render(self): #on step | |
if not self.titleScreen: | |
self.win() | |
if self.statBarTimers[3]%10==0 and self.statBarTimers[3] != 0: | |
self.changeStudentsDirection() | |
self.moveStudents() | |
self.statBarKeep() | |
if self.statBarTimers[3]%2==0 and self.statBarTimers[3] != 0: | |
self.updateFire() | |
self.deleteFire() | |
for bar in range(len(self.statBarTimers)): | |
self.statBarTimers[bar]+=1 | |
#safety increased by safewalkcount | |
if self.statBarTimers[3]%50==0 and self.statBarTimers[3] != 0: | |
if 0 < self.safewalkCount() <=5: | |
self.statBarList[3] = self.statBarList[3]+(0.003*self.safewalkCount()) | |
elif self.safewalkCount() > 5: | |
self.statBarList[3] += 0.01 | |
#tuition comes in | |
if self.statBarTimers[2]%75 == 0 and self.statBarTimers[2] != 0: | |
self.statBarList[2] = self.statBarList[2] + 0.05*(self.tuition/100000.0) | |
self.statBarList[2] = self.statBarList[2] + 0.03*self.foodCount() | |
#wellness increases trees and building quality | |
if self.statBarTimers[0]%75 == 0 and self.statBarTimers[0] != 0: | |
self.statBarList[0]+=0.004*(self.treeCount()/10.0) | |
self.statBarList[0]+=0.0001*self.buildingUpgradeCount()[0]+0.0003*self.buildingUpgradeCount()[2]+0.0007*self.buildingUpgradeCount()[1] | |
#population increases with prestige | |
if self.statBarTimers[1]%150 == 0 and self.statBarTimers[1] != 0: | |
if self.statBarList > 0.4: | |
self.population += int(100*self.statBarList[1]) | |
else: | |
self.population -= int(100*self.statBaList[1]) | |
self.populationText = self.font.render("population: " + str(self.population), 1, (255, 255, 255)) | |
#prestige increases with the quality of the buildings | |
if self.statBarTimers[1]%75 == 0 and self.statBarTimers[1] != 0: | |
self.statBarList[1]+=0.03*(self.buildingUpgradeCount()[2]) | |
self.statBarList[1]+=0.01*(self.buildingUpgradeCount()[1]) | |
#faculty performance increases with building quality | |
if self.statBarTimers[5]%150 == 0 and self.statBarTimers[5] != 0: | |
self.statBarList[5]+=0.02*(self.buildingUpgradeCount()[2]) | |
self.statBarList[5]+=0.01*(self.buildingUpgradeCount()[1]) | |
self.statBarList[5]+= 0.04*self.facultyCount | |
#diversity increases with the population | |
if self.statBarTimers[4]%100 == 0 and self.statBarTimers[4] != 0: | |
self.statBarList[4] += 0.05*(self.population/10000.0) | |
if self.statBarTimers[bar]%50 == 0 and self.statBarTimers[bar] != 0: | |
if self.statBarList[bar]-0.01 > 0.0: | |
self.statBarList[bar]=self.statBarList[bar]-0.01 | |
#self.statBarTimers[bar]=0 | |
def on_cleanup(self): | |
pygame.quit() | |
def on_execute(self): #runs main game loop | |
if self.on_init() == False: | |
self.running = False | |
while self.running: | |
self.on_loop() #on draw | |
if not self.gameOver: | |
self.on_render()#on step | |
for event in pygame.event.get(): | |
self.on_event(event) | |
pygame.display.update() | |
pygame.display.flip() | |
self.on_cleanup() | |
class Tile(object): | |
def __init__(self, width, height, x, y, gridX, gridY): | |
self.width=width | |
self.height=height | |
self.x=x | |
self.y=y | |
self.gridX=gridX | |
self.gridY=gridY | |
def draw(self): | |
vertices=((self.x+self.width/2, self.y), | |
(self.x+self.width, self.y+self.height/2), | |
(self.x+self.width/2, self.y+self.height), | |
(self.x, self.y+self.height/2)) | |
pygame.draw.polygon(DISPLAYSURF, GREEN, vertices) | |
for vertice in xrange(1, len(vertices)): | |
pygame.draw.aaline(DISPLAYSURF, BLACK, vertices[vertice-1], | |
vertices[vertice], 1) | |
def drawButtons(self): | |
for newButton in self.buttonObjectList: | |
newButton.draw() | |
def clickButtons(self, coords): | |
clickX=coords[0] | |
clickY=coords[1] | |
for item in self.buttonObjectList: | |
if (item.x0 <= clickX <= item.x1) and (item.y0 <= clickY <= item.y1): | |
item.clicked=not item.clicked | |
else: | |
item.clicked=False | |
def mapDraw(self,mapWidth, mapHeight, tileWidth, tileHeight, origin): | |
for x in xrange(mapWidth): | |
for y in xrange(mapHeight): | |
tile=self.Tile(tileWidth, tileHeight, | |
origin[0]+(x-y)*(tileWidth/2), | |
origin[1]+(x+y)*(tileHeight/2), | |
x, y) | |
tile.draw() | |
def createBuilding(self, mapWidth, mapHeight, tileWidth, tileHeight, origin, coord): | |
clickX=coord[0] | |
clickY=coord[1] | |
for x in xrange(mapWidth): | |
for y in xrange(mapHeight): | |
#testing for clicks by dividing tile into 2 absolute value functions | |
left = origin[0]+(x-y)* (tileWidth/2) | |
right = origin[0]+(x-y)* (tileWidth/2)+tileWidth | |
cx = origin[0]+(x-y)*(tileWidth/2)+tileWidth/2 | |
cy = origin[1]+(x+y)*(tileHeight/2)+tileHeight/2 | |
if left <= clickX <= right: | |
if cy <= clickY <= (-0.5*abs(clickX - cx) + cy + tileHeight/2) or (0.5*abs(clickX - cx) + cy - tileHeight/2) <= clickY <= cy: | |
#upgrades a building | |
if self.mapGrid[x][y][:8]=="building" and self.mapGrid[x][y]!="building11": | |
if self.statBarList[2]-0.07 >= 0.0: | |
self.mapGrid[x][y]+="1" | |
self.statBarList[2]-=0.07 #endowment decreases | |
self.statBarTimers[2]=0 | |
elif self.mapGrid[x][y]=="food": | |
self.mapGrid[x][y]="food1" | |
elif self.mapGrid[x][y]=="food1": | |
self.mapGrid[x][y]="food" | |
elif self.mapGrid[x][y]=="empty": | |
for item in self.buttonObjectList: | |
if item.clicked: | |
if item.name[:-6] != "sidewalk": | |
if self.statBarList[2]-0.02 >= 0.0: | |
self.mapGrid[x][y] = item.name[:-6] | |
self.statBarList[2]-=0.02 | |
self.statBarTimers[2]=0 | |
elif item.name[:-6] == "sidewalk": | |
self.mapGrid[x][y] = item.name[:-6] | |
if __name__== "__main__" : | |
mellonto = MellonTopia() | |
mellonto.on_execute() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment