I have 2 problems with my code:
- Jump assigned on w doesn't work.
- 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()