为什么当 x 和 y 大小不同时,网格无法正确显示 [英] why when the x and y size are not the same the grid is not showing properly
问题描述
我有这个用于显示网格的代码,但是当 size_x
和 size_y
不相同时,网格无法正确显示.例如,如果 size_x = 16
和 size_y =8
,显示的网格是这样的:
I have this code that is used to display a grid but when size_x
and size_y
are not the same the grid is not shown properly. For exemple, if size_x = 16
and size_y =8
, the grid that is displayed is this:
如果 size_x
和 size_y
相同,一切正常,如果 size_x
和 size_y
在在这种情况下,值为 16:
If size_x
and size_y
are the same everything works fine, if size_x
and size_y
are the same in this case there value is 16:
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 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()
推荐答案
您不小心交换了 size_x
和 size_y
.x 轴是水平轴,y 轴是垂直轴.行是从上到下的 y 维度,列是从左到右的 x 维度.因此,size_y
是行数,size_x
是列数:
You accidentally swapped size_x
and size_y
. The x axis is the horizontal axis and the y axis is the vertical axis.
The rows are the y dimension from top to bottom and the columns are the x dimension from left to right. Hence size_y
is the number of rows and size_x
is the number of columns:
# 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,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
这篇关于为什么当 x 和 y 大小不同时,网格无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!