我的问题是我不能在正确的位置正确地blit
几个正方形。你知道吗
我已经写了一些代码来显示棋盘。方格不是精灵,而是pygame.表面物体。你知道吗
我的项目目前包含两个.py文件:
协调器是一个函数,它返回一个包含两个元组列表的列表,一个用于偶数列,一个用于奇数列。元组是我传递给blit方法的实际坐标。你知道吗
在这个阶段,我只处理白色方块。你知道吗
那个显示.py文件是实际显示,目前还包含正方形类和棋盘类。你知道吗
这是我的协调员.py地址:
# Returns a list containing two lists 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()
这是我的显示.py地址:
import pygame
import sys
from coordinator import coordinator
# Sets up the display
pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')
# Event loop (outer)
while 1:
white_columns = coordinator()
# print(white_columns)
odd_white_columns = white_columns[0]
# print(odd_white_columns)
even_white_columns = white_columns[1]
# print(even_white_columns)
l = len(even_white_columns)
# print(l)
n = 0
for n in range(l + 1):
x = odd_white_columns[n]
print(x)
# Event loop (inner)
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 BlackSquare:
height = int(window_size[0] / 8)
width = int(window_size[1] / 8)
a = height
b = width
black_square = pygame.Surface((a, b))
black_square.fill((0, 0, 255), )
bs = BlackSquare()
class ChessBoard:
game_window.blit(ws.white_square, x) # test
game_window.blit(bs.black_square, (0, 100)) # test
cb = ChessBoard()
pygame.display.update()
请不要考虑以下因素:
game_window.blit(bs.black_square, (0, 100))
这只是为了确保当我传递一些坐标时,我实际上可以blit
平方。你知道吗
让我烦恼的是,在第61行有以下内容:
game_window.blit(ws.white_square, x)
实际上,我要做的是访问协调器返回的每个元组,代码从第21行开始:
l = len(even_white_columns)
# print(l)
n = 0
for n in range(l + 1):
x = odd_white_columns[n]
print(x)
我打印元组只是为了确保它们是正确的,并且它们都是正确的。嗯,他们没事。你知道吗
我不明白的是,为什么在运行代码时,显示的唯一一个白色方块看起来就是(200600)。你知道吗
我觉得这与存储元组或更新显示有关,但目前看来这与我的能力不符。你知道吗
有人能解释一下我做错了什么吗?我可能也做了一些错误的事情,通过这种方式迭代并试图传递坐标。你知道吗
或者我必须使用blit,而不是blit,这样我才能传递一系列blit。如果是这样的话,我真的不知道如何使用这个方法,因为在堆栈溢出的例子中,它只是作为一个函数显示的。你知道吗
谢谢你的帮助。你知道吗
我建议阅读Instance Objects和Method Objects的概念。你知道吗
无论如何,给类
BlackSquare
和WhiteSquare
添加一个构造函数,并将其移动到主游戏循环之前。e、 g.:将类
ChessBoard
移到主游戏循环之前,并使用白方、黑方和白列的参数向中添加构造函数(稍后还必须添加黑列)。添加一个Method(
draw
),它可以在2个嵌套循环中绘制存储在self.white_columns
中的位置的白色正方形(self.ws
)。e、 g.:在游戏循环之前创建对象,并通过调用
cb.draw()
在循环中绘制棋盘。e、 g.:相关问题 更多 >
编程相关推荐