在pygame中移动精灵前后

1 投票
1 回答
854 浏览
提问于 2025-04-18 03:41

我有一个精灵(就是游戏中的角色或物体)从屏幕的一边移动到另一边,但现在我想让它一直来回移动。有什么好主意吗?

u = 1 #u is the x coordinate
if u < 430: #480 is the window siz, i want it to stop at 430
   u += 5 # Move up by 5

我是不是应该把我的完整代码也贴上来,这样会更容易理解?

1 个回答

1

你可以使用一个叫做“delta”的变量:

delta = 5
u = 1

然后可以调整这个变量来改变方向:

u += delta
if u >= 430: 
   delta = -5
elif u < 50:
   delta = 5

撰写回答