Drknessfall
Drknessfall

Reputation: 1

Pygame image switches to the next image but doesn't follow old attributes?

Ok, so I am making a small game where an axe rotates to the side and back as the mouse is clicked and points are also racked up in a counter as this happens. When the player reaches 20 clicks the image which is following the mouse is supposed to change and continue following the mouse(and it does so beautifully!) But my problem is that when it does it stops rotating as the mouse is being clicked but the counter and everything else works fine.What am I missing or doing wrong?

So this is this is what I have so far(pseudocode?):

counter = 0
img = pygame.image.load('C:/Users/Myname/Desktop/art/shadedaxe.bmp')#loads img
img = pygame.transform.scale(img,(150,140))#scales

img3 = pygame.image.load('C:/Users/Myname/Desktop/art/shadedaxe2.bmp')
img3 = pygame.transform.scale(img3,(150,140))#scales

while running:
    mx,my = pygame.mouse.get_pos()
    imgx=mx-50 #sets img at
    imgy=my-70
    screen.blit(img, (imgx,imgy))

    for event in pygame.event.get():
        if event.type == MOUSEBUTTONDOWN:
            if event.button != 4 and event.button != 5 and event.button != 3:
                img = pygame.transform.rotate(img, -90) #make it turn on click
                counter += 1 #Gives points to the player

        elif event.type == MOUSEBUTTONUP: 
            if event.button != 5 and event.button !=4 and event.button != 3:
                img = pygame.transform.rotate(img, 90)

        if counter > 20:
            img = img3

PS: I just started coding in Python namely Pygame and have only been coding for a few months so i don't have a full idea of what I'm doing if you also have any tips for code organization they would be greatly appreciated! Thank you!

Upvotes: 0

Views: 33

Answers (1)

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11170

The issue is in the following lines:

if counter > 20:
    img = img3

Since counter is only increased, the assignment is executed on every event. Since img3 is unrotated, the following line result is not seen, as the img assigned as img3 again.

 img = pygame.transform.rotate(img, -90)

To solve this you should have an additional variable that will tell you if the shadeaxe has been "upgraded". Then test for counter > 20 and axeUpgraded == False

Upvotes: 1

Related Questions