python - 通过传递坐标来位击棋盘上的白色方 block

标签 python python-3.x pygame blit

我的问题是我无法在正确的位置正确blit几个方 block 。

我编写了一些代码来显示棋盘。这些方 block 不是 Sprite ,而是 pygame.Surface 对象。

我的项目目前包含两个 .py 文件:

  • 显示.py
  • 协调员.py

协调器是一个函数,它返回一个包含两个元组列表的列表,一个用于偶数列,一个用于奇数列。这些元组是我传递给 blit 方法的实际坐标。

在这个阶段,我只处理白色方 block 。

display.py 文件是实际显示,目前还包含正方形类和棋盘类。

这是我的 coordinator.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()

这是我的 display.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方 block 。

令我困扰的是第 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)

我打印元组只是为了确保它们是正确的并且它们都是元组。嗯,他们都很好。

我不明白的是,为什么在运行代码时,显示的唯一白色方 block 看起来是 (200, 600) 处的方 block 。

我觉得它与存储元组或更新显示有关,但目前它看起来超出了我的范围。

有人可以向我解释一下我做错了什么吗?通过实际迭代并尝试传递坐标,我也可能做错了什么。

或者我可能必须使用 blit,而不是 blit,这样我才能传递一系列 blit。如果是这样,我实际上无法弄清楚如何使用此方法,因为在 Stack Overflow 上的示例中,它仅显示为函数。

感谢您的帮助。

最佳答案

我建议阅读 Instance Objects 的概念和 Method Objects .

无论如何,向类 BlackSquareWhiteSquare 添加一个构造函数,并将其移到主游戏循环之前。例如:

class BlackSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.black_square = pygame.Surface((self.width, self.height))
        self.black_square.fill((0, 0, 255))

class WhiteSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.white_square = pygame.Surface((self.width, self.height))
        self.white_square.fill((255, 255, 255))

将类 ChessBoard 移到主游戏循环之前,并添加一个构造函数,其中包含白色方 block 、黑色方 block 和白色列的参数(稍后您还必须添加黑色列) ).
添加Method (draw),它可以在 2 个嵌套循环中将存储在 self.white_columns 中的位置绘制白色方 block (self.ws)。例如:

class ChessBoard:
      def __init__(self, ws, bs, white_columns):
          self.ws = ws
          self.bs = bs
          self.white_columns = white_columns

      def draw(self):
          for colums in self.white_columns:
              for p in colums: 
                  game_window.blit(self.ws.white_square, p)

在游戏循环之前创建对象,并通过调用cb.draw()在循环中绘制棋盘。例如:

pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(window_size)
pygame.display.set_caption('My Game')

white_columns = coordinator()
# print(white_columns)
odd_white_columns = white_columns[0]
# print(odd_white_columns)
even_white_columns = white_columns[1]

ws = WhiteSquare()
bs = BlackSquare()
cb = ChessBoard(ws, bs, white_columns)

# Event loop (outer)
while 1:

    # Event loop (inner)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    game_window.fill(0)
    cb.draw()
    pygame.display.update()

关于python - 通过传递坐标来位击棋盘上的白色方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352999/

相关文章:

python-3.x - 加载预先训练的 Keras 模型并进行预测

python - 列中缺少/NaT 值时如何转换日期格式

python - 我的 pygame 播放器 Sprite 没有移动和/或更新

python - Pygame如何改变背景颜色而不删除其他任何东西

python - 鼠标点击未被识别

python - 是否可以声明一个类变量,该变量将产生其他两个类变量的总和

Python:克莱因瓶的 3D 图

python - 如何使用输入打印类的信息 [PYTHON 3]

python - 无法将.Net的SaveFileDialog与pythonnet一起使用

python - 使用 tor 和 torctl 绕过代理