Skip to main content
 首页 » 编程设计

Python Pygame之如何为我的游戏添加时间计数器

2024年11月24日8yyy_WW

所以我想知道如何为 Pygame 添加时间计数器?或者为 Pygame 程序添加时间计数器的最佳方法是什么。时间计数器应在 Pygame 初始化时开始,并在 inventory[coins] > 100 时结束: 计时器应位于右下角,以秒为单位。

import pygame, sys 
import time 
import random 
from pygame.locals import * 
 
#MAIN GAME 
 
pygame.init() 
pygame.display.set_mode((200,200)) 
 
#colours 
black = (0, 0, 0) 
white = (255, 255, 255) 
brown = (153, 76, 0) 
blue  = (0, 0, 255) 
grey  = (192,192,192) 
 
#game dimensions 
tilesize = 20 
mapwidth = 30 
mapheight = 20 
 
coins = 0 
ship = 1 
water = 2 
rock = 3 
movesMade = 4 
 
#dictionary for texture of the map 
textures = { #the transform function scales the photo to the tile size 
    ship : pygame.transform.smoothscale(pygame.image.load('ship.png').convert_alpha(), (tilesize, tilesize)), 
    water: pygame.transform.smoothscale(pygame.image.load('water.png'), (tilesize, tilesize)), 
    rock: pygame.transform.smoothscale(pygame.image.load('rock.png').convert_alpha(), (tilesize, tilesize)), 
    coins: pygame.transform.smoothscale(pygame.image.load('chest.png'), (tilesize, tilesize)), 
    movesMade: pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize)) 
    } 
 
inventory = { 
    coins: 0, 
    movesMade: 0 
    } 
 
#image that will represent player 
PLAYER = pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize)) 
 
#position of the player 
playerPos = [0,0] 
 
resources = [coins, movesMade] 
 
#utilise list comprehension to create grid 
tilemap = [[water for w in range(mapwidth)] for h in range(mapheight)] 
 
#set up display 
displaysurf = pygame.display.set_mode((mapwidth*tilesize,mapheight*tilesize + 60)) 
 
 
invfont = pygame.font.Font('FreeSansBold.ttf', 18) 
 
#loops through each row 
for rw in range(mapheight): 
    for cl in range(mapwidth): 
        randomnumber = random.randint(0,15) 
        if randomnumber == 0 or randomnumber == 1: 
            tile = rock 
        elif randomnumber == 2 or randomnumber == 3 : 
            tile = ship 
        else: 
            tile = water 
        #sets position in the grid 
        tilemap[rw][cl] = tile 
 
visit = {} 
while True: 
    #start = time.time() 
    displaysurf.fill(black) #background color 
 
    #user events 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit() 
        elif event.type == KEYDOWN: 
            if event.key == K_RIGHT and playerPos[0] < mapwidth - 1: 
                playerPos[0] += 1 
            if event.key == K_LEFT and playerPos[0] > 0: 
                playerPos[0] -= 1 
            if event.key == K_UP and playerPos[1] > 0: 
                playerPos[1] -= 1 
            if event.key == K_DOWN and playerPos[1] < mapheight -1: 
                playerPos[1] += 1 
            if event.key == K_SPACE: 
                pos = (playerPos[1], playerPos[0]) 
                if not pos in visit: # checks whether a tile has been used 
                    visit[pos] = True  
                    currentTile = tilemap[playerPos[1]][playerPos[0]] 
                    if currentTile == rock: 
                        inventory[movesMade] += 1 
                        percent = 30 # coins are kept with a probability of 30 percent and will be lost by 70 percent 
                        ran1 = random.randint(0,100) # random value in [0, 99] 
                        if ran1 >= percent and inventory[coins] > 30: 
                            inventory[coins] -= 30 
                        else: 
                            inventory[coins] += 20 
 
                    elif currentTile == ship: 
                        inventory[coins] += 10 
                        inventory[movesMade] += 1 
 
    #loops through each row  
    for row in range(mapheight): 
        #loops through each column in row 
        for column in range(mapwidth): 
            displaysurf.blit(textures[tilemap[row][column]], (column*tilesize,row*tilesize)) 
 
    displaysurf.blit(PLAYER,(playerPos[0]*tilesize,playerPos[1]*tilesize)) 
 
    placePosition = 10 
    for item in resources: 
        displaysurf.blit(textures[item],(placePosition, mapheight*tilesize + 20)) 
        placePosition += 30 
        #text displays amount of coin 
        textObj = invfont.render(str(inventory[item]), True, white, black) 
        displaysurf.blit(textObj,(placePosition, mapheight*tilesize + 20)) 
        placePosition += 50 
 
    if inventory[coins] > 100: 
        break 
    #end = time.time() 
    pygame.display.update() 
 
#final = (end) 
#print(final) 

请您参考如下方法:

进口pygame.freetype并创建一个 pygame.freetype.Font对象:

例如

import pygame 
import pygame.freetype 
 
pygame.init() 
font = pygame.freetype.SysFont('Times New Roman', 30)  

使用pygame.time.get_ticks在主循环之前获取当前时间(以毫秒为单位)并设置开始时间:

start_time = pygame.time.get_ticks() 
while True: 
 
    # [...] 

计算每一帧耗时。要从毫秒转换为秒,时间差必须除以 1000。
通过 ( font.render) 将时间文本渲染到表面。注意,.render() 返回 pygame.Surface对象和一个 pygame.Rect .矩形可用于计算文本的位置。在下面的文本中,文本位于右下角,与窗口边框的边距为 20:

while True: 
 
    # [...] 
 
    if inventory[coins] < 100: 
        current_time = pygame.time.get_ticks() 
        delta_time_s = (current_time - start_time) // 1000 
 
        text_surf, text_rect = font.render(str(delta_time_s), (255, 255, 255), size=30)  
        margin = 20       # margin to the window  
        size = (200, 200) # window size 
        text_pos = (size[0] - text_rect.width - margin, size[1] - text_rect.height - margin) 
 
        displaysurf.blit(text_surf, text_pos) 

如果要显示十分之一秒,则必须更改 delta_time_s 的计算:

delta_time_s = (current_time - start_time) // 100 / 10