I have a question.
So, I have a game, written on Python 3.3.5, using the Pygame module. I've made the class for my character, and I'm now at a point where I have no idea how to write the code for making my character attack. The class is here:
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")
I would like the attack control to be something similar to how I did the movement: If I press a button, it stops the movement of the character, plays the frame for a second, and then it goes back to standing. Is there a way? Thanks for your help!