1

When i run my pygame code, i get the following error:

>>> 
 RESTART: C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py 
Traceback (most recent call last):
  File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py", line 1, in <module>
    import pygame
  File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame\pygame.py", line 3, in <module>
    pygame.init()
AttributeError: module 'pygame' has no attribute 'init'

My code:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))

pygame.display.set_caption("first game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run= True
while run:
    pygame.time.delay(100)

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


    pygame.draw.rect(win, (255,0,0))

    pygame.quit()
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • The error says that there's no function named `init` in `pygame`. Have you checked the reference for what `pygame` exists of? – Johan Sep 26 '18 at 10:00
  • 1
    You called your program `pygame 2.py`, and it seems that you already had another called `pygame.py` in the same directory. That is the one that gets imported. Remove or rename it. – Thierry Lathuille Sep 26 '18 at 10:00
  • 2
    You must not name your file `pygame.py` or Python will think your file is the pygame library. Just rename it. – skrx Sep 26 '18 at 10:01
  • @skrx you could/should've actually VTC, in particular because the OP can univocally accept closure as duplicate. – ivan_pozdeev Sep 26 '18 at 10:12
  • @ivan_pozdeev: Sorry my mistake. That answer is for some reason displayed at the bottom of answers list for me, below a deleted answer. It is the most upvoted answer, but not accepted. It *does* look like a duplicate. – Neil Slater Sep 26 '18 at 10:21
  • @NeilSlater you have answer sorting set to "active". – ivan_pozdeev Sep 26 '18 at 10:35
  • @NeilSlater I may've been mistaken, too: while the answer is the same, in that case, it's impossible to say whether the reason is that or not since there's zero info provided. – ivan_pozdeev Sep 26 '18 at 10:37
  • You guys are very smart!!! I did it!! – DragonProgram Sep 26 '18 at 10:40

1 Answers1

3

Looks like you have a script called pygame.py locally to your test script. This is getting imported instead of the library.

The fix is to rename your local pygame.py script (which is presumably another version of this one as you are figuring out Pygame) so it does not conflict. In general, avoid naming your project files the same as the libraries that you are using.

You also have other errors in your code (please read the Pygame documentation and examples), but this will be the first fix you need to apply, and it is not specific to Pygame.

Here is a working version of your code:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))

pygame.display.set_caption("first game")

x = 50
y = 50
width = 40
height = 60
vel = 5

while True:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.draw.rect(win, (255,0,0), win.get_rect())

    pygame.display.update()

Note the extra required param to pygame.draw.rect and call to pygame.display.update(). Also modified the while loop, as once you have called pygame.quit() you don't want to call things like pygame.event.get() else you will get error messages about no video system.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94