为什么我的表面似乎已经正确放置了

发布于 2025-02-04 12:56:35 字数 2149 浏览 4 评论 0原文

我是Python的新手,并且正在关注一个名为“ Pygame的最终介绍”的YouTube视频。我已经有一切正常,我只是打开文件以开始添加更多代码,而我的屏幕不再填充。它应该显示的是一个天空,地面,一个小太空人和一个从左到右移动的蜗牛。似乎正在放置表面,因为我仍然可以在屏幕上与它们互动并获得打印语句,但它只是黑色。

这是代码,我不确定如何开始对此进行故障排除,因为我没有得到追溯或任何东西。从技术上讲,一切都在起作用。

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
test_font = pygame.font.Font("font/pixeltype.ttf", 75)

sky_surface = pygame.image.load('graphics/sky.png').convert_alpha()
ground_surface = pygame.image.load('graphics/ground.png').convert_alpha()
score_surface = test_font.render("Runnin' Round!", False, (64, 64, 64))
score_rect = score_surface.get_rect(midtop=(400, 50))


snail_surface = pygame.image.load("graphics/snail/snail1.png").convert_alpha()
snail_rect = snail_surface.get_rect(bottomright=(600, 300))
player_surface = pygame.image.load("graphics/player/player_walk_1.png").convert_alpha()
player_rect = player_surface.get_rect(topleft=(80, 200))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        # if event.type == pygame.MOUSEMOTION:
        #    if player_rect.collidepoint(event.pos): print("collision") #works with black screen??

    screen.blit(sky_surface, (0, 0))  # place sky
    screen.blit(ground_surface, (0, 300))  # place ground
    pygame.draw.rect(screen, '#c0e8ec', score_rect)
    pygame.draw.rect(screen, '#c0e8ec', score_rect, 10)
    screen.blit(score_surface, score_rect)
    print('placed background') #test to see if above is broke

    snail_rect.x -= 5
    if snail_rect.right <= 0:
        snail_rect.left = 800
    screen.blit(snail_surface, snail_rect)  # place snail
    screen.blit(player_surface, player_rect)  # place player

    if player_rect.colliderect(snail_rect):
        print("colission") #also works with black screen???

    mouse_pos = pygame.mouse.get_pos()
    if player_rect.collidepoint(mouse_pos):
        pygame.mouse.get_pressed()
        print(pygame.mouse.get_pressed()) #this works even with black screen????

    clock.tick(60)

I'm new to Python and I'm following along on a youtube video called the ultimate introduction to Pygame. I've had everything working and I just opened the file to start adding more code and my screen no longer populates. what it should show is a sky, the ground, a little space man, and a snail that moves left to right. it seems like the surfaces are being placed because i can still interact with them on the screen and get the print statements to go, but its just black.

heres the code, I'm not sure how to even start troubleshooting this because im not getting a traceback or anything. it's technically all working.

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
test_font = pygame.font.Font("font/pixeltype.ttf", 75)

sky_surface = pygame.image.load('graphics/sky.png').convert_alpha()
ground_surface = pygame.image.load('graphics/ground.png').convert_alpha()
score_surface = test_font.render("Runnin' Round!", False, (64, 64, 64))
score_rect = score_surface.get_rect(midtop=(400, 50))


snail_surface = pygame.image.load("graphics/snail/snail1.png").convert_alpha()
snail_rect = snail_surface.get_rect(bottomright=(600, 300))
player_surface = pygame.image.load("graphics/player/player_walk_1.png").convert_alpha()
player_rect = player_surface.get_rect(topleft=(80, 200))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        # if event.type == pygame.MOUSEMOTION:
        #    if player_rect.collidepoint(event.pos): print("collision") #works with black screen??

    screen.blit(sky_surface, (0, 0))  # place sky
    screen.blit(ground_surface, (0, 300))  # place ground
    pygame.draw.rect(screen, '#c0e8ec', score_rect)
    pygame.draw.rect(screen, '#c0e8ec', score_rect, 10)
    screen.blit(score_surface, score_rect)
    print('placed background') #test to see if above is broke

    snail_rect.x -= 5
    if snail_rect.right <= 0:
        snail_rect.left = 800
    screen.blit(snail_surface, snail_rect)  # place snail
    screen.blit(player_surface, player_rect)  # place player

    if player_rect.colliderect(snail_rect):
        print("colission") #also works with black screen???

    mouse_pos = pygame.mouse.get_pos()
    if player_rect.collidepoint(mouse_pos):
        pygame.mouse.get_pressed()
        print(pygame.mouse.get_pressed()) #this works even with black screen????

    clock.tick(60)

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

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

发布评论

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

评论(1

等风也等你 2025-02-11 12:56:35

在clock.tick之前或之后,
type pygame.display.update()
您没有更新显示,所以它是黑色的

In the while true before clock.tick or after
Typepygame.display.update()
You are not updating the display so it is black

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