Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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_Python_Pygame - Fatal编程技术网

显示未更新python

显示未更新python,python,pygame,Python,Pygame,谁能告诉我我做错了什么。当我在pegs类中使用clicked函数时,在ids第一次绘制完所有内容之后,它不会更新,我使用console和print语句来查看它是否真的在更改,是否确实在更改。问题是它不会显示更新的颜色。我知道这不是pygame,这是我的代码。猪0,0永远不会被画出来: import random import pygame from pygame.locals import * import sys class Peg: def __init__(se

谁能告诉我我做错了什么。当我在pegs类中使用clicked函数时,在ids第一次绘制完所有内容之后,它不会更新,我使用console和print语句来查看它是否真的在更改,是否确实在更改。问题是它不会显示更新的颜色。我知道这不是pygame,这是我的代码。

0,0
永远不会被画出来:

import random  
import pygame  
from pygame.locals import *  
import sys  

class Peg:

    def __init__(self, color):
        self.color = Row.colors[color]
        self.num = color

    def clicked(self):
        self.num += 1
        if(self.num>5):
            self.num = 0
        self.color = Row.colors[self.num]

class Row:  
    colors = ['red', 'white', 'blue', 'yellow', 'green', 'orange', 'none']

    def __init__(self, solution):
        if(not solution):
            self.pegs = [Peg(6) for x in range(4)]
        else:
            self.pegs = [Peg(random.randint(0, 5)) for x in range(4)]

class Board:

    def __init__(self):
        self.rows = [Row(False) for x in range(12)]
        self.solution = Row(True)

pygame.init()

b = Board()

def draw_board(b):  
    c=0  
    for x in range(50, 600, 50):  
        screen.blit(row, (50, x))  
        for y in range(0, 4):  
            screen.blit(pegs[b.rows[(int)(x/50)].pegs[y].num], ((84+(52*y), x+1)))  
        c+=1  


screen = pygame.display.set_mode((400, 700))  
pygame.display.set_caption('Mastermind')  

pegs = [pygame.image.load('images/peg_{}.png'.format(Row.colors[x])) for x in range(7)]  
row = pygame.image.load('images/row.png')  



draw_board(b)  
pygame.display.update()  


while True:  
    for event in pygame.event.get():  
            if event.type == QUIT:  
                pygame.quit()  
                sys.exit()  
            if event.type == KEYDOWN:  
                if event.key == K_ESCAPE:  
                    pygame.quit()  
                    sys.exit()  
            if event.type == MOUSEBUTTONDOWN:  
                if(event.button==1):  
                    b.rows[0].pegs[0].clicked()  

    draw_board(b)  
    pygame.display.update()  
如果您查看以下语句:
int(x/50)
,然后查看
x
,您会发现
x
将始终至少是
50
,因此
x/50
将始终大于一,因此不会绘制第一行。您可以通过执行与y相同的操作来修复此问题:

def draw_board(b):  
    c = 0
    for x in range(50, 600, 50):
        screen.blit(row, (50, x))
        for y in range(0, 4):
            screen.blit(pegs[b.rows[int(x/50)].pegs[y].num], ((84+(52*y), x+1)))
        c += 1  

谢谢,我不知道发生了什么事。
def draw_board(b):  
    c = 0
    for x in range(12):
        screen.blit(row, (50, 50 + x * 50))
        for y in range(0, 4):
            screen.blit(pegs[b.rows[x].pegs[y].num], (84 + 52 * y, 51 + x * 50))
        c += 1