So my problem is that my bot will move at an inconsistant speed and sometimes not even move. I've tried changing fps with clock.ticks(30)
(And less then that) but it just won't go any smoother!
pygame.init()
clock = pygame.time.Clock()
bot4x = random.randint(0, 600)
bot4y = random.randint(0, 600)
randb4 = round(random.randint(0, 4))
def bot4():
screen.blit(pygame.image.load("DOT.png"), (bot4x, bot4y))
clock.tick(60)
run = True
while run:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if randb4 == 1:
bot4x = bot4x + 5
elif randb4 == 2:
bot4x = bot4x - 5
elif randb4 == 3:
bot4y = bot4y + 5
elif randb4 == 4:
bot4y = bot4y - 5
if bot4x >= 568:
bot4x = 568
elif bot4x <= 0:
bot4x = 0
elif bot4y >= 568:
bot4y = 568
elif bot4y <= 0:
bot4y = 0
bot4()
pygame.display.update()
It is a matter of Indentation. You have to compute the movement in the application loop instead of the event loop:
pygame.init()
clock = pygame.time.Clock()
bot4x = random.randint(0, 600)
bot4y = random.randint(0, 600)
randb4 = round(random.randint(0, 4))
def bot4():
screen.blit(pygame.image.load("DOT.png"), (bot4x, bot4y))
clock.tick(60)
run = True
while run:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# INDENTATION
#<--|
if randb4 == 1:
bot4x = bot4x + 5
elif randb4 == 2:
bot4x = bot4x - 5
elif randb4 == 3:
bot4y = bot4y + 5
elif randb4 == 4:
bot4y = bot4y - 5
if bot4x >= 568:
bot4x = 568
elif bot4x <= 0:
bot4x = 0
elif bot4y >= 568:
bot4y = 568
elif bot4y <= 0:
bot4y = 0
bot4()
pygame.display.update()