Python 3.x Pygame,当我制作游戏时,你如何修复屏幕的一部分是黑色的?
添加背景后,屏幕的一部分(特别是中上部)是黑色的,但我可以看到背景的其余部分 我试过运行它几次,我试过搜索可能导致它的原因,我还没有一个游戏循环,因为什么都不应该发生,这可能是问题所在。。。对不起,代码太长了Python 3.x Pygame,当我制作游戏时,你如何修复屏幕的一部分是黑色的?,python-3.x,pygame,Python 3.x,Pygame,添加背景后,屏幕的一部分(特别是中上部)是黑色的,但我可以看到背景的其余部分 我试过运行它几次,我试过搜索可能导致它的原因,我还没有一个游戏循环,因为什么都不应该发生,这可能是问题所在。。。对不起,代码太长了 import pygame SCREEN_WIDTH = 1500 SCREEN_HEIGHT = 800 SCREEN_TITLE = "road cross game thing" WHITE_COLOR = (255,255,255) clock = pygame.time.
import pygame
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 800
SCREEN_TITLE = "road cross game thing"
WHITE_COLOR = (255,255,255)
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont("comicsans", 75)
title = SCREEN_TITLE
width = SCREEN_WIDTH
height = SCREEN_HEIGHT
class Game:
TICK_RATE = 60
title = SCREEN_TITLE
width = SCREEN_WIDTH
height = SCREEN_HEIGHT
image = pygame.image.load("photo.jpg")
game_screen = pygame.display.set_mode((width, height))
game_screen.fill(WHITE_COLOR)
game_screen.fill(WHITE_COLOR)
game_screen.blit(image, (0, 0))
pygame.display.set_caption(title)
is_game_over = False
did_win = False
direction = 0
while not is_game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_game_over = True
elif event.type == pygame.MOUSEBUTTONUP:
pos= pygame.mouse.get_pos()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
direction = 0
print(event)
game_screen.fill(WHITE_COLOR)
pygame.display.update()
clock.tick(TICK_RATE)
object_image1 = pygame.image.load("photo.jpg")
image1 = pygame.transform.scale(object_image1, (50, 50))
x_pos = 50
y_pos = 50
game_screen.blit(image1,(x_pos, y_pos))
pygame.init()
Game()
pygame.quit()
quit()
我希望代码能使整个背景成为我的图片。你的代码大部分都在那里,只是有点没有条理 屏幕更新不是您所期望的,原因是代码正在绘制,然后按顺序更新。简言之,它应该先进行所有绘图操作,然后将它们全部更新到显示器上。显然,如果您绘制图像,然后调用
fill()
,则第一个绘制操作将不可见,因为它已被过度绘制
image1
不会填满整个显示屏,因为它只会拉伸到(50,50)
,而屏幕是1500
x800
在调试代码时,我将初始化函数移到了成员函数Game.\uu init\uuu()
,并将主游戏循环移到了Game.run()
函数中
OP的缩进有点混乱,我想这是由于粘贴到SO中造成的(否则程序将不会产生所描述的结果)。但看起来你正在主游戏循环中加载图像。最好在用户事件循环开始之前只加载一次图像、声音等资源
import pygame
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 800
TICK_RATE = 60
SCREEN_TITLE = "road cross game thing"
WHITE_COLOR = (255,255,255)
# Initialise PyGame
pygame.init()
pygame.display.set_caption( SCREEN_TITLE )
game_screen = pygame.display.set_mode( ( SCREEN_WIDTH, SCREEN_HEIGHT ) )
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont("comicsans", 75)
class Game:
def __init__( self, game_screen ):
self.game_screen = game_screen
self.image = pygame.image.load( "photo.jpg" )
self.object_image1 = pygame.image.load( "photo.jpg" )
#self.image1 = pygame.transform.scale( self.object_image1, (50, 50) )
# Scale to fill window
self.image1 = pygame.transform.scale( self.object_image1, ( SCREEN_WIDTH, SCREEN_HEIGHT) )
def run( self ):
is_game_over = False
did_win = False
direction = 0
while not is_game_over:
# Handle user input events
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_game_over = True
elif event.type == pygame.MOUSEBUTTONUP:
pos= pygame.mouse.get_pos()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
direction = 0
print(event)
# Update the screen
#game_screen.fill( WHITE_COLOR )
#game_screen.blit( self.image, (0, 0) )
#x_pos = 50
#y_pos = 50
#game_screen.blit( self.image1, (x_pos, y_pos) )
# Fill the window with image1
game_screen.blit( self.image1, ( 0, 0 ) )
pygame.display.update()
clock.tick( TICK_RATE )
# Run the Game
game = Game( game_screen )
game.run()
pygame.quit()
quit()
请有条理地解释预期输出和您当前看到的内容。如果您有错误的缩进。在上次blit之后,您忘记了
pygame.display.update()
。您的类看起来像函数,但类是以不同的方式构建的。谢谢!很多帮助!很高兴我能帮忙!