[英](Pygame) .blit won't display While function
So I'm trying to create a timer in which when the game runs, a number of seconds in which the game had been running would be displayed in my pygame window. 因此,我试图创建一个计时器,在该计时器中运行游戏时,我的pygame窗口中将显示运行游戏的秒数。 But for some reason, it doesn't seem to be working.
但是由于某种原因,它似乎没有用。 I'm not quite sure exactly what the issue is.
我不太清楚到底是什么问题。
Line 35-44 is where the timer function is... Line 71 is where the time is expected to display... 第35-44行是计时器功能所在的位置...第71行是希望显示时间的位置...
import pygame
pygame.init()
pygame.font.init()
FPS = 60
fpsClock = pygame.time.Clock()
time = 10
WHITE = (255,255,255)
dimension = [936, 520]
SCREEN = pygame.display.set_mode(dimension)
font = pygame.font.Font("8-BIT WONDER.TTF",16)
gameEnd= False
#Images
blacksmith = pygame.image.load("blacksmith.png")
blacksmith = pygame.transform.scale(blacksmith,(144,144))
background = pygame.image.load("background_img.png")
#Positions
blacksmith_position = [400,275]
anvil_position = [0,0]
song = pygame.mixer.music.load("Pixelland.mp3")
pygame.mixer.music.play(0, 0.0)
#Time
get_time = True
#Clock
while get_time == True:
time = pygame.time.get_ticks() / 1000
pygame.time.delay(1000)
display_time = font.render(time, True, WHITE)
#Game Loop
while not gameEnd:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameEnd = True
#Player Controls
keys = pygame.key.get_pressed()
if keys[pygame.K_F11]:
pygame.display.toggle_fullscreen()
if keys[pygame.K_LEFT]:
blacksmith_position[0] = max(95, blacksmith_position[0] - 12)
if keys[pygame.K_RIGHT]:
blacksmith_position[0] = min(685, blacksmith_position[0] + 12)
SCREEN.blit(background, (0, 0))
SCREEN.blit(blacksmith,(blacksmith_position))
SCREEN.blit(display_time, (70,10))
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
quit()
You run while get_time == True:
which stops everything. 您
while get_time == True:
运行while get_time == True:
这将停止所有操作。 It runs all the time. 它一直运行。
You can have only one while
loop which runs all the time (endless loop) - mainloop
. 您只能有一个始终运行的
while
循环(无限循环) mainloop
。
Working example (without bitmaps and fonts files) 工作示例(无位图和字体文件)
import pygame
# --- constants ---
FPS = 60
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
DIMENSION = (936, 520)
# --- classes ---
# empty
# --- functions ---
# empty
# --- main ---
# - init -
pygame.init()
#pygame.font.init() # pygame.init() should run `pygame.font.init()` automatically
screen = pygame.display.set_mode(DIMENSION)
# - resources -
font = pygame.font.Font(None, 16)
# - mainloop -
fps_clock = pygame.time.Clock()
game_end = False
while not game_end:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_end = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_F11
pygame.display.toggle_fullscreen()
#keys = pygame.key.get_pressed()
#if keys[pygame.K_F11]:
# pygame.display.toggle_fullscreen()
# - updates (without draws) -
# render current time
cur_time = pygame.time.get_ticks() / 1000
display_time = font.render(str(cur_time), True, WHITE)
# - draws (without updates) -
screen.fill(BLACK)
screen.blit(display_time, (70,10))
pygame.display.update()
# - FPS -
fps_clock.tick(FPS)
# - end -
pygame.quit()
#quit() # doesn't need it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.