当物体按下上键盘时,我想让它跳起来。但是这个错误不断地弹出,并且它不是work.It是这样一种配置,在这种配置中,列表中存储的照片随着时间的推移而变化,当您按上键盘时会跳转。
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()中。
该更新函数是接收键盘输入的函数,跳转函数是跳转函数。
这是我的完整密码。
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)发布于 2021-12-06 11:54:29
调用all_sprites.update(mt)。因此,update的实际论据是mt。
您必须将pygame.key.get_pressed的返回值传递给update
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.Sprites的pygame.sprite.Sprites方法--您必须实现该方法。请参阅pygame.sprite.Group.update()
对组中的所有Sprite调用
update()方法。..。
前者使用包含的image和rect属性来绘制对象--您必须确保pygame.sprite.Sprite具有所需的属性。请参阅pygame.sprite.Group.draw()
将包含的Sprite绘制到Surface参数。这使用了源面的
Sprite.image属性和Sprite.rect。.
https://stackoverflow.com/questions/70244705
复制相似问题