python - 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()
解决方案
是的,这似乎是一个错误。老实说,整个代码看起来很糟糕。
第一步,从类中删除blit
和position
函数,将函数更改为:Alien
update
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)
调用update
aGroup
将调用thatupdate
中的所有Sprite
实例Group
,因此无需手动迭代所有精灵。
同样,调用draw
aGroup
会将其中的所有精灵绘制Group
到Surface
作为参数传递的(通常是屏幕表面)。Alien
同样,在应该代表单个精灵的类中没有必要这样做。
其他一些问题:
Alien
每次创建新实例时,都会从磁盘加载精灵的图像- 没有 FPS 限制,因此游戏不会以恒定速度运行
- 通常将您的主要功能命名为“主要”
- 创建一个实例
Alien
只是用create
实例方法创建更多实例,然后update
函数像类方法一样使用
推荐阅读
- python - 附加到空列表会产生意想不到的结果
- python - 使用 PythonNet 将 Bytes[] 转换为 Python 字节
- corda - corda 4.0 - 部署更新的cordpp,修改了流程
- python - 这个表达式将如何对结果进行排序 sorted(list1 ,key = lambda x:-x[0])
- java - 使用 FirefoxDriver 时的 Selenium 空白浏览器
- php - 需要支持来修复以下 XML 解析器问题。我尝试使用 xpath & var_dump 但没有成功
- kotlin - 我可以在 Windowed KTable 上调用 mapValues 来创建另一个具有类似行为的 Windowed KTable 吗?
- kubernetes - Jenkins 无法在 EKS 集群内部署应用程序
- python - 子类化 numpy ndarray 无法正常工作
- tensorflow - Keras 设置数据集