0

I have 2 problems with my code:

  1. Jump assigned on w doesn't work.
  2. Visual studio code shows a lot of errors and I don't know why.

I will really appreciate your help! Some functions are names in polish so if you want me translate them there is no problem!

Errors I get:

Module 'pygame' has no 'init' member   
Undefined variable 'K_a'
Undefined variable 'K_d'
Undefined variable 'QUIT'
Module 'pygame' has no 'quit' member
Module 'pygame' has no 'KEYDOWN' member
Module 'pygame' has no 'K_w' member

Link for screenshot of them : https://i.stack.imgur.com/2miss.png

import pygame
from pygame.locals import *
import sys
 
pygame.init()
vec = pygame.math.Vector2 
 
HEIGHT = 450
WIDTH = 400
ACC = 1
FRIC = -0.12
FPS = 60
 
FramePerSec = pygame.time.Clock()
 
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gierka")

class Gracz(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.surf = pygame.Surface((30,30))
        self.surf.fill((153, 255, 51))
        self.rect = self.surf.get_rect(center=(10,415))

        self.pos = vec((10, 285))
        self.vel = vec(0,0)
        self.acc = vec(0,0)

    def ruch(self):
        self.acc = vec(0,0.5)

        pressed_keys = pygame.key.get_pressed()

        if pressed_keys[K_a]:
            self.acc.x = -ACC
            print("a")
        if pressed_keys[K_d]:
            self.acc.x = ACC
            print("d")

        self.acc.x += self.vel.x * FRIC
        self.vel += self.acc
        self.pos += self.vel + 0.5 * self.acc

        if self.pos.x > WIDTH:
            self.pos.x = 0
        if self.pos.x < 0:
            self.pos.x = WIDTH

    def update(self):
        hits = pygame.sprite.spritecollide(P1 , platforms, False)
        if hits:
            self.pos.y = hits[0].rect.top + 1
            self.vel.y = 0

        self.rect.midbottom = self.pos
    
    def jump(self):
        self.vel.y = -15

class platforma(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.surf = pygame.Surface((WIDTH,20))
        self.surf.fill((204, 102, 0))
        self.rect = self.surf.get_rect(center=(WIDTH/2,HEIGHT - 10))
    
    

PT1 = platforma()
P1 = Gracz()

all_sprites = pygame.sprite.Group()
all_sprites.add(PT1)
all_sprites.add(P1)

platforms = pygame.sprite.Group()
platforms.add(PT1)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:    
            if event.key == pygame.K_w:
                P1.jump()

    displaysurface.fill((0, 150, 255))

    for entity in all_sprites:
        displaysurface.blit(entity.surf, entity.rect)
    
    pygame.display.update()
    FramePerSec.tick(FPS)

    P1.ruch()
    P1.update()
Anurag A S
  • 725
  • 10
  • 23
kiciarozek
  • 47
  • 6

2 Answers2

0

I'm not sure, but I think the problem is that pygame isn't well-installed. How did you try to install it ? Did you use the

pip install pygame

command ?

Try intalling it again :)

Cold Fire
  • 91
  • 5
0

This looks like VS code screenshot and the file seems to work indicating its a linting issue therefor gonna link a previous answer Why does it say that module pygame has no init member?

sachsom
  • 383
  • 2
  • 18