简体   繁体   English

pygame - 黑屏

[英]pygame - black screen

I did a bit of research to see if i could solve the issue that way, but didn't seem to find anything to solve my problem.我做了一些研究,看看我是否可以通过这种方式解决问题,但似乎没有找到任何可以解决我的问题的方法。 I found both of these : Why isn't my pygame display displaying anything?我发现了这两个:为什么我的 pygame 显示器不显示任何内容? and Confused at why PyGame display's a black screen.并且对 PyGame 显示黑屏的原因感到困惑。 I tried to solve my problem with what was adviced in the comments but it didn't work, or the reason for the problem was different than mine.我试图用评论中的建议解决我的问题,但没有奏效,或者问题的原因与我的不同。

When i run the code the pygame window shows, but is just completely black, but no errors are called.当我运行代码时,pygame 窗口显示,但只是全黑,但没有调用错误。

import pygame
import sys
import random
from time import sleep



padWidth = 480
padHeight = 640
rockImage = ['C:/Users/yount/Downloads/PyShooting/rock01.png', 'C:/Users/yount/Downloads/PyShooting/rock02.png', 'C:/Users/yount/Downloads/PyShooting/rock03.png', 'C:/Users/yount/Downloads/PyShooting/rock04.png', 'C:/Users/yount/Downloads/PyShooting/rock05.png',]
def drawObject(obj, x, y):
    global gamePad
    gamePad.blit(obj, (x, y))

def initGame():
    global gamePad, clock, background, fighter, missile, explosion
    pygame.init()
    gamePad = pygame.display.set_mode((padWidth, padHeight))
    pygame.display.set_caption('PyShooting')
    background = pygame.image.load("C:/Users/yount/Downloads/PyShooting/background.png")  
    fighter = pygame.image.load("C:/Users/yount/Downloads/PyShooting/fighter.png")
    missile = pygame.image.load("C:/Users/yount/Downloads/PyShooting/missile.png")
    explosion = pygame.image.load("C:/Users/yount/Downloads/PyShooting/explosion.png")
    clock = pygame.time.Clock()

def runGame():
    global gamePad, clock, background, fighter, explosion

    fighterSize = fighter.get_rect().size
    fighterWidth = fighterSize[0]
    fighterHeight = fighterSize[1]

    x = padWidth * 0.45
    y = padHeight * 0.9
    fighterX = 0

    missileXY = []

    rock = pygame.image.load(random.choice(rockImage))
    rockSize = rock.get_rect().size
    rockWidth = rockSize[0]
    rockHeight = rockSize[1]

    rockX = random.randrange(0, padWidth - rockWidth)
    rockY = 0
    rockSpeed = 2

    isShot = False
    shotCount = 0
    rockPassed = 0

    onGame = False
    while not onGame:


    rockX = random.randrange(0, padWidth - rockWidth)
    rockY = 0
    rockSpeed = 2

    fighterSize = fighter.get_rect().size
    fighterWidth = fighterSize[0]
    fighterHeight = fighterSize[1]

    x = padWidth * 0.45
    y = padHeight * 0.9
    fighterX = 0

    onGame = False
    while not onGame:
        for event in pygame.event.get(): 
            if event.type in [pygame.QUIT]:
                pygame.quit()
                sys.exit()

            if event.type in [pygame.KEYDOWN]:
                if event.key == pygame.K_LEFT:
                    fighterX -= 5

                elif event.key == pygame.K_RIGHT:
                    fighterX += 5

                elif event.key == pygame.K_SPACE:
                    missileX = x + fighterWidth/2
                    missileY = y - fighterHeight
                    missileXY.append([missileX, missileY])

            if event.type in [pygame.KEYUP]:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    fighterX = 0
    
        drawObject(background, 0, 0)

        x += fighterX
        if x < 0:
            x = 0
        elif x > padWidth - fighterWidth:
            x = padWidth - fighterWidth

        drawObject(fighter, x, y)

        if len(missileXY) != 0:
            for i, bxy in enumerate(missileXY):
                bxy[1] -= 10
                missileXY[i][1] = bxy[1]

                if bxy[1] < rockY:
                    if bxy[0] > rockX and bxy[0] < rockX + rockWidth:
                        missileXY.remove(bxy)
                        isShot = True
                        shoutCount += 1

                if bxy[1] <= 0:
                    try:
                        missileXY.remove(bxy)
                    except:
                        pass
        if len(missileXY) != 0:
            for bx, by in missileXY:
                drawObject(missile, bx, by)

        rockY += rockSpeed

        if rockY > padHeight: 
            rock = pygame.image.load(random.choice(rockImage))
            rockSize = rock.get_rect().size
            rockWidth = rockSize[0]
            rockHeight = rockSize[1]
            rockX = random.randrange(0, padWidth - rockWidth)
            rockY = 0

        if isShot:
            drawObject(explosion, rockX, rockY)
            rock = pygame.image.load(random.choice(rockImage))
            rockSize = rock.get_rect().size
            rockWidth = rockSize[0]
            rockHeight = rockSize[1]
            rockX = random.randrange(0, padWidth - rockWidth)
            rockY = 0
            isShot = False

        drawObject(rock, rockX, rockY)

        pygame.display.flip()

        clock.tick(60)

    pygame.quit()

initGame()
runGame()

Remove these lines (line 57\\58).删除这些行(第 57\\58 行)。 They create a loop that does nothing.他们创建了一个什么都不做的循环。

onGame = False
while not onGame:

You can also remove the next 9 lines since they seem to be repeats.您还可以删除接下来的 9 行,因为它们似乎是重复的。

Also change也变

shoutCount += 1

To

shotCount += 1

With these changes, the game runs correctly.通过这些更改,游戏可以正常运行。

Full code完整代码

import pygame
import sys
import random
from time import sleep

padWidth = 480
padHeight = 640
rockImage = ['C:/Users/yount/Downloads/PyShooting/rock01.png', 'C:/Users/yount/Downloads/PyShooting/rock02.png', 'C:/Users/yount/Downloads/PyShooting/rock03.png', 'C:/Users/yount/Downloads/PyShooting/rock04.png', 'C:/Users/yount/Downloads/PyShooting/rock05.png',]

def drawObject(obj, x, y):
    global gamePad
    gamePad.blit(obj, (x, y))

def initGame():
    global gamePad, clock, background, fighter, missile, explosion
    pygame.init()
    gamePad = pygame.display.set_mode((padWidth, padHeight))
    pygame.display.set_caption('PyShooting')
    background = pygame.image.load("C:/Users/yount/Downloads/PyShooting/background.png")  
    fighter = pygame.image.load("C:/Users/yount/Downloads/PyShooting/fighter.png")
    missile = pygame.image.load("C:/Users/yount/Downloads/PyShooting/missile.png")
    explosion = pygame.image.load("C:/Users/yount/Downloads/PyShooting/explosion.png")

    clock = pygame.time.Clock()

def runGame():
    global gamePad, clock, background, fighter, explosion

    fighterSize = fighter.get_rect().size
    fighterWidth = fighterSize[0]
    fighterHeight = fighterSize[1]

    x = padWidth * 0.45
    y = padHeight * 0.9
    fighterX = 0

    missileXY = []

    rock = pygame.image.load(random.choice(rockImage))
    rockSize = rock.get_rect().size
    rockWidth = rockSize[0]
    rockHeight = rockSize[1]

    rockX = random.randrange(0, padWidth - rockWidth)
    rockY = 0
    rockSpeed = 2

    isShot = False
    shotCount = 0
    rockPassed = 0

#    onGame = False
#    while not onGame:

#    rockX = random.randrange(0, padWidth - rockWidth)
#    rockY = 0
#    rockSpeed = 2

#    fighterSize = fighter.get_rect().size
#    fighterWidth = fighterSize[0]
#    fighterHeight = fighterSize[1]

#    x = padWidth * 0.45
#    y = padHeight * 0.9
#    fighterX = 0
        

    onGame = False
    while not onGame:
        for event in pygame.event.get(): 
            if event.type in [pygame.QUIT]:
                pygame.quit()
                sys.exit()

            if event.type in [pygame.KEYDOWN]:
                if event.key == pygame.K_LEFT:
                    fighterX -= 5

                elif event.key == pygame.K_RIGHT:
                    fighterX += 5

                elif event.key == pygame.K_SPACE:
                    missileX = x + fighterWidth/2
                    missileY = y - fighterHeight
                    missileXY.append([missileX, missileY])

            if event.type in [pygame.KEYUP]:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    fighterX = 0
    
        drawObject(background, 0, 0)

        x += fighterX
        if x < 0:
            x = 0
        elif x > padWidth - fighterWidth:
            x = padWidth - fighterWidth

        drawObject(fighter, x, y)

        if len(missileXY) != 0:
            for i, bxy in enumerate(missileXY):
                bxy[1] -= 10
                missileXY[i][1] = bxy[1]

                if bxy[1] < rockY:
                    if bxy[0] > rockX and bxy[0] < rockX + rockWidth:
                        missileXY.remove(bxy)
                        isShot = True
                        shotCount += 1  # not shoutCount

                if bxy[1] <= 0:
                    try:
                        missileXY.remove(bxy)
                    except:
                        pass
        if len(missileXY) != 0:
            for bx, by in missileXY:
                drawObject(missile, bx, by)

        rockY += rockSpeed

        if rockY > padHeight: 
            rock = pygame.image.load(random.choice(rockImage))
            rockSize = rock.get_rect().size
            rockWidth = rockSize[0]
            rockHeight = rockSize[1]
            rockX = random.randrange(0, padWidth - rockWidth)
            rockY = 0

        if isShot:
            drawObject(explosion, rockX, rockY)
            rock = pygame.image.load(random.choice(rockImage))
            rockSize = rock.get_rect().size
            rockWidth = rockSize[0]
            rockHeight = rockSize[1]
            rockX = random.randrange(0, padWidth - rockWidth)
            rockY = 0
            isShot = False

        drawObject(rock, rockX, rockY)

        pygame.display.flip()

        clock.tick(60)

    pygame.quit()

initGame()
runGame()

You never actually called:你从来没有真正调用过:

pygame.display.update()

That way the display won't ever update.这样显示就永远不会更新。

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

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