使用 'Blit' Pygame 图像未打印到屏幕
Image not printing to screen using 'Blit' Pygame
我正在尝试在 python 中制作类似 Undertale 的游戏 - 但是我遇到了问题。当我尝试 'blit' 图像到屏幕时,它没有显示。我是 'blitting' 一颗心,我已在第 3 方程序中将其重新调整为合适的大小。
import pygame # Imports Pygame Library
import keyboard # Imports Keyboard Library
# Defining Image Components
undertale_logo_ico = pygame.image.load('Undertale_Logo.ico')
Soul_Heart_Red = pygame.image.load('UndertaleHeart.png')
# Define Key-presses
def movement(key_pressed):
if key_pressed == 'right':
print('Right')
elif key_pressed == 'left':
print('Left')
elif key_pressed == 'up':
print('Up')
elif key_pressed == 'down':
print('Down')
else:
print('Not registered key')
# Execute Key Presses:
keyboard.on_press_key("right arrow", lambda _: movement('right'))
keyboard.on_press_key("left arrow", lambda _: movement('left'))
keyboard.on_press_key("up arrow", lambda _: movement('up'))
keyboard.on_press_key("down arrow", lambda _: movement('down'))
keyboard.on_press_key("d", lambda _: movement('right'))
keyboard.on_press_key("a", lambda _: movement('left'))
keyboard.on_press_key("w", lambda _: movement('up'))
keyboard.on_press_key("s", lambda _: movement('down'))
# Display Window Configuration
# area = screen.get_rect()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
background_colour = (0, 0, 0)
(width, height) = (300, 200)
pygame.display.set_caption('Game 1')
screen.fill(background_colour)
pygame.display.set_icon(undertale_logo_ico)
pygame.display.flip()
# Drawing Player
screen.blit(Soul_Heart_Red, [0, 0])
# Closing Script
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
blit() 位于代码底部附近。
您必须在 blit
之后更新显示:
screen.blit(Soul_Heart_Red, [0, 0])
pygame.display.flip() # <---
你必须在 blit 之后更新显示或者在显示之前更新 blit
screen.blit(Soul_Heart_Red, [0, 0])
pygame.display.update
我正在尝试在 python 中制作类似 Undertale 的游戏 - 但是我遇到了问题。当我尝试 'blit' 图像到屏幕时,它没有显示。我是 'blitting' 一颗心,我已在第 3 方程序中将其重新调整为合适的大小。
import pygame # Imports Pygame Library
import keyboard # Imports Keyboard Library
# Defining Image Components
undertale_logo_ico = pygame.image.load('Undertale_Logo.ico')
Soul_Heart_Red = pygame.image.load('UndertaleHeart.png')
# Define Key-presses
def movement(key_pressed):
if key_pressed == 'right':
print('Right')
elif key_pressed == 'left':
print('Left')
elif key_pressed == 'up':
print('Up')
elif key_pressed == 'down':
print('Down')
else:
print('Not registered key')
# Execute Key Presses:
keyboard.on_press_key("right arrow", lambda _: movement('right'))
keyboard.on_press_key("left arrow", lambda _: movement('left'))
keyboard.on_press_key("up arrow", lambda _: movement('up'))
keyboard.on_press_key("down arrow", lambda _: movement('down'))
keyboard.on_press_key("d", lambda _: movement('right'))
keyboard.on_press_key("a", lambda _: movement('left'))
keyboard.on_press_key("w", lambda _: movement('up'))
keyboard.on_press_key("s", lambda _: movement('down'))
# Display Window Configuration
# area = screen.get_rect()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
background_colour = (0, 0, 0)
(width, height) = (300, 200)
pygame.display.set_caption('Game 1')
screen.fill(background_colour)
pygame.display.set_icon(undertale_logo_ico)
pygame.display.flip()
# Drawing Player
screen.blit(Soul_Heart_Red, [0, 0])
# Closing Script
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
blit() 位于代码底部附近。
您必须在 blit
之后更新显示:
screen.blit(Soul_Heart_Red, [0, 0])
pygame.display.flip() # <---
你必须在 blit 之后更新显示或者在显示之前更新 blit
screen.blit(Soul_Heart_Red, [0, 0])
pygame.display.update