Как сделать движение картинки снизу вверх, при наведении на неё

Мне нужно чтобы была картинка которая немного выходила из низа экрана, и при наведении на неё она сдвигалась по 3-5 пикселей вверх, тем самым создавав анимацию. Сейчас у меня получилось это:

import pygame, ctypes, time
from pygame.locals import *

user32 = ctypes.windll.user32
w = user32.GetSystemMetrics(0)
h = user32.GetSystemMetrics(1)
hc = int(h/2)
wc = int(w/2)
 
pygame.init()
 
game_display = pygame.display.set_mode((w, h), pygame.RESIZABLE)
kartinka = pygame.image.load("assets/kartinka.png")
kartinkar = pygame.transform.scale(kartinka, (160, 240))
 
pygame.display.update()
speedCard = 5

while True:
    InGameSPP_MOUSE_POS = pygame.mouse.get_pos()
    InGameSPP_MOUSE_POS_XW = InGameSPP_MOUSE_POS[0]
    InGameSPP_MOUSE_POS_YH = InGameSPP_MOUSE_POS[1]

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            quit()

    game_display.blit(kartinkar, ((w-1080)-175, h-(188-125)))

    if (InGameSPP_MOUSE_POS_XW in range(int((w-1080)-175), int((w-1080)-175+160))) and (InGameSPP_MOUSE_POS_YH in range(h-(188-125), h-(188-125)+120)): # InHands0Card
        print("1")
        newposcard0 = h-(188-125)
        runwhilecard0 = True
        while runwhilecard0 == True:
            time.sleep(0.05)
            newposcard0 -= speedCard
            # print(newposcard0)
            if newposcard0 <= h-(188+125):
                newposcard0 = h-(188+125)
                runwhilecard0 = False
            game_display.blit(kartinkar, (w-1080-175, newposcard0))

    pygame.display.update()

вот, именно такой результат как тут у меня в программе происходит.

Приведённый код выше ^ не работает(


Ответы (1 шт):

Автор решения: Сергей Кох

Я не смог воспроизвести вашу программу, вот образец выполняющий нужную функцию:

import pygame
from pygame.locals import *

RED = (255, 0, 0)
GRAY = (150, 150, 150)

pygame.init()
w, h = 640, 440
screen = pygame.display.set_mode((w, h))
running = True

img = pygame.image.load('bird.png')
img.convert()
rect = img.get_rect()
rect.center = w // 2, h
moving = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        elif event.type == MOUSEMOTION and rect.collidepoint(event.pos) and moving:
            rect.move_ip(0, -5)
            moving = False

        elif event.type == MOUSEMOTION and not rect.collidepoint(event.pos) and not moving:
            rect.move_ip(0, +5)
            moving = True

    screen.fill(GRAY)
    screen.blit(img, rect)
    pygame.draw.rect(screen, RED, rect, 1)
    pygame.display.update()

pygame.quit()

За основу взял код отсюда, там и изображение.

→ Ссылка