gpt4 book ai didi

python - 创建自定义 Sprite 类时 Pygame 抛出 "TypeError"

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:17 29 4
gpt4 key购买 nike

我正在尝试创建一个场景,其中图像以不同的速度从屏幕的一侧移动到另一侧,从而创建视差效果。我使用一个名为“BackgroundObject”的 Sprite 类,但是当我尝试使用“BackgroundObject”创建 Sprite 时,我收到一个 TypeError。

我使用的代码如下:

import pygame
from pygame.locals import *
import sys, random, os
import vector

# Background objects (for parallax)
class BackgroundObject(pygame.sprite.Sprite):
def _init_(self, x, y, image, speed):
pygame.sprite.Sprite._init_(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.speed = speed

def update(self, time_passed):
moved_distance = time_passed * self.speed
self.rect.left += moved_distance
if self.rect.right < 0:
self.rect.left= self.rect.width


pygame.init()
screen = pygame.display.set_mode((768, 1024), 0, 32)

obstacleImage = "obstacle.png"
fenceImage = "fences.png"
cloudImage = "clouds.png"
cityImage = "cityscape.png"
groundImage = "bottom.png"
backgroundImage = "background.png"

pathName = "Assets"

# Sprites in the game
obstacles = pygame.sprite.Group()
backgrounds = pygame.sprite.OrderedUpdates()
backgroundOrder = []
backdrop = pygame.image.load(os.path.join(pathName + os.sep + backgroundImage))

fences = pygame.image.load(os.path.join(pathName + os.sep + fenceImage))
clouds = pygame.image.load(os.path.join(pathName + os.sep + fenceImage))
cityscape = pygame.image.load(os.path.join(pathName + os.sep + cityImage))
ground = pygame.image.load(os.path.join(pathName + os.sep + cityImage))

sprite = BackgroundObject(1280, 192, clouds, 192)
backgrounds.add(sprite)
backgrounds.add(BackgroundObject(0, 192, clouds, 192))
backgrounds.add(BackgroundObject(832, 768, cityscape, 154))
backgrounds.add(BackgroundObject(0, 768, cityscape, 154))
backgrounds.add(BackgroundObject(1728, 768, fences, 384))
backgrounds.add(BackgroundObject(0, 768, fences, 384))
backgrounds.add(BackgroundObject(992, 896, ground, 512))
backgrounds.add(BackgroundObject(0, 896, ground, 512))

titleScreen = pygame.image.load(os.path.join(pathName + backgroundImage))

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():
if event.type == QUIT:
exit()

screen.blit(titleScreen, titleScreen.rect)

backgrounds.update(time_passed)

backgrounds.draw()

for item in background:
if item.rect.right < 0:
item.rect.right += (item.rect.width * 2)

我收到的错误消息如下,但我无法理解它:

Traceback (most recent call last):
File "test.py", line 45, in <module>
sprite = BackgroundObject(1280, 192, clouds, 192)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 114, in __init__
if groups: self.add(groups)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 129, in add
else: self.add(*group)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 129, in add
else: self.add(*group)
TypeError: add() argument after * must be a sequence, not int

最佳答案

您拼写错误__init__;前后使用两个下划线:

class BackgroundObject(pygame.sprite.Sprite):
def __init__(self, x, y, image, speed):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.speed = speed

您的回溯显示 BackgroundObject() 直接进入 Sprite.__init__ 而无需先通过您的方法。

关于python - 创建自定义 Sprite 类时 Pygame 抛出 "TypeError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24432956/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com