简体   繁体   English

Pygame不会将图像拖到画布上

[英]Pygame doesn't blit images onto canvas

I've made an icon for a gui in a little project I'm working on, and pygame doesn't display it. 我在一个正在进行的小项目中为gui创建了一个图标,而pygame却不显示它。 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

There are two small errors 有两个小错误

  1. You forgot to draw toolscanvas on main pygame display ( gameDisplay.blit(toolscanvas, (0, 0)) ) 您忘记在pygame主显示器上绘制toolscanvasgameDisplay.blit(toolscanvas, (0, 0))
  2. An image is read with alfa channel and has only black pixels. 使用Alfa通道读取的图像只有黑色像素。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM