问题描述
所以我在使用 pygame 时遇到了一个问题,我试图制作一个迷宫构建器,用户可以点击网格中的方块,然后方块变成黑色。虽然我在调试器/打印语句中看到颜色值发生了变化,但我的 ui 中没有看到任何变化。有人可以帮我看看吗?
我的源代码:
import pygame
import math
WIDTH = 800
# initialize window
WINDOW = pygame.display.set_mode((WIDTH,WIDTH)) # always square
pygame.display.set_caption("Path finding")
# Color constants-------
RED = (255,0)
GREEN = (0,255,0)
BLUE = (0,0)
YELLOW = (255,0)
WHITE = (255,255)
BLACK = (0,0)
PURPLE = (128,128)
ORANGE = (255,165,0)
GREY = (128,128,128)
TURQUOISE = (64,224,208)
# Classes --------
class Spot:
def __init__(self,row,col,width,height):
self.row = row
self.col = col
self.x = row * width
self.y = col * height
self.color = WHITE
self.width = width
self.height = height
self.neighbors = []
# determine position
def get_pos(self):
return (self.col,self.row)
# let me draw the spots
def draw(self,win):
pygame.draw.rect(win,self.color,(self.x,self.y,self.width,self.height))
# color change methods
def make_wall(self):
self.color = BLACK
print("Made a wall!")
def make_start(self):
self.color = ORANGE
def make_end(self):
self.color = TURQUOISE
def make_closed(self):
self.color = RED
def make_open(self):
self.color = GREEN
def reset(self):
self.color = WHITE
def make_path(self):
self.color = PURPLE
# Boolean accessors is start/is end etc
def is_wall(self):
return self.color == BLACK
def is_start(self):
return self.color == ORANGE
def is_end(self):
return self.color == TURQUOISE
def is_closed(self):
return self.color == RED
def is_open(self):
return self.color == GREEN
# function to fill in neighbors
def update_neighbors(self,grid):
self.neighbors = [] # erases current neighbors
# if it isn't the last row and the one below it isn't a wall,if self.x < self.height-1 and not grid[self.x][self.y+1].is_wall():
# then add that to neighbors
self.neighbors.append(grid[self.x][self.y+1])
if self.x > 0 and not grid[self.x][self.y-1].is_wall():
# then add that to neighbors
self.neighbors.append(grid[self.x][self.y-1])
if self.y < self.width-1 and not grid[self.x+1][self.y].is_wall():
# then add that to neighbors
self.neighbors.append(grid[self.x+1][self.y])
if self.y > 0 and not grid[self.x-1][self.y].is_wall():
# then add that to neighbors
self.neighbors.append(grid[self.x-1][self.y])
# create an array (grid) of Spots,def make_grid(rows,height):
grid = []
gap = width // height
for i in range(rows):
grid.append([])
for j in range(rows):
spot = Spot(i,j,gap,height)
grid[i].append(spot)
return grid
# Draw a grid on screen
def draw_grid(win,rows,width):
space = width // rows
for i in range(rows):
pygame.draw.line(win,GREY,(0,i * space),(width,i*space))
for j in range(rows):
pygame.draw.line(win,(j*space,0),width))
# when user clicks mouse on grid,they change the color of the cell
# Allow for a few small buttons to place start and end
# draws background
def draw(win,grid,width):
win.fill(WHITE)
for row in grid:
for spot in row:
spot.draw(win)
draw_grid(win,width)
pygame.display.update()
# where did the user click?
def get_clicked_pos(pos,width):
y,x = pos;
gap = width // rows
row = y // gap
col = x // gap
return row,col
# game loop
def main(win,width):
rows = 50
run = True
grid = make_grid(rows,width)
while(run):
draw(win,width)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mouse.get_pressed()[0]: #left click
pos = pygame.mouse.get_pos()
print(pos)
row,col = get_clicked_pos(pos,width)
spot = grid[row][col]
print(spot,col)
spot.make_wall()
pygame.quit()
main(WINDOW,WIDTH)
解决方法
我认为您的 make_grid
函数搞砸了。
代替
def make_grid(rows,width,height):
grid = []
gap = width // height # this will always be 1
for i in range(rows):
grid.append([])
for j in range(rows):
spot = Spot(i,j,gap,height) # all your spots a 1px wide and 800px tall
grid[i].append(spot)
return grid
使用
def make_grid(rows,height):
grid = []
gap = width // rows # TODO: use better variable names
for i in range(rows):
grid.append([])
for j in range(rows):
spot = Spot(i,gap)
grid[i].append(spot)
return grid
这似乎有效: