Python Pygame中屏幕上未显示文本
我在pygame中做了一个乒乓球游戏,我有一个功能,当玩家或对手进球时,将球置于中间,等待2-3秒 当球移动到中间并等待2-3秒时,我添加了一个计时器,它将在进球时初始化 在ball_start()中,我获取当前时间并检查持续时间,分别显示3、2、1 该功能可以很好地将球移动到中心并停止2-3秒,但屏幕上不会显示此3,2,1文本 使用的字体和颜色Python Pygame中屏幕上未显示文本,python,pygame,Python,Pygame,我在pygame中做了一个乒乓球游戏,我有一个功能,当玩家或对手进球时,将球置于中间,等待2-3秒 当球移动到中间并等待2-3秒时,我添加了一个计时器,它将在进球时初始化 在ball_start()中,我获取当前时间并检查持续时间,分别显示3、2、1 该功能可以很好地将球移动到中心并停止2-3秒,但屏幕上不会显示此3,2,1文本 使用的字体和颜色 self.light_grey = (200, 200, 200) self.game_font = pygame.font.Font("f
self.light_grey = (200, 200, 200)
self.game_font = pygame.font.Font("freesansbold.ttf", 50)
功能代码:
# when player or opponent scores, shift ball to center and randomise its initial direction
def ball_start(self):
self.ball.center = (self.screen_width / 2, self.screen_height / 2)
# checking after goal is scored if 2-3 secs are passed or not
current_time = pygame.time.get_ticks()
# 3
if current_time - self.score_time < 800:
num_3 = self.game_font.render("3", False, self.light_grey)
self.screen.blit(num_3, (self.screen_width / 2 - 10, self.screen_height / 2 + 20))
# 2
if current_time - self.score_time < 1600:
num_2 = self.game_font.render("2", False, self.light_grey)
self.screen.blit(num_2, (self.screen_width / 2 - 10, self.screen_height / 2 + 20))
# 1
if current_time - self.score_time < 2400:
num_1 = self.game_font.render("1", False, self.light_grey)
self.screen.blit(num_1, (self.screen_width / 2 - 10, self.screen_height / 2 + 20))
if current_time - self.score_time < 2400:
# making speeds 0 so ball won't move
self.ball_x_speed = 0
self.ball_y_speed = 0
else:
# if 2-3 secs are passed make ball move
self.ball_x_speed = 8 * random.choice((1, -1))
self.ball_y_speed = 8 * random.choice((1, -1))
self.score_time = None
#当球员或对手得分时,将球移到中间,并随机化其初始方向
def ball_启动(自):
self.ball.center=(self.screen\u宽度/2,self.screen\u高度/2)
#进球后检查是否通过2-3秒
当前时间=pygame.time.get_ticks()
# 3
如果当前时间-自我评分时间<800:
num\u 3=self.game\u font.render(“3”,假,self.light\u灰)
self.screen.blit(num_3,(self.screen_宽度/2-10,self.screen_高度/2+20))
# 2
如果当前时间-自我评分时间<1600:
num_2=self.game_font.render(“2”,假,self.light_灰)
self.screen.blit(num_2,(self.screen_宽度/2-10,self.screen_高度/2+20))
# 1
如果当前时间-自我评分时间<2400:
num\u 1=self.game\u font.render(“1”,False,self.light\u grey)
self.screen.blit(num_1,(self.screen_宽度/2-10,self.screen_高度/2+20))
如果当前时间-自我评分时间<2400:
#使速度为0,使球不会移动
self.ball_x_速度=0
self.ball\u y\u速度=0
其他:
#如果超过2-3秒,使球移动
self.ball_x_speed=8*随机选择((1,-1))
self.ball_y_speed=8*随机选择((1,-1))
self.score\u time=无
文本被绘制到屏幕表面(BLITED),
但是屏幕表面没有显示出来
使用pygame.display.flip()或任何其他显示更新调用来显示文本。正如预期的那样,问题不在您显示的代码中 在主循环中,这是您的渲染顺序:
if pong.score_time:
pong.ball_start()
pong.ball_animation()
pong.player_animation()
pong.opponent_animation()
pong.draw_objects()
这意味着列表中的所有内容都将覆盖前面的内容
最值得注意的是,ball\u start
位于底部。这可能不是什么大问题,但确实是,因为背景填充位于draw\u objects
中,它位于ball\u start
之后
要修复此问题,只需在绘制对象之后移动ball\u start
,或者(在我看来,更好的做法是)将背景填充直接移动到主循环中
pong.ball_animation()
pong.player_animation()
pong.opponent_animation()
pong.draw_objects()
if pong.score_time:
pong.ball_start()
如果在If
语句中的blit
旁边添加print
语句,它们是否输出?e、 g.if
stations是否被正确调用?我认为问题不在于你发布的代码。请尝试创建一个(稍微)完整的示例,我们可以执行该示例并查看问题。是的,它正在打印在blit()之后编写的任何内容。下面是完整的代码:如果发现问题,请告诉我谢谢!我在这个blit()之后加了一句话,但随后文本闪烁了很多,现在该怎么办?是的,我从您的解释中理解了发生了什么,代码现在正在运行。谢谢