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.