can't get counter to move across a board anymore

Question:

I’m creating a board game and i’m in the initial stages of displaying the board and getting the counter to move across the squares. The code to get the counter to move worked, but I had to change where it was because the square colours were constantly updating rather than staying one solid colour as they were in the ‘while running’ loop.

Now the colours are solid, but the counter won’t move.

Here’s the code:

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

pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("board")
exit = False

grey = (224,224, 224)
green = (204, 255, 204)
blue = (204, 255, 255)
purple = (204, 204, 255)
black = (0, 0, 0)
white = (255, 255, 255)
rows = 10
cols = 14
colors = [grey, green, blue, purple]


def display_board(screen_width, screen_height, square_size, margin):
    counter_pos = [-0.35, -0.52]
    direction = 'right'
    for row in range(rows):
            for col in range(cols):
                if (row == 0 or row == rows-1 or col == cols or col == cols-1):
                    color = random.choice(colors)
                    pygame.draw.rect(screen, color, (col * square_size + margin + 2 * col, row * square_size + margin + 2 * row, square_size, quare_size))
                elif (row == 0 and col > 0 and col < cols - 1) or (row == rows - 1 and col > 0 and col < cols - 1) or (col == 0 and row > 0 nd row < rows - 1) or (col == cols - 1 and row > 0 and row < rows - 1):
                    color = random.choice(colors)
                    pygame.draw.rect(screen, color, (col * square_size + margin + 2 * col, row * square_size + margin + 2 * row, square_size, quare_size))
                  
    # Draw the counter
    pygame.draw.rect(screen, (255, 255, 255), (counter_pos[1] * square_size + margin + 2 * col, counter_pos[0] * square_size + margin + 2 * ow, square_size, square_size))
    pygame.display.update()

    # Main loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if direction == 'right':
                    counter_pos[1] += 1
                    if counter_pos[1] >= cols - 1:
                        direction = 'down'
                elif direction == 'down':
                    counter_pos[0] += 1
                    if counter_pos[0] >= rows - 1:
                        direction = 'left'
                elif direction == 'left':
                    counter_pos[1] -= 1
                    if counter_pos[1] <= 0:
                        direction = 'up'
                elif direction == 'up':
                    counter_pos[0] -= 1
                    if counter_pos[0] <= 0:
                       direction = 'right'

display_board(800, 600, 50, 20)
Asked By: Aryan Redwood

||

Answers:

You must redraw the scene in each frame. You must do the drawing in the application loop. The typical PyGame application loop has to:

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

pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("board")
exit = False

grey = (224,224, 224)
green = (204, 255, 204)
blue = (204, 255, 255)
purple = (204, 204, 255)
black = (0, 0, 0)
white = (255, 255, 255)
rows = 10
cols = 14
colors = [grey, green, blue, purple]


def display_board(screen_width, screen_height, square_size, margin):
    counter_pos = [0, 0]
    direction = 'right'

    fields = []
    for row in range(rows):
        rect = pygame.Rect(margin, margin + row * (square_size + 2), square_size, square_size)
        fields.append((random.choice(colors), rect))
        rect = pygame.Rect(margin + (cols-1) * (square_size + 2), margin + row * (square_size + 2), square_size, square_size)
        fields.append(( random.choice(colors), rect))
    for col in range(1, cols-1):
        rect = pygame.Rect(margin + col * (square_size + 2), margin, square_size, square_size)
        fields.append((random.choice(colors), rect))
        rect = pygame.Rect(margin + col * (square_size + 2), margin + (rows-1) * (square_size + 2), square_size, square_size)
        fields.append(( random.choice(colors), rect))

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if direction == 'right':
                    counter_pos[0] += 1
                    if counter_pos[0] >= cols - 1:
                        direction = 'down'
                elif direction == 'down':
                    counter_pos[1] += 1
                    if counter_pos[1] >= rows - 1:
                        direction = 'left'
                elif direction == 'left':
                    counter_pos[0] -= 1
                    if counter_pos[0] <= 0:
                        direction = 'up'
                elif direction == 'up':
                    counter_pos[1] -= 1
                    if counter_pos[1] <= 0:
                        direction = 'right'

        screen.fill(0)
        for color, rect in fields:
            pygame.draw.rect(screen, color, rect)
        pygame.draw.rect(screen, (255, 255, 255), (margin + counter_pos[0] * (2 + square_size), margin + counter_pos[1] * (square_size + 2), square_size, square_size))
        pygame.display.update()

display_board(800, 600, 50, 20)
Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.