微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame 不注册按键输入

如何解决pygame 不注册按键输入

我的代码有两个问题:

  1. 分配给 w 的跳转不起作用。
  2. Visual Studio 代码显示了很多错误,我不知道为什么。

非常感谢您的帮助!有些函数是波兰语名称,所以如果你想让我翻译它们,没有问题!

我得到的错误

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

他们截图的链接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()

解决方法

我不确定,但我认为问题是 pygame 没有安装好。你是如何尝试安装它的?你用过

pip install pygame

命令?

尝试重新安装它:)

,

这看起来像 VS 代码屏幕截图,并且该文件似乎可以正常工作,表明它存在 linting 问题,因此将链接之前的答案 Why does it say that module pygame has no init member?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。