微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么当 x 和 y 大小不同时,网格无法正确显示

如何解决为什么当 x 和 y 大小不同时,网格无法正确显示

我有代码用于显示网格,但当 size_xsize_y 不相同时,网格无法正确显示。例如,如果size_x = 16size_y =8显示的网格是这样的:

grid is size_x=16 and size_y=8

如果 size_xsize_y 相同,一切正常,如果 size_xsize_y 在这种情况下相同,则值为 16:

enter image description here

import pygame
 
# Define some colors
BLACK = (0,0)
WHITE = (255,255,255)
GREEN = (0,0)
RED = (255,0)
 
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 10
HEIGHT = 10
 
# This sets the margin between each cell
MARGIN = 5


# gride size
size_x=16
size_y=16

# window size
window_size_x=size_x*WIDTH+((size_x+1)*MARGIN)
window_size_y=size_y*HEIGHT+((size_y+1)*MARGIN)
#window_size=


# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
for row in range(size_x):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(size_y):
        grid[row].append(0)  # Append a cell
 
# Set row 1,cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
 
# Initialize pygame
pygame.init()
 
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [window_size_x,window_size_y]
screen = pygame.display.set_mode(WINDOW_SIZE)
 
# Set title of screen
pygame.display.set_caption("Array Backed Grid")
 
# Loop until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
 
# -------- Main Program Loop -----------

print(grid)

    while not done:
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                done = True  # Flag that we are done so we exit this loop
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # User clicks the mouse. Get the position
                pos = pygame.mouse.get_pos()
                # Change the x/y screen coordinates to grid coordinates
                column = pos[0] // (WIDTH + MARGIN)
                row = pos[1] // (HEIGHT + MARGIN)
                # Set that location to one
                grid[row][column] = 1
                print("Click ",pos,"Grid coordinates: ",row,column)
     
        # Set the screen background
        screen.fill(BLACK)
     
        # Draw the grid
        for row in range(size_x):
            for column in range(size_y):
                color = WHITE
                if grid[row][column] == 1:
                    color = GREEN
                pygame.draw.rect(screen,color,[(MARGIN + WIDTH) * column + MARGIN,(MARGIN + HEIGHT) * row + MARGIN,WIDTH,HEIGHT])
     
        # Limit to 60 frames per second
        clock.tick(60)
     
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
     
    # Be IDLE friendly. If you forget this line,the program will 'hang'
    # on exit.
    pygame.quit()

解决方法

您在绘制正方形的嵌套 for 循环中数学错误,此修复程序:

for row in range(size_x):
    for column in range(size_y):
        color = WHITE
        if grid[row][column] == 1:
            color = GREEN
        pygame.draw.rect(screen,color,**notice the change** --------v
                         [(MARGIN + WIDTH) * row + MARGIN,(MARGIN + HEIGHT) * column + MARGIN,WIDTH,HEIGHT])

您需要使用宽度作为第一点(左上角)绘制矩形。

,

您不小心交换了 size_xsize_y。 x 轴是水平轴,y 轴是垂直轴。 行是从上到下的 y 维度,列是从左到右的 x 维度。因此,size_y 是行数,size_x 是列数:

# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
for row in range(size_y):                              # <--- size_y
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(size_x):                       # <--- size_x
        grid[row].append(0)  # Append a cell
while not done:
    # [...]

     # Draw the grid
    for row in range(size_y):                          # <--- size_y
        for column in range(size_x):                   # <--- size_x
            color = WHITE
            if grid[row][column] == 1:
                color = GREEN
            pygame.draw.rect(screen,[(MARGIN + WIDTH) * column + MARGIN,(MARGIN + HEIGHT) * row + MARGIN,HEIGHT])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。