为什么我用for循环迭代精灵组找不到自己定义的方法和属性
最近看了小甲鱼飞机大战的视频,自己也在做,可是我的for循环迭代精灵组找不到自己定义的属性和方法。那几段代码我几乎是照搬小甲鱼的代码了,可还是不行,pycharm没给出提示。有谁能帮我解答吗?困扰我很久了。侧重要看的代码旁备注了关注点(总共五个关注点),方便各位找出问题,
可以不用看全部的代码。
from PlaneModule import *
from pygame.locals import *
from enemy import*
pygame.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((480,800))
pygame.display.set_caption("飞机大战")
#加载声音文件
pygame.mixer.music.load("venv/planesound/game_music.ogg")
pygame.mixer.music.play(-1)
#创建背景精灵
bg1=Background()
bg2=Background()
bg2.rect.top=-800
bg=pygame.sprite.Group(bg1,bg2)
background=pygame.image.load("venv/bg.png")
bg_w,bg_h=background.get_size()
#创建玩家
myplane=Myplane(5)
#创建敌机精灵组
enemies = pygame.sprite.Group()
minenemy=pygame.sprite.Group()
midenemy=pygame.sprite.Group()
maxenemy=pygame.sprite.Group() (关注点)
#创建敌机机群的函数
def make_mineneny(group1,group2,number):
for e in range(number):
e=Minenemy(bg_h)
group1.add(e)
group2.add(e)
def make_mideneny(group1,group2,number):
for e in range(number):
e=Midenemy(bg_h)
group1.add(e)
group2.add(e)
def make_maxeneny(group1,group2,number): (关注点)
for e in range(number):
e=Maxenemy(bg_h)
group1.add(e)
group2.add(e)
#创建子弹
commonbullet=[]
commonbullet_index=0
commonbullet_num=4
for i in range(commonbullet_num):
commonbullet.append(Commonbullet(myplane.rect.midtop))
make_mineneny(minenemy, enemies, 15)
make_mideneny(midenemy, enemies, 4)
make_maxeneny(maxenemy, enemies, 2) (关注点)
#用于延迟
delay=100
#中弹图片索引
e1_destroy_index = 0
e2_destroy_index = 0
e3_destroy_index = 0
me_destroy_index = 0
while True:
delay-=1
if delay<0:
delay=100
if not(delay % 15):
commonbullet.reset(myplane.rect.midtop)
commonbullet_index=(commonbullet_index+1)%commonbullet_num
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
exit()
keydown=pygame.key.get_pressed()
if keydown:
myplane.moveup()
if keydown:
myplane.movedown()
if keydown:
myplane.moveleft()
if keydown:
myplane.moveright()
for each in maxenemy: (关注点)
each.h
peng=pygame.sprite.spritecollide(myplane,enemies,True)
if peng:
pygame.quit()
exit()
bg.update()
bg.draw(screen)
for b in commonbullet:
b.move()
screen.blit(b.image,b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, True)
screen.blit(myplane.image,myplane.rect)
maxenemy.update()
maxenemy.draw(screen)
midenemy.update()
midenemy.draw(screen)
minenemy.update()
minenemy.draw(screen)
pygame.display.update()
clock.tick(60)
飞机大战要用到的类
from random import *
import pygame
#获取敌机大小
enemy_plane = pygame.image.load("venv/planeimage/enemy1.png")
enemy_x,enemy_y=enemy_plane.get_size()
#背景类
class Background(pygame.sprite.Sprite):
def __init__(self,speed=1):
super().__init__()
self.image = pygame.image.load("venv/bg.png").convert_alpha()
self.rect = self.image.get_rect()
self.speed=speed
def update(self):
self.rect.top += self.speed
if self.rect.top>=800:
self.rect.top=-800
#小型敌机类
class Minenemy(pygame.sprite.Sprite):
def __init__(self,h,speed=3):
super().__init__()
self.image = pygame.image.load("venv/planeimage/enemy1.png").convert_alpha()
self.rect = self.image.get_rect()
self.speed=speed
self.h=h
self.rect.left=randint(0, 480-enemy_x)
self.rect.top=randint(-4*self.h, -self.h)
self.mask=pygame.mask.from_surface(self.image)
def update(self):
self.rect.top+=self.speed
if self.rect.top>800:
self.rect.left = randint(0, 480-enemy_x)
self.rect.top = randint(-4*self.h, -self.h)
#中型敌机类
class Midenemy(pygame.sprite.Sprite):
def __init__(self,h,speed=2):
super().__init__()
self.image = pygame.image.load("venv/planeimage/enemy2.png").convert_alpha()
self.rect = self.image.get_rect()
self.speed = speed
self.h = h
self.rect.left = randint(0, 480 - enemy_x)
self.rect.top = randint(-8 * self.h, -self.h)
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect.top += self.speed
if self.rect.top > 800:
self.rect.left = randint(0, 480 - enemy_x)
self.rect.top = randint(-8 * self.h, -self.h)
#大型敌机类
class Maxenemy(pygame.sprite.Sprite): (关注点)
def __init__(self,h,speed=1):
super().__init__()
self.image = pygame.image.load("venv/planeimage/enemy3_n1.png").convert_alpha()
self.rect = self.image.get_rect()
self.speed = speed
self.h = h
self.rect.left = randint(0, 480 - enemy_x)
self.rect.top = randint(-10*self.h, -self.h)
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect.top += self.speed
if self.rect.top > 800:
self.rect.left = randint(0, 480 - enemy_x)
self.rect.top = randint(-10*self.h, -self.h)
#玩家类
class Myplane(pygame.sprite.Sprite):
def __init__(self,speed=2):
super().__init__()
self.image = pygame.image.load("venv/plane.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.top=600
self.rect.centerx=240
self.speed=speed
self.live=True
self.mask = pygame.mask.from_surface(self.image)
def moveup(self):
if self.rect.top>0:
self.rect.top-=self.speed
else:
self.rect.top=0
def movedown(self):
if self.rect.bottom<800:
self.rect.bottom+=self.speed
else:
self.rect.bottom=800
def moveleft(self):
if self.rect.left>0:
self.rect.left-=self.speed
else:
self.rect.left=0
def moveright(self):
if self.rect.right<480:
self.rect.right+=self.speed
else:
self.rect.right=480
#子弹类
class Commonbullet(pygame.sprite.Sprite):
def __init__(self,position,speed=12):
super().__init__()
self.image=pygame.image.load("venv/planeimage/bullet1.png")
self.rect=self.image.get_rect()
self.rect.left,self.rect.top=position
self.speed=speed
self.live=True
def move(self):
self.rect.top-=self.speed
if self.rect.top<0:
self.live=False
def reset(self,position):
self.rect.left,self.rect.top=position
self.live=True 感觉问题在第四个关注点处,不知道是不是 不能懒 发表于 2021-8-8 22:22
感觉问题在第四个关注点处,不知道是不是
是第四个,我想知道的是为什么会出现问题。
页:
[1]