import pygame
# Some colors
GREEN = ( 0,255,0)
BLUE = ( 0, 0, 255)
WHITE = ( 255, 255, 255)
BLACK = ( 0, 0, 0)
pygame.init()
clock = pygame.time.Clock()
#Screen
SCREEN = pygame.display.set_mode([1000,700])
#Title
pygame.display.set_caption("Trying to move things")
#Variables
x_position = 100
y_position = 100
x_speed = 0
y_speed = 0
#Positions
image_image_positions = [x_position,y_position]
#Graphics
image_image = pygame.image.load("izzat.png").convert()
image_image.set_colorkey(BLACK)
#Main loop ____
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Keyboard commands.
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_speed = -5
elif event.key == pygame.K_DOWN:
y_speed = 5
elif event.key == pygame.K_w:
x_speed = -5
elif event.key == pygame.K_s:
x_speed = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
x_speed += 0
if y_position + y_speed >=0 and y_position + y_speed + 60 <=500:
y_position += y_speed
x_position += x_speed
SCREEN.fill(GREEN)
SCREEN.blit(image_image, image_image_positions)
print(x_position)
print(y_position)
pygame.display.flip()
clock.tick(60)
pygame.quit()
你好,我遇到了一个我真的不高兴的问题,因为我一直试图修复它一段时间。所以基本上我有一个图像,我希望根据键盘输入移动图像,但无论我尝试什么都没有用。然后我想知道也许y和x位置根本没有变化,这就是为什么图像位置没有变化,我确实打印(那些位置)但它的位置肯定在变化,变量,所以我不知道怎么样图像位置根本不会改变。然后我想也许是因为它是一个元组,所以我把它改成了括号,这也没用。我只是不明白为什么如果图像位置的变量发生变化,我的图像位置不会移动。如果你能以任何方式帮助我,谢谢你。我看了这个,但我找不到任何帮助。如果你帮助我,谢谢你的帮助!
更新__________ 好吧显然,尽管我打印了image_image_position的位置时变量发生了变化,但image_image_position保持不变。有没有办法改变它们而不是让它们一直保持在100,100并且随着变量的变化而变化?
答案 0 :(得分:1)
您尚未指定速度。
image_image_positions [0] = xspeed image_image_positions [1] = yspeed
答案 1 :(得分:1)
您只是更改了变量yspeed
和xspeed
,但您没有设置图像的实际位置。在blit
:
image_image_positions = [x_position,y_position]
完整代码:
import pygame
# Some colors
GREEN = ( 0,255,0)
BLUE = ( 0, 0, 255)
WHITE = ( 255, 255, 255)
BLACK = ( 0, 0, 0)
pygame.init()
clock = pygame.time.Clock()
#Screen
SCREEN = pygame.display.set_mode([1000,700])
#Title
pygame.display.set_caption("Trying to move things")
#Variables
x_position = 100
y_position = 100
x_speed = 0
y_speed = 0
#Positions
image_image_positions = [x_position,y_position]
#Graphics
image_image = pygame.image.load("izzat.png").convert()
image_image.set_colorkey(BLACK)
#Main loop ____
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Keyboard commands.
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_speed = -5
elif event.key == pygame.K_DOWN:
y_speed = 5
elif event.key == pygame.K_w:
x_speed = -5
elif event.key == pygame.K_s:
x_speed = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
x_speed += 0
if y_position + y_speed >=0 and y_position + y_speed + 60 <=500:
y_position += y_speed
x_position += x_speed
image_image_positions = [x_position,y_position]
SCREEN.fill(GREEN)
SCREEN.blit(image_image, image_image_positions)
print(x_position)
print(y_position)
pygame.display.flip()
clock.tick(60)
pygame.quit()
答案 2 :(得分:1)
在更新x / y位置时,这不会改变图像的绘制位置:
SCREEN.blit(image_image, image_image_positions)
image_image_positions,在您的应用程序的整个生命周期内(除启动之外)永远不会更改。
要解决此问题,只需将更新添加到循环中:
image_image_positions = [x_position,y_position]