python - 为什么这段代码没有绘制我想要的开始按钮?

标签 python image pygame

我已经尝试了很多,但仍然不明白为什么它不加载按钮。

import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()

pygame.display.set_caption("GermanBall")
bg = pygame.image.load("Tan.jpg")
icon = pygame.image.load("box.png")
button1 = pygame.image.load("shirt.png").convert_alpha()
class Button():
    def __init__(self,x,y,image):
        self.image = image 
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)

    def draw(self):
        screen.blit(self.image,(self.rect.x,self.rect.y)) 
start = Button(0,0,button1)


pygame.display.set_icon(icon)
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    start.draw()
    screen.blit(bg,(0,0))
    pygame.display.update()


我想要加载按钮。它不会给我错误,只是无法加载。

最佳答案

您必须在 screen.blit(bg,(0,0)) 之后、pygame.display.update 之前调用 start.draw() ()。注意:screen.blit(bg, (0,0) 在整个屏幕上绘制背景图像,这样之前绘制的所有内容都会被 overdraw 。pygame.display.update() 更新显示并使显示表面的所有更改立即可见:

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

    # draw background
    screen.blit(bg,(0,0))

    # draw button
    start.draw()

    # update display
    pygame.display.update()

关于python - 为什么这段代码没有绘制我想要的开始按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75074144/

相关文章:

python - 在python中搜索具有相同文件名的所有文件的最有效方法

python - 通过 Python 为 Excel 中的单元格着色的更快方法

android - 选择图像时无法解码流

html - 将不同尺寸的图像放在彼此下面

python - png 图像移动速度太快,如何减慢速度? (Python)

python 对 Flask-SQLAlchemy 中 AppenderBaseQuery 属性的赋值无效

javascript - Flask:服务器发送事件(SSE)流功能在处理输入时停止

css - 将表格放置在 float 图像旁边

python - 我的游戏结束代码中的碰撞在 pygame 中无法正常工作

python - 声音文件不在pygame中播放