Python Forum

Full Version: "Random" module is not working as expected
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm trying to make a spaceship fly around on the screen with stars twinkling in the backround. When I went to test the stars, I was told that I did not define "randint" any idea what I could do?
import pygame
from pathlib import Path
import random

pygame.init()
WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
pygame.display.set_caption("Space Shooter")
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
running = True
surf = pygame.Surface((100, 200))
surf.fill('orange')
x = 100

path = Path("/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video /17021362771.png").parent
player_surf = pygame.image.load(f'{path}/17021362771.png')
path = Path("/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video /star.png").parent
star_surf = pygame.image.load(f'{path}/star.png')

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    display_surface.fill('darkgray')
    x += 0.1
    display_surface.blit(player_surf, (x, 150))
    for i in range(20):
        display_surface.blit(star_surf, (randint(0, WINDOW_WIDTH), randint(0, WINDOW-HEIGHT)))
    pygame.display.update()

pygame.quit()
There are two ways to use randint (or any library function)
>>> import random
>>> random.randint(20, 30)
23
>>> from random import randint
>>> randint(20, 30)
20