def showPauseMenu(self, screen): menuRect = pygame.Rect(0,0, 400, 400) menuRect.center = screen.get_rect().center self.selections = ["RESUME", "SAVE GAME", "MAIN MENU", "QUIT"] selectionsTexts = [] selectionsRects = [] menuFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 40) for index, item in enumerate(self.selections): text = menuFont.render(item, True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.y = menuRect.top + 30 + index * 60 selectionsTexts.append(text) selectionsRects.append(textRect) self.selectionsRects = selectionsRects highlightRect = selectionsRects[self.currentSelection].copy() highlightRect.top = selectionsRects[self.currentSelection].bottom highlightRect.height = 2 pygame.draw.rect(screen, (0,254,253), highlightRect, 0) for i, text in enumerate(selectionsTexts): screen.blit(text, selectionsRects[i])
def renderNearestPlanetGUI(self, screen, camera): # GUI - Object desc color = (0,254,253) # heading guiFont = pygame.font.Font(Config.getFile(Config.neuropol), 18) heading = guiFont.render("Nearest Planet", True, color) headingRect = pygame.Rect(screen.get_rect().left + 10, screen.get_rect().bottom - 200, 400, 30) screen.blit(heading, (headingRect.left + 10, headingRect.top + 7)) #border descRect = pygame.Rect(headingRect.left, headingRect.bottom + 10, 400, 150) borderRect = descRect.copy() borderRect.height = 2 pygame.draw.rect(screen, color, borderRect, 0) # planet name closestPlanet = None closestDistanceSq = 0 for planet in self.planets: distanceToPlanetSq = (self.probe.position - planet.position).magnitude_squared() if closestPlanet is None or distanceToPlanetSq < closestDistanceSq: closestPlanet = planet closestDistanceSq = distanceToPlanetSq font = pygame.font.Font(Config.getFile(Config.ethnocentric), 30) text = font.render(closestPlanet.name, True, color) screen.blit(text, (descRect.left + 10, descRect.top + 10)) font = pygame.font.Font(Config.getFile(Config.neuropol), 17) text = font.render("Distance : " + str(int(math.sqrt(closestDistanceSq))), True, color) screen.blit(text, (descRect.left + 10, descRect.top + 60)) hasHumanPresence = "Yes" if closestPlanet.zoneRadius > 0 else "No" text = font.render("Human Presence : " + hasHumanPresence, True, color) screen.blit(text, (descRect.left + 10, descRect.top + 85)) text = font.render("Distance From Sun: " + str(int(closestPlanet.distance)) + " AU", True, color) screen.blit(text, (descRect.left + 10, descRect.top + 110)) originalImage = closestPlanet.originalImage zoomedImage = pygame.transform.smoothscale(originalImage, (100,100)) probeRect = zoomedImage.get_rect() probeRect.topright = (descRect.right - 10, descRect.top + 10) screen.blit(zoomedImage, probeRect)
def __init__(self): self.mass = 33300 self.position = Vector2(0,0) self.size = 15 self.imageSize = Vector2(self.size*2, self.size*2) self.zoom = 0 self.originalImage = pygame.image.load(Config.getFile("img/planets/sun.png")).convert_alpha() self.tempImage = pygame.transform.smoothscale(self.originalImage, map(int, (self.imageSize * self.zoom))) self.rect = self.tempImage.get_rect()
def renderObjectDescGUI(self, screen): # GUI - Object desc color = (0,254,253) windowSize = Vector2(400, 150) # heading guiFont = pygame.font.Font(Config.getFile(Config.neuropol), 18) heading = guiFont.render("Object description", True, color) headingRect = pygame.Rect(screen.get_rect().right - 10 - windowSize.x, screen.get_rect().bottom - 200, windowSize.x, 30) screen.blit(heading, (headingRect.left + 10, headingRect.top + 7)) descRect = pygame.Rect(headingRect.left, headingRect.bottom + 10, windowSize.x, windowSize.y) # border borderRect = descRect.copy() borderRect.height = 2 pygame.draw.rect(screen, color, borderRect, 0) # object descriptions font = pygame.font.Font(Config.getFile(Config.ethnocentric), 30) text = font.render("FR-71", True, color) screen.blit(text, (descRect.left + 10, descRect.top + 10)) font = pygame.font.Font(Config.getFile(Config.neuropol), 17) text = font.render("Health : " + str(self.probe.health) + "/" + str(self.probe.maxHealth), True, color) screen.blit(text, (descRect.left + 10, descRect.top + 60)) text = font.render("Fuel : " + str(round(self.probe.fuel, 1)) + "/" + str(self.probe.fuelCapacity), True, color) screen.blit(text, (descRect.left + 10, descRect.top + 85)) text = font.render("Speed: " + str(int(self.probe.speed.magnitude() * 100)) + "/" + str(self.probe.maxSpeed * 100), True, color) screen.blit(text, (descRect.left + 10, descRect.top + 110)) # object image originalImage = pygame.image.load(Config.getFile("img/rocket4.png")).convert_alpha() imageSize = Vector2(173, 291) * 0.5 zoomedImage = pygame.transform.smoothscale(originalImage, map(int, (imageSize))) probeRect = zoomedImage.get_rect() probeRect.topright = (descRect.right - 10, descRect.top + 10) screen.blit(zoomedImage, probeRect)
def __init__(self): super(TitleScene, self).__init__() # generate bg stars self.bgStars = [] for i in range(0, 100): self.bgStars.append(Vector2(random.randrange(0, Config.getScreenSize()[0]), random.randrange(0, Config.getScreenSize()[1]))) # menu manager self.menuManager = MenuManager(MainMenu(), self) # bg music pygame.mixer.music.load(Config.getFile(Config.titleBgMusic)) pygame.mixer.music.play(-1) # loop forever
def __init__(self): super(IntroScene, self).__init__() # generate bg stars self.bgStars = [] for i in range(0, 100): self.bgStars.append(Vector2(random.randrange(0, Config.getScreenSize()[0]), random.randrange(0, Config.getScreenSize()[1]))) # bg music pygame.mixer.music.load(Config.getFile(Config.introBgMusic)) pygame.mixer.music.play(-1) # loop forever # Render Lines self.fontColor = (255,255,0) self.titleFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 40) self.font = pygame.font.Font(Config.getFile(Config.ethnocentric), 20) self.lineHeight = 55 self.lines = [ "HOME DEFENCE", "", "It is a period of space war. Alien", "spaceships, striking from a hidden base,", "have conquered the planets of our solar", "system.", "", "During the battle, alien spies managed", "to destroy our colonies of Uranus and", "Neptune, using armoured space station", "called DEATH STAR.", "", "We must act fast to stop the aliens from", "destroying all of our homeland!" ] # start scrolling from the bottom of the screen self.y = Config.getScreenSize()[1] self.scrollSpeed = 50
def render(self, screen, camera): if self.zoom != camera.zoom: self.zoomedImage = pygame.transform.smoothscale(self.originalImage, map(int, (self.imageSize * camera.zoom))) self.tempImage = pygame.transform.rotate(self.zoomedImage, self.direction) self.rect = self.tempImage.get_rect() self.zoom = camera.zoom if abs(self.directionChange) > 0: oldCenter = self.rect.center self.tempImage = pygame.transform.rotate(self.zoomedImage, self.direction) self.rect = self.tempImage.get_rect() self.rect.center = oldCenter self.directionChange = 0 if self.isHit: self.tempImage.fill((255,0,0), special_flags=pygame.BLEND_RGBA_MULT) self.rect.center = camera.convertCoordinates(self.position) screen.blit(self.tempImage, self.rect) if self.isHit: self.zoomedImage = pygame.transform.scale(self.originalImage, map(int, (self.imageSize * camera.zoom))) self.tempImage = pygame.transform.rotate(self.zoomedImage, self.direction) self.isHit = False for bullet in self.bullets: bullet.blit(screen, camera) # health bar and info maxWidth = 0.02 * camera.zoom * self.maxHealth healthRect = pygame.Rect((self.rect.topleft), (maxWidth, 2)) healthRect.bottom = self.rect.top - 4 healthRect.centerx = self.rect.centerx oldLeft = healthRect.left healthRect.width = maxWidth * self.health / self.maxHealth healthRect.left = oldLeft pygame.draw.rect(screen, (0,255,0), healthRect, 0) font = pygame.font.Font(Config.getFile(Config.ethnocentric), int(round(0.4 * camera.zoom))) text = font.render("HP:" + str(self.health), True, (255,255,255)) textRect = text.get_rect() textRect.left = healthRect.left textRect.bottom = healthRect.top - 1 screen.blit(text, textRect)
def render(self, screen): screen.fill((0,0,0)) # render bg stars for star in self.bgStars: position = map(int, star) randomVal = random.randrange(150, 255) color = (randomVal,randomVal,randomVal) pygame.gfxdraw.pixel(screen, position[0], position[1], (randomVal,randomVal,randomVal)) # Render Game title titleFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 80) titleText = titleFont.render("PLANET WARS", True, (255,255,255)) titleTextRect = titleText.get_rect() titleTextRect.centerx = screen.get_rect().centerx titleTextRect.y = 40 screen.blit(titleText, titleTextRect) # Render Menu self.menuManager.menu.render(screen)
def render(self, screen): selectionsTexts = [] selectionsRects = [] menuFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 40) for i, selection in enumerate(self.selections): text = menuFont.render(selection, True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.y = 180 + i * 60 selectionsTexts.append(text) selectionsRects.append(textRect) self.selectionsRects = selectionsRects highlightRect = selectionsRects[self.currentSelection].copy() highlightRect.height = 2 highlightRect.top = selectionsRects[self.currentSelection].bottom pygame.draw.rect(screen, (0,254,253), highlightRect, 0) for i, text in enumerate(selectionsTexts): screen.blit(text, selectionsRects[i])
def render(self, screen): super(SavedGamesMenu, self).render(screen) texts = [] rects = [] menuFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 20) text = menuFont.render("ENTER - Load saved game", True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().width / 4 textRect.bottom = screen.get_rect().bottom - 20 texts.append(text) rects.append(textRect) text = menuFont.render("BACKSPACE - Delete saved game", True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().width / 4 * 3 textRect.bottom = screen.get_rect().bottom - 20 texts.append(text) rects.append(textRect) for i, text in enumerate(texts): screen.blit(text, rects[i])
def __init__(self, position, speed, direction, imageSize, size, imageFile, health, maxHealth, thrust): self.position = position self.speed = speed self.direction = direction self.gravitationalPull = Vector2(0,0) self.directionChange = 0 self.thrust = thrust self.imageSize = imageSize self.size = size # for collision purpose self.originalImage = pygame.image.load(Config.getFile(imageFile)).convert_alpha() self.zoom = 0 self.zoomedImage = pygame.transform.smoothscale(self.originalImage, map(int, (self.imageSize * self.zoom))) self.tempImage = pygame.transform.rotate(self.zoomedImage, self.direction) self.rect = self.tempImage.get_rect() self.maxHealth = maxHealth self.health = health self.isHit = False self.bullets = []
def render(self, screen): super(ControlsMenu, self).render(screen) controlsTexts = [] controlsRects = [] menuFont = pygame.font.Font(Config.getFile(Config.ethnocentric), 15) for i, control in enumerate(self.controls1): text = menuFont.render(control, True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().width / 4 textRect.y = 250 + i * 30 controlsTexts.append(text) controlsRects.append(textRect) for i, control in enumerate(self.controls2): text = menuFont.render(control, True, (255,255,255)) textRect = text.get_rect() textRect.centerx = screen.get_rect().width / 4 * 3 textRect.y = 250 + i * 30 controlsTexts.append(text) controlsRects.append(textRect) for i, text in enumerate(controlsTexts): screen.blit(text, controlsRects[i])
def save(self): s = shelve.open(Config.getFile(Config.savedGamesDB)) appended = s["savedGames"] appended.append(self) s["savedGames"] = appended s.close()
def loadAll(): db = shelve.open(Config.getFile(Config.savedGamesDB)) savedGames = db["savedGames"] db.close() return savedGames
def saveBulk(savedGames): db = shelve.open(Config.getFile(Config.savedGamesDB)) db["savedGames"] = savedGames db.close()