首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么电子游戏键盘事件不会发生?

为什么电子游戏键盘事件不会发生?
EN

Stack Overflow用户
提问于 2021-12-06 11:22:35
回答 1查看 35关注 0票数 1

当物体按下上键盘时,我想让它跳起来。但是这个错误不断地弹出,并且它不是work.It是这样一种配置,在这种配置中,列表中存储的照片随着时间的推移而变化,当您按上键盘时会跳转。

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "C:/osp_git/T01/RUN/Run.py", line 123, in main
    all_sprites.update(mt)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pygame\sprite.py", line 531, in update
    sprite.update(*args, **kwargs)
  File "C:/osp_git/T01/RUN/Run.py", line 58, in update
    if userInput[pygame.K_UP] and not self.SM_jump:
TypeError: 'float' object is not subscriptable

错误发生在def update()中。

该更新函数是接收键盘输入的函数,跳转函数是跳转函数。

这是我的完整密码。

代码语言:javascript
运行
复制
import pygame

pygame.init()

SCREEN_H = 560
SCREEN_W = 1000
SCREEN = pygame.display.set_mode((SCREEN_W,SCREEN_H))

BG01 = pygame.image.load("IMG/background.jpg")
BG02 = BG01.copy()

clock = pygame.time.Clock()


class MeltingSnowman(pygame.sprite.Sprite):
    SnowMan_X = 80
    SnowMan_Y = 419
    position = (SnowMan_X, SnowMan_Y)
    JUMP_VEL = 8

    def __init__(self, position):

        SnowMan_X = 80
        SnowMan_Y = 419
        position = (SnowMan_X, SnowMan_Y)

        self.SM_jump = False
        self.walk_index = 0
        self.jump_vel = self.JUMP_VEL

        super(MeltingSnowman, self).__init__()
        size = (100, 100)

        images = [(pygame.image.load('IMG/DinoRun1.png')),
                  (pygame.image.load('IMG/DinoRun2.png')),
                  (pygame.image.load('IMG/DinoJump.png')),
                  (pygame.image.load('IMG/DinoDuck1.png')),
                  (pygame.image.load('IMG/DinoDuck2.png'))]

        self.rect = pygame.Rect(position, size)
        self.rect.x = self.SnowMan_X
        self.rect.y = self.SnowMan_Y
        self.images = [pygame.transform.scale(image, size) for image in images]

        self.index = 0
        self.image = images[self.index]

        self.animation_time = 1
        self.current_time = 0

    def update(self, userInput):
        if self.SM_jump:
            self.jump()

        if self.walk_index >= 10:
            self.walk_index = 0

        if userInput[pygame.K_UP] and not self.SM_jump:
            self.SM_jump = True
        elif userInput[pygame.K_DOWN] and not self.SM_jump:
            self.SM_jump = False
        elif not (self.SM_jump or userInput[pygame.K_DOWN]):
            self.SM_jump = False

    def jump(self):
        self.image = self.images[self.index]
        if self.SM_jump:
            self.rect.y -= self.jump_vel * 4
            self.jump_vel -= 1
            MeltingSnowman.SnowMan_Y = 100

        if self.jump_vel < - 8:
            self.SM_jump = False
            self.jump_vel = self.JUMP_VEL
            MeltingSnowman.SnowMan_Y = 400


    def run(self, mt):
        self.current_time += mt

        if self.current_time >= self.animation_time:
            self.current_time = 0

            self.index += 1
            if self.index == len(self.images):
                GameOver()
                pygame.display.flip()
                pygame.time.delay(2000)
                pygame.quit()
                exit()

            self.image = self.images[min(self.index, len(self.images) - 1)]


    def draw(self, SCREEN):
        SCREEN.blit(self.image, (self.rect.x, self.rect.y))

def GameOver():
    font = pygame.font.Font('NanumGothic.ttf', 30)
    GAMEOVER = font.render("GAME OVER", True, (255,255,255))
    SCREEN.blit(GAMEOVER, (400, 250))

def Background(BG, x, y):
    global SCREEN, BG01
    SCREEN.blit(BG01, (x, y))

def main():

    player = MeltingSnowman(position=(80, 419))
    all_sprites = pygame.sprite.Group(player)

    BG01_x = 0
    BG02_x = SCREEN_W

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        mt = clock.tick(100) / 1000

        all_sprites.update(mt)
        all_sprites.draw(SCREEN)
        pygame.display.update()

        BG01_x -= 4; BG02_x -= 4

        if BG01_x == -SCREEN_W:
            BG01_x = SCREEN_W
        if BG02_x == -SCREEN_W:
            BG02_x = SCREEN_W

        Background(BG01, BG01_x, 0)
        Background(BG02, BG02_x, 0)

        clock.tick(30)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-06 11:54:29

调用all_sprites.update(mt)。因此,update的实际论据是mt

您必须将pygame.key.get_pressed的返回值传递给update

代码语言:javascript
运行
复制
keys = pygame.key.get_pressed()
all_sprites.update(keys)

注意,如果您有不同的Sprite类和不同的Update方法,则必须将Sprite放在不同的组中,并在每个组上单独调用update。或者,您需要确保每个update方法都有相同的参数,或者需要使用关键字参数。

pygame.sprite.Group.draw()pygame.sprite.Group.update()pygame.sprite.Group提供的方法。

后者将委托给包含pygame.sprite.Spritespygame.sprite.Sprites方法--您必须实现该方法。请参阅pygame.sprite.Group.update()

对组中的所有Sprite调用update()方法。..。

前者使用包含的imagerect属性来绘制对象--您必须确保pygame.sprite.Sprite具有所需的属性。请参阅pygame.sprite.Group.draw()

将包含的Sprite绘制到Surface参数。这使用了源面的Sprite.image属性和Sprite.rect。.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70244705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档