首页 > 解决方案 > python pygame 让外星人组移动

问题描述

更新 12/7

最后我找到了我不能习惯Group调用方法的原因,因为方法名称必须update()这样Group才能调用。

在我的代码中,我像这样修改然后它起作用了!

def update(self):
     self.x =self.rect.x + self.speed
     self.rect.x = self.x
def position(aliens):
    aliens.update()

==============================================

编辑问题使其清楚

def update(self):
            self.x =self.ai.setting.alien_speed_factor
            self.rect.x = self.x

def update_aliens(aliens):
       aliens.update()

这两个来自书中,我在我的代码中使用它,因为外星人是一个称为错误#AttributeError:'Group'对象没有属性'position'的组

如何让Group外星人执行update()???

我正在写脚本让外星人小组移动,一个是我自己的方式,另一个是书本,当我参考书时发现一个错误,我不确定是不是错了。

预订“ Python 速成课程”第 243 页

我用

def update(aliens):
    for alien in aliens:
        alien.position()

而不是书中的代码:

def update(aliens):
        aliens.position()

返回

>> AttributeError: 'Group' object has no attribute 'position'

那么运行脚本是正确的,那么这是书中的错误吗?还是我使用错误的方式运行原始代码?

这是我的代码:

#!/usr/bin/python
import sys
import pygame as p


class Setting():
    def __init__(self,width,height):
        self.w=width
        self.h=height
        self.flag=p.RESIZABLE
        self.screen=p.display.set_mode((self.w,self.h),self.flag)
        p.display.set_caption("Muhaha")



class Alien(p.sprite.Sprite):
    def __init__(self):
        super().__init__()
        pic=p.image.load("../image/ship.jpg").convert_alpha()
        self.image=p.transform.smoothscale(pic,(100,100))
        self.rect=self.image.get_rect()
        self.x = float(self.rect.x)
        self.rect.x=(self.rect.width)
        self.speed=1
    def create(self,setting,aliens):
        spacex=setting.w-(self.rect.x)*2
        spacey=(setting.h)/2-self.rect.y
        alien_number=int(spacex/(2*(self.rect.width)))
        alien_row=int(spacey/(2*(self.rect.height)))
        for row in range(alien_row):
           for number in range(alien_number):
               alien=Alien()
               alien.rect.x=alien.rect.x+2*alien.rect.width*number
               alien.rect.y=alien.rect.y+2*alien.rect.height*row
               aliens.add(alien)

    def position(self):
        self.x =self.rect.x + self.speed
        self.rect.x = self.x
    def update(aliens):
        aliens.position()        #AttributeError: 'Group' object has no attribute 'position'
                                 #i think the correct code is>> for alien in aliens:
                                 #                              alien.position()

    def blit(setting,aliens):
        aliens.draw(setting.screen)

def game():
    p.init()
    setting=Setting(1200,800)
    alien=Alien()
    aliens=p.sprite.Group()
    alien.create(setting,aliens)
    while True:
         for event in p.event.get():
             if event.type == p.QUIT:
                 sys.exit()
         setting.screen.fill((255,0,0))
         Alien.blit(setting,aliens)
         Alien.update(aliens)
         p.display.flip()
game()

标签: pythonpygame

解决方案


是的,这似乎是一个错误。老实说,整个代码看起来很糟糕。

第一步,从类中删除blitposition函数,将函数更改为:Alienupdate

def update(self):
    self.x = self.rect.x + self.speed
    self.rect.x = self.x  

在主循环中,而不是

     Alien.blit(setting,aliens)
     Alien.update(aliens)

    aliens.update()
    aliens.draw(setting.screen)

调用updateaGroup将调用thatupdate中的所有Sprite实例Group,因此无需手动迭代所有精灵。

同样,调用drawaGroup会将其中的所有精灵绘制GroupSurface作为参数传递的(通常是屏幕表面)。Alien同样,在应该代表单个精灵的类中没有必要这样做。

其他一些问题:

  • Alien每次创建新实例时,都会从磁盘加载精灵的图像
  • 没有 FPS 限制,因此游戏不会以恒定速度运行
  • 通常将您的主要功能命名为“主要”
  • 创建一个实例Alien只是用create实例方法创建更多实例,然后update函数像类方法一样使用

推荐阅读