samedi 2 avril 2022

How to make a random number a multiple of 16 in Python?

I am trying to make a random multiple of 16 between 32 and 160 in Python using the 'random' library. Can anyone help me with this? Here is the code:

import pygame, random

pygame.init()
screen = pygame.display.set_mode((300,208))
pygame.display.set_caption("TinyRacer")
car = pygame.image.load("car.png")
car2 = pygame.image.load("car2.png")
bg = pygame.image.load("bg.png")
run = True
y = 64
e_y = random.randint(32,160)
e_x = 0

while run:
    for event in pygame.event.get():
        clock = pygame.time.Clock()  
        if event.type == pygame.QUIT:
            run = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP] and y > 32:
      y -= 16
    if key[pygame.K_DOWN] and y < 160:
      y += 16
    e_x = e_x - 18
    if e_x < 0:
      e_x = 300
      e_y = random.randint(32,160)

    screen.fill((255,255,255))
    screen.blit(car, (0,y))
    screen.blit(car2, (e_x,e_y))
    screen.blit(bg,(0,0))   
    pygame.display.update()
    clock.tick(16) 
    
pygame.quit()

I cannot seem to find anything on this topic.




Aucun commentaire:

Enregistrer un commentaire