[英]PyGame only shows black screen
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)
Hello i'm new to pygame and I was trying to make something with tutorials, but I didn't wanted to copy evrything from the tutorials I watch, and I tried to figure it my own.你好,我是 pygame 的新手,我想用教程做点什么,但我不想从我看过的教程中复制所有东西,我试着自己想办法。 However, I can't figure this one out.
但是,我无法弄清楚这一点。 I had a black screen when I run the program.
运行程序时出现黑屏。 Can anyone tell me what did I did wrong?
谁能告诉我我做错了什么? I cannot figure where I did do wrong
我不知道我哪里做错了
You have to target surfaces, screen
and display
.您必须定位表面、
screen
和display
。 However, just 1 of this surfaces can be associated to the window.但是,只有其中一个表面可以与 window 相关联。
screen = pygame.display.set_mode(window_size, 0, 32)
Therefore you will only see the parts of the scene that are drawn on the screen
Surface , but not the parts that are drawn on the display
Surface .因此,您只会看到绘制在
screen
Surface上的场景部分,而看不到绘制在display
Surface上的部分。 Remove the variable display
and replace display
with screen
删除变量
display
并用screen
替换display
As a quick fix you can simply set display
with screen
:作为快速修复,您可以简单地设置
display
with screen
:
display = pygame.Surface((350,250))
display = screen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.