2

I've made an icon for a gui in a little project I'm working on, and pygame doesn't display it. What am I doing wrong?

import pygame
black = (0,0,0)
toolscanvas = pygame.Surface((700,120))

pygame.init()
gameDisplay = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
gameDisplay.fill(black)
gameDisplay.convert()
clock = pygame.time.Clock()


class GuiHouse:
    def __init__(self):
        self.x = 0
        self.y = 20
        self.canvas = pygame.Surface((300,300))
        self.canvas.set_alpha(128)
        self.iconed = pygame.image.load("house_icon.png").convert_alpha()
        self.iconed = pygame.transform.scale(self.iconed, (60, 60))
    def display(self):
        global toolscanvas
        toolscanvas.fill((0,0,0))
        self.canvas.blit(self.iconed, (0, 0))
        toolscanvas.blit(self.canvas, (self.x, self.y))
        gameDisplay.blit(toolscanvas,(0,0))


guihouse = GuiHouse()
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                quit()
    guihouse.display()
    pygame.display.update()
    clock.tick(120)

Real code is a lot longer, let me know if it doesn't work. Here's what the icon should look like house_icon

Anonymous
  • 131
  • 8

1 Answers1

2

There are two small errors

  1. You forgot to draw toolscanvas on main pygame display (gameDisplay.blit(toolscanvas, (0, 0)))
  2. An image is read with alfa channel and has only black pixels. So you are drawing a black picture on a black background. At example solution I have added filling image canvas with white color, so now the image is visible, but not pretty. But I hope you will find better solution :)

Example solution:

black = (0, 0, 0)
white = (255, 255, 255)
toolscanvas = pygame.Surface((700, 120))

pygame.init()
gameDisplay = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
gameDisplay.fill(black)
gameDisplay.convert()
clock = pygame.time.Clock()


class GuiHouse:
    def __init__(self):
        self.x = 0
        self.y = 20
        self.canvas = pygame.Surface((300, 300))
        self.canvas.set_alpha(128)
        self.canvas.fill(white)
        self.iconed = pygame.image.load("house_icon.png").convert_alpha()
        self.iconed = pygame.transform.scale(self.iconed, (60, 60))

    def display(self):
        global toolscanvas
        toolscanvas.fill((0, 0, 0))
        self.canvas.blit(self.iconed, (0, 0))
        toolscanvas.blit(self.canvas, (self.x, self.y))
        gameDisplay.blit(toolscanvas, (0, 0))

guihouse = GuiHouse()
while True:
    guihouse.display()
    pygame.display.update()
    clock.tick(120)
  • nope i checked the code and it is actually being drawn on screen, I just forgot to include it here. Thanks for the suggestion though. – Anonymous Oct 10 '18 at 08:52
  • @Anonymous `self.canvas` is black and the house icon is black as well, so you'll just get a black transparent surface if you blit the house onto the canvas. In this solution the canvas is filled with white to fix this problem. If that's not what you want, you should add more information to your question. By the way, I'm not sure why you need the `toolscanvas` and `self.canvas` and don't just blit the `self.iconed` surface onto the `gameDisplay` directly. – skrx Oct 10 '18 at 09:36
  • i was gonna blit image onto another canvas so that i could fade it out of the menu, menu being another canvas, whenever it reaches a limit coordinates but that's still not implemented. – Anonymous Oct 10 '18 at 11:40
  • I changed icon to white color and it seemed to have worked, thanks for the suggestion – Anonymous Oct 10 '18 at 11:43