def createBlock(self, block_id, *args): if block_id == "-": floor = blocks.Platform(args[0], args[1]) self.entities.add(floor) self.platforms.append(floor) elif block_id == "*": spike = blocks.Spike(args[0], args[1]) self.entities.add(spike) self.platforms.append(spike)
def main(): pygame.init() # инициация PyGame screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Super Mario Boy") BACKGROUND = pygame.image.load(os.path.join('fon', '0.jpg')).convert() timer = pygame.time.Clock() level = [ "--------------------------------------", "- -", "- -- - -", "- -", "- -- -", "- - -", "-- ------ -", "- Z -", "- - ---- --- -", "- -", "-- -- -- -", "- -- -", "- --- -", "- -- -- -", "- -- ---", "- --- * -", "- * -", "- ------- ---- -", "- -", "- - -", "-- -- -", "---- * -", "- -", "--------------------------------------" ] player = hero.Hero(40, 50) # создаем персонажа по (x,y) координатам left = right = False # по умолчанию — стоим up = False player2 = hero.Hero(70, 40) entities = pygame.sprite.Group() # все объекты platforms = [] # проверка на поверхность entities.add(player) entities.add(player2) animatedEntities = pygame.sprite.Group( ) # все анимированные объекты, за исключением героя tp = blocks.BlockTeleport(128, 512, 800, 64) entities.add(tp) platforms.append(tp) animatedEntities.add(tp) #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!# x = y = 0 # координаты for row in level: # вся строка for col in row: # каждый символ if col == "-": pf = blocks.Platform(x, y) entities.add(pf) platforms.append(pf) if col == "*": bd = blocks.BlockDie(x, y) entities.add(bd) platforms.append(bd) if col == "Z": pr = blocks.Emblem(x, y) entities.add(pr) platforms.append(pr) animatedEntities.add(pr) x += PLATFORM_WIDTH #блоки платформы ставятся на ширине блоков y += PLATFORM_HEIGHT #то же самое и с высотой x = 0 total_level_width = len( level[0]) * PLATFORM_WIDTH # Высчитываем фактическую ширину уровня total_level_height = len(level) * PLATFORM_HEIGHT # высоту camera = Camera(camera_configure, total_level_width, total_level_height) TimeFor = time.time() ff = True running = False NFOn = 1 Nmonsters = pygame.sprite.Group() # Все передвигающиеся объекты mn1 = monsters.Monster(190, 200, 2, 2, 150, 15) mn2 = monsters.Monster(344, 511, 0, 1, 0, 150) entities.add(mn1) platforms.append(mn1) Nmonsters.add(mn1) entities.add(mn2) platforms.append(mn2) Nmonsters.add(mn2) pygame.joystick.init() while not player.winner: # основной цикл программы timer.tick(60) if time.time() - TimeFor > 5: if NFOn < 8: BACKGROUND = RFon(NFOn) TimeFor = time.time() NFOn += 1 else: NFOn = 0 BACKGROUND = RFon(NFOn) TimeFor = time.time() for e in pygame.event.get(): # обрабатываем события if e.type == pygame.QUIT: sys.exit() if e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT: left = True if e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT: right = True if e.type == pygame.KEYUP and e.key == pygame.K_RIGHT: right = False if e.type == pygame.KEYUP and e.key == pygame.K_LEFT: left = False if e.type == pygame.KEYDOWN and e.key == pygame.K_UP or e.type == pygame.JOYBUTTONDOWN: up = True if e.type == pygame.KEYUP and e.key == pygame.K_UP or e.type == pygame.JOYBUTTONUP: up = False if e.type == pygame.KEYDOWN and e.key == pygame.K_LSHIFT: running = True if e.type == pygame.KEYUP and e.key == pygame.K_LSHIFT: running = False screen.blit(BACKGROUND, (0, 0)) # каждую итерацию необходимо всё перерисовывать #на каждой новой строчке начинаем с нуля camera.update(player) player.update(left, right, up, running, platforms) # player2.update(left,right, up, platforms) #player.draw(screen) #player.collide(screen) #entities.draw(screen) for e in entities: screen.blit(e.image, camera.apply(e)) animatedEntities.update() # показываем анимацию Nmonsters.update(platforms) # передвигаем всех монстров pygame.display.update() # обновление и вывод всех изменений на экран
"- - - -", "- - -", "- -------- -----------", "- -", "- - -", "----------------------------------------------------------", ] # #Координаты для отрисовки пробного уровня x = y = 0 # Цикл, который добавляет в массив platforms все "твёрдые блоки", а также в отрисовку for line in level1: for col in line: if col == "-": # Создаём экземпляр класса pf = blocks.Platform(x, y) # Добавляем его к группе спрайтов entities.add(pf) # Добавляем в массив объектов, с которыми можно сталкиваться platforms.append(pf) elif col == "*": # Создаём экземпляр класса pf = objects.Fire(x, y) # Добавляем его к группе спрайтов bg = blocks.Background(x, y) entities.add(pf) fires.add(pf) bg_blocks.append(bg) # Добавляем в массив объектов, от которых умираем dieblocks.append(pf) elif col == "+":
def levels(level_score, entities, platforms): global COIN1, COIN2, COIN3, COIN4, COIN5, COIN6, COIN7, COIN8, COIN9 level = [''] level1 = [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "x x", "x s k x", "x * c --- --- x", "x s---- s--- --- s x", "x s --- --- x", "x s x", "x --- x", "x s ---s s x", "x --- --- x", "x f x", "x --- s * s---- --- ------x", "x --- ----- s x", "x --- x", "x --- * x", "x * s s--- --- x", "x --- --- n --- x", "x --- - --- - x", "x * - * - - * - * - * x", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"] level2 = [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "x g j x", "x --- s --- s x", "x x", "x --- --- --- --- --- x", "x m x", "x s--- s ---s x", "x x", "x --- --- --- --- --- x", "x s--- x", "x s x", "x x", "x x", "x s s--- x", "x --- --- s x", "x x", "x h x", "x - - - - - --- - - - x", "x **************************************************************************************************x", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"] level3 = [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "x x", "x u x", "x --- --- x", "x --- --- x", "x -b --- --- z x", "x --- --- --- x", "x --- --- x", "x --- --- s x", "x x", "x x", "x x", "x x", "x --- --- x", "x --- --- x", "x s --- --- x", "x --- --- x", "x --- --- x", "x - * - * v *x", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"] level_time = [level1, level2, level3, level] for i in range(len(level_time)): if i == level_score: level = level_time[i] total_level_width = len(level[0]) * PLATFORM_WIDTH total_level_height = len(level) * PLATFORM_HEIGHT camera = Camera(camera_configure, total_level_width, total_level_height) y = 0 x = -64 for row in level: for col in row: if col == "-": pf = blocks.Platform(x, y) entities.add(pf) platforms.append(pf) if col == "*": bd = blocks.BlockDie(x, y) entities.add(bd) platforms.append(bd) if col == "1": pf = blocks.Dirt1(x, y) entities.add(pf) platforms.append(pf) if col == "x": pf = blocks.Barrier(x, y - 64) entities.add(pf) platforms.append(pf) if col == "s": pf = blocks.Stairs(x, y) entities.add(pf) platforms.append(pf) if col == "c": COIN1 = blocks.Coin1(x, y) entities.add(COIN1) platforms.append(COIN1) if col == "n": COIN2 = blocks.Coin2(x, y) entities.add(COIN2) platforms.append(COIN2) if col == "k": COIN3 = blocks.Coin3(x, y) entities.add(COIN3) platforms.append(COIN3) if col == "m": COIN4 = blocks.Coin4(x, y) entities.add(COIN4) platforms.append(COIN4) if col == "h": COIN5 = blocks.Coin5(x, y) entities.add(COIN5) platforms.append(COIN5) if col == "j": COIN6 = blocks.Coin6(x, y) entities.add(COIN6) platforms.append(COIN6) if col == "b": COIN7 = blocks.Coin7(x, y) entities.add(COIN7) platforms.append(COIN7) if col == "v": COIN8 = blocks.Coin8(x, y) entities.add(COIN8) platforms.append(COIN8) if col == "z": COIN9 = blocks.Coin9(x, y) entities.add(COIN9) platforms.append(COIN9) if col == "f": a = blocks.Flag(x, y) entities.add(a) platforms.append(a) if col == "g": a = blocks.Flag2(x, y) entities.add(a) platforms.append(a) if col == "u": a = blocks.Flag3(x, y) entities.add(a) platforms.append(a) x += PLATFORM_WIDTH y += PLATFORM_HEIGHT x = -64 return level, camera
def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("PAOWL'S TRIP") bg = Surface(DISPLAY) hero = player.Player(75 , 75) entities = pygame.sprite.Group() platforms = [] level = [ "----------------------------------", "- -", "- -- -", "- -", "- -- -", "- -", "-- -", "- -", "- ---- --- -", "- -", "-- -", "- -", "- --- -", "- -", "- -", "- --- -", "- -", "- ------- ---- -", "- -", "- - -", "- -- -", "- -", "- -", "----------------------------------"] timer = pygame.time.Clock() bcgd_im = pygame.image.load("bg.png") bcgd_im_2 = pygame.image.load("bg2.png") x = y = 0 for row in level: for col in row: if col == "-": pl = blocks.Platform(x, y, 0) entities.add(pl) platforms.append(pl) #bg.blit(bcgd_im_2 , (x , y)) else: pl = blocks.Platform(x, y , 1) entities.add(pl) #bg.blit(bcgd_im_2 , (x , y)) x += PL_WIDTH y += PL_HEIGHT x = 0 entities.add(hero) total_level_width = len(level[0])*PL_WIDTH # Высчитываем фактическую ширину уровня total_level_height = len(level)*PL_HEIGHT # высоту camera = Camera(camera_configure, total_level_width, total_level_height) run = True left = right = up = down = False while run: timer.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == KEYDOWN and event.key == K_LEFT: left = True right = False up = False down = False if event.type == KEYDOWN and event.key == K_RIGHT: right = True left = False up = False down = False if event.type == KEYUP and event.key == K_RIGHT: right = False left = False up = False down = False if event.type == KEYUP and event.key == K_LEFT: left = False right = False up = False down = False if event.type == KEYDOWN and event.key == K_UP: up = True left = False right = False down = False if event.type == KEYUP and event.key == K_UP: up = False left = False right = False down = False if event.type == KEYDOWN and event.key == K_DOWN: down = True left = False right = False up = False if event.type == KEYUP and event.key == K_DOWN: down = False left = False right = False up = False #screen.blit(bg,camera.apply(blocks.Platform(0,0)))########################################### #entities.draw(screen) for event in entities: screen.blit(event.image, camera.apply(event)) #hero.draw(screen) camera.update(hero) hero.update(left , right , up, down, platforms) pygame.display.update()
def main(): pygame.init() # инициация PyGame screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Super Mario Boy") BACKGROUND = pygame.image.load(os.path.join('fon', 'Afternoon.jpg')).convert() timer = pygame.time.Clock() level = [ "----------------------------------", "- -", "- -- -", "- -", "- -- -", "- -", "-- -", "- -", "- ---- --- -", "- -", "-- -", "- -", "- --- -", "- -", "- -", "- --- -", "- -", "- ------- ---- -", "- -", "- - -", "- -- -", "- -", "- -", "----------------------------------" ] player = hero.Hero(40, 50) # создаем персонажа по (x,y) координатам left = right = False # по умолчанию — стоим up = False player2 = hero.Hero(70, 40) entities = pygame.sprite.Group() # все объекты platforms = [] # поверхности entities.add(player) entities.add(player2) #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!# x = y = 0 # координаты for row in level: # вся строка for col in row: # каждый символ if col == "-": pf = blocks.Platform(x, y) entities.add(pf) platforms.append(pf) x += PLATFORM_WIDTH #блоки платформы ставятся на ширине блоков y += PLATFORM_HEIGHT #то же самое и с высотой x = 0 total_level_width = len( level[0]) * PLATFORM_WIDTH # Высчитываем фактическую ширину уровня total_level_height = len(level) * PLATFORM_HEIGHT # высоту camera = Camera(camera_configure, total_level_width, total_level_height) TimeFor = time.time() ff = True while 1: # основной цикл программы timer.tick(60) if time.time() - TimeFor > 4: if ff: BACKGROUND = pygame.image.load( os.path.join('fon', 'Morning.jpg')).convert() ff = False else: BACKGROUND = pygame.image.load( os.path.join('fon', 'Late Afternoon.jpg')).convert() ff = True TimeFor = time.time() for e in pygame.event.get(): # обрабатываем события if e.type == pygame.QUIT: sys.exit() if e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT: left = True if e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT: right = True if e.type == pygame.KEYUP and e.key == pygame.K_RIGHT: right = False if e.type == pygame.KEYUP and e.key == pygame.K_LEFT: left = False if e.type == pygame.KEYDOWN and e.key == pygame.K_UP: up = True if e.type == pygame.KEYUP and e.key == pygame.K_UP: up = False screen.blit(BACKGROUND, (0, 0)) # каждую итерацию необходимо всё перерисовывать #на каждой новой строчке начинаем с нуля camera.update(player) player.update(left, right, up, platforms) mes = list(UDP.DOWN()) if mes[0] == 0: SL = False else: SL = True if mes[0] == 0: SR = False else: SR = True if mes[0] == 0: SUp = False else: SUp = True player2.update(SL, SR, SUp, platforms) #player.draw(screen) #player.collide(screen) #entities.draw(screen) for e in entities: screen.blit(e.image, camera.apply(e)) pygame.display.update() # обновление и вывод всех изменений на экран
def main(): pygame.init() hero = player.Player(100, 600) left = right = up = False entities = pygame.sprite.Group() entities.add(hero) timer = pygame.time.Clock() main_win = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("THE GAME") bg = pygame.Surface((WIN_WIDTH, WIN_HEIGHT)) bg.fill(pygame.Color(BACKGROUND)) total_level_width = len(level[0]) * PF_WIDTH total_level_height = len(level) * PF_HEIGHT camera = Camera(camera_configure, total_level_width, total_level_height) x = y = 0 for row in level: for col in row: if col == "*": pf = blocks.Platform(x, y) entities.add(pf) platforms.append(pf) x += PF_WIDTH y += PF_HEIGHT x = 0 done = True while done: timer.tick(30) for e in pygame.event.get(): if e.type == pygame.QUIT: done = False if e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT: left = True if e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT: right = True if e.type == pygame.KEYDOWN and e.key == pygame.K_UP: up = True if e.type == pygame.KEYUP and e.key == pygame.K_LEFT: left = False if e.type == pygame.KEYUP and e.key == pygame.K_RIGHT: right = False if e.type == pygame.KEYUP and e.key == pygame.K_UP: up = False bg.fill(pygame.Color(BACKGROUND)) hero.update(left, right, up, platforms) camera.update(hero) for e in entities: bg.blit(e.image, camera.apply(e)) #entities.draw(bg) main_win.blit(bg, (0, 0)) pygame.display.flip() pygame.quit()