Python 在函数中生成坐标的两元素元组
我需要一个函数以元组形式传递坐标,例如(x,y) 这是我的第一个项目 我想用pygame创建一个棋盘,只需在显示屏上显示黑白方块 现在,我已经成功地在正确的目的地闪动了所有的白色方块 我在这里尝试的是自动创建白色方块(稍后黑色方块也会如此),更准确地说,我尝试传递以下元组:Python 在函数中生成坐标的两元素元组,python,pygame,Python,Pygame,我需要一个函数以元组形式传递坐标,例如(x,y) 这是我的第一个项目 我想用pygame创建一个棋盘,只需在显示屏上显示黑白方块 现在,我已经成功地在正确的目的地闪动了所有的白色方块 我在这里尝试的是自动创建白色方块(稍后黑色方块也会如此),更准确地说,我尝试传递以下元组: # first column window.blit(square, (0, 0)) window.blit(square, (0, 200)) window.blit(square, (0,
# first column
window.blit(square, (0, 0))
window.blit(square, (0, 200))
window.blit(square, (0, 400))
window.blit(square, (0, 600))
window.blit(square, (0, 800))
# second column
window.blit(square, (100, 100))
window.blit(square, (100, 300))
window.blit(square, (100, 500))
window.blit(square, (100, 700))
# third column
window.blit(square, (200, 0))
window.blit(square, (200, 200))
window.blit(square, (200, 400))
window.blit(square, (200, 600))
window.blit(square, (200, 800))
# fourth column
window.blit(square, (300, 100))
window.blit(square, (300, 300))
window.blit(square, (300, 500))
window.blit(square, (300, 700))
# fifth column
window.blit(square, (400, 0))
window.blit(square, (400, 200))
window.blit(square, (400, 400))
window.blit(square, (400, 600))
window.blit(square, (400, 800))
# seventh column
window.blit(square, (500, 100))
window.blit(square, (500, 300))
window.blit(square, (500, 500))
window.blit(square, (500, 700))
# seventh column
window.blit(square, (600, 0))
window.blit(square, (600, 200))
window.blit(square, (600, 400))
window.blit(square, (600, 600))
window.blit(square, (600, 800))
# eighth column
window.blit(square, (700, 100))
window.blit(square, (700, 300))
window.blit(square, (700, 500))
window.blit(square, (700, 700))
在我的协调函数中,我成功地编写了以下代码,但从现在起,我真的不知道该怎么做:
def coordinator():
x = 0
y = 0
for column in range(0, 5):
print((x, y))
y += 200
x = 200
y = 0
for column in range(0, 5):
print((x, y))
y += 200
coordinator()
Wich作为输出给出:
(0, 0)
(0, 200)
(0, 400)
(0, 600)
(0, 800)
(200, 0)
(200, 200)
(200, 400)
(200, 600)
(200, 800)
提前感谢您的帮助
好了,伙计们好消息,我已经写了一些代码,可以打印出我需要的所有坐标。这是我的“Main”类,即display.py:
import pygame
import sys
from coordinator import coordinator
pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
class WhiteSquare:
height = int(window_size[0] / 8)
width = int(window_size[1] / 8)
a = height
b = width
white_square = pygame.Surface((a, b))
white_square.fill((255, 255, 255), )
ws = WhiteSquare()
class BlueSquare:
height = int(window_size[0] / 8)
width = int(window_size[1] / 8)
a = height
b = width
blue_square = pygame.Surface((a, b))
blue_square.fill((0, 0, 255), )
bs = BlueSquare()
class ChessBoard:
game_window.blit(ws.white_square, (0, 0))
game_window.blit(bs.blue_square, (0, 100))
cb = ChessBoard()
coordinator()
pygame.display.update()
对于那些有一个黑色正方形或以任何方式引用黑色正方形的解决方案,我显然已将我的“蓝色”改为“黑色”。我只使用蓝色,因为我想确保黑色方块也能在黑色背景上正常工作(如果有意义的话)
这里是coordinator.py,它本身就可以很好地工作。当我试图在display.py文件中调用它时,问题就出现了
看看:
def coordinator():
o = white_odd_columns()
e = white_even_columns()
print('o: ' + str(o))
print('e: ' + str(e))
def white_odd_columns():
for x in range(0, 800, 200):
print('odd column: ')
y = 0
for first_column in range(0, 5):
odd_coordinate = (x, y)
print('This is odd_coordinate ' + str(odd_coordinate))
y += 200
#return odd_coordinate
def white_even_columns():
for x in range(100, 800, 200):
print('even column: ')
y = 100
for first_column in range(0, 4):
even_coordinate = (x, y)
print('This is even_coordinate ' + str(even_coordinate))
y += 200
#return even_coordinate
white_even_columns()
white_odd_columns()
coordinator()
现在,当我运行coordinator时,我得到以下输出:
even column:
This is even_coordinate (100, 100)
This is even_coordinate (100, 300)
This is even_coordinate (100, 500)
This is even_coordinate (100, 700)
even column:
This is even_coordinate (300, 100)
This is even_coordinate (300, 300)
This is even_coordinate (300, 500)
This is even_coordinate (300, 700)
even column:
This is even_coordinate (500, 100)
This is even_coordinate (500, 300)
This is even_coordinate (500, 500)
This is even_coordinate (500, 700)
even column:
This is even_coordinate (700, 100)
This is even_coordinate (700, 300)
This is even_coordinate (700, 500)
This is even_coordinate (700, 700)
odd column:
This is odd_coordinate (0, 0)
This is odd_coordinate (0, 200)
This is odd_coordinate (0, 400)
This is odd_coordinate (0, 600)
This is odd_coordinate (0, 800)
odd column:
This is odd_coordinate (200, 0)
This is odd_coordinate (200, 200)
This is odd_coordinate (200, 400)
This is odd_coordinate (200, 600)
This is odd_coordinate (200, 800)
odd column:
This is odd_coordinate (400, 0)
This is odd_coordinate (400, 200)
This is odd_coordinate (400, 400)
This is odd_coordinate (400, 600)
This is odd_coordinate (400, 800)
odd column:
This is odd_coordinate (600, 0)
This is odd_coordinate (600, 200)
This is odd_coordinate (600, 400)
This is odd_coordinate (600, 600)
This is odd_coordinate (600, 800)
odd column:
This is odd_coordinate (0, 0)
This is odd_coordinate (0, 200)
This is odd_coordinate (0, 400)
This is odd_coordinate (0, 600)
This is odd_coordinate (0, 800)
odd column:
This is odd_coordinate (200, 0)
This is odd_coordinate (200, 200)
This is odd_coordinate (200, 400)
This is odd_coordinate (200, 600)
This is odd_coordinate (200, 800)
odd column:
This is odd_coordinate (400, 0)
This is odd_coordinate (400, 200)
This is odd_coordinate (400, 400)
This is odd_coordinate (400, 600)
This is odd_coordinate (400, 800)
odd column:
This is odd_coordinate (600, 0)
This is odd_coordinate (600, 200)
This is odd_coordinate (600, 400)
This is odd_coordinate (600, 600)
This is odd_coordinate (600, 800)
even column:
This is even_coordinate (100, 100)
This is even_coordinate (100, 300)
This is even_coordinate (100, 500)
This is even_coordinate (100, 700)
even column:
This is even_coordinate (300, 100)
This is even_coordinate (300, 300)
This is even_coordinate (300, 500)
This is even_coordinate (300, 700)
even column:
This is even_coordinate (500, 100)
This is even_coordinate (500, 300)
This is even_coordinate (500, 500)
This is even_coordinate (500, 700)
even column:
This is even_coordinate (700, 100)
This is even_coordinate (700, 300)
This is even_coordinate (700, 500)
This is even_coordinate (700, 700)
o: None
e: None
我遇到的问题是,在coordinator.py中,当我运行coordinator()函数时,我希望看到包含所有坐标的元组列表,这样我就可以通过列表索引轻松访问它们
如果这样做有效,那么我希望可以将坐标传递给ChessBoard类
我将继续尝试您的解决方案,尽管它们对我来说太难理解了,因为我刚刚开始使用python和pygame
也就是说,谢谢你们的时间,伙计们,我会关注你们的答案 您需要使用模运算符生成“每隔一个”样式的坐标 范围(8)中的行的
:
对于范围(8)中的列:
如果列%2==行%2:
x=col*100
y=行*100
窗板(方形,(x,y))
与原始代码具有相同的效果
要在一次过程中绘制黑白方块
范围(8)中的行的:
对于范围(8)中的列:
是黑色=(列%2==行%2)
x=col*100
y=行*100
blit((如果是黑色,则为黑色,否则为白色,则为黑色)(x,y))
(假设您有
黑色方格
和白色方格
精灵)在这个答案的末尾,我给您一个函数,该函数将生成棋盘的所有坐标,以及blit的右方格:
for square, coord in coordinator((ws.white_square, bs.blue_square)):
window.blit(square, coord)
但我将首先介绍如何使用几种不同的循环方法来解决这个问题
最简单的方法是创建一个列表,并添加坐标:
def coordinator():
coordinates = []
x = 0
y = 0
for column in range(0, 5):
coordinates.append((x, y))
y += 200
x = 200
y = 0
for column in range(0, 5):
coordinates.append((x, y))
y += 200
return coordinates
[[(0, 0), (0, 200), (0, 400), (0, 600), (0, 800), (200, 0), (200, 200), (200, 400), (200, 600), (200, 800), (400, 0), (400, 200), (400, 400), (400, 600), (400, 800), (600, 0), (600, 200), (600, 400), (600, 600), (600, 800)], [(100, 100), (100, 300), (100, 500), (100, 700), (300, 100), (300, 300), (300, 500), (300, 700), (500, 100), (500, 300), (500, 500), (500, 700), (700, 100), (700, 300), (700, 500), (700, 700)]]
Process finished with exit code 0
然后调用者可以循环每个元组的列表:
for coord in coordinator():
window.blit(square, coord)
请注意,您可以大大简化循环y
可以通过乘法计算,您只需在同一迭代中附加(0,…)
和(200,…)
元组:
def coordinator():
coordinates = []
for column in range(5):
coordinates.append((0, column * 200))
coordinates.append((200, column * 200))
return coordinates
或者,代替列计数器,您可以告诉range()
使步数为200:
def coordinator():
coordinates = []
for y in range(0, 900, 200):
coordinates.append((0, y))
coordinates.append((200, y))
return coordinates
您还可以将x
作为一个循环,循环的两个值如下:
def coordinator():
coordinates = []
for x in (0, 200):
for y in range(0, 900, 200):
coordinates.append((x, y))
return coordinates
然后可以将其转换为单个列表:
def coordinator():
return [(x, y) for x in (0, 200) for y in range(0, 900, 200)]
你不需要整个列表,你也可以把它变成一个生成器表达式;这将根据需要生成值,并且一旦使用了每个坐标,内存将再次被清除:
def coordinator():
return ((x, y) for x in (0, 200) for y in range(0, 900, 200))
我只将[…for target in iterable]
替换为(…for target in iterable)
,告诉Python不要将所有内容都保存在内存中
当然,这一切只会产生您已经用print()
语句创建的输出
要生成所有可能的白色方块(9行100x100个方块,偶数行5个奇数方块,奇数行4个偶数方块),您只需为x
使用完整的范围()
,并为奇数列添加另一个这样的循环:
for x in range(0, 900, 200):
for y in range(0, 900, 200):
coordinates.append((x, y))
for x in range(100, 900, 200):
for y in range(100, 900, 200):
coordinates.append((x, y))
请注意,第二组循环唯一改变的是y
的起始值。实际上,你可以从x
算出起始值,如果x
除以平方(100)是偶数,你希望y
从0
开始,否则你希望y
从100
开始。以100为增量进行x
步进:
for x in range(0, 900, 100):
even = (x // 100) % 2 == 0 # true for even, false for odd
for y in range(0 if even else 100, 900, 200):
coordinates.append((x, y))
这将生成所有预期的坐标,从第一行的(0,0)、(0,200)、(0,400)、(0,600)、(0,800)
,到第二行的(100,100)、(100,300)、(100,500)、(100,700)
,一直到最后一行的(800,0)、(800,200)、(800,400)、(800,600)、(800,800)
请注意,最好不要硬编码上面的数字,最好可以配置宽度、高度和正方形大小。作为一项功能,这将成为:
def coordinator(boardsize=900, squaresize=100):
coordinates = []
for x in range(0, boardsize, squaresize):
even = x // squaresize % 2 == 0 # true for even, false for odd
for y in range(0 if even else squaresize, boardsize, squaresize * 2):
coordinates.append((x, y))
return coordinates
这将产生精确的输出:
>>> from pprint import pprint
>>> pprint(coordinator())
[(0, 0),
(0, 200),
(0, 400),
(0, 600),
(0, 800),
(100, 100),
(100, 300),
(100, 500),
(100, 700),
(200, 0),
(200, 200),
(200, 400),
(200, 600),
(200, 800),
(300, 100),
(300, 300),
(300, 500),
(300, 700),
(400, 0),
(400, 200),
(400, 400),
(400, 600),
(400, 800),
(500, 100),
(500, 300),
(500, 500),
(500, 700),
(600, 0),
(600, 200),
(600, 400),
(600, 600),
(600, 800),
(700, 100),
(700, 300),
(700, 500),
(700, 700),
(800, 0),
(800, 200),
(800, 400),
(800, 600),
(800, 800)]
但是这还不够,因为这只是白色的方块。你必须改变逻辑来产生蓝色方块
然而,实际上只需生成所有正方形,并让函数告诉您要将哪个正方形放在那里会更容易。您将生成所有坐标,将x
和y
增加100((0,0),(0,100),(0,200),…,(100,0),(100,100),(100,200),…
,等等),然后将这些坐标与交替的白色和蓝色正方形组合
最简单的方法是使用;它有一个函数来生成(x,y)
坐标()所需的笛卡尔积,并且它有一个函数来在正方形()之间重复交替:
这将为您生成(平方,(x,y))
值;我曾经
>>> pprint(list(coordinator(("white", "blue"))))
[('white', (0, 0)),
('blue', (0, 100)),
('white', (0, 200)),
('blue', (0, 300)),
('white', (0, 400)),
('blue', (0, 500)),
('white', (0, 600)),
('blue', (0, 700)),
('white', (0, 800)),
('blue', (100, 0)),
('white', (100, 100)),
('blue', (100, 200)),
('white', (100, 300)),
('blue', (100, 400)),
('white', (100, 500)),
('blue', (100, 600)),
('white', (100, 700)),
('blue', (100, 800)),
('white', (200, 0)),
('blue', (200, 100)),
('white', (200, 200)),
('blue', (200, 300)),
('white', (200, 400)),
('blue', (200, 500)),
('white', (200, 600)),
('blue', (200, 700)),
('white', (200, 800)),
('blue', (300, 0)),
('white', (300, 100)),
('blue', (300, 200)),
('white', (300, 300)),
('blue', (300, 400)),
('white', (300, 500)),
('blue', (300, 600)),
('white', (300, 700)),
('blue', (300, 800)),
('white', (400, 0)),
('blue', (400, 100)),
('white', (400, 200)),
('blue', (400, 300)),
('white', (400, 400)),
('blue', (400, 500)),
('white', (400, 600)),
('blue', (400, 700)),
('white', (400, 800)),
('blue', (500, 0)),
('white', (500, 100)),
('blue', (500, 200)),
('white', (500, 300)),
('blue', (500, 400)),
('white', (500, 500)),
('blue', (500, 600)),
('white', (500, 700)),
('blue', (500, 800)),
('white', (600, 0)),
('blue', (600, 100)),
('white', (600, 200)),
('blue', (600, 300)),
('white', (600, 400)),
('blue', (600, 500)),
('white', (600, 600)),
('blue', (600, 700)),
('white', (600, 800)),
('blue', (700, 0)),
('white', (700, 100)),
('blue', (700, 200)),
('white', (700, 300)),
('blue', (700, 400)),
('white', (700, 500)),
('blue', (700, 600)),
('white', (700, 700)),
('blue', (700, 800)),
('white', (800, 0)),
('blue', (800, 100)),
('white', (800, 200)),
('blue', (800, 300)),
('white', (800, 400)),
('blue', (800, 500)),
('white', (800, 600)),
('blue', (800, 700)),
('white', (800, 800))]
for square, coord in coordinator((ws.white_square, bs.blue_square)):
window.blit(square, coord)
for square, coords in coordinator((ws.white_square, bs.blue_square), 800):
# ...
# Returns two list of tuples, one for even columns (white squares), and
# one for odd columns (white squares).
# Needs to be implemented to achieve the same for black squares.
def coordinator():
odd_columns = white_odd_columns()
even_columns = white_even_columns()
columns = [odd_columns, even_columns]
# print(columns)
return columns
# print('odd_columns: ' + str(odd_columns))
# print('even_columns: ' + str(even_columns))
# Returns a list with coordinates
# for white squares in odd columns
def white_odd_columns():
odd_coordinates = []
for x in range(0, 800, 200):
y = 0
for first_column in range(0, 5):
odd_coordinates.append((x, y))
y += 200
# print('This should be the complete list of odd coordinates' + str(odd_coordinates))
return odd_coordinates
# Returns a list with coordinates
# for white squares in even columns
def white_even_columns():
even_coordinates = []
for x in range(100, 800, 200):
y = 100
for first_column in range(0, 4):
even_coordinates.append((x, y))
y += 200
# print('This should be the complete list of even coordinates' + str(even_coordinates))
return even_coordinates
# white_even_columns()
# white_odd_columns()
coordinator()
[[(0, 0), (0, 200), (0, 400), (0, 600), (0, 800), (200, 0), (200, 200), (200, 400), (200, 600), (200, 800), (400, 0), (400, 200), (400, 400), (400, 600), (400, 800), (600, 0), (600, 200), (600, 400), (600, 600), (600, 800)], [(100, 100), (100, 300), (100, 500), (100, 700), (300, 100), (300, 300), (300, 500), (300, 700), (500, 100), (500, 300), (500, 500), (500, 700), (700, 100), (700, 300), (700, 500), (700, 700)]]
Process finished with exit code 0