3

I have installed vs code and added pygame snippets to use pygame library. My big problem is, every time I try to use any key option of pygame, like pygame.KEYDOWN or pygame.QUIT it tells me that QUIT is not a function of pygame. Can someone help me?

Everything else seems to work, like display or surface even pygame.key.get_pressed() don’t make problems.

import pygame, random, sys
from pygame.locals import *
from pygame.key import *

def set_Background():
    screen = pygame.display.set_mode((500,500))
    surface = pygame.image.load('Background.png')
    surface = pygame.transform.scale(surface, (500, 500))
    screen.blit(surface, (0,0))
    pygame.display.update()
    return screen

def set_Enemy():
    enemy = pygame.image.load('Enemy.png')
    enemy = pygame.transform.scale(enemy, (50, 50))
    return enemy

def set_Player():
    player = pygame.image.load('Player.png')
    player = pygame.transform.scale(player, (70, 70))
    return player

RUNNING = True

while RUNNING:
    background = set_Background()
    enemy = set_Enemy()
    player = set_Player()
    enemy_rect = enemy.get_rect()
    player_rect = player.get_rect()

    e_x = random.randint(10,450)
    e_y = random.randint(10,450)
    background.blit(enemy, (e_x, e_y))
    pygame.display.update()

    for event in pygame.event.get():
        key = pygame.key.get_pressed()
        if event.type == key[pygame.K_ESCAPE]: 
        #module pygame has no K_ESCAPE member
            sys.exit()
        if event.type == pygame.QUIT: 
        #says module pygame has no QUIT member
            sys.exit()
Anton Wolf
  • 31
  • 1
  • 5

3 Answers3

1

pygame.key.get_pressed() shouldn't be in the event loop, but in the main while loop. In the event loop you need to check if the event type is pygame.QUIT and then set the running flag to False.

Here's a fixed version:

import pygame


pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
running = True  # Uppercase names are for constants not variables.

while running:
    # The event loop.
    for event in pygame.event.get():
        # If a pygame.QUIT event is in the queue.
        if event.type == pygame.QUIT:
            running = False
        # To check if it was a `KEYDOWN` event.
        elif event.type == pygame.KEYDOWN:
            # If the escape key was pressed.
            if event.key == pygame.K_ESCAPE:
                running = False

    # Use pygame.key.get_pressed to see if a key is held down.
    # This should not be in the event loop.
    key = pygame.key.get_pressed()
    if key[pygame.K_UP]:
        print('up arrow pressed')

    screen.fill((30, 30, 30))
    pygame.display.flip()
    clock.tick(60)
skrx
  • 19,980
  • 5
  • 34
  • 48
  • PS: Use a [`pygame.time.Clock`](http://www.pygame.org/docs/ref/time.html#pygame.time.Clock) to limit the frame rate. – skrx Mar 27 '18 at 06:55
  • file: 'file:///c%3A/Users/AW/Desktop/VCode_Projects/Snake.py' severity: 'Fehler' message: 'E1101:Module 'pygame' has no 'init' member' at: '5,1' source: 'pylint' code: 'E1101' – Anton Wolf Mar 27 '18 at 07:27
  • the same problem with all other things like QUIT, KEYDOWN, K_UP, K_ESCAPE – Anton Wolf Mar 27 '18 at 07:28
  • I've edited the example to make it runnable. Check out if this version works. – skrx Mar 27 '18 at 07:44
  • i saw i tried it and it still the same problem as i posted, they are all not members of pygame – Anton Wolf Mar 27 '18 at 07:50
  • Are you only getting pylint error messages or Python errors / exceptions? Can you actually run the program or does it crash? – skrx Mar 27 '18 at 07:57
  • it crashes and says pygame.error with ur code, my old code was starting an spawning the enemies but i cant use ESC to close it becouse he is telling me that K_ESCAPE is not a member of pygame – Anton Wolf Mar 27 '18 at 08:09
  • Please post the complete traceback (error message). – skrx Mar 27 '18 at 08:34
  • File "C:\Users\AW\.vscode\extensions\ms-python.python-2018.2.1\pythonFiles\PythonTools\visualstudio_py_util.py", line 95, in exec_code exec(code_obj, global_variables) File "c:\Users\AW\Desktop\VCode_Projects\Snake.py", line 46, in pygame.display.update() pygame.error – Anton Wolf Mar 27 '18 at 08:48
  • Traceback (most recent call last): File "C:\Users\AW\.vscode\extensions\ms-python.python-2018.2.1\pythonFiles\PythonTools\visualstudio_py_launcher_nodebug.py", line 74, in run _vspu.exec_file(file, globals_obj) File "C:\Users\AW\.vscode\extensions\ms-python.python-2018.2.1\pythonFiles\PythonTools\visualstudio_py_util.py", line 119, in exec_file exec_code(code, file, global_variables) – Anton Wolf Mar 27 '18 at 08:50
  • Have you found a solution yet? I searched a bit but couldn't find anything. I'm not familiar with VS Code, so I'm not sure what is causing the errors. Can you run other programs in VS Code without problems? Also, try to run your pygame program from the command-line or in IDLE to check if pygame is installed correctly. – skrx Mar 27 '18 at 19:41
  • ne it still cause the problem, i could run the programm and it worked and spawned the enemies randomly, only thing is not working is the keyboard detection, becouse it tells me its not member of pygame.. – Anton Wolf Mar 28 '18 at 14:17
0

Add from pygame.locals import * at the top of your code.

gnawydna
  • 385
  • 1
  • 5
  • 22
0

You are mixing two types of key presses in one go. You should instead either

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SOMEKEY:
            do_something()

or

keys = pygame.key.get_pressed()
if keys[pygame.K_somekey]:
    do_something()

so the code above with the pygame.key.get_pressed() should not be in the event loop

LMCuber
  • 113
  • 1
  • 11