Python-loop增加列表中的一个值附加到另一个列表,只需添加sum';总共
人们!我正试图编写一个SpaceInvaders Python程序,以达到学习的目的,但在循环内的列表和数据操作问题上一直步履蹒跚。 我刚刚创建了一个Squid类,将圆的数据封装到PyGame中,格式为:RGB,[X,Y]intPython-loop增加列表中的一个值附加到另一个列表,只需添加sum';总共,python,list,loops,increment,Python,List,Loops,Increment,人们!我正试图编写一个SpaceInvaders Python程序,以达到学习的目的,但在循环内的列表和数据操作问题上一直步履蹒跚。 我刚刚创建了一个Squid类,将圆的数据封装到PyGame中,格式为:RGB,[X,Y]int class Space_Ship: def space_ship(): space_ship_X = 100 space_ship_Y = 550 space_ship_color = (255, 255, 2
class Space_Ship:
def space_ship():
space_ship_X = 100
space_ship_Y = 550
space_ship_color = (255, 255, 255)
space_ship_radius = 10
space_ship = space_ship_color, [space_ship_X, space_ship_Y], space_ship_radius
return space_ship
稍后我需要一个squid的小队,所以我创建了一个空列表,并尝试在将“squid”添加到列表之前增加squid_X值,但计数器只对总数(600)求和,并将其添加到列表中的每个元素。。。[(255,0,0),[600100],10]
from invader_squid.squid import Squid
from space_ship.space_ship import Space_Ship
import pygame
from pygame.constants import K_LEFT, K_RIGHT
pygame.init()
pygame.display.set_caption("Space Invaders - for poors")
clock = pygame.time.Clock()
FPS = 60
color_background = (0, 0, 0)
screen = pygame.display.set_mode((800, 600))
''' space contenders '''
space_ship = Space_Ship.space_ship()
squid = Squid.squid()
''' game logic '''
space_ships = 1
squids = 10
crabs = 10
octopusses = 5
mother_ships = 3
循环:
提前谢谢!
欢迎任何深化本主题的阅读资料 以下是我的建议:
使用类Squid作为Squid的蓝图,并从类中创建Squid实例。每个squid实例都有一个位置、一个半径(如果未指定,则有一个默认值)和一个颜色(也有一个默认值)
然后我们生成新的鱿鱼,并将它们添加到一个列表中,即我们的小队。在这样做的时候,我们使用迭代索引作为我们各自的X位置,同时保持Y值不变——只是为了举例
classsquid:
定义初始值(self,x,y,半径=10,颜色=(255,0,0)):
#将此函数中传递的所有值绑定到我们在此构造的对象
self.x=x
self.y=y
自半径=半径
self.color=颜色
定义(自我):
#当我们调用str(mySquid)时会发生什么?
#让我们返回一些人类可读的东西
#您可以在调用repr(mySquid)时将其与输出进行比较
返回f'Squid([x={self.x},y={self.y}],r={self.radius},col={self.color})'
乌贼数量=10
班次=[]
#重复num_squids=10次
y=10
对于范围内的i(num_squids):
sq=鱿鱼(i,y)
小队附加(sq)
打印(f'Squid{i+1}准备就绪:{sq}!')
打印()
打印(“这是我的团队…”)
对于小队中的乌贼:
打印(str(squid))
输出:
Squid 1 is ready: Squid([x=0, y=10], r=10, col=(255, 0, 0))!
Squid 2 is ready: Squid([x=1, y=10], r=10, col=(255, 0, 0))!
Squid 3 is ready: Squid([x=2, y=10], r=10, col=(255, 0, 0))!
Squid 4 is ready: Squid([x=3, y=10], r=10, col=(255, 0, 0))!
Squid 5 is ready: Squid([x=4, y=10], r=10, col=(255, 0, 0))!
Squid 6 is ready: Squid([x=5, y=10], r=10, col=(255, 0, 0))!
Squid 7 is ready: Squid([x=6, y=10], r=10, col=(255, 0, 0))!
Squid 8 is ready: Squid([x=7, y=10], r=10, col=(255, 0, 0))!
Squid 9 is ready: Squid([x=8, y=10], r=10, col=(255, 0, 0))!
Squid 10 is ready: Squid([x=9, y=10], r=10, col=(255, 0, 0))!
Here is my squad...
Squid([x=0, y=10], r=10, col=(255, 0, 0))
Squid([x=1, y=10], r=10, col=(255, 0, 0))
Squid([x=2, y=10], r=10, col=(255, 0, 0))
Squid([x=3, y=10], r=10, col=(255, 0, 0))
Squid([x=4, y=10], r=10, col=(255, 0, 0))
Squid([x=5, y=10], r=10, col=(255, 0, 0))
Squid([x=6, y=10], r=10, col=(255, 0, 0))
Squid([x=7, y=10], r=10, col=(255, 0, 0))
Squid([x=8, y=10], r=10, col=(255, 0, 0))
Squid([x=9, y=10], r=10, col=(255, 0, 0))
请浏览、和,以了解此网站的工作原理,并帮助您改进当前和未来的问题,从而帮助您获得更好的答案。“演示如何解决此编码问题?”与堆栈溢出无关。您必须诚实地尝试解决方案,然后询问有关实现的特定问题。堆栈溢出并不是为了取代现有的教程和文档。非常简单,在您深入到更广泛的环境(如游戏)中实现它之前,您需要返回到有关类和实例的资料,并学习使用该概念的基础知识。没有
\uuuu init\uuuu
方法的类严重表明:(1)您不清楚如何使用类,(2)您的概念不应该是一个类,或者(3)您有一个奇怪的应用程序,并且会问一些比如何处理累积和更深奥的问题。这超出了堆栈溢出的范围。要求阅读参考资料被明确列为离题。但是,一旦掌握了类和实例的基本知识,您可以尝试查看PyGame和其他动画支持包的功能。变量squid
未在print('squid'+str(squid[1][0])
中定义。非常感谢!这很完美,但我一直被代码中的求和问题弄糊涂。你知道吗?恐怕你帖子里的代码不完整。在while
循环中,您使用的是一个变量squid
,它在任何地方都没有定义。所以代码本身不会像这样运行。你可以更新这篇文章,我可以和你一起仔细看看。我只是拍一个片段。现在它完成了。我真的很感谢你的努力和时间,我会认真对待你的建议。
squids = 10
squadron_squids = []
i = 0
while i < squids:
print('squid ' + str( squid[1][0]))
squid[1][0] += 50
print('New squid ' + str( squid[1][0]))
squadron_squids.append(squid)
i += 1
print('squid_X: ' + str(squid[1][0]) + '\nsquadron_squids ' + str(squadron_squids))
loop = True
while loop:
clock.tick(FPS)
# mandatory exit condition
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
# screen draw
screen.fill(color_background)
# space_ship
pygame.draw.circle(screen, *space_ship) # *args 'scatter'
# squid
'''for i in range(squids):
pygame.draw.circle(screen, squadron_squids[i])
'''
'''
MOVEMENT
user input - move left or right
circle can't surpass screen size
'''
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
space_ship[1][0] -= 5
if space_ship[1][0] < 10:
space_ship[1][0] = 10
if keys[K_RIGHT]:
space_ship[1][0] += 5
if space_ship[1][0] > 790:
space_ship[1][0] = 790
pygame.display.update()
pygame.quit()
squid_X 100
New squid_X 150
squid_X 150
New squid_X 200
squid_X 200
New squid_X 250
squid_X 250
New squid_X 300
squid_X 300
New squid_X 350
squid_X 350
New squid_X 400
squid_X 400
New squid_X 450
squid_X 450
New squid_X 500
squid_X 500
New squid_X 550
squid_X 550
New squid_X 600
squid_X: 600
squadron_squids [[(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10], [(255, 0, 0), [600, 100], 10]]
Squid 1 is ready: Squid([x=0, y=10], r=10, col=(255, 0, 0))!
Squid 2 is ready: Squid([x=1, y=10], r=10, col=(255, 0, 0))!
Squid 3 is ready: Squid([x=2, y=10], r=10, col=(255, 0, 0))!
Squid 4 is ready: Squid([x=3, y=10], r=10, col=(255, 0, 0))!
Squid 5 is ready: Squid([x=4, y=10], r=10, col=(255, 0, 0))!
Squid 6 is ready: Squid([x=5, y=10], r=10, col=(255, 0, 0))!
Squid 7 is ready: Squid([x=6, y=10], r=10, col=(255, 0, 0))!
Squid 8 is ready: Squid([x=7, y=10], r=10, col=(255, 0, 0))!
Squid 9 is ready: Squid([x=8, y=10], r=10, col=(255, 0, 0))!
Squid 10 is ready: Squid([x=9, y=10], r=10, col=(255, 0, 0))!
Here is my squad...
Squid([x=0, y=10], r=10, col=(255, 0, 0))
Squid([x=1, y=10], r=10, col=(255, 0, 0))
Squid([x=2, y=10], r=10, col=(255, 0, 0))
Squid([x=3, y=10], r=10, col=(255, 0, 0))
Squid([x=4, y=10], r=10, col=(255, 0, 0))
Squid([x=5, y=10], r=10, col=(255, 0, 0))
Squid([x=6, y=10], r=10, col=(255, 0, 0))
Squid([x=7, y=10], r=10, col=(255, 0, 0))
Squid([x=8, y=10], r=10, col=(255, 0, 0))
Squid([x=9, y=10], r=10, col=(255, 0, 0))