Pygame 能够加载图像但出现黑屏
Pygame is able to load images but gives a black screen
我编写了这段代码,当我 运行 程序时,我只看到黑屏。没有关于无法加载图像的错误。
代码基本上是这样的:
1.绘制地图和玩家(这部分在某些时候有效,但我没有最新版本的代码了)
2.获取玩家的位置(这个有效)
这些是不起作用的部分:
3.更新玩家位置
4.重新绘制地图&绘制玩家
import sys
import pygame
pygame.init()
print(pygame.ver)
#just to make it easier
false = False
true = True
#lenght and width of player image
xPlayer = 50
yPlayer = 50
#LOADED IMAGES
imagePlayer = pygame.image.load("image_player.png")
imageWall = pygame.image.load("image_wall.png")
imageStart = pygame.image.load("image_start.png")
imageFinish = pygame.image.load("image_finish.png")
#SCREEN
windowWidth = 500
windowLenght = 500
display_surf = pygame.display.set_mode((windowWidth, windowLenght))
#maze and everything related
class maze:
def __init__(self):
self.gameOver = false
self.width = 10
self.lenght = 10
self.map = [
[1, 1, 1, 1, 1, 1, 1, 3, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 30, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
[2, 0, 0, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
def drawMap(self):
for i in range(0,self.width):
for j in range(0,self.lenght):
if self.map[i][j] == 1:
display_surf.blit(imageWall, (i*xPlayer, j*yPlayer))
elif self.map[i][j] == 2:
display_surf.blit(imageStart, (i*xPlayer, j*yPlayer))
elif self.map[i][j] == 3:
display_surf.blit(imageFinish, (i*xPlayer, j*yPlayer))
def blitPlayer(self, positionHorizontal, positionVertical):
display_surf.blit(imagePlayer, (positionHorizontal*xPlayer, positionVertical*yPlayer))
def findPlayer(self):
# TODO: find where player is and return value
initialPositions = []
initialPositionY = 0
initialPositionX = 0
initialPositionYBuffer = 0
initialPositionXBuffer = 0
foundPlayer = false
while initialPositionXBuffer in range(0, self.width) and not foundPlayer:
while initialPositionYBuffer in range(0, self.lenght) and not foundPlayer:
if self.map[initialPositionXBuffer][initialPositionYBuffer] == 30:
foundPlayer = true
initialPositionY = initialPositionYBuffer
initialPositionX = initialPositionXBuffer
initialPositionYBuffer += 1
initialPositionXBuffer += 1
if foundPlayer:
initialPositions = [initialPositionX, initialPositionY]
initialPositionYBuffer = 0
return initialPositions
def movePlayerTest(self):
newPositions = self.findPlayer()
while not self.gameOver:
for event in pygame.event.get():
self.drawMap()
self.blitPlayer(self.findPlayer()[0], self.findPlayer()[1])
if event.type == pygame.QUIT:
self.gameOver = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
newPositions = self.findPlayer()
newPositions[1] += 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_RIGHT:
newPositions = self.findPlayer()
newPositions[1] -= 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_UP:
newPositions = self.findPlayer()
newPositions[0] -= 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_DOWN:
newPositions = self.findPlayer()
newPositions[0] += 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
pointCountP1 = 0
pointCountP2 = 0
playerNumber = 30
mazeDisplayed = maze()
print(mazeDisplayed.findPlayer())
#mazeDisplayed.drawMap()
mazeDisplayed.movePlayerTest()
我得到的是黑屏。即使 drawMap 方法也不起作用。
self.drawMap()
和 self.blitPlayer
必须在主循环而不是事件循环中完成。 pygame.display.update()
(or pygame.display.flip()
) 丢失,需要使显示的更改可见。
class maze:
# [...]
def movePlayerTest(self):
newPositions = self.findPlayer()
while not self.gameOver:
self.drawMap()
self.blitPlayer(self.findPlayer()[0], self.findPlayer()[1])
pygame.display.update()
for event in pygame.event.get():
# [...]
我编写了这段代码,当我 运行 程序时,我只看到黑屏。没有关于无法加载图像的错误。
代码基本上是这样的: 1.绘制地图和玩家(这部分在某些时候有效,但我没有最新版本的代码了) 2.获取玩家的位置(这个有效)
这些是不起作用的部分: 3.更新玩家位置 4.重新绘制地图&绘制玩家
import sys
import pygame
pygame.init()
print(pygame.ver)
#just to make it easier
false = False
true = True
#lenght and width of player image
xPlayer = 50
yPlayer = 50
#LOADED IMAGES
imagePlayer = pygame.image.load("image_player.png")
imageWall = pygame.image.load("image_wall.png")
imageStart = pygame.image.load("image_start.png")
imageFinish = pygame.image.load("image_finish.png")
#SCREEN
windowWidth = 500
windowLenght = 500
display_surf = pygame.display.set_mode((windowWidth, windowLenght))
#maze and everything related
class maze:
def __init__(self):
self.gameOver = false
self.width = 10
self.lenght = 10
self.map = [
[1, 1, 1, 1, 1, 1, 1, 3, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 30, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
[2, 0, 0, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
def drawMap(self):
for i in range(0,self.width):
for j in range(0,self.lenght):
if self.map[i][j] == 1:
display_surf.blit(imageWall, (i*xPlayer, j*yPlayer))
elif self.map[i][j] == 2:
display_surf.blit(imageStart, (i*xPlayer, j*yPlayer))
elif self.map[i][j] == 3:
display_surf.blit(imageFinish, (i*xPlayer, j*yPlayer))
def blitPlayer(self, positionHorizontal, positionVertical):
display_surf.blit(imagePlayer, (positionHorizontal*xPlayer, positionVertical*yPlayer))
def findPlayer(self):
# TODO: find where player is and return value
initialPositions = []
initialPositionY = 0
initialPositionX = 0
initialPositionYBuffer = 0
initialPositionXBuffer = 0
foundPlayer = false
while initialPositionXBuffer in range(0, self.width) and not foundPlayer:
while initialPositionYBuffer in range(0, self.lenght) and not foundPlayer:
if self.map[initialPositionXBuffer][initialPositionYBuffer] == 30:
foundPlayer = true
initialPositionY = initialPositionYBuffer
initialPositionX = initialPositionXBuffer
initialPositionYBuffer += 1
initialPositionXBuffer += 1
if foundPlayer:
initialPositions = [initialPositionX, initialPositionY]
initialPositionYBuffer = 0
return initialPositions
def movePlayerTest(self):
newPositions = self.findPlayer()
while not self.gameOver:
for event in pygame.event.get():
self.drawMap()
self.blitPlayer(self.findPlayer()[0], self.findPlayer()[1])
if event.type == pygame.QUIT:
self.gameOver = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
newPositions = self.findPlayer()
newPositions[1] += 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_RIGHT:
newPositions = self.findPlayer()
newPositions[1] -= 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_UP:
newPositions = self.findPlayer()
newPositions[0] -= 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
if event.key == pygame.K_DOWN:
newPositions = self.findPlayer()
newPositions[0] += 1
self.map[newPositions[0]][newPositions[1]] = playerNumber
self.blitPlayer(newPositions[0], newPositions[1])
pointCountP1 = 0
pointCountP2 = 0
playerNumber = 30
mazeDisplayed = maze()
print(mazeDisplayed.findPlayer())
#mazeDisplayed.drawMap()
mazeDisplayed.movePlayerTest()
我得到的是黑屏。即使 drawMap 方法也不起作用。
self.drawMap()
和 self.blitPlayer
必须在主循环而不是事件循环中完成。 pygame.display.update()
(or pygame.display.flip()
) 丢失,需要使显示的更改可见。
class maze:
# [...]
def movePlayerTest(self):
newPositions = self.findPlayer()
while not self.gameOver:
self.drawMap()
self.blitPlayer(self.findPlayer()[0], self.findPlayer()[1])
pygame.display.update()
for event in pygame.event.get():
# [...]