我做了一些研究,看看我是否可以用这种方式解决这个问题,但似乎没有找到任何解决我问题的方法。我发现了这两个问题:为什么我的pygame显示器没有显示任何东西?对为什么PyGame显示屏是黑屏感到困惑。我试图用评论中的建议来解决我的问题,但没有成功,或者问题的原因与我的不同。
当我运行代码时,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()
你从来没有打过电话:
pygame.display.update()
这样显示器就永远不会更新。
删除这些行(第57\58行)。他们创建了一个什么都不做的循环。
onGame = False
while not onGame:
您还可以删除接下来的9行,因为它们似乎是重复的。
也改变
shoutCount += 1
到
shotCount += 1
通过这些更改,游戏可以正常运行。
完整代码
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()
我以前也发布过同样的问题,但我现在再次发布,因为我在代码中发现了更多的错误并更正了它们。然而,我仍然面临着和以前一样的问题! 原始帖子:我几周前刚开始学习Python,我正在学习一个教程,用pygame构建一个数独解算器! 我现在面临的问题是,每当我尝试运行代码时,只会弹出一个黑色的空白窗口。我已经一遍又一遍地检查我的代码,但我似乎找不到问题。。。我不确定我的代码中到底是哪里出了问题,所以请原谅我
我是python新手,我想用pygame在屏幕上可视化一个算法。这是我的代码: 但这总是显示一个黑色或有点深灰色的屏幕。有人知道我做错了什么吗?我尝试了多个教程,它们都给了我相同的屏幕。 我正在使用MacOS 10.14
我正在为平台游戏做教程,但我的Pygame窗口显示一个黑屏。 我不知道怎么解决这个问题。 我尝试再次关闭和打开Visual Studio Code,但没有任何结果。
我试图让这个游戏工作,但它只是显示一个黑屏。这是一个简单的游戏,你只要避免掉块。我看过相关的问题,但没有一个答案对我有用。它说我必须添加更多细节,所以希望这一行足够了,因为idk在这一行还需要写些什么来为我的文章添加细节。我的代码:
这是我的代码,请记住,我在几天前学习了python,所以我的代码可能制作不正确,等等。我正在尝试制作一个窗口,该窗口将显示一些文本(测试版),并将显示两个小矩形,我想成为按钮。
有人知道这个黑色矩形是什么吗?这是我的主要任务。py: 这是我的马里奥。py: 最后,我的levels.py: 我使用的是Windows10计算机,python版本为3.8。3和pygame版本1.9。6.