我正在创建一个基于网格的游戏,所以我想要它,因此,如果我单击屏幕上的某个单元格,它将变为彩色。由于某种原因,这是行不通的,我不知道为什么。我已经使用2D数组系统来索引我的图形网格,如果我在数组中手动预设了一个像这样的值:
grid = [ [1]*8 for n in range(8)]
grid[6][7] = -1
它工作正常,单元格已着色。
但是,使用MOUSEBUTTONDOWN
事件,我得到了鼠标的位置并正确地标识了鼠标的位置以及鼠标单击的单元格。我登录到控制台,看到该数组正在正确更新,并且正在显示鼠标和单击的单元格的正确坐标。
这是完整的脚本:
import pygame
pygame.init()
win = pygame.display.set_mode((900,900))
pygame.display.set_caption('Game')
board = pygame.image.load('Assets/boardfull.png')
pygame.draw.rect(win, (243, 235, 215), (0, 0, 900, 900))
pygame.display.update()
board = pygame.transform.scale(board, (900,900))
GAME_RUN = True
WIDTH = 112.5
HEIGHT = 112.5
MARGIN = 2
pawn_x = -60
pawn_y = 600
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
grid = [ [1]*8 for n in range(8)]
grid[6][7] = -1
gridcolumn = 0
gridrow = 0
while GAME_RUN == True:
pygame.time.delay(100)
#GAME LOGIC
for event in pygame.event.get():
if event.type == pygame.QUIT:
GAME_RUN = False
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 = int(pos[0] // (WIDTH + MARGIN))
row = int(pos[1] // (HEIGHT + MARGIN))
# Set that location to one
grid[row][column] = -1
print(grid)
print("Click ", pos, "Grid coordinates: ", row, column)
#GRID
for row in grid:
for col in row:
color = WHITE
if col == -1:
color = RED
pygame.draw.rect(win, (color), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5))
pygame.draw.rect(win,(BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 2))
pygame.draw.rect(win, (BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 2, 112.5))
gridcolumn = gridcolumn + 1
gridrow = gridrow + 1
gridcolumn = 0
#UPDATE SURFACE
pygame.display.update()
pygame.quit()
quit()
没有错误消息。根本什么都没有发生,程序继续运行。可能是显示器未正确更新?老实说,我没有任何线索!
答案 0 :(得分:0)
好吧,我想我找到了真正的原因。本质上,一切都很好。唯一的问题是您的gridRow
未被重置。只需在gridRow = 0
之后添加gridColumn=0
(但不在for row in grid:
循环中),即可解决此问题。现在应该是您的更新代码:
while GAME_RUN == True:
pygame.time.delay(100)
#GAME LOGIC
for event in pygame.event.get():
if event.type == pygame.QUIT:
GAME_RUN = False
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 = int(pos[0] // (WIDTH + MARGIN))
row = int(pos[1] // (HEIGHT + MARGIN))
# Set that location to one
grid[row][column] = -1
print(grid)
print("Click ", pos, "Grid coordinates: ", row, column)
#GRID
for row in grid:
for col in row:
color = WHITE
if col == -1:
color = RED
pygame.draw.rect(win, (color), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5))
pygame.draw.rect(win,(BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 2))
pygame.draw.rect(win, (BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 2, 112.5))
gridcolumn = gridcolumn + 1
gridrow = gridrow + 1
gridcolumn = 0
gridrow = 0
#UPDATE SURFACE
pygame.display.update()
好的,所以我无法真正找到发生这种情况的适当原因,但是我能够解决它。本质上,您必须将pygame.draw.rect(win, (color), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5))
移到此处:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = int(pos[0] // (WIDTH + MARGIN))
row = int(pos[1] // (HEIGHT + MARGIN))
# Set that location to red (this is where you would put it, with the correct changes [column,row instead of gridcolumn,gridrow])
pygame.draw.rect(win, (RED), (112.5 * column, 112.5 * row, 112.5, 112.5))
print(grid)
print("Click ", pos, "Grid coordinates: ", row, column)
我认为问题是GridRow
和GridColumn
返回的内容与column
和row
不同。我的理由是,如果我们稍微修复一下GRID代码,它只会在行上设置颜色。该专栏似乎始终保持在第七名。
我替换的代码:
for row in grid:
for col in row:
color = WHITE
if col == -1:
# not setting properly
color = RED
if (color == RED):
print(gridcolumn,gridrow)
pygame.draw.rect(win, (color), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5))
pygame.draw.rect(win,(BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 2))
pygame.draw.rect(win, (BLACK), (112.5 * gridcolumn, 112.5 * gridrow, 2, 112.5))
gridcolumn = gridcolumn + 1
gridrow = gridrow + 1
gridcolumn = 0
# never got reset, therefore gridrow would go over 7
gridrow = 0
希望这会有所帮助。
答案 1 :(得分:0)
只有1期。在绘制网格之前,您未设置gridrow=0
。因此gridrow
不断递增计数,并且单元格被绘制在窗口之外的某个位置。由于未清除窗口,因此您没有注意到仅在窗口中第一次绘制网格:
gridrow = 0 # <---- this was missing
for row in grid:
# [...]
gridrow = gridrow + 1
无论如何,我建议在这种情况下使用enumerate
。 enumerate
返回一个由元素索引和元素组成的元组:
for gridrow, row in enumerate(grid):
for gridcolumn, col in enumerate(row):
color = RED if col == -1 else WHITE
pygame.draw.rect(win, color, (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5))
pygame.draw.rect(win, BLACK, (112.5 * gridcolumn, 112.5 * gridrow, 112.5, 112.5), 2)
请注意,如果pygame.draw.rect()
中的width > 0
,则不会填充矩形并绘制轮廓。