Python Forum
"Random" module is not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Random" module is not working as expected
#1
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()
Gribouillis write Jul-25-2024, 04:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python trivial endgame engine is not working as expected max22 0 1,120 Feb-24-2024, 04:41 PM
Last Post: max22
  Simple conditional not working as expected return2sender 8 2,363 Aug-27-2023, 10:39 PM
Last Post: return2sender
  working with TLV module Object Jennifer_Jone 3 2,330 Mar-14-2023, 07:54 PM
Last Post: Jennifer_Jone
  Unsure why module is not working garynewport 0 1,284 Feb-15-2023, 03:21 PM
Last Post: garynewport
  Custom method to handle exceptions not working as expected gradlon93 3 2,072 Dec-22-2022, 07:12 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 2,170 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  Pandas module not working Hass 2 2,982 Apr-14-2022, 03:13 PM
Last Post: snippsat
  set and sorted, not working how expected! wtr 2 1,985 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  <while> cycle is not interrupted when using the <random>module ShityCoder 3 3,008 Sep-04-2020, 04:05 PM
Last Post: ShityCoder
  importing module - not working jdhamblett 3 4,082 Jun-22-2020, 07:33 PM
Last Post: jdhamblett

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020