def getText(self, fileContents): """gets the image for each letter in the text given in the layout file""" fileStuff = fileContents.split("\n") #text,xval,yval,words #line 0 starts at A (first letter): ascii 65 #lint 1 starts at N (13th letter) selectionCount = 0 for line in fileStuff: info = line.split(",") if info[0] == "text": text = info[3].upper() #xCenter = (self._worldsize[0] + int(info[1]))/4 - (len(text)//2 * 8) if self._filename == "startmenu.txt": xCenter = 10 else: xCenter = 15 for i in range(len(text)): if text[i] != " ": if text[i] == "!": self._text.append(Drawable("exclamation.png", Vector2(int(xCenter) + 8 * i, int(info[2])), (0,0))) else: aVal = ord(text[i]) numInAlph = aVal - 65 offsetY = numInAlph // 13 offsetX = numInAlph - 13*offsetY self._text.append(Drawable("font.png", Vector2(int(xCenter) + 8 * i, int(info[2])), (2 + offsetX, 7 + offsetY)))
def __init__(self, filename): """intializes a level""" self._filename = filename self._background = Drawable(self.getBackground(), Vector2(0, 0), (0, 0)) self._ground = Drawable(self.getGround(), Vector2(0, 300), (0, 0)) self._blob = Blob(Vector2(0, 300 - CHAR_SPRITE_SIZE.y)) self._decorations = [] self._platforms = [] self._traps = {"bra": [], "pan": [], "ring": []} self._enemies = {"devil": [], "gaston": [], "boss": []} self._powerups = {"floppy": [], "sign": [], "vote": []} self._worldsize = (2400, 400) self._elevator = elevator = Elevator( Vector2(self._worldsize[0] - 50, 300), self._worldsize[1]) if self._filename == "level3.txt": self._ceiling = Ceiling(Vector2(0, 0), final=False) elif self._filename == "level6.txt": self._ceiling = Ceiling(Vector2(0, 0), final=True) else: self._ceiling = None self._deathCycle = 0 self._keydown = {1: False, 2: False, 3: False} self._otherblobs = [] self._otherblobsCollideRect = None self._block = None self._spot = None self._activeBlobs = [] self._downbar = None self._downbarSelections = [] self._selectCount = 0 self._samePlat = 0
def getActiveBlobs(self): """determines if this is a level that the player can switch their controls between""" if self._filename == "level6.txt": self._activeBlobs.append( Blob(Vector2(25, 100 - CHAR_SPRITE_SIZE.y), color="pink")) self._activeBlobs.append( Blob(Vector2(25, self._worldsize[1] - 100 - CHAR_SPRITE_SIZE.y), color="blue")) self._activeBlobs.append( Blob(Vector2(200, self._worldsize[1] - 100 - CHAR_SPRITE_SIZE.y), color="green")) self._activeBlobs.append( Blob(Vector2(300, self._worldsize[1] - 100 - CHAR_SPRITE_SIZE.y), color="orange")) self._blob = self._activeBlobs[0] self._downbar = Drawable("downbar.png", Vector2(0, self._worldsize[1] - 28), (0, 0)) for i in range(4): self._downbarSelections.append( Drawable("downbarselection.png", Vector2(i * 28, self._worldsize[1] - 28), (0, 0)))
def __init__(self, filename): """intializes an animation""" self._filename = filename self._background = Drawable(self.getBackground(), Vector2(0, 0), (0, 0)) self._frame = Drawable("animation_frame.png", Vector2(0, 0), (0, 0)) self._worldsize = (400, 400) self._text = [] self._animationTimer = 0 self._animationTime = 10 self._ready = False
def __init__(self, filename): """intializes a menu""" self._filename = filename self._background = Drawable(self.getBackground(), Vector2(0,0), (0,0)) self._accent = Drawable("menu_accent.png", Vector2(0,45), (0,0)) self._worldsize = (400, 400) self._selectionAreas = [] self._blobs = [] self._text = [] self._selectedBlob = None self._ready = False self._startButton = None
def getOtherBlobs(self, fileContents): """determines if this is a level that has other blobs in it that don't move""" fileStuff = fileContents.split("\n") for line in fileStuff: info = line.split(",") if info[0] == "otherblobs": self._otherblobs.append( Blob(Vector2(int(info[2]), int(info[3]) - CHAR_SPRITE_SIZE.y - 50), color=info[1])) if len(self._otherblobs) != 0: self._otherblobsCollideRect = pygame.Rect(300, 234, 100, 66) self._block = Drawable("block.png", Vector2(300, 250), (0, 0)) self._spot = Drawable("ground.png", Vector2(250, 300), (0, 0))
def getBlobSelectionAreas(self, fileContents): """loads in the selection areas for choosing a blob to play with on a menu""" SELECTION_SIZE = Vector2(112, 112) BLOB_SIZE = Vector2(64, 64) fileStuff = fileContents.split("\n") selectionCount = 0 for line in fileStuff: info = line.split(",") if info[0] == "selection": if self._filename == "blobmenu.txt": self._selectionAreas.append(Drawable("blob_selection.png", Vector2(int(info[1]), int(info[2])), (selectionCount,0))) blobXpos = int(info[1]) + SELECTION_SIZE.x//2 - BLOB_SIZE.x//2 blobYpos = int(info[2]) + SELECTION_SIZE.y//2 - BLOB_SIZE.y//2 - 14 self._blobs.append(Drawable("menu_blobs.png", Vector2(blobXpos, blobYpos), (selectionCount + 1,0))) selectionCount += 1
def plantFlowers(self): """plants flowers randomly for decoration""" flowerSize = 16 for xPos in range(0, 2400, 20): randomNumber = random.randint(10, 13) self._decorations.append( Drawable("nuts_and_milk.png", Vector2(xPos, self._worldsize[1] - 100 - flowerSize), (randomNumber, 8)))
def getStartButton(self, fileContents): """creates a start button with the given position""" fileStuff = fileContents.split("\n") #selection,xval,yval selectionCount = 0 for line in fileStuff: info = line.split(",") if info[0] == "start": xPos = (self._worldsize[0] - int(info[1]))//2 - 64//2 self._startButton = Drawable("startbutton.png", Vector2(xPos, int(info[2])), (0,0))
def __init__(self, position, worldHeight): """initializes an elevator object""" self._position = position self._parts = {"back": [], "front": [], "doors": [], "top": []} totalHeight = worldHeight - (50 + MAIN_SPRITE_SIZE.x) for i in range(0, totalHeight, MAIN_SPRITE_SIZE.y): self._parts["back"].append( Drawable("elevator_back.png", Vector2(self._position.x, i), (0, 0))) for j in range(0, totalHeight - 60, MAIN_SPRITE_SIZE.y): self._parts["front"].append( Drawable("elevator_front.png", Vector2(self._position.x, j), (0, 0))) for k in range(totalHeight - 60, totalHeight, MAIN_SPRITE_SIZE.y): self._parts["doors"].append( Drawable("elevator_doors.png", Vector2(self._position.x, k), (0, 0))) self._parts["top"].append( Drawable("elevator_top.png", Vector2(self._position.x, totalHeight - 60), (0, 0))) self._ranInto = False
def getPlatforms(self, fileContents): """returns the appropriate platform tile image""" if self._filename == "level1.txt": platformImage = "platform.png" elif self._filename == "level2.txt": platformImage = "platform2.png" elif self._filename == "level3.txt" or self._filename == "level6.txt": platformImage = "platform3.png" elif self._filename == "level4.txt" or self._filename == "level5.txt": platformImage = "platform4.png" fileStuff = fileContents.split("\n") for line in fileStuff: info = line.split(",") if info[0] == "platform": for i in range(int(info[3])): self._platforms.append( Drawable(platformImage, Vector2(int(info[1]) + 50 * i, int(info[2])), (0, 0)))
def loadLevel(self): """controls the loading of the level""" file = open(os.path.join("resources", "levels", self._filename)) fileContents = file.read() file.close() self.getWorldSize(fileContents) self._ground = Drawable(self.getGround(), Vector2(0, self._worldsize[1] - 100), (0, 0)) if self._filename != "level6.txt": self._blob = Blob(Vector2( 0, self._worldsize[1] - 100 - CHAR_SPRITE_SIZE.y), color=self._blob._color) self.plantFlowers() self.getPlatforms(fileContents) self.getOtherBlobs(fileContents) self.getActiveBlobs() self.getPowerUps(fileContents) self.getTraps(fileContents) self.getEnemies(fileContents) self.getWorldSize(fileContents)
def main(): # initialize the pygame module pygame.init() # load and set the logo pygame.display.set_caption("Mercury Simulation") # initialize the screen screen = pygame.display.set_mode(list(SCREEN_SIZE)) obstacleList = [] robot = Robot(Vector2(360, 450)) # Let's make a background so we can see if we're moving background = Drawable(Vector2(0, 0), "background.png", offset=None) # Initialize the gameClock for more realistic velocity gameClock = pygame.time.Clock() # define a variable to control the main loop RUNNING = True # main loop while RUNNING: # Draw everything, adjust by offset background.draw(screen) robot.draw(screen) for obstacle in obstacleList: obstacle.draw(screen) # Flip the display to the monitor pygame.display.flip() # event handling, gets all event from the eventqueue for event in pygame.event.get(): # only do something if the event is of type QUIT or ESCAPE is pressed if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): # change the value to False, to exit the main loop RUNNING = False robot.handleEvent(event) if event.type == pygame.MOUSEBUTTONDOWN: adjustedPos = list([int(x) for x in event.pos]) obstacleList.append( Obstacle(Vector2(*background.adjustMousePos(adjustedPos)), "sidewall.png", SCREEN_SIZE)) # if event.key == pygame.K_S: # obstacleList.append(Obstacle(Vector2(*background.adjustMousePos(adjustedPos)), "sidewall.png", SCREEN_SIZE)) # elif event.key == pygame.K_T: # obstacleList.append(Obstacle(Vector2(*background.adjustMousePos(adjustedPos)), "topwall.png", SCREEN_SIZE)) gameClock.tick(60) ticks = gameClock.get_time() / 1000 # Update everything robot.update(WORLD_SIZE, ticks) for obstacle in obstacleList: if robot.getCollideRect().colliderect(obstacle.getCollideRect()): print("You hit the wall") # Update the camera background.updateOffset(robot, SCREEN_SIZE, WORLD_SIZE) pygame.quit()