通过传递坐标在棋盘上打出白色方块

2024-04-26 10:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是我不能在正确的位置正确地blit几个正方形。你知道吗

我已经写了一些代码来显示棋盘。方格不是精灵,而是pygame.表面物体。你知道吗

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

  • 你知道吗显示.py你知道吗
  • 你知道吗协调员.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。如果是这样的话,我真的不知道如何使用这个方法,因为在堆栈溢出的例子中,它只是作为一个函数显示的。你知道吗

谢谢你的帮助。你知道吗


Tags: columnsinpyforsizerangewindowpygame
1条回答
网友
1楼 · 发布于 2024-04-26 10:59:30

我建议阅读Instance ObjectsMethod Objects的概念。你知道吗

无论如何,给类BlackSquareWhiteSquare添加一个构造函数,并将其移动到主游戏循环之前。e、 g.:

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移到主游戏循环之前,并使用白方、黑方和白列的参数向中添加构造函数(稍后还必须添加黑列)。
添加一个Methoddraw),它可以在2个嵌套循环中绘制存储在self.white_columns中的位置的白色正方形(self.ws)。e、 g.:

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()在循环中绘制棋盘。e、 g.:

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()

相关问题 更多 >