简体   繁体   English

Pygame 显示黑屏(“Pro Python“最佳实践”)

[英]Pygame displays Black screen ("Pro Python "Best Practices")

Ive taken this code from book "Pro Python Best Practices", but cannot run the game.我已经从“Pro Python Best Practices”一书中获取了这段代码,但无法运行游戏。 It shows me black screen, after running this code:运行此代码后,它向我显示黑屏:

from load_tiles import load_tiles
from generate_maze import create_maze
from event_loop import event_loop
from draw_maze import draw_grid, parse_grid
from moves import move, LEFT, RIGHT, UP, DOWN
from pygame import Rect
import pygame


DIRECTIONS = {
    276: LEFT, 275: RIGHT,
    273: UP, 274: DOWN
}


def draw():
    """Displays the maze on the screen"""
    img = draw_grid(maze, tile_img, tiles)
    display.blit(img, Rect((0, 0, 384, 224)), Rect((0, 0, 384, 224)))
    pygame.display.update()


def handle_key(key):
    """Handles key events in the game"""
    direction = DIRECTIONS.get(key)
    if direction:
        move(maze, direction)
    draw()


if __name__ == '__main__':
    # initialize display
    pygame.init()
    pygame.display.set_mode((800, 600))
    display = pygame.display.get_surface()
    tile_img, tiles = load_tiles()

    # prepare the maze
    maze = parse_grid(create_maze(12, 7))
    maze[1][1] = '*'
    maze[5][10] = 'x'

    # start the game
    draw()
    event_loop(handle_key)

Terminal does not show me any errors.终端没有显示任何错误。 It's just run black screen.就是运行黑屏。 Ive tried to find similar problems, but in most cases the problem is in code.我试图找到类似的问题,但在大多数情况下,问题出在代码中。 According to book it have to work.根据书,它必须工作。 I would appreciate for any help!我将不胜感激!

I happen to have a copy of this book, and it has a number of requirements in a textfile on the github directory where i assume you pasted this code from.我碰巧有这本书的副本,它在 github 目录上的文本文件中有许多要求,我假设您是从那里粘贴此代码的。 Ensure you have all the requirements as this is likely the issue确保您满足所有要求,因为这可能是问题所在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM