问题描述
我有此代码用于显示网格,但当 size_x
和 size_y
不相同时,网格无法正确显示。例如,如果size_x = 16
和size_y =8
,显示的网格是这样的:
如果 size_x
和 size_y
相同,一切正常,如果 size_x
和 size_y
在这种情况下相同,则值为 16:
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_x
和 size_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])