Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';不要让rect在pygame中更改颜色?_Python_Pygame - Fatal编程技术网

Python Can';不要让rect在pygame中更改颜色?

Python Can';不要让rect在pygame中更改颜色?,python,pygame,Python,Pygame,因此,我在pygame中遇到了一个问题,我试图制作一个迷宫生成器,用户可以点击网格中的一个正方形,而这个正方形会变成黑色。虽然我在调试器/打印语句中看到颜色值发生了变化,但在ui中没有看到任何变化。谁能帮我看看吗 我的源代码: import pygame import math WIDTH = 800 # initialize window WINDOW = pygame.display.set_mode((WIDTH, WIDTH)) # always square pygame.disp

因此,我在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, 0)
GREEN = (0, 255, 0)
BLUE = (0, 255, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PURPLE = (128, 0, 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, width, 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, GREY, (j*space, 0), (j*space, 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, rows, width):
    win.fill(WHITE)
    for row in grid:
        for spot in row:
            spot.draw(win)
    draw_grid(win, rows, width)
    pygame.display.update()
# where did the user click?


def get_clicked_pos(pos, rows, 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, width)
    while(run):
        draw(win, grid, rows, 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, rows, width)
                spot = grid[row][col]
                print(spot,row,col)
                spot.make_wall()
    pygame.quit()

main(WINDOW, WIDTH)
导入pygame
输入数学
宽度=800
#初始化窗口
WINDOW=pygame.display.set_模式((宽度,宽度))#始终为方形
pygame.display.set_标题(“路径查找”)
#颜色常数-------
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,255,0)
黄色=(255,255,0)
白色=(255,255,255)
黑色=(0,0,0)
紫色=(128,0,128)
橙色=(255,165,0)
灰色=(128128128)
绿松石=(64224208)
#班级--------
上课地点:
定义初始值(自身、行、列、宽度、高度):
self.row=行
self.col=col
self.x=行*宽度
self.y=列*高度
self.color=白色
self.width=宽度
自我高度=高度
self.neights=[]
#定位
def获取位置(自身):
返回(self.col、self.row)
#让我画点
def抽签(自我,赢):
pygame.draw.rect(win,self.color,(self.x,self.y,self.width,self.height))
#变色方法
def make_墙(自):
self.color=黑色
打印(“做了一面墙!”)
def使_启动(自):
self.color=橙色
def make_end(自):
self.color=绿松石色
def使_关闭(自身):
self.color=红色
def使_打开(自身):
self.color=绿色
def重置(自):
self.color=白色
def生成路径(自身):
self.color=紫色
#布尔访问器是开始/结束等
def是_墙(自身):
返回self.color==黑色
def为自动启动(自):
返回self.color==橙色
def为_端(自身):
返回self.color==绿松石色
def已关闭(自身):
返回self.color==红色
def打开(自身):
返回self.color==绿色
#函数来填充邻居
def更新_邻居(自身、网格):
self.neighbors=[]删除当前邻居
#如果它不是最后一排,下面的那一排也不是墙,
如果self.x0而不是grid[self.x][self.y-1],则为_wall():
#然后将其添加到邻居中
self.neights.append(网格[self.x][self.y-1])
如果self.y0,而不是grid[self.x-1][self.y]。是_wall():
#然后将其添加到邻居中
self.neights.append(网格[self.x-1][self.y])
#创建点阵列(栅格),
def生成网格(行、宽度、高度):
网格=[]
间隙=宽度//高度
对于范围内的i(行):
grid.append([])
对于范围内的j(行):
点=点(i,j,间隙,高度)
网格[i]。追加(点)
回流栅
#在屏幕上画一个网格
def绘制网格(win、行、宽度):
空格=宽度//行
对于范围内的i(行):
pygame.draw.line(赢,灰色,(0,i*空格),(宽度,i*空格))
对于范围内的j(行):
pygame.draw.line(赢,灰色,(j*空格,0),(j*空格,宽度))
#当用户在网格上单击鼠标时,他们会更改单元格的颜色
#允许使用几个小按钮来放置开始和结束
#绘制背景
def绘图(win、栅格、行、宽度):
win.fill(白色)
对于网格中的行:
对于行中的点:
现场抽签(获胜)
绘制网格(win、行、宽度)
pygame.display.update()
#用户点击了哪里?
def点击位置(位置、行、宽度):
y、 x=位置;
间隙=宽度//行
行=y//间隙
col=x//gap
返回行,列
#游戏循环
def主(win,宽度):
行数=50
运行=真
网格=制作网格(行、宽度、宽度)
同时(运行):
绘制(win、栅格、行、宽度)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果pygame.mouse.get_pressed()[0]:#左键单击
pos=pygame.mouse.get_pos()
打印(pos)
行,列=点击位置(位置,行,宽度)
spot=网格[行][列]
打印(点、行、列)
spot.make_wall()
pygame.quit()
主(窗口、宽度)

我认为您的
生成网格
功能有问题

而不是

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, width, 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, j, gap, gap)
            grid[i].append(spot)
    return grid