def state_over(): global score background = objects.Background() gameover = objects.StaticImage(0.5, 0.4, WIDTH, HEIGHT, 'gameover') score_text = arial_font.render('FINAL SCORE: ' + str(score), False, (0, 0, 0)) score_rect = score_text.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return STATE_QUIT keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: return STATE_QUIT if keys[pygame.K_RETURN]: score = 0 return STATE_MENU background.draw(screen) gameover.draw(screen) screen.blit(score_text, (0.5 * WIDTH - 0.5 * score_rect.w, 0.6 * HEIGHT)) pygame.display.update()
def state_game(): global score pygame.mixer.music.play(-1) counter = 1 players = make_players() geese = [] background = objects.Background() warning = objects.StaticImage(0.5, 0.25, WIDTH, HEIGHT, 'warning') goose_interval = GOOSE_INTERVAL_BASE while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return STATE_QUIT background.update() keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: return STATE_QUIT if counter >= goose_interval: geese.append(objects.Goose(WIDTH, HEIGHT)) goose_interval = GOOSE_INTERVAL_BASE + random.randrange( -GOOSE_INTERVAL_SPREAD / 2, GOOSE_INTERVAL_SPREAD / 2) goose_interval *= math.exp(-SPEEDUP_RATE * score) counter = 0 for player in players: player_jump = keys[player.jump_key] player.update(player_jump) for goose in geese: goose.update() for player in players: if player.rect.collidelist( list(map(lambda goose: goose.hitbox, geese))) != -1: pygame.mixer.music.stop() library.sound_library['goose'].play() return STATE_OVER background.draw(screen) for player in players: player.draw(screen) for goose in geese: goose.draw(screen) if counter >= 0.8 * goose_interval: warning.draw(screen) score_text = arial_font.render('SCORE: ' + str(score), False, (0, 0, 0)) screen.blit(score_text, (0.1 * WIDTH, 0.1 * HEIGHT)) pygame.display.update() clock.tick(60) counter += 1 score += 1
def state_menu(): background = objects.Background() menu = objects.StaticImage(0.5, 0.5, WIDTH, HEIGHT, 'menu') while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return STATE_QUIT keys = pygame.key.get_pressed() if keys[pygame.K_q]: return STATE_GAME if keys[pygame.K_ESCAPE]: return STATE_QUIT background.draw(screen) menu.draw(screen) pygame.display.update()
def loadLevel(self, level): print("level loading") levelfile = open(self.levelDir + "/" + self.levelList[level]) levelDict = json.load(levelfile) levelfile.close() self.assetsDir = levelDict[ "assetsDir"] if "assetsDir" in levelDict else "assets\\" self.spriteSheet = objs.spritesheet( levelDict["sprite sheet"] ) if "sprite sheet" in levelDict else None #check for a sprite sheet otherwise none self.physics = levelDict["physics"] if "physics" in levelDict else { "gravity": 100 } #check if physics are set self.player = objs.Player(levelDict["player"], levelDict["assetsDir"]) self.objects = [] for x in levelDict["objects"]: self.objects.append( objs.Sprite(x, assetdir=self.assetsDir, ss=self.spriteSheet)) self.background = [] if "backgrounds" in levelDict: for x in levelDict["backgrounds"]: self.background.append(objs.Background(x, self.assetsDir)) self.background = sorted(self.background, key=lambda x: x.distance, reverse=True) map = levelDict["map"] map = map[::-1] for y in range(len(map)): for x in range(len(map[y])): if map[y][x] != 0: self.objects[map[y][x] - 1].instances.append([x, y])
import pygame, sys from pygame.locals import * import objects pygame.init() white = (255,255,255) black = (0,0,0) winSize =(900,600) window = objects.Background(winSize,white,"./img/bg.png") node = objects.Node(winSize,"./img/ball.png") paddle = objects.Paddle(winSize,(20,110),0) comPaddle = objects.Paddle(winSize,(20,110),1) score = objects.Score() clock = pygame.time.Clock() done = True while done: window.drawBgImg() for event in pygame.event.get(): if event.type == QUIT: done = False paddle.getEvent(event) node.collision(comPaddle.targetCollision(node) or paddle.targetCollision(node), winSize[0], winSize[1])
if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() levels.FLOOR_LEVEL_LIST = levels.update_floor_height() for bg in objects.BG_LIST: bg.update() keys.process_inputs() for object in objects.STATIC_LIST: object.update() player.update() resized_window = pygame.transform.scale(screen.DISPLAY, (screen.REAL_WIDTH, screen.REAL_HEIGHT)) screen.SCREEN.blit(resized_window, (0, 0)) pygame.display.update() CLOCK.tick(screen.FPS) bg = objects.Background(animations.bg, 0, 0, velX=0.2, falls=False) bg2 = objects.Background(animations.bg, - screen.WIDTH, 0, velX=0.2, falls=False) player = objects.Player(animations.playerAnimationRepertoire, screen.WIDTH/2, screen.HEIGHT/2, 0, 0, 0.5) for i in range(4): objects.Static(animations.palmAnimationRepertoire, random.randint(0, screen.WIDTH), screen.HEIGHT - 21, falls=True, collisions=False) for i in range(10): objects.Static(animations.box1AnimationRepertoire, random.randint(0, screen.WIDTH), random.randint(0, levels.FLOOR_LEVEL), falls=False) keys = Key_commands() if __name__ == '__main__': playing = True gameloop() pygame.quit()