PyGame如何让攻击控制工作?

2024-03-29 15:58:50 发布

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

我有个问题。在

所以,我有一个用Pygame模块在python3.3.5上编写的游戏。我已经为我的角色创建了一个类,现在我不知道如何编写使我的角色攻击的代码。课程在这里:

import pygame

class playerSprite(pygame.sprite.Sprite):
    def __init__(self,position):
         self.sheet = pygame.image.load('data/playerSheet.png') #Import sprite sheet
         self.sheet.set_clip(pygame.Rect(90,0,31,43)) #set first frame of player.
         self.image = self.sheet.subsurface(self.sheet.get_clip())
         self.rect = self.image.get_rect()
         self.rect.topleft = position
         self.frame = 0 #Used for switching between frames
         #The below are the: (Top, Left, Width, Height) of each frame for walking.
         self.left_states = { 0:(238,0,31,43),1:(7,187,31,43),2:(37,187,31,43),3:(70,187,31,43),4:(100,187,31,43),5:(130,187,31,43),6:(164,187,31,43) }
         self.right_states = { 0:(141,0,31,43),1:(10,93,31,43),2:(42,93,31,43),3:(72,93,31,43),4:(101,93,31,43),5:(132,93,31,43),6:(164,93,31,43) }
         self.down_states = { 0:(90,0,31,43),1:(23,46,31,43),2:(49,46,30,43),3:(70,46,31,43),4:(98,46,31,43),5:(121,46,31,43),6:(146,46,31,43) }
         self.up_states = { 0:(189,0,31,43),1:(26,140,31,43),2:(51,140,31,43),3:(75,140,31,43),4:(102,140,31,43),5:(125,140,31,43),6:(151,140,31,43) }


      def get_frame(self, frame_set):
         self.frame += 1
         if self.frame > (len(frame_set) - 1):
             self.frame = 0
         return frame_set[self.frame]

      def clip(self, clipped_rect):
         if type(clipped_rect) is dict:
             self.sheet.set_clip(pygame.Rect(self.get_frame(clipped_rect)))
         else:
             self.sheet.set_clip(pygame.Rect(clipped_rect))
         return clipped_rect

     def update(self, direction):
         if direction == 'left':
             self.clip(self.left_states)
             self.rect.x -= 5
         if direction == 'right':
             self.clip(self.right_states)
             self.rect.x += 5
         if direction == 'up':
             self.clip(self.up_states)
             self.rect.y -= 5
         if direction == 'down':
             self.clip(self.down_states)
             self.rect.y += 5

         if direction == "stand_left":
             self.clip(self.left_states[0])
         if direction == "stand_right":
             self.clip(self.right_states[0])
         if direction == "stand_up":
             self.clip(self.up_states[0])
         if direction == "stand_down":
             self.clip(self.down_states[0])

         self.image = self.sheet.subsurface(self.sheet.get_clip())

     def handle_event(self, event):
         if event.type == pygame.QUIT:
             running = False

         if event.type == pygame.KEYDOWN:

             if event.key == pygame.K_LEFT:
                 self.update('left')
             if event.key == pygame.K_RIGHT:
                 self.update('right')
             if event.key == pygame.K_UP:
                 self.update('up')
             if event.key == pygame.K_DOWN:
                 self.update('down')

         if event.type == pygame.KEYUP:

             if event.key == pygame.K_LEFT:
                 self.update("stand_left")
             if event.key == pygame.K_RIGHT:
                 self.update("stand_right")
             if event.key == pygame.K_UP:
                 self.update("stand_up")
             if event.key == pygame.K_DOWN:
                 self.update("stand_down")

我希望攻击控制类似于我的移动方式:如果我按下一个按钮,它会停止角色的移动,播放帧一秒钟,然后又回到站立状态。有办法吗?谢谢你的帮助!在


Tags: keyrectselfeventclipifupdateleft