Python pygame don';t寄存器键输入
我的代码有两个问题:Python pygame don';t寄存器键输入,python,python-3.x,visual-studio-code,pygame,key,Python,Python 3.x,Visual Studio Code,Pygame,Key,我的代码有两个问题: 在w上指定的跳转无效 VisualStudio代码显示了很多错误,我不知道为什么 我将非常感谢你的帮助!有些函数是波兰语的名称,所以如果你想让我翻译它们就没有问题了 我得到的错误: Module 'pygame' has no 'init' member Undefined variable 'K_a' Undefined variable 'K_d' Undefined variable 'QUIT' Module 'pygame' has no 'quit' me
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
它们的屏幕截图链接:
导入pygame
从pygame.locals导入*
导入系统
pygame.init()
vec=pygame.math.Vector2
高度=450
宽度=400
ACC=1
FRIC=-0.12
FPS=60
FramePerSec=pygame.time.Clock()
displaysurface=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“Gierka”)
Gracz类(pygame.sprite.sprite):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.surf=pygame.Surface((30,30))
自冲浪填充((153255,51))
self.rect=self.surf.get_rect(中心=(10415))
self.pos=vec((10285))
self.vel=vec(0,0)
self.acc=vec(0,0)
def ruch(自我):
self.acc=vec(0,0.5)
按下按键=pygame.key.get_pressed()
如果按下按键[K\U a]:
self.acc.x=-acc
打印(“a”)
如果按下按键[K\U d]:
self.acc.x=acc
打印(“d”)
自加速x+=自加速x*FRIC
self.vel+=self.acc
self.pos+=self.vel+0.5*self.acc
如果self.pos.x>宽度:
self.pos.x=0
如果自身位置x<0:
self.pos.x=宽度
def更新(自我):
hits=pygame.sprite.spritecollide(P1,平台,False)
如果点击:
self.pos.y=hits[0].rect.top+1
自我水平y=0
self.rect.midbottom=self.pos
def跳转(自):
自我水平y=-15
类平台(pygame.sprite.sprite):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.surf=pygame.Surface((宽度,20))
自冲浪填充((204102,0))
self.rect=self.surf.get_rect(中心=(宽度/2,高度-10))
PT1=平台()
P1=Gracz()
all_sprites=pygame.sprite.Group()
所有精灵添加(PT1)
所有精灵。添加(P1)
platforms=pygame.sprite.Group()
平台。添加(PT1)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_w:
P1.跳转()
displaysurface.fill((0,150,255))
对于所有_精灵中的实体:
blit(entity.surf、entity.rect)
pygame.display.update()
帧间隔刻度(FPS)
P1.ruch()
P1.更新()
我不确定,但我认为问题在于pygame安装得不好。你是如何安装它的?你用过这个吗
pip install pygame
指挥部
再次尝试插入:)这看起来像是VS代码截图,文件似乎工作正常,这表明它是一个linting问题,因此要链接以前的答案您是否使用
pip install pygame安装pygame
安装了pygame?它的名称为gierka.pyIt,在我运行文件时工作正常(至少它启动时没有错误)。您能跳转吗?bcs i can'tdef update(self):如果P1.vel.y>0,则hits=pygame.sprite.spritecollide(P1,platforms,False):如果hits:self.vel.y=0 self.pos.y=hits[0]。rect.top+1
pip install pygame