gpt4 book ai didi

python - Pygame:收到一个错误,表明 Sprite 是不可迭代的

转载 作者:行者123 更新时间:2023-12-03 23:39:07 26 4
gpt4 key购买 nike

我正在编写一个 pygame 代码来制作汽车游戏。这是一款基本的汽车游戏,英雄 Sprite 汽车应该避免与敌方 Sprite 接触。因此,对于碰撞部分,我们必须为敌车和英雄车创建一个 mask 。

代码如下:

import pygame
from pygame.locals import *
import sys
import random



road = pygame.image.load(r"C:\Users\Binoy\Downloads\ezgif.com-gif-maker-0.jpg")
screen = pygame.display.set_mode((800, 600))
class Enemy(pygame.sprite.Sprite):
def __init__(self,image):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load(image)
self.rect=self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)


class Hero(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("Untitled.png")
self.rect=self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
hero=Hero()
enemy = Enemy("enemy.png")
enemy2 = Enemy("enemy 2.0.png")
enemy3 = Enemy("enemy 3.0.png")
enemy4 = Enemy("enemy 4.0.png")

if pygame.sprite.spritecollide(hero, enemy2, False, pygame.sprite.collide_mask):
print("sprites have collided!")
velocity = 2
roadx = 0
roady = 0
x=200
y=400
y1=100
y2=350

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type== pygame.KEYDOWN and event.key ==pygame.K_LEFT:
x-=200
if x==-200:
x=0
elif event.type== pygame.KEYDOWN and event.key ==pygame.K_RIGHT:
x+=200
if x==800:
x=600


roady = roady + velocity
if roady == 600:
roady = 0
y=y-2
y1-=2
y2-=2
if y==0:
y=600
if y1==0:
y1=600
if y2==0:
y2=600
screen.blit(road, [roadx, roady -600])
screen.blit(road, [roadx, roady])
screen.blit(pygame.transform.scale(enemy.image , (150, 150)), [0,y1-600])
screen.blit(pygame.transform.scale(enemy.image , (150, 150)), [0,y1])
screen.blit(pygame.transform.scale(enemy2.image , (150, 150)), [400,y1-600])
screen.blit(pygame.transform.scale(enemy2.image , (150, 150)), [400,y1])
screen.blit(pygame.transform.scale(enemy3.image, (150, 150)), [610, y2-600])
screen.blit(pygame.transform.scale(enemy3.image , (150, 150)), [610, y2])
screen.blit(pygame.transform.scale(enemy4.image, (150, 150)), [200, y-600])
screen.blit(pygame.transform.scale(enemy4.image , (150, 150)), [200, y])
screen.blit(pygame.transform.scale(hero.image , (150, 150)), [x, 250])
pygame.display.update()

但是在运行脚本时它返回错误:

Traceback (most recent call last):


File "c:\Users\USERNAME\game.py", line 30, in <module>
if pygame.sprite.spritecollide(hero, enemy2, False, pygame.sprite.collide_mask):
File "C:\Users\USERNAME\AppData\Roaming\Python\Python39\site-packages\pygame\sprite.py", line 1665, in spritecollide
return [group_sprite
TypeError: 'Enemy' object is not iterable

所以任何人都可以在这段代码中帮助我们并给出错误的解决方案。谢谢。

最佳答案

spritecollide 的第二个参数必须是 pygame.sprite.Group .与敌人创建一个Group并进行碰撞测试:

hero=Hero()
enemy = Enemy("enemy.png")
enemy2 = Enemy("enemy 2.0.png")
enemy3 = Enemy("enemy 3.0.png")
enemy4 = Enemy("enemy 4.0.png")

enemies = pygame.sprite.Group([enemy, enemy2, enemy3, enemy4])
if pygame.sprite.spritecollide(hero, enemies, False, pygame.sprite.collide_mask):
print("sprites have collided!")

对于 2 pygame.sprite.Sprite 的碰撞您可以使用的对象 pygame.sprite.collide_mask直接:

if pygame.sprite.collide_mask(hero, enemy):
print("sprites have collided!")

关于python - Pygame:收到一个错误,表明 Sprite 是不可迭代的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67123433/

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