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()  

can some one tell me what I'm doing wrong. after everything ids first drawn it wont update when i use the clicked function in the pegs class, i used the console and print statement to see if it was actually changing and it was. the problem is that it wont show the updated color. i know its not pygame its my code.

1 Answer 1

1

The pig 0,0 is never drawn:

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  

If you look at this statement : int(x/50) and then look at x, you see that x will always be at least 50, so x/50 will always be bigger than one, so the first row will not be drawn. You can fix this by doing the same as you did for y:

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  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I could not figure out what was happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.