0

I am coding a basic program that reads data from a file where each character represents a pixel's color and then displays that info on the screen using pygame.

I've got a block of code that reads the file which works beautifully, but when I try to display the pixels one by one, I get an error that says "descriptor 'set_at' for 'pygame.Surface' objects doesn't apply to 'tuple' object". "Grid" is a 2D array that has the 0 and 1 data from the file.

print("Done processing file, Drawing pixels...")

for i in range(0, height - 2):
    for j in range(0, width - 2):
        if grid[i][j] == 0:
            pygame.Surface.set_at((i, j), "black") #Throws the error
        elif grid[i][j] == 1:
            pygame.Surface.set_at((i, j), "white")

print("Pixels drawn!")

I'm trying to use the "i" and "j" values to describe the "x" and "y" of the pixel I want to modify, but pygame seems to be reading that as a different data type than I want? I'm not entirely sure why pygame doesn't expect a tuple for coordinates (seems like the most logical data type for cartesian coordinates to me).

I'm very new to pygame, so I'm hoping this is a relatively simple mistake with an easy fix. If you want the docs for this specific function: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_at

  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Oct 11 '19 at 00:06
  • surface should be instance of class `pygame.Surface`, not class `pygame.Surface` - ie. `my_surface = pygame.Surface()` and `my_surface.set_at(...)`. Or `screen = pygame.display.set_mode(...)` and `screen.set_at(...)` – furas Oct 11 '19 at 00:08

1 Answers1

2

Your problem is not (x,y) but pygame.Surface.

You have to use instance of class pygame.Surface, not class pygame.Surface

surface = pygame.Surface((800,600))
surface.set_at((i, j), (255,255,255))

or

screen = pygame.display.set_mode((800,600))
screen.set_at((i, j), (255,255,255))

or

image = pygame.image.load('image.png')
image.set_at((i, j), (255,255,255))

or

font = pygame.font.SysFont(None, 50)
text = font.render("Hello World", True, (255,255,255))
text.set_at((i, j), (255,255,255))

You will have another problem with color. It has to be tuple (R,G,B) - eventually (R,G,B,A) if you use transparency (alpha channel).

For white it will be (255,255,255), for black it will be (0,0,0).

To use string with name you would have to use class pygame.color.Color(name)

screen.set_at((i, j), pygame.color.Color('white'))    

Example code:

import pygame

pygame.init()
screen = pygame.display.set_mode((800,600))

#WHITE = pygame.color.Color('white')
WHITE = (255,255,255)

for x in range(100):
    for y in range(100):
        screen.set_at((350+x, 250+y), WHITE)

pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()
furas
  • 134,197
  • 12
  • 106
  • 148