I can’t get my actor to move around the screen randomly. I tried getting a random position to be generated and then slowly make it to drift diagonally or whichever way but I couldn’t get this to work as it just disappeared and respawned in the position instead of drifting. The reason I tried to already set the random position beforehand is so I can make sure it doesn’t go out of bounds.
The game is where you have to move the spaceship (ufo) and dodge the asteroids that are moving around randomly across the screen. There are three asteroids. I have currently coded the ufo’s movement, made the background and imported the asteroids. I don’t want it to just respawn in the position but for it to actually drift
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()
Adi Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.