def __init__(self, screen, gameSelection=False): self.screen = screen self.gameSelection = gameSelection self.font = load.load_font("Nexa Light", 40) self.timer = pygame.time.Clock() self.selection = 0 self.bg = Surface((32, 32)) self.titleImg = load.load_image("title") self.offset = 175 self.mouse = pygame.mouse s = random.choice("01") self.snowing = int(s) if not self.snowing: r = random.choice("00000001") self.raining = int(r) else: self.raining = False self.rain = Rain() self.snow = Snow() # random trees self.numTrees = random.randint(10, 15) self.trees = pygame.sprite.Group() increment = SCREEN_WIDTH // self.numTrees for t in range(self.numTrees): # better random distribution x = random.randint(t * increment - 5, (t + 1) * increment + 5) img = int(random.choice("111223")) y = SCREEN_HEIGHT + random.randint(10, 60) tree = Tree(x, y, img) self.trees.add(tree)
def __init__(self, screen): self.mouse = pygame.mouse self.timer = pygame.time.Clock() self.screen = screen self.bgColor = (208, 229, 246) self.canvas = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), SRCALPHA, 32) self.screen.set_colorkey(self.bgColor) self.points = [] self.filename = None self.numDrawings = 0 self.slopeColor = (236, 247, 255) self.selection = 0 self.font = load.load_font("Nexa Light", 40) self.drawingMode = False self.selectionMode = True self.loadMode = False self.findingImages = True self.terrains = [] self.platforms = [] # random trees self.numTrees = random.randint(10, 15) self.trees = pygame.sprite.Group() increment = SCREEN_WIDTH // self.numTrees for t in range(self.numTrees): # better random distribution x = random.randint(t * increment - 5, (t + 1) * increment + 5) img = int(random.choice("111223")) y = SCREEN_HEIGHT + random.randint(10, 60) tree = Tree(x, y, img) self.trees.add(tree)
def __init__(self): super().__init__() self.score = 0 self.updatedScore = 0 self.font = load.load_font("Nexa Light", 30) self.image = self.font.render("SCORE: 0", True, ((236, 247, 255))) self.rect = Rect(50, 30, self.image.get_width(), self.image.get_height()) self.rect.left = 100 self.rect.top = 5 # revise these numbers
def drawMessage(screen): msgNum = 0 messages = ["SMASHED INTO ROCK!", "JUMPED OVER ROCK!", "SUCCESSFUL TRICK!"] font = load.load_font("FuturaT_Bold", 20) for i in range(len(Player.msgBools)): if Player.msgBools[i]: msgNum += 1 msg = font.render(messages[i], True, (127, 178, 215)) screen.blit(msg, (SCREEN_WIDTH - 250, SCREEN_HEIGHT - 50 - msgNum * 35)) # if multiple messages have to be blitted msgNum = 0
def __init__(self, pos): """ fps constructor. """ pg.sprite.Sprite.__init__(self, self.containers) self.font = load_font(font_name='casio-fx-702p\casio-fx-702p.ttf', size=70) self.color = (200, 200, 200) self.background_color = (0, 0, 0) self.last_score = -1 self.current_score = 0 self.update() self.rect = self.image.get_rect(center=pos)
def __init__(self, screen, score=0): self.screen = screen self.font = load.load_font("Nexa Light", 40) self.titleFont = load.load_font("Nexa Bold", 50) self.score = score message = "YOUR SCORE WAS " + str(int(self.score)) self.gameover = load.load_image("game over") self.title = self.titleFont.render(message, True, (255, 255, 255)) self.done = False self.bg = Surface((32,32)) self.selection = 0 self.timer = pygame.time.Clock() # random trees self.numTrees = random.randint(10, 15) self.trees = pygame.sprite.Group() increment = SCREEN_WIDTH//self.numTrees for t in range(self.numTrees): # better random distribution x = random.randint(t*increment-5, (t+1)*increment+5) img = int(random.choice("11123")) y = SCREEN_HEIGHT + random.randint(10, 60) tree = Tree(x, y, img) self.trees.add(tree)
def __init__(self, screen, timer): self.screen = screen self.timer = timer self.font = load.load_font("Nexa Light", 20) self.bg = Surface((32, 32)) # random trees self.numTrees = random.randint(10, 15) self.trees = pygame.sprite.Group() increment = SCREEN_WIDTH // self.numTrees for t in range(self.numTrees): # better random distribution x = random.randint(t * increment - 5, (t + 1) * increment + 5) img = int(random.choice("111223")) y = SCREEN_HEIGHT + random.randint(10, 60) tree = Tree(x, y, img) self.trees.add(tree)
def __init__(self): """ fps constructor. """ pg.sprite.Sprite.__init__(self, self.containers) self.font = load_font(font_name='casio-fx-702p\casio-fx-702p.ttf', size=10) self.color = (200, 200, 200) self.background_color = (0, 0, 0) msg = "Fps:" self.fps = 0 self.image = self.font.render(msg, 0, self.color, self.background_color) self.rect = self.image.get_rect( topright=(pg.display.get_surface().get_rect().topright[0] * 24 / 25, 2)) self.visible = False
def setGameOver(self, message="Game Over"): # sets game over, shows a message depending on cause of death self.gameover = True images = [] font = load.load_font("Nexa Light", 30) height = 0 width = 0 for text in message.split("\n"): images.append(font.render(text, True, (25, 51, 71))) height += images[-1].get_height() width = images[-1].get_width() self.gameover_image = pygame.Surface((width, height), SRCALPHA, 32) self.gameover_image.fill((0, 0, 0, 0)) for i in range(len(images)): rect = images[i].get_rect() rect.top = i * images[i].get_height() rect.centerx = width / 2 self.gameover_image.blit(images[i], rect) self.gameover_rect = self.gameover_image.get_rect() self.gameover_rect.center = self.screen.get_rect().center
def run(self): self.done = False self.resetScreen() while not self.done: self.screen.fill(self.bgColor) if self.selectionMode: title = load.load_image("drawing mode") self.screen.blit(title, (0, 20)) self.makeButtons("NEW DRAWING", Draw.NEW) self.makeButtons("LOAD DRAWINGS", Draw.LOAD) self.makeButtons("BACK", Draw.BACK) for tree in self.trees: self.screen.blit(tree.image, tree.rect) if self.drawingMode: font = load.load_font("Nexa Bold", 20) self.screen.blit(load.load_image("instructions"), (100, 0)) # guide dots pygame.draw.circle(self.screen, (159, 194, 252), (3, SCREEN_HEIGHT * 2 // 5 + 20), 5) # the screen_height*2//5 is used in platform also so change pygame.draw.circle( self.screen, (159, 194, 252), (SCREEN_WIDTH - 3, SCREEN_HEIGHT * 2 // 5 + 20), 5) pygame.draw.circle(self.screen, (236, 247, 255), (self.mouse.get_pos()), 5) if self.renderLine: self.render() self.canvas.convert_alpha() self.screen.convert_alpha() self.screen.blit(self.canvas, (0, 0)) if self.numDrawings >= 1: color = Draw.TEXTCOLOR else: color = (250, 250, 255) self.screen.blit( font.render( str(self.numDrawings) + " drawings saved", True, color), (SCREEN_WIDTH - 200, SCREEN_HEIGHT - 30)) if self.loadMode: if self.findingImages: for drawing in range(1, 10): try: name = "terrain" + str(drawing * 100) img = pygame.transform.scale( load.load_image(str(name)), (200, 119)) self.terrains.append(img) except: # no more platform images self.findingImages = False for i in range(len(self.terrains)): x = 240 + (245 * (i % 3)) y = 200 * (i // 3) width = 200 height = 119 rect = Rect(x - 200, y, width, height) self.screen.blit(self.terrains[i], rect) help = self.font.render("Click to select platforms", True, Draw.TEXTCOLOR) help2 = self.font.render("Press P to play", True, Draw.TEXTCOLOR) self.screen.blit(help, (175, SCREEN_HEIGHT - 150)) self.screen.blit(help2, (250, SCREEN_HEIGHT - 100)) pygame.display.update() left_pressed, middle_pressed, right_pressed = mouse.get_pressed() self.timer.tick(60) for e in pygame.event.get(): if e.type == QUIT: pygame.quit() sys.exit() if self.selectionMode: if e.type == KEYDOWN: if e.key == K_UP: self.selection -= 1 if self.selection < 0: self.selection = 0 if e.key == K_DOWN: self.selection += 1 if self.selection > Draw.BACK: self.selection = Draw.BACK if self.selection < 0: self.selction = 0 if e.key == K_RETURN: self.selectionMode = False if self.selection == 0: self.drawingMode = True elif self.selection == 1: self.loadMode = True else: self.done = True if self.drawingMode: if left_pressed: pygame.draw.circle(self.canvas, self.slopeColor, pygame.mouse.get_pos(), 2) self.points.append((pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] - 5)) elif e.type == KEYDOWN and e.key == K_r: self.renderLine = not self.renderLine elif e.type == KEYDOWN and e.key == K_p: self.done = True elif e.type == KEYDOWN and (e.key == K_s): if self.points != [] and self.renderLine: # can't save empty drawing or dots self.filename = ("terrain%s.png" % (str( (self.numDrawings + 1) * 100))) pygame.image.save(self.canvas, self.filename) self.numDrawings += 1 # num drawings only increases after a save print("file %s has been saved" % str(self.filename)) elif e.type == KEYDOWN and e.key == K_n: self.resetScreen() if self.loadMode: # this feature is mostly for demo purposes if left_pressed: if 20 < pygame.mouse.get_pos()[1] < 120: if 40 < pygame.mouse.get_pos()[0] < 220: if "1" not in self.platforms: self.platforms.append("1") elif 240 < pygame.mouse.get_pos()[0] < 460: if "2" not in self.platforms: self.platforms.append("2") elif pygame.mouse.get_pos()[0] > 520: if "3" not in self.platforms: self.platforms.append("3") if 220 < pygame.mouse.get_pos()[1] < 320: if 40 < pygame.mouse.get_pos()[0] < 220: if "4" not in self.platforms: self.platforms.append("4") elif 240 < pygame.mouse.get_pos()[0] < 460: if "5" not in self.platforms: self.platforms.append("5") elif pygame.mouse.get_pos()[0] > 520: if "6" not in self.platforms: self.platforms.append("6") elif e.type == KEYDOWN and e.key == K_p: if self.platforms != []: self.done = True for num in self.platforms: if int(num) > len(self.terrains): self.platforms.remove(num) if self.drawingMode: return self.numDrawings elif self.loadMode: return self.platforms else: return None
def loadIcon(self): self.blood1 = load.load_image("blood1.png") self.blood2 = load.load_image("blood2.png") self.sparkIcon = load.load_image("sparkIcon.png") self.font = load.load_font("Transformers.ttf",25)
def __init__(self, screen, timer, custom=False): self.screen = screen self.timer = timer '''Game Mode''' if custom != False: # 0, if no platforms are drawn self.custom = True if isinstance(custom, list): self.customPlatTypes = custom self.customNumPlatforms = None else: self.customNumPlatforms = custom self.customPlatTypes = None else: self.custom = False '''Game Mechanics''' self.totalGameTime = 1000 self.gameover = False self.gameover_image = None self.gameover_rect = None self.pause = False self.done = False font = load.load_font("OpenSans-Semibold", 40) self.pause_image = font.render("Paused", True, (236, 247, 255)) self.pause_rect = self.pause_image.get_rect() self.pause_rect.center = self.screen.get_rect().center self.exit = True self.showFPS = False self.noCrashes = False '''Background''' self.bg = Surface((32, 32)) self.bgTime = 0 self.bgColor = Game.bgColor self.sundownTime = 0 self.rainStart = random.randint(0, self.totalGameTime) self.rainEnd = self.rainStart + random.randint(150, 500) self.rainEndColor = None self.darkenTime = (self.rainEnd - self.rainStart) / 3 self.lightenTime = 2 * (self.rainEnd - self.rainStart) / 3 self.raining = self.darkening = self.lightening = False self.snowing = False self.snow = Snow() '''Game entities''' self.entities = pygame.sprite.Group() self.rocks = pygame.sprite.Group() self.trees = pygame.sprite.Group() self.birds = pygame.sprite.Group() self.coins = pygame.sprite.Group() self.ice = pygame.sprite.Group() self.player = Player() self.entities.add(self.player) self.coinCount = 0 self.flyingBirds = [] self.collectedCoins = [] self.clouds = None '''Score''' self.score = Score() self.scoreSprite = pygame.sprite.Group() self.scoreSprite.add(self.score) '''Platforms''' self.platforms = [] self.numPlatforms = 30 self.generatePlatforms() self.createPlatforms() '''Camera''' self.totalWidth = 1024 * len(self.platformTypes) self.totalHeight = 800 self.camera = Camera(complex_camera, self.totalWidth, self.totalHeight) '''Generating obstacles''' self.iceCoords = makeIce(self.platforms) rockInfo = makeRock(self.platforms) self.treeDict = makeTrees(self.platforms) self.birdDict = generateBirds(self.platforms) self.coinDict = makeCoins(self.platforms) (self.rockRects, self.rockCoords, self.rockAngles) = \ (rockInfo[0], rockInfo[1], rockInfo[2]) # ROCKS offset = 1024 for item in range(1, len(self.rockRects)): currentPlatform = self.platforms[item] x = self.rockRects[currentPlatform][0] + offset y = self.rockRects[currentPlatform][1] angle = self.rockAngles[currentPlatform] obstacle = random.choice("001") rock = Rock(x, y, angle) if obstacle == "0" else Penguin( x, y, angle) self.rocks.add(rock) offset += 1024 # ICE iceOffset = 0 for item in range(self.numPlatforms): currentPlatform = self.platforms[item] x = iceOffset ice = Ice(x, 299, currentPlatform, self.iceCoords) self.ice.add(ice) iceOffset += 1024 # TREES offset = 0 for item in range(len(self.treeDict)): currentPlatform = self.platforms[item] treeCoords = self.treeDict[currentPlatform] for tree in treeCoords: x = offset + tree[0] yOffset = random.randint(5, 20) y = tree[1] + 300 + yOffset tree = Tree(x, y) self.trees.add(tree) offset += 1024 # BIRBS & COINS offset = 0 for item in range(self.numPlatforms): currentPlatform = self.platforms[item] coords = self.birdDict[currentPlatform] coinCoords = self.coinDict[currentPlatform] if coords != None: for point in coords: x = offset + point[0] y = point[1] bird = Bird(x, y, 0) self.birds.add(bird) if coinCoords != None: for coin in coinCoords: x = offset + coin[0] y = coin[1] coin = Coin(x, y) self.coins.add(coin) offset += 1024
def draw(self): self.bg.convert() if not self.pause: self.bgTime += 1 if self.bgTime + 1 == self.totalGameTime: print(self.player.rect.right // 1024) self.exit = False self.bgTime += 1 # avoid an endless loop self.setGameOver("Time's Up! Press ENTER to exit.") if self.bgTime % 5 == 0: if self.darkening or self.lightening or self.raining: self.bgColor = self.rainSky() else: self.bgColor = self.getTimeColor() color = (self.bgColor[0], self.bgColor[1], self.bgColor[2]) try: self.bg.fill(color) except: self.bg.fill((59, 82, 119)) # blit the background for y in range(32): for x in range(32): self.screen.blit(self.bg, (x * 32, y * 32)) # draw the sun based on "time of day" sun = pygame.transform.scale(load.load_image("sun"), (150, 150)) if 50 + (SCREEN_HEIGHT) * (self.bgTime / self.totalGameTime) <= 400: self.screen.blit(sun, (SCREEN_WIDTH - 150 - 50 * (self.bgTime / self.totalGameTime), 50 + (SCREEN_HEIGHT) * (self.bgTime / self.totalGameTime))) if abs((50 + (SCREEN_HEIGHT) * (self.bgTime / self.totalGameTime)) - 250) <= 2: self.sundownTime = self.bgTime # draw game entities for tree in self.trees: self.screen.blit(tree.image, self.camera.apply(tree)) for e in self.entities: self.screen.blit(e.image, self.camera.apply(e)) for ice in self.ice: self.screen.blit(ice.image, self.camera.apply(ice)) for rock in self.rocks: self.screen.blit(rock.image, self.camera.apply(rock)) for bird in self.birds: self.screen.blit(bird.image, self.camera.apply(bird)) for coin in self.coins: self.screen.blit(coin.image, self.camera.apply(coin)) self.scoreSprite.draw(self.screen) clouds.draw(self.screen) player.drawMessage(self.screen) if self.snowing: self.snow.draw(self.screen) self.checkForRain() particles.draw(self.screen, self.camera) # Draw game info font = load.load_font("Nexa Light", 30) fps = font.render("FPS: " + str(int(self.timer.get_fps())), True, Game.textColor) speed = font.render("Speed: " + str(int(self.groundSpeed)), True, Game.textColor) if self.showFPS: self.screen.blit(fps, (50, 60)) self.screen.blit(speed, (SCREEN_WIDTH - 200, 30)) timeLeft = "Time Left: " timer = font.render(timeLeft, True, Game.textColor) rect = timer.get_rect() rect.right = int(self.screen.get_rect().centerx) rect.top = 30 self.screen.blit(timer, rect) time = font.render(str(self.totalGameTime - self.bgTime), True, Game.textColor) rect = time.get_rect() rect.left = int(self.screen.get_rect().centerx) rect.top = 30 self.screen.blit(time, rect) if self.gameover: if self.exit == False: # when time runs out self.pause = True self.screen.blit(self.gameover_image, self.gameover_rect) else: self.done = True pygame.display.update()