Pygame能够加载图像,但会给出黑屏



我编写了这段代码,当我运行程序时,我只得到一个黑屏。无法加载图像没有错误。

代码基本上是这样的: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()(或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():
                # [...]

相关内容

  • 没有找到相关文章

最新更新