I cant figure out this space key and attack animation
up vote
1
down vote
favorite
I decided to add the attack images into the walking ones to see what happened and I got them to load up.
I have been attempting to break it down and piece it back together to try and find what I have been doing wrong and it may be an issue of my experience.
I have butchered my code trying to figure this out after someone gave me an explanation about my classes. I thought maybe I should try and set it up as global, and then I realize there's something I'm not doing right. I don't want someone to just write the code for me I should mention. But an explanation for why I am screwing up would be nice.
Some tutorials are helping me but not as much as when someone tells me where specifically where I went wrong. I think I made more progress there.
##a simple punch without the whole animation atm
class attacks(object):
attack = pygame.image.load("atk3.png")
def __init__(self, x, y, width, height, attack):
self.x = x
self.y = y
self.width = width
self.height = height
self.attackCount = 0
def draw(self, win):
## is it my redraw method?
if (self.attack):
if self.attackCount + 1 >= 9:
win.blit(attack, self.attackCount//3(self.x, self.y))
self.attackCount += 1
So with the class set up here im sure that something is wrong, ive been diving through forums and I know something is not right. I dont know what though.
def redrawGameWindow():
global walkCount
global attackCount
win.blit(bg, (0, 0))
if walkCount +1 >= 27:
walkCount = 0
bob.draw(win)
jerk.draw(win)
for attack in attacks:
attack.draw(win)
pygame.display.update()
#While true
#This calls the classes created
bob = player(100, 200, 128, 128)
jerk = enemy(500, 200, 164, 100, 600)
attacks =
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
#attempting to make this into a bool that changes the value
if not(bob.attacking):
if keys[pygame.K_SPACE]:
bob.attacking = True
bob.standing = False
bob.left = False
bob.right = False
if keys[pygame.K_LEFT] and bob.x > bob.vel:
bob.x -= bob.vel
bob.left = True
bob.right = False
bob.standing = False
elif keys[pygame.K_RIGHT] and bob.x < 800 - bob.width - bob.vel:
bob.x += bob.vel
bob.right = True
bob.left = False
bob.standing = False
else:
bob.standing = True
bob.walkCount = 0
if not(bob.isJump):
if keys[pygame.K_UP]:
bob.isJump = True
bob.right = False
bob.left = False
bob.walkCount = 0
else:
if bob.jumpCount >= -10:
neg = 1
if bob.jumpCount < 0:
neg = -1
bob.y -= (bob.jumpCount ** 2) * 0.5 * neg
bob.jumpCount -= 1
else:
bob.isJump = False
bob.jumpCount = 10
redrawGameWindow()
pygame.quit()
Im almost sure my True loop has nothing to do with it now since im able to run walk and jump. And when I press space it loads a frame thats not an attack one so i know for sure something is happening when I hit space
python class object pygame blit
add a comment |
up vote
1
down vote
favorite
I decided to add the attack images into the walking ones to see what happened and I got them to load up.
I have been attempting to break it down and piece it back together to try and find what I have been doing wrong and it may be an issue of my experience.
I have butchered my code trying to figure this out after someone gave me an explanation about my classes. I thought maybe I should try and set it up as global, and then I realize there's something I'm not doing right. I don't want someone to just write the code for me I should mention. But an explanation for why I am screwing up would be nice.
Some tutorials are helping me but not as much as when someone tells me where specifically where I went wrong. I think I made more progress there.
##a simple punch without the whole animation atm
class attacks(object):
attack = pygame.image.load("atk3.png")
def __init__(self, x, y, width, height, attack):
self.x = x
self.y = y
self.width = width
self.height = height
self.attackCount = 0
def draw(self, win):
## is it my redraw method?
if (self.attack):
if self.attackCount + 1 >= 9:
win.blit(attack, self.attackCount//3(self.x, self.y))
self.attackCount += 1
So with the class set up here im sure that something is wrong, ive been diving through forums and I know something is not right. I dont know what though.
def redrawGameWindow():
global walkCount
global attackCount
win.blit(bg, (0, 0))
if walkCount +1 >= 27:
walkCount = 0
bob.draw(win)
jerk.draw(win)
for attack in attacks:
attack.draw(win)
pygame.display.update()
#While true
#This calls the classes created
bob = player(100, 200, 128, 128)
jerk = enemy(500, 200, 164, 100, 600)
attacks =
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
#attempting to make this into a bool that changes the value
if not(bob.attacking):
if keys[pygame.K_SPACE]:
bob.attacking = True
bob.standing = False
bob.left = False
bob.right = False
if keys[pygame.K_LEFT] and bob.x > bob.vel:
bob.x -= bob.vel
bob.left = True
bob.right = False
bob.standing = False
elif keys[pygame.K_RIGHT] and bob.x < 800 - bob.width - bob.vel:
bob.x += bob.vel
bob.right = True
bob.left = False
bob.standing = False
else:
bob.standing = True
bob.walkCount = 0
if not(bob.isJump):
if keys[pygame.K_UP]:
bob.isJump = True
bob.right = False
bob.left = False
bob.walkCount = 0
else:
if bob.jumpCount >= -10:
neg = 1
if bob.jumpCount < 0:
neg = -1
bob.y -= (bob.jumpCount ** 2) * 0.5 * neg
bob.jumpCount -= 1
else:
bob.isJump = False
bob.jumpCount = 10
redrawGameWindow()
pygame.quit()
Im almost sure my True loop has nothing to do with it now since im able to run walk and jump. And when I press space it loads a frame thats not an attack one so i know for sure something is happening when I hit space
python class object pygame blit
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I decided to add the attack images into the walking ones to see what happened and I got them to load up.
I have been attempting to break it down and piece it back together to try and find what I have been doing wrong and it may be an issue of my experience.
I have butchered my code trying to figure this out after someone gave me an explanation about my classes. I thought maybe I should try and set it up as global, and then I realize there's something I'm not doing right. I don't want someone to just write the code for me I should mention. But an explanation for why I am screwing up would be nice.
Some tutorials are helping me but not as much as when someone tells me where specifically where I went wrong. I think I made more progress there.
##a simple punch without the whole animation atm
class attacks(object):
attack = pygame.image.load("atk3.png")
def __init__(self, x, y, width, height, attack):
self.x = x
self.y = y
self.width = width
self.height = height
self.attackCount = 0
def draw(self, win):
## is it my redraw method?
if (self.attack):
if self.attackCount + 1 >= 9:
win.blit(attack, self.attackCount//3(self.x, self.y))
self.attackCount += 1
So with the class set up here im sure that something is wrong, ive been diving through forums and I know something is not right. I dont know what though.
def redrawGameWindow():
global walkCount
global attackCount
win.blit(bg, (0, 0))
if walkCount +1 >= 27:
walkCount = 0
bob.draw(win)
jerk.draw(win)
for attack in attacks:
attack.draw(win)
pygame.display.update()
#While true
#This calls the classes created
bob = player(100, 200, 128, 128)
jerk = enemy(500, 200, 164, 100, 600)
attacks =
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
#attempting to make this into a bool that changes the value
if not(bob.attacking):
if keys[pygame.K_SPACE]:
bob.attacking = True
bob.standing = False
bob.left = False
bob.right = False
if keys[pygame.K_LEFT] and bob.x > bob.vel:
bob.x -= bob.vel
bob.left = True
bob.right = False
bob.standing = False
elif keys[pygame.K_RIGHT] and bob.x < 800 - bob.width - bob.vel:
bob.x += bob.vel
bob.right = True
bob.left = False
bob.standing = False
else:
bob.standing = True
bob.walkCount = 0
if not(bob.isJump):
if keys[pygame.K_UP]:
bob.isJump = True
bob.right = False
bob.left = False
bob.walkCount = 0
else:
if bob.jumpCount >= -10:
neg = 1
if bob.jumpCount < 0:
neg = -1
bob.y -= (bob.jumpCount ** 2) * 0.5 * neg
bob.jumpCount -= 1
else:
bob.isJump = False
bob.jumpCount = 10
redrawGameWindow()
pygame.quit()
Im almost sure my True loop has nothing to do with it now since im able to run walk and jump. And when I press space it loads a frame thats not an attack one so i know for sure something is happening when I hit space
python class object pygame blit
I decided to add the attack images into the walking ones to see what happened and I got them to load up.
I have been attempting to break it down and piece it back together to try and find what I have been doing wrong and it may be an issue of my experience.
I have butchered my code trying to figure this out after someone gave me an explanation about my classes. I thought maybe I should try and set it up as global, and then I realize there's something I'm not doing right. I don't want someone to just write the code for me I should mention. But an explanation for why I am screwing up would be nice.
Some tutorials are helping me but not as much as when someone tells me where specifically where I went wrong. I think I made more progress there.
##a simple punch without the whole animation atm
class attacks(object):
attack = pygame.image.load("atk3.png")
def __init__(self, x, y, width, height, attack):
self.x = x
self.y = y
self.width = width
self.height = height
self.attackCount = 0
def draw(self, win):
## is it my redraw method?
if (self.attack):
if self.attackCount + 1 >= 9:
win.blit(attack, self.attackCount//3(self.x, self.y))
self.attackCount += 1
So with the class set up here im sure that something is wrong, ive been diving through forums and I know something is not right. I dont know what though.
def redrawGameWindow():
global walkCount
global attackCount
win.blit(bg, (0, 0))
if walkCount +1 >= 27:
walkCount = 0
bob.draw(win)
jerk.draw(win)
for attack in attacks:
attack.draw(win)
pygame.display.update()
#While true
#This calls the classes created
bob = player(100, 200, 128, 128)
jerk = enemy(500, 200, 164, 100, 600)
attacks =
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
#attempting to make this into a bool that changes the value
if not(bob.attacking):
if keys[pygame.K_SPACE]:
bob.attacking = True
bob.standing = False
bob.left = False
bob.right = False
if keys[pygame.K_LEFT] and bob.x > bob.vel:
bob.x -= bob.vel
bob.left = True
bob.right = False
bob.standing = False
elif keys[pygame.K_RIGHT] and bob.x < 800 - bob.width - bob.vel:
bob.x += bob.vel
bob.right = True
bob.left = False
bob.standing = False
else:
bob.standing = True
bob.walkCount = 0
if not(bob.isJump):
if keys[pygame.K_UP]:
bob.isJump = True
bob.right = False
bob.left = False
bob.walkCount = 0
else:
if bob.jumpCount >= -10:
neg = 1
if bob.jumpCount < 0:
neg = -1
bob.y -= (bob.jumpCount ** 2) * 0.5 * neg
bob.jumpCount -= 1
else:
bob.isJump = False
bob.jumpCount = 10
redrawGameWindow()
pygame.quit()
Im almost sure my True loop has nothing to do with it now since im able to run walk and jump. And when I press space it loads a frame thats not an attack one so i know for sure something is happening when I hit space
python class object pygame blit
python class object pygame blit
edited Nov 19 at 10:22
Billal Begueradj
5,606132637
5,606132637
asked Nov 18 at 14:26
Grayve Oniboru
111
111
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27
add a comment |
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361931%2fi-cant-figure-out-this-space-key-and-attack-animation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Ok im not sure what to do with these comments and I hope someone can help me. Im not sure what I dont get. I just know the issue lies within these lines that are giving me the "AttributeError: list object has no attribute draw" and the other issue I get when I change the attack to attacks is: UnboundLocalError: local variable 'attack' reference before assignment.
– Grayve Oniboru
Nov 18 at 17:04
class attacks(object): attack = pygame.image.load("atk3.png") def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.attackCount = 0
– Grayve Oniboru
Nov 18 at 17:05
def draw(self, win): self.attacks() if (self.attack): if self.attackCount + 1 >= 9: win.blit(attack[0], [self.attackCount//3],(self.x, self.y)) self.attackCount += 1 else: win.blit(char[0], [self.attackCount//3] (self.x ,self.y)) self.attackCount += 1
– Grayve Oniboru
Nov 18 at 17:05
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
Nov 20 at 14:27