MOVE_EXTRA_SPEED = 2.5 # JUMP_EXTRA_POWER = 1 # ANIMATION_SUPER_SPEED_DELAY = 0.05 #
# boltAnim = [] boltAnimSuperSpeed = [] for anim in ANIMATION_RIGHT: boltAnim.append((anim, ANIMATION_DELAY)) boltAnimSuperSpeed.append((anim, ANIMATION_SUPER_SPEED_DELAY)) self.boltAnimRight = pyganim.PygAnimation(boltAnim) self.boltAnimRight.play() self.boltAnimRightSuperSpeed = pyganim.PygAnimation(boltAnimSuperSpeed) self.boltAnimRightSuperSpeed.play() # boltAnim = [] boltAnimSuperSpeed = [] for anim in ANIMATION_LEFT: boltAnim.append((anim, ANIMATION_DELAY)) boltAnimSuperSpeed.append((anim, ANIMATION_SUPER_SPEED_DELAY)) self.boltAnimLeft = pyganim.PygAnimation(boltAnim) self.boltAnimLeft.play() self.boltAnimLeftSuperSpeed = pyganim.PygAnimation(boltAnimSuperSpeed) self.boltAnimLeftSuperSpeed.play()
def update(self, left, right, up, running, platforms):
if up: if self.onGround: # , self.yvel = -JUMP_POWER if running and (left or right): # self.yvel -= JUMP_EXTRA_POWER # self.image.fill(Color(COLOR)) self.boltAnimJump.blit(self.image, (0, 0)) if left: self.xvel = -MOVE_SPEED # = x- n self.image.fill(Color(COLOR)) if running: # self.xvel-=MOVE_EXTRA_SPEED # if not up: # self.boltAnimLeftSuperSpeed.blit(self.image, (0, 0)) # else: # if not up: # self.boltAnimLeft.blit(self.image, (0, 0)) # if up: # self.boltAnimJumpLeft.blit(self.image, (0, 0)) # if right: self.xvel = MOVE_SPEED # = x + n self.image.fill(Color(COLOR)) if running: self.xvel+=MOVE_EXTRA_SPEED if not up: self.boltAnimRightSuperSpeed.blit(self.image, (0, 0)) else: if not up: self.boltAnimRight.blit(self.image, (0, 0)) if up: self.boltAnimJumpRight.blit(self.image, (0, 0))
running = False *** if e.type == KEYDOWN and e.key == K_LSHIFT: running = True *** if e.type == KEYUP and e.key == K_LSHIFT: running = False
hero.update(left, right, up, running, platforms)
class BlockDie(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image = image.load("%s/blocks/dieBlock.png" % ICON_DIR)
def die(self): time.wait(500) self.teleporting(self.startX, self.startY) # def teleporting(self, goX, goY): self.rect.x = goX self.rect.y = goY
*** if isinstance(p, blocks.BlockDie): # - blocks.BlockDie self.die()# ***
level = [ "----------------------------------", "- -", "- -- -", "- * -", "- -", "- -- -", "-- -", "- -", "- ---- --- -", "- -", "-- -", "- * -", "- --- -", "- -", "- -", "- * --- * -", "- -", "- ------- ---- -", "- -", "- - -", "- -- -", "- *** -", "- -", "----------------------------------"]
if col == "*": bd = BlockDie(x,y) entities.add(bd) platforms.append(bd)
ANIMATION_BLOCKTELEPORT = [ ('%s/blocks/portal2.png' % ICON_DIR), ('%s/blocks/portal1.png' % ICON_DIR)]
class BlockTeleport(Platform): def __init__(self, x, y, goX,goY): Platform.__init__(self, x, y) self.goX = goX # self.goY = goY # boltAnim = [] for anim in ANIMATION_BLOCKTELEPORT: boltAnim.append((anim, 0.3)) self.boltAnim = pyganim.PygAnimation(boltAnim) self.boltAnim.play() def update(self): self.image.fill(Color(PLATFORM_COLOR)) self.boltAnim.blit(self.image, (0, 0))
*** elif isinstance(p, blocks.BlockTeleport): self.teleporting(p.goX, p.goY) ***
animatedEntities = pygame.sprite.Group() # ,
tp = BlockTeleport(128,512,800,64) entities.add(tp) platforms.append(tp) animatedEntities.add(tp)
animatedEntities.update() #
#!/usr/bin/env python # -*- coding: utf-8 -*- from pygame import * import pyganim import os MONSTER_WIDTH = 32 MONSTER_HEIGHT = 32 MONSTER_COLOR = "#2110FF" ICON_DIR = os.path.dirname(__file__) # ANIMATION_MONSTERHORYSONTAL = [('%s/monsters/fire1.png' % ICON_DIR), ('%s/monsters/fire2.png' % ICON_DIR )] class Monster(sprite.Sprite): def __init__(self, x, y, left, up, maxLengthLeft,maxLengthUp): sprite.Sprite.__init__(self) self.image = Surface((MONSTER_WIDTH, MONSTER_HEIGHT)) self.image.fill(Color(MONSTER_COLOR)) self.rect = Rect(x, y, MONSTER_WIDTH, MONSTER_HEIGHT) self.image.set_colorkey(Color(MONSTER_COLOR)) self.startX = x # self.startY = y self.maxLengthLeft = maxLengthLeft # , self.maxLengthUp= maxLengthUp # , , self.xvel = left # c , 0 - self.yvel = up # , 0 - boltAnim = [] for anim in ANIMATION_MONSTERHORYSONTAL: boltAnim.append((anim, 0.3)) self.boltAnim = pyganim.PygAnimation(boltAnim) self.boltAnim.play() def update(self, platforms): # self.image.fill(Color(MONSTER_COLOR)) self.boltAnim.blit(self.image, (0, 0)) self.rect.y += self.yvel self.rect.x += self.xvel self.collide(platforms) if (abs(self.startX - self.rect.x) > self.maxLengthLeft): self.xvel =-self.xvel # , if (abs(self.startY - self.rect.y) > self.maxLengthUp): self.yvel = -self.yvel # , , def collide(self, platforms): for p in platforms: if sprite.collide_rect(self, p) and self != p: # - - self.xvel = - self.xvel # self.yvel = - self.yvel
if isinstance(p, blocks.BlockDie): # - blocks.BlockDie self.die()#
if isinstance(p, blocks.BlockDie) or isinstance(p, monsters.Monster): # - blocks.BlockDie Monster self.die()#
monsters = pygame.sprite.Group() #
mn = Monster(190,200,2,3,150,15) entities.add(mn) platforms.append(mn) monsters.add(mn)
monsters.update(platforms) #
self.winner = False
elif isinstance(p, blocks.Princess): # self.winner = True # !!!
if col == "P": pr = Princess(x,y) entities.add(pr) platforms.append(pr) animatedEntities.add(pr)
[ ----------------------------------| - * -| - * *P -| ---- *--** --| - -- -| - -| - -| - -| -- ---- | - -| -- -| - ** -| - --- -| - -| - -| - --- -| - -| - -------- * ---- -| - -| - - -| - ** -- -| - * -| - ** -| --------------- *** -- -| - -| - -| ----------------------------------| ] player 55 44 portal 128 512 900 35 portal 170 512 700 64 monster 190 250 2 1 150 10 monster 190 400 2 3 150 150 monster 150 200 1 2 150 100 /
*** level = [] entities = pygame.sprite.Group() # animatedEntities = pygame.sprite.Group() # , monsters = pygame.sprite.Group() # platforms = [] # , if __name__ == "__main__": main()
def loadLevel(): global playerX, playerY # , levelFile = open('%s/levels/1.txt' % FILE_DIR) line = " " commands = [] while line[0] != "/": # line = levelFile.readline() # if line[0] == "[": # while line[0] != "]": # , line = levelFile.readline() # if line[0] != "]": # endLine = line.find("|") # level.append(line[0: endLine]) # "|" if line[0] != "": # commands = line.split() # if len(commands) > 1: # > 1, if commands[0] == "player": # - player playerX= int(commands[1]) # playerY = int(commands[2]) if commands[0] == "portal": # portal, tp = BlockTeleport(int(commands[1]),int(commands[2]),int(commands[3]),int(commands[4])) entities.add(tp) platforms.append(tp) animatedEntities.add(tp) if commands[0] == "monster": # monster, mn = Monster(int(commands[1]),int(commands[2]),int(commands[3]),int(commands[4]),int(commands[5]),int(commands[6])) entities.add(mn) platforms.append(mn) monsters.add(mn)
def main(): loadLevel() *** hero = Player(playerX,playerY) # (x,y) ***
Source: https://habr.com/ru/post/196432/
All Articles