Python - 循环递增列表中的值附加到另一个列表只需添加总和的总和
Python - loop to increment a value in list append to another list just add sum's total
人!我正在尝试编写一个 Space Invaders Python 程序来学习目的,但在 list 和 data manipulation 中不断绊倒循环内的问题。
我刚刚创建了一个 Class 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, 255)
space_ship_radius = 10
space_ship = space_ship_color, [space_ship_X, space_ship_Y], space_ship_radius
return space_ship
class Squid:
def squid():
squid_X = 100
squid_Y = 100
squid_radius = 10
squid_color = (255, 0, 0)
squid = [squid_color, [squid_X, squid_Y], squid_radius]
return squid
稍后我需要一个鱿鱼小队,所以我创建了一个空列表并尝试在将“鱿鱼”添加到列表之前增加 squid_X 值,但计数器只是将总数 (600) 和将它添加到列表中的每个元素... [(255, 0, 0), [600, 100], 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
循环:
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]]
提前致谢!
欢迎任何加深主题的阅读资源!
这是我的建议:
使用 class Squid
作为鱿鱼的蓝图,并从您的 class 创建鱿鱼 实例 。每个 squid 实例都有一个位置、一个半径(如果您不指定,则使用默认值)和颜色(也使用默认值)。
然后我们生成新的鱿鱼并将它们添加到我们的小队列表中。在这样做的同时,我们使用迭代索引作为我们各自的 X 位置,同时保持 Y 值不变 - 只是为了举例。
class Squid:
def __init__(self, x, y, radius = 10, color = (255, 0, 0)):
# bind all the values passed in this function to the object we're constructing here
self.x = x
self.y = y
self.radius = radius
self.color = color
def __str__(self):
# What happens when we call str(mySquid)?
# Let's return something human readible
# You can compare this to the output when calling repr(mySquid)
return f'Squid([x={self.x}, y={self.y}], r={self.radius}, col={self.color})'
num_squids = 10
squad = []
# repeat num_squids=10 times
y = 10
for i in range(num_squids):
sq = Squid(i, y)
squad.append(sq)
print(f'Squid {i+1} is ready: {sq}!')
print()
print("Here is my squad...")
for squid in squad:
print(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))
人!我正在尝试编写一个 Space Invaders Python 程序来学习目的,但在 list 和 data manipulation 中不断绊倒循环内的问题。 我刚刚创建了一个 Class 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, 255)
space_ship_radius = 10
space_ship = space_ship_color, [space_ship_X, space_ship_Y], space_ship_radius
return space_ship
class Squid:
def squid():
squid_X = 100
squid_Y = 100
squid_radius = 10
squid_color = (255, 0, 0)
squid = [squid_color, [squid_X, squid_Y], squid_radius]
return squid
稍后我需要一个鱿鱼小队,所以我创建了一个空列表并尝试在将“鱿鱼”添加到列表之前增加 squid_X 值,但计数器只是将总数 (600) 和将它添加到列表中的每个元素... [(255, 0, 0), [600, 100], 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
循环:
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]]
提前致谢! 欢迎任何加深主题的阅读资源!
这是我的建议:
使用 class Squid
作为鱿鱼的蓝图,并从您的 class 创建鱿鱼 实例 。每个 squid 实例都有一个位置、一个半径(如果您不指定,则使用默认值)和颜色(也使用默认值)。
然后我们生成新的鱿鱼并将它们添加到我们的小队列表中。在这样做的同时,我们使用迭代索引作为我们各自的 X 位置,同时保持 Y 值不变 - 只是为了举例。
class Squid:
def __init__(self, x, y, radius = 10, color = (255, 0, 0)):
# bind all the values passed in this function to the object we're constructing here
self.x = x
self.y = y
self.radius = radius
self.color = color
def __str__(self):
# What happens when we call str(mySquid)?
# Let's return something human readible
# You can compare this to the output when calling repr(mySquid)
return f'Squid([x={self.x}, y={self.y}], r={self.radius}, col={self.color})'
num_squids = 10
squad = []
# repeat num_squids=10 times
y = 10
for i in range(num_squids):
sq = Squid(i, y)
squad.append(sq)
print(f'Squid {i+1} is ready: {sq}!')
print()
print("Here is my squad...")
for squid in squad:
print(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))