Python Forum
[PyGame] Random.randint not working please help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Random.randint not working please help
#1
Hello im trying to build my first game SPACEINVADERS but when i try to make the enemy pop-up in different/random locations i get the following :

Error:
Ozz.llc@DESKTOP-H29PUP8 MINGW64 ~/Desktop/PYTHON $ C:/Users/Ozz.llc/AppData/Local/Programs/Python/Python38/python.exe c:/Users/Ozz.llc/Desktop/PYTHON/main.py File "c:/Users/Ozz.llc/Desktop/PYTHON/main.py", line 24 enemyX=random.randint: (0,800) ^ SyntaxError: invalid syntax
Ozz.llc@DESKTOP-H29PUP8 MINGW64 ~/Desktop/PYTHON

Wink Wink Wink Wink THIS IS THE ENTIRE CODE: Wink Wink Wink Wink
import pygame
import random
#initalize the pygame
pygame.init()

#create screen
screen=pygame.display.set_mode((800,600))

#title and icon
pygame.display.set_caption("Dee's Space Invaders")
icon=pygame.image.load("flying.png")
pygame.display.set_icon(icon)

#player
playerImg=pygame.image.load("spacecraft.png")
#player  location on screen
playerX=370
playerY=480
playerX_change = 0

#enemy
enemyImg=pygame.image.load("alien.png")
#enemy  location on screen
enemyX=random.randint (0,800)
enemyY=random.randint (50,150)
enemyX_change = 0

#blit =drawing the player on the screen
def player(X,Y):
    screen.blit(playerImg, (X,Y))
#blit =drawing the enemy on the screen
def enemy(X,Y):
    screen.blit(enemyImg, (X,Y))

#game loops whatever you want to keep open or  veiwable at all time
running= True
while running:
   
    #rgb 0,0,0, =red green blue 
    screen.fill((0,0,0))
    #event is any action that player does such as pressing a key
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False   
# if keystroke is pressed check wheather its right of left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.3
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.3   
#check if key has been relased 
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
                 
    playerX += playerX_change
    if playerX <= 0:
        playerX = 0 
    elif playerX >=736:
        playerX = 736
    
    player(playerX,playerY)
    enemy(enemyX,enemyY)
# this keeps the screen updating very important    
    pygame.display.update()
Reply
#2
1. Use code tags.
2. It a typo on line 24.
enemyX=random.randint: (0,800)
should be

enemyX = random.randint(0,800)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Hello Ive changed the line to the following but im still receiving an error on line 24:

21-#enemy
22-enemyImg=pygame.image.load("alien.png")
23-#enemy location on screen
24-enemyX = random.randint(0,800)
25-enemyY = random.randint(50,150)
26-enemyX_change = 0


ERROR:::::

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "c:/Users/Ozz.llc/Desktop/PYTHON/main.py", line 24, in <module>
enemyX = random.randint(0,800)
AttributeError: module 'random' has no attribute 'randint'

Ozz.llc@DESKTOP-H29PUP8 MINGW64 ~/Desktop/PYTHON
Reply
#4
Do you have another module named random.py? That may be interfering with the import of the built-in random module.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
lol ichabod801 how dod you figure that out you were correct i had an other file called random i just deleted it and everything worked thank you !!!!!!!!!!!!
Reply


Forum Jump:

User Panel Messages

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