我正在尝试将精灵图像插入 pygame 窗口,但它不会弹出

发布于 2025-01-20 03:37:24 字数 563 浏览 0 评论 0 原文

(我跟着yt tut所以idk)我的代码:idk如果我把代码放在错误的地方

import pygame
   
 white = (255,255,255)
 black = (0,0,0)
 orange = (255,165,0) 
 player_sprite = pygame.image.load("will smith.png") .convert_alpha


 pygame.init()
 display = pygame.display.set_mode((850,850))
 pygame.display.set_caption('Conners Game')

 exit = False

while not exit:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        exit = True
    print(event)

display.fill(white)
pygame.display.update()

pygame.quit()
quit()

不太确定为什么这样做,我是python新手所以idk

(i followed a yt tut so idk) my code: idk if i put code in wrong place

import pygame
   
 white = (255,255,255)
 black = (0,0,0)
 orange = (255,165,0) 
 player_sprite = pygame.image.load("will smith.png") .convert_alpha


 pygame.init()
 display = pygame.display.set_mode((850,850))
 pygame.display.set_caption('Conners Game')

 exit = False

while not exit:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        exit = True
    print(event)

display.fill(white)
pygame.display.update()

pygame.quit()
quit()

not really sure why it does this, im new to python so idk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2025-01-27 03:37:24

有多种需要修复的事情。首先,您实际上需要 blit 屏幕上的图像。

while not exit:

    for event in pygame.event.get():
        #...[block of code]

    display.fill(white)
    display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN

.convert_alpha()是一种方法,在初始化pygame并设置视频模式后,您需要调用它。

您可以阅读有关 convert_alpha() 在这里

pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')

#converting it after init and setting game mode

player_sprite = pygame.image.load("will smith.png").convert_alpha() 

这是最终的工作代码

import pygame
   
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0) 



pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False

while not exit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        # print(event)

    display.fill(white)
    display.blit(player_sprite, (100,100))
    pygame.display.update()

pygame.quit()
quit()

There are multiple things that needs to fixed. First of all to you actually need to blit the image on the screen.

while not exit:

    for event in pygame.event.get():
        #...[block of code]

    display.fill(white)
    display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN

.convert_alpha() is a method and you need to call it after you initialize pygame and set the video mode.

You can read more about convert_alpha() here

pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')

#converting it after init and setting game mode

player_sprite = pygame.image.load("will smith.png").convert_alpha() 

Here's the final working code

import pygame
   
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0) 



pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False

while not exit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        # print(event)

    display.fill(white)
    display.blit(player_sprite, (100,100))
    pygame.display.update()

pygame.quit()
quit()
口干舌燥 2025-01-27 03:37:24

您必须 >应用程序循环中的映像,清除显示后,然后更新显示器之前:

display.blit(player_sprite, (100, 100))

convert_alpha 是功能。您错过了括号

player_sprite = pygame.image.load(“ will smith.png”)。convert_alpha

player_sprite = pygame.image.load("will smith.png").convert_alpha()

,您在初始化pygame之前也无法调用pygame函数。 pygame.init()应该是第一个调用的pygame函数。

完整示例:

import pygame
   
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0) 

pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')

player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        print(event)

    display.fill(white)
    display.blit(player_sprite, (100, 100))
    pygame.display.update()

pygame.quit()
quit()

典型的PyGame应用程序循环必须:

You have to blit the image in the application loop, after clearing the display and before updating the display:

display.blit(player_sprite, (100, 100))

convert_alpha is function. You missed the parentheses

player_sprite = pygame.image.load("will smith.png").convert_alpha

player_sprite = pygame.image.load("will smith.png").convert_alpha()

Also, you cannot call a pygame function before you initialize pygame. pygame.init() should be the very first pygame function called.

Complete example:

import pygame
   
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0) 

pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')

player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        print(event)

    display.fill(white)
    display.blit(player_sprite, (100, 100))
    pygame.display.update()

pygame.quit()
quit()

The typical PyGame application loop has to:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文