Can't import any pygame functions (E1101)

2019-07-26 14:15发布

问题:

When I run the following code, a white screen appears and then quickly disappears. I installed pygame on the VSCode terminal with pip3 (I am on mac) and now that I finally got it to import pygame, I get multiple errors for every function called such as "clock" or "pygame.KEYDOWN". They say "Module 'pygame' has no 'KEYDOWN' member" [E1101]. I also get an error with the "init" member.

I've seen other posts that tell me to copy and paste something into the json settings, but when I tried that I got even more errors.

#Game

#By Robert Smith

#Initialize python
import pygame
pygame.init()

#Set the screen
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
screen.blit(background, (0 , 0))

#Make the window exitable
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        mainloop == False #close the window
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            mainloop = False

#Set the framerate
milliseconds = clock.tick(60)#DO NOT GO FASTER THAN THIS FRAMERATE

回答1:

Try this:

keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
    mainloop == False #close the window
elif event.type == keys[pygame.K_DOWN]:
    if event.key == keys[pygame.K_ESCAPE]:
        mainloop = False

And this:

milliseconds = pygame.time.Clock.tick(60)