![]() |
[PyGame] Random.randint not working please help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] Random.randint not working please help (/thread-23053.html) |
Random.randint not working please help - SupaNub - Dec-09-2019 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 : Ozz.llc@DESKTOP-H29PUP8 MINGW64 ~/Desktop/PYTHON![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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() RE: Random.randint not working please help - Windspar - Dec-09-2019 1. Use code tags. 2. It a typo on line 24. enemyX=random.randint: (0,800)should be enemyX = random.randint(0,800) RE: Random.randint not working please help - SupaNub - Dec-09-2019 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 RE: Random.randint not working please help - ichabod801 - Dec-09-2019 Do you have another module named random.py? That may be interfering with the import of the built-in random module. RE: Random.randint not working please help - SupaNub - Dec-09-2019 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 !!!!!!!!!!!! |