I am writing a simple python game for class using pygame. I have coded for a short amount of time but have not used graphics until this project. I have already created a 'fireball' that moves from the player to the point clicked. I am working on a warrior, he has a sword the travels with him. I also have got the slash-looking motion I want. You press the space bar to have the sword change angles then it comes back up to its regular position. The problem I am having is that once the image/object(sword) is drawn in the down position it stays in that position. Though the original image rises back to the original position. I was hoping someone would have a point in the right direction. This is my first graphical game. I know this usually has something to do with what line the object is drawn on in the main, however, i have messed around with that quite a bit with no luck. Also, if I am going about this incorrectly don't hesitate to say anything. Any help is appreciated, Thanks.
class Player(pygame.sprite.Sprite):
def __init__(self, name):
pygame.sprite.Sprite.__init__(self)
self.name = name
self.level = 1
self.xp = 0
self.health = 100
self.power = 100
self.disc = 0
self.currentAtk = 1
def draw(self, screen):
screen.blit(self.image, (self.rect.x, self.rect.y))
def update(self):
key = pygame.key.get_pressed()
if key[K_SPACE]:
if self.disc == 1 and self.currentAtk == 1:
self.warsword.stab()
class Sword(pygame.sprite.Sprite):
image = pygame.image.load('images\\sword.png')
def __init__(self, x1, y1, angle = -25, stabtime = 5):
pygame.sprite.Sprite.__init__(self)
self.angle = angle
self.image = pygame.transform.rotate(Sword.image, self.angle)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x1, y1
self.stabtime = stabtime
self.repeat = 2
def update(self):
key = pygame.key.get_pressed()
if key[K_a] and self.rect.x >= 72:
self.rect.x -= 1
if key[K_d] and self.rect.x <= 572:
self.rect.x += 1
if key[K_w] and self.rect.y >= 154:
self.rect.y -= 1
if key[K_s] and self.rect.y <= 309:
self.rect.y += 1
def draw(self, screen, x, y):
screen.blit(self.image, (x, y))
def stab(self):
if self.angle != -90 and self.stabtime == 5:
self.angle = -90
self.rect.y += 15
if self.stabtime >= -5:
self.image = pygame.transform.rotate(self.image, self.angle)
else:
pygame.sprite.Sprite.update(self)
self.stabtime -= 1
class Warrior(Player):
image = pygame.image.load('images\\warrior.png')
SwordX = 0
SwordY = 0
def __init__(self, name, x, y):
super(Warrior, self).__init__(name)
self.disc = 1
self.image = Warrior.image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
Warrior.SwordX = self.rect.x + 12
Warrior.SwordY = self.rect.y + 10
self.warsword = Sword(Warrior.SwordX, Warrior.SwordY)
atks.add(self.warsword)
def main():
chars = pygame.sprite.Group(())
screen = pygame.display.set_mode((640, 480))
a = Warrior('Fago', 250, 200)
chars.add(a)
screen = pygame.display.set_mode((640, 480))
while True:
chars.update()
atks.update()
screen.blit(pygame.image.load('images\\arena.png'), (0,0))
atks.draw(screen)
chars.draw(screen)
pygame.display.update()
CLOCK.tick(75)
pygame.event.pump()
main()