首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >( .blit )函数时不显示

问( .blit )函数时不显示
EN

Stack Overflow用户
提问于 2016-11-15 06:12:33
回答 1查看 186关注 0票数 0

因此,我试图创建一个计时器,当游戏运行时,游戏运行的几秒钟将显示在我的pygame窗口中。但出于某种原因,它似乎不起作用。我不太清楚到底是什么问题。

第35-44行是计时器函数的位置.第71行是时间显示的地方.

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-15 06:27:59

你运行while get_time == True:,停止一切。它一直在运行。

您只能有一个while循环,它一直运行(没完没了的循环)- mainloop。

工作示例(没有位图和字体文件)

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40603192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档