Reputation: 11
When I try to fire my knife in my game, it doesn't work. I am not very experienced in pygame. This is my first game that I'm making. It worked the first time, but now I try to call a new knife in a function and it doesn't work. I have tried a lot but it won't work. Here's my is my code:
# first I do this
realknifeImg = pygame.image.load('realknife.png')
Realknife_state = "ready"
# then this
def fire_realknife(x, y):
global Realknife_state
Realknife_state = "fire"
screen.blit(realknifeImg, (x, y))
# and finally this
def outro():
RealknifeX = 750
RealknifeY = 400
fire_realknife(RealknifeX, RealknifeY)
if Realknife_state == "fire":
fire_realknife(RealknifeX, RealknifeY)
RealknifeX += 1
Upvotes: 1
Views: 259
Reputation: 68
You need to use a rect for this. It is the standard way of moving images in pygame.
Instead of having all these variables you can just say:
r_knife_rect = realKnifeImg.get_rect()
r_knife_rect.x = 750
r_knife_rect.y = 400
Then whenever you want to move it call:
r_knife_rect.x += 1
Pygame wont like you using a tuple of (x,y) in your blit, it likes rect style objects instead. To blit just say:
screen.blit(realKnifeImg, real_knife_rect)
Edit: Using this also means your knife has a premade hitbox.
Upvotes: 1
Reputation: 210890
The position of the knife is continuously initialized in the outro
. Set to position before the function and use the global
statement to change the position in the function:
RealknifeX = 750
RealknifeY = 400
def outro():
global RealknifeX, RealknifeY
fire_realknife(RealknifeX, RealknifeY)
if Realknife_state == "fire":
RealknifeX += 1
Upvotes: 1