如何在 Python 中移动导入的图像

how can I move imported image in Python

所以我想知道是否有一种方法可以在 pygame 中导入图像(作为精灵)并让它们移动,就像我们可以使用绘制的多边形一样。下面我给出了我尝试过的代码。

import pygame
from pygame import (
KEYDOWN,
QUIT,
K_ESCAPE,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_SPACE
)
x=30 ; y = 30
white =(255,255,255)
width = 720
height = 1280
screen = pygame.display.set_mode((height, width))
surface = pygame.Surface((640,360),pygame.SRCALPHA)
screen.fill(white)
image = pygame.image.load(r'C:\Users\spars\Downloads\a.jpg')
pygame.display.flip()
running = True
press = pygame.key.get_pressed() 
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running= False
if event.key == K_SPACE:
screen.blit(image, (0,0))
pygame.display.flip()
elif event.type == QUIT:
running = False
if press[K_UP]:
screen.blit(image,(x,y))
pygame.display.update()
y+=10
if press[K_DOWN]:
screen.blit(image,(x,y))
pygame.display.update()
y-=10
if press[K_LEFT]:
screen.blit(image,(x,y))
pygame.display.update()
x-=10
if press[K_RIGHT]:
screen.blit(image,(x,y))
pygame.display.update()
x=+10

我很抱歉,因为这是我第一次在堆栈溢出时添加代码,如果代码格式错误。 我尝试了另一个代码让它移动(更像是跳跃)

import pygame
from pygame import (
KEYDOWN,
QUIT,
K_ESCAPE,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_SPACE
)
white =(255,255,255)
width = 720
height = 1280
screen = pygame.display.set_mode((height, width))
surface = pygame.Surface((640,360),pygame.SRCALPHA)
screen.fill(white)
image = pygame.image.load(r'C:\Users\spars\Downloads\a.jpg')
pygame.display.flip()
running = True
press = pygame.key.get_pressed() 
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running= False
if event.key == K_SPACE:
for i in range (0,50):
screen.blit(image, (0,i))
i+=1
pygame.display.flip()
elif event.type == QUIT:
running = False

说明

我对您的代码进行了一些重大更改。我在 while 循环之后添加了 pygame.quit(),这样 window 将在 runningFalse 时关闭。我将所有按键事件(按下箭头键或按下 space 键或按下转义键时调用的事件)移至 if pygame.event == pygame.KEYDOWN 块内。这样,当您按下一个键时,程序将检查按下的是哪个键。在向上箭头事件中,我将 y += 10 切换为 y -= 10 因为 y 越小,精灵被绘制得越高。在向下箭头事件中,由于相同的原因,我将 y -= 10 切换为 y += 10 。我从每个事件中删除了 screen.blit(image (x, y) 函数。没有必要在同一个循环中多次调用同一个函数。相反,我在检查事件后调用了一次。在循环的最后,我添加了 pygame.display.update() 以便屏幕在每一帧都得到更新。在 while 循环的开始,在检查事件之前,我添加了 window.fill(white) 以便清除之前帧中绘制的所有内容。接下来,我添加了一个名为 spacePressed 的布尔变量,我在 while 循环之前将其设置为 False,并在按下 space 键时将其设置为 True。然后,在 while 循环中,我检查 spacePressed 是否等于 True。如果是,精灵将被绘制。最后,我缩进了你的代码。

代码

import pygame

x=30 ; y = 30
white =(255,255,255)
width = 720
height = 1280
screen = pygame.display.set_mode((height, width))
image = pygame.image.load(r'C:\Users\spars\Downloads\a.jpg')
running = True
spacePressed = False
while running:
    screen.fill(white)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            press = pygame.key.get_pressed()

            if press[pygame.K_ESCAPE]:
                running = False

            if press[pygame.K_SPACE]:
                spacePressed = True

            if press[pygame.K_UP]:
                y -= 10

            if press[pygame.K_DOWN]:
                y += 10

            if press[pygame.K_LEFT]:
                x -= 10

            if press[pygame.K_RIGHT]:
                x += 10

    if spacePressed:
        screen.blit(image, (x, y))

    pygame.display.update()

pygame.quit()

如果你有任何问题,请随时问我。