【问题标题】:Why is this code not drawing the start button I want?为什么这段代码没有画出我想要的开始按钮?
【发布时间】:2023-01-11 02:18:03
【问题描述】:

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

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)) 立即擦除您绘制的所有内容。

标签: python image pygame


【解决方案1】:

您必须在screen.blit(bg,(0,0))之后、pygame.display.update()之前致电start.draw()。注意:screen.blit(bg, (0,0)在整个屏幕上绘制背景图像,因此之前绘制的所有内容都被透支了。 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()

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多