使用PyGame、Pyganim和类的动画

2024-09-28 22:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的问题是我的玩家类,我仍然在努力理解类。 所以,基本上,我使用的是Pyganim的一个例子中的播放器移动代码,但是我似乎不能在我的播放器类中实现它,可能是因为我不太了解类。在

首先。我的播放器现在再也不显示了,在我尝试添加类之前,所有的东西都起作用了。我用这个动作可能有点复杂。我的代码是:

 import pygame, sys, random, pyganim
 from pygame.locals import *

 class Scene(object):
     screen = pygame.display.set_mode((800, 600))

 class Intro(Scene):
     def __init__(self):
         self.c = (32, 32, 100)

     def draw(self):
         Scene.screen.fill(self.c)

     def update(self):
         # since scenes are classes, they have a state that we can modify
         r,g,b = self.c
         r += 1
         g += 1
         b += 2
         if r > 255: r = 0
         if g > 255: g = 0
         if b > 255: b = 0
         self.c = r, g, b

     def handle(self, event):
         # move to Menu-scene when space is pressed
         if event.type == pygame.KEYDOWN:
             if event.key == pygame.K_m:
                 # returning a scene from handle or update changes the current scene
                 return Menu()

 class Menu(Scene):
     def draw(self):
         # draw menu
         Scene.screen.fill((200, 200, 100))

     def update(self):
         pass
         # do something

     def handle(self, event):
         # handle menu input
         if event.type == pygame.KEYDOWN:
             if event.key == pygame.K_a:
                 return Game()
             if event.key == pygame.K_b:
                 return Intro()

 class Game(Scene):
     def draw(self):
         Scene.screen.fill((0,0,0))
         bg = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\fort.png')
         Scene.screen.blit(pygame.transform.scale(bg,(400,300)),(0,0))


     def update(self):
         pass

     def handle(self, event):
         if event.type == pygame.KEYDOWN:
             if event.key == pygame.K_m:
                 return meun()

 class Player(pygame.sprite.Sprite):


     def __init__(self):
         self.UP = 'up'
         self.DOWN = 'down'
         self.LEFT = 'left'
         self.RIGHT = 'right'
         self.direction = DOWN # player starts off facing down (front)
         # load the "standing" sprites (these are single images, not animations)
         self.playerWidth, playerHeight = front_standing.get_size()
         # creating the PygAnimation objects for walking/running in all directions
         self.animTypes = 'back_run back_walk front_run front_walk left_run left_walk'.split()
         self.animObjs = {}
         for animType in animTypes:
             self.imagesAndDurations = [('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_%s.%s.gif' % (animType, str(num).rjust(3, '0')), 1) for num in range(6)]
             self.animObjs[animType] = pyganim.PygAnimation(imagesAndDurations)
         # create the right-facing sprites by copying and flipping the left-facing sprites
         self.animObjs['right_walk'] = animObjs['left_walk'].getCopy()
         self.animObjs['right_walk'].flip(True, False)
         self.animObjs['right_walk'].makeTransformsPermanent()
         self.animObjs['right_run'] = animObjs['left_run'].getCopy()
         self.animObjs['right_run'].flip(True, False)
         self.animObjs['right_run'].makeTransformsPermanent()
         # have the animation objects managed by a conductor.
         # With the conductor, we can call play() and stop() on all the animtion
         # objects at the same time, so that way they'll always be in sync with each
         # other.
         self.moveConductor = pyganim.PygConductor(animObjs)
         self.x = 100
         self.y = 100
         self.WALKRATE = 10
         self.RUNRATE = 18
         self.pygame.sprite.Sprite.__init__(self) #init the Pygame sprite
         #load all images
         #load the player image
         self.front_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_front.gif')
         self.back_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_back.gif')
         self.left_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_left.gif')
         self.right_standing = pygame.transform.flip(left_standing, True, False)
         self.running = moveUp = moveDown = moveLeft = moveRight = False

     def update(self):
         for event in pygame.event.get():
             if event.key == KEYDOWN:
                 if event.key in (K_LSHIFT, K_RSHIFT):
                         # player has started running
                         running = True
                 if event.key == K_UP:
                     moveUp = True
                     moveDown = False
                     if not moveLeft and not moveRight:
                         # only change the direction to up if the player wasn't moving left/right
                         direction = UP
                 elif event.key == K_DOWN:
                     moveDown = True
                     moveUp = False
                     if not moveLeft and not moveRight:
                         direction = DOWN
                 elif event.key == K_LEFT:
                     moveLeft = True
                     moveRight = False
                     if not moveUp and not moveDown:
                         direction = LEFT
                 elif event.key == K_RIGHT:
                     moveRight = True
                     moveLeft = False
                     if not moveUp and not moveDown:
                         direction = RIGHT

             elif event.type == KEYUP:
                 if event.key in (K_LSHIFT, K_RSHIFT):
                     # player has stopped running
                     running = False
                 if event.key == K_UP:
                     moveUp = False
                     # if the player was moving in a sideways direction before, change the direction the player is facing.
                     if moveLeft:
                         direction = LEFT
                     if moveRight:
                         direction = RIGHT
                 elif event.key == K_DOWN:
                     moveDown = False
                     if moveLeft:
                         direction = LEFT
                     if moveRight:
                         direction = RIGHT
                 elif event.key == K_LEFT:
                     moveLeft = False
                     if moveUp:
                         direction = UP
                     if moveDown:
                         direction = DOWN
                 elif event.key == K_RIGHT:
                     moveRight = False
                     if moveUp:
                         direction = UP
                     if moveDown:
                         direction = DOWN
         if moveUp or moveDown or moveLeft or moveRight:
                 # draw the correct walking/running sprite from the animation object
                 moveConductor.play() # calling play() while the animation objects are already playing is okay; in that case play() is a no-op
                 if running:
                     if direction == UP:
                         animObjs['back_run'].blit(screen, (x, y))
                     elif direction == DOWN:
                         animObjs['front_run'].blit(screen, (x, y))
                     elif direction == LEFT:
                         animObjs['left_run'].blit(screen, (x, y))
                     elif direction == RIGHT:
                         animObjs['right_run'].blit(screen, (x, y))
                 else:
                     # walking
                     if direction == UP:
                         animObjs['back_walk'].blit(screen, (x, y))
                     elif direction == DOWN:
                         animObjs['front_walk'].blit(screen, (x, y))
                     elif direction == LEFT:
                         animObjs['left_walk'].blit(screen, (x, y))
                     elif direction == RIGHT:
                         animObjs['right_walk'].blit(screen, (x, y))


                 # actually move the position of the player
                 if running:
                     rate = RUNRATE
                 else:
                     rate = WALKRATE
                 if moveUp:
                     y -= rate
                 if moveDown:
                     y += rate
                 if moveLeft:
                     x -= rate
                 if moveRight:
                     x += rate

         else:
                 # standing still
                 moveConductor.stop() # calling stop() while the animation objects are already stopped is okay; in that case stop() is a no-op
                 if direction == UP:
                     Scene.screen.blit(back_standing, ((x, y)))
                 elif direction == DOWN:
                     Scene.screen.blit(front_standing, ((x, y)))
                 elif direction == LEFT:
                     screen.blit(left_standing, ((x, y)))
                 elif direction == RIGHT:
                     screen.blit(right_standing, (x, y))

             # make sure the player does move off the screen
                 if x < 0:
                     x = 0
                 if x > WINDOWWIDTH - playerWidth:
                     x = WINDOWWIDTH - playerWidth
                 if y < 0:
                     y = 0
                 if y > WINDOWHEIGHT - playerHeight:
                     y = WINDOWHEIGHT - playerHeight


 pygame.init()
 clock = pygame.time.Clock()
 Scene.screen = pygame.display.set_mode((800, 600))

 scene = Intro()
 while True:
     if pygame.event.get(pygame.QUIT): break
     for e in pygame.event.get(): 
         scene = scene.handle(e) or scene
     scene = scene.update() or scene
     scene.draw()
     Player.update(scene)
     pygame.display.flip()
     clock.tick(20)

这是新的工作代码。 我改变了处理和更新,并在需要的地方添加了很多self。不确定如何在draw()中添加此代码,这可能会被清理并编写得更好,但至少它可以工作。在

^{pr2}$

Tags: thekeyselfrighteventfalseifdef