PyGame 只显示黑屏

时间:2021-01-14 14:30:56

标签: python pygame pygame-surface

import pygame, sys
#from pygame.locals import *

def load_map(path):
    f = open(path + ".txt","r")
    data = f.read()
    f.close()
    data = data.split("\n")
    game_map = []
    for row in data:
        game_map.append(list(row))
    return(game_map)

#main
pygame.init()
pygame.display.set_caption("test1")
main_map = load_map("map/main_map")

#main Variables
clock = pygame.time.Clock()
window_size = (700, 500)
screen = pygame.display.set_mode(window_size, 0, 32)
scroll = [0,0]
display = pygame.Surface((350,250))

#tiles
dirt1 = pygame.image.load("tiles/grass1.png")
water1 = pygame.image.load("tiles/grass.png")

#player variables
playerx = 350
playery = 350
player_image = pygame.transform.scale(pygame.image.load("player/player.png").convert_alpha(), (150, 150))
player_rect = player_image.get_rect(center = (80,50))

#button variables
move_right = False
move_left = False
move_up = False
move_down = False

game_map = load_map("map/main_map")

while True:
    screen.blit(player_image, [playerx, playery], player_rect)

    y = 0
    for layer in game_map:
        x = 0
        for tile in game_map:
            if tile == "G":
                display.blit(dirt1, (x*16, y*16))
            if tile == "W":
                display.blit(water1, (x*16, y*16))
            x += 1
        y += 1

    if move_right == True:
        playerx += 2
    if move_left == True:
        playerx -= 2
    if move_up == True:
        playery -= 2
    if move_down == True:
         playery += 2

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                move_right = True
            if event.key == pygame.K_a:
                move_left = True
            if event.key == pygame.K_w:
                move_up = True
            if event.key == pygame.K_s:
                move_down = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d:
                move_right = False
            if event.key == pygame.K_a:
                move_left = False
            if event.key == pygame.K_w:
                move_up = False
            if event.key == pygame.K_s:
                move_down = False

    screen.blit(pygame.transform.scale(display, window_size),(0,0))
    pygame.display.update()
    clock.tick(120)

你好,我是 pygame 的新手,我试图用教程制作一些东西,但我不想从我观看的教程中复制所有东西,我试图自己弄清楚。但是,我无法弄清楚这一点。我运行程序时出现黑屏。谁能告诉我我做错了什么?我不知道我哪里做错了

1 个答案:

答案 0 :(得分:0)

您必须定位表面 screendisplay。但是,只有 1 个表面可以与窗口相关联。

<块引用>
screen = pygame.display.set_mode(window_size, 0, 32)

因此,您只会看到在 screen Surface 上绘制的场景部分,而看不到在 display Surface 上绘制的部分。删除变量 display 并将 display 替换为 screen

作为快速修复,您可以简单地将 display 设置为 screen

display = pygame.Surface((350,250))

display = screen
相关问题