1

I'm trying to create a sidescroller game in pygame and it just makes a black screen when it should make a player sprite walking across a screen which increases speed as time goes along.

When I debug in vscode it comes up with E1101:Module 'pygame' has no 'QUIT' member.

QUIT seems to be in the event.get module so I don't know why this is happening

I'm following the code from https://www.youtube.com/watch?v=PjgLeP0G5Yw&index=1&list=PLzMcBGfZo4-nh5Txa7ypUiv_4EVq4dgKc

import pygame
from pygame.locals import *
import os
import sys
import math

pygame.init()

W, H = 800, 447
win = pygame.display.set_mode((W,H))
pygame.display.set_caption('Side Scroller')

bg = pygame.image.load(os.path.join('images','bg.png')).convert()
bgX = 0
bgX2 = bg.get_width()

clock = pygame.time.Clock()

class player(object):
    run = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(8,16)]
    jump = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(1,8)]
    slide = [pygame.image.load(os.path.join('images', 'S1.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S3.png')), pygame.image.load(os.path.join('images', 'S4.png')), pygame.image.load(os.path.join('images', 'S5.png'))]
    jumpList = [1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4]
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.jumping = False
        self.sliding = False
        self.slideCount = 0
        self.jumpCount = 0
        self.runCount = 0
        self.slideUp = False

    def draw(self, win):
        if self.jumping:
            self.y -= self.jumpList[self.jumpCount] * 1.2
            win.blit(self.jump[self.jumpCount//18], (self.x,self.y))
            self.jumpCount += 1
            if self.jumpCount > 108:
                self.jumpCount = 0
                self.jumping = False
                self.runCount = 0
        elif self.sliding or self.slideUp:
            if self.slideCount < 20:
                self.y += 1
            elif self.slideCount == 80:
                self.y -= 19
                self.sliding = False
                self.slideUp = True
            if self.slideCount >= 110:
                self.slideCount = 0
                self.slideUp = False
                self.runCount = 0
            win.blit(self.slide[self.slideCount//10], (self.x,self.y))
            self.slideCount += 1

        else:
            if self.runCount > 42:
                self.runCount = 0
            win.blit(self.run[self.runCount//6], (self.x,self.y))
            self.runCount += 1

def redrawWindow():
    win.blit(bg,(bgX,0)) #pastes background onto 0,0
    win.blit(bg, (bgX2,0)) #pastes background onto width,0
    #runner.draw(win)
    pygame.display.update

runner = player(200,313,64,64)
pygame.time.set_timer(USEREVENT+1,500) #sets userevent to be true every half a second
speed = 30
run = True
while run:
    redrawWindow()

    bgX -= 1.4
    bgX2 -= 1.4
    if bgX < bg.get_width() * -1:
        bgX = bg.get_width()
    if bgX2 < bg.get_width() * -1:
        bgX2 = bg.get_width()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()
        if event.type == USEREVENT+1:
            speed +=1

    clock.tick(speed)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Monty
  • 107
  • 1
  • 3
  • 9
  • looks like you import the locals directly, try with `if event.type == QUIT:` – PRMoureu Oct 28 '18 at 10:19
  • 2
    Possible duplicate of [Imports failing in VScode for pylint when importing pygame](https://stackoverflow.com/questions/53012461/imports-failing-in-vscode-for-pylint-when-importing-pygame) – skrx Oct 28 '18 at 19:09

2 Answers2

2

As @PRMoureu said, you imported everything (including QUIT) from pygame.locals directly, so you don't need a namespace before it.

if event.type == QUIT
0

"E1101:Module 'pygame' has no 'QUIT' member" is a lint error. PyLint (which VSCode runs to check your code for ['lint'](https://en.wikipedia.org/wiki/Lint_(software), reports this as it's not able to find the constant definition.

In this case, it's due to it being in a c extension module which pylint needs to do something special with, so you can either tell the linter to load the module or you can disable this for your project by adding c-extension-no-member to the list of disabled checks.

To get pylint to detect this constant cleanly, you should reference the constants with the prefix pygame.constants, e.g. pygame.constants.QUIT

Of course, you can ignore this as it's a suggestion to help you improve your coding standards and not an error in your code, do read the answer suggested by @skrx (Imports failing in VScode for pylint when importing pygame) which has some good suggestions.

Murf
  • 11
  • 2