我无法让我的演员在屏幕上随机移动。我尝试生成一个随机位置,然后慢慢地使其沿对角线或任何方向漂移,但我无法使其工作,因为它只是消失并在该位置重生,而不是漂移。我尝试预先设置随机位置的原因是这样我可以确保它不会超出范围。在游戏中,你必须移动宇宙飞船(ufo)并躲避在屏幕上随机移动的小行星。一共有三颗小行星。我目前已经对不明飞行物的运动进行了编码,制作了背景并导入了小行星。 请记住,我对 pygame 还很陌生 我不希望它只是在该位置重生,而是让它真正漂移
import pgzrun
import random
import time
WIDTH = 800
HEIGHT = 500
TITLE = "Dodge The Asteroid"
ufo = Actor("spaceship")
ufo.pos = 0,0
asteroid1 = Actor("reasteroid")
asteroid2 = Actor("reasteroid")
asteroid3 = Actor("reasteroid")
asteroid1.pos = 100,100
asteroid2.pos = 200,200
asteroid3.pos = 300,300
def draw():
screen.blit("starrybackground",(0,0))
ufo.draw()
asteroid1.draw()
asteroid2.draw()
asteroid3.draw()
#For the movement of the ufo
def update():
if keyboard.right:
ufo.x = ufo.x + 3
if keyboard.left:
ufo.x = ufo.x - 3
if keyboard.down:
ufo.y = ufo.y + 3
if keyboard.up:
ufo.y = ufo.y - 3
while True:
asteroid1xaxis = random.randint(0,WIDTH)
asteroid1yaxis = random.randint(0,HEIGHT)
while asteroid1.pos != (asteroid1xaxis,asteroid1yaxis):
asteroid1.x = asteroid1.x + 2
asteroid1.x = asteroid1.x + 2
pgzrun.go()
只需为小行星 X 和 Y 创建一个变量,然后在循环中不断更改它,然后将其传输到屏幕上。在我看来,您的代码不必要地复杂。另外,您仅移动 X 位置。这是故意的吗?