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
New contributor
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
New contributor
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
2 days ago
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
2 days ago
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
2 days ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago
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
New contributor
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
New contributor
New contributor
edited 2 days ago
Billal Begueradj
5,516132637
5,516132637
New contributor
asked 2 days ago
Grayve Oniboru
111
111
New contributor
New contributor
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
2 days ago
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
2 days ago
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
2 days ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago
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
2 days ago
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
2 days ago
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
2 days ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Grayve Oniboru is a new contributor. Be nice, and check out our Code of Conduct.
Grayve Oniboru is a new contributor. Be nice, and check out our Code of Conduct.
Grayve Oniboru is a new contributor. Be nice, and check out our Code of Conduct.
Grayve Oniboru is a new contributor. Be nice, and check out our Code of Conduct.
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
2 days ago
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
2 days ago
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
2 days ago
Please read the minimal, complete and verifiable example page and edit your code to make it runnable.
– skrx
20 hours ago