gpt4 book ai didi

python - 我如何添加一个显示在 pygame 屏幕上的正在运行的计时器

转载 作者:行者123 更新时间:2023-12-01 05:30:39 24 4
gpt4 key购买 nike

这只是一个简单的抓水果游戏。

import pygame, random, time, os, webbrowser
from time import sleep
from pygame import*
pygame.init()
myname=input('What is your name')

#set the window size
window= pygame.display.set_mode((800,600) ,0,24)
pygame.display.set_caption("Fruit Catch")

#game variables
gameover=pygame.image.load('gameover.jpg')
myscore=0
file = 'score.txt'
eating=pygame.mixer.Sound('data/eating.wav')
die=pygame.mixer.Sound('die.wav')
mylives=3
mouth_x=300
fruit_x=250
fruit_y=75
fruitlist=['broccoli.gif','chicken.gif']
keyclicks=0
#prepare for screen
myfont=pygame.font.SysFont("Britannic Bold", 55)
label1=myfont.render(myname, 1, (240, 0, 0))
label3=myfont.render(str(mylives), 1, (20, 255, 0))
#grapchics
fruit=pygame.image.load('data/chicken.png')
mouth=pygame.image.load('data/v.gif')
backGr=pygame.image.load('data/kfc.jpg')
backGr1=pygame.image.load('sj.jpg')
#endless loop

start=pygame.image.load('chibro.jpg')
end_it=False
while (end_it==False):
window.blit(start, (0,0))
myfont=pygame.font.SysFont("Impact", 55)
label7=myfont.render("Welcome "+myname+" Click here to begin", 1, (255, 0, 0))
myfont=pygame.font.SysFont("Impact", 30)
label8=myfont.render("Catch as much Chicken and brocoli as you can", 1, (255, 0, 0))
label9=myfont.render("If you miss 3, you will starve to death!", 1, (255, 0, 0))
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
end_it=True
window.blit(label7,(10,500))
window.blit(label8,(10,300))
window.blit(label9,(10,400))
pygame.display.update()
running=True
while running:
if fruit_y>=460:#check if at bottom, if so prepare new fruit
fruit_x=random.randrange(50,530,1)
fruit_y=75
fruit=pygame.image.load('data/'+fruitlist[random.randrange(0,2,1)])
else:fruit_y+=5

#check collision
if fruit_y>=456:
mylives-=1
if fruit_y>=440:
if fruit_x>=mouth_x and fruit_x<=mouth_x+300 :
myscore+=1
fruit_y=600#move it off screen
eating.play()

pygame.mouse.get_pressed()
for event in pygame.event.get():
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
mouth_x-=55
keyclicks+=1
if (event.key==pygame.K_RIGHT):
mouth_x+=55
keyclicks+=1
if event.type==MOUSEBUTTONDOWN and keyclicks>=2 :
pygame.quit()
label3=myfont.render(str(mylives), 1, (20, 255, 0))
label2=myfont.render(str(myscore), 1, (20, 255, 0))
text1='You caught'+str(myscore)
text3='Press the mouse to close the game'
label4=myfont.render(text1, 1, (135, 206, 250))
myfont1=pygame.font.SysFont("Britannic Bold", 40)
label5=myfont1.render(text3, 1, (255, 0, 0))

if mylives==0:
window.blit(gameover, (0,0))
window.blit(label4, (500,400))
die.play()
pygame.time.get_ticks
pygame.display.update()
running=False
webbrowser.open(file)

else:

window.blit(backGr,(0,0))
window.blit(mouth, (mouth_x,440))
window.blit(fruit,(fruit_x, fruit_y))
window.blit(label1, (174, 537))
window.blit(label2, (700, 157))
window.blit(label3, (700, 400))
window.blit(label5, (10, 0))
pygame.display.update()

我该如何做到这一点,以便当主循环启动并且运行为 true 时,计时器会显示在屏幕上以秒为单位计数,并且当运行为 false 时计时器结束大家有什么想法吗?

最佳答案

使用pygame.time.get_ticks()

完整示例 - 时间以毫秒为单位,计时器位于屏幕中央,ESC - 退出,SPACE - 暂停计时器

编辑:现在它显示分钟、秒、毫秒 - Python 2.x。对于 Python 3.x,使用 // 代替 / 来表示 counting_monthscounting_seconds

import pygame

pygame.init()

screen = pygame.display.set_mode( (800,600) )

font = pygame.font.SysFont(None, 32)

clock = pygame.time.Clock()

start_time = pygame.time.get_ticks()

paused = False
running = True
while running:

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False

if event.key == pygame.K_SPACE:
paused = not paused

if not paused:
counting_time = pygame.time.get_ticks() - start_time

# change milliseconds into minutes, seconds, milliseconds
counting_minutes = str(counting_time/60000).zfill(2)
counting_seconds = str( (counting_time%60000)/1000 ).zfill(2)
counting_millisecond = str(counting_time%1000).zfill(3)

counting_string = "%s:%s:%s" % (counting_minutes, counting_seconds, counting_millisecond)

counting_text = font.render(str(counting_string), 1, (255,255,255))
counting_rect = counting_text.get_rect(center = screen.get_rect().center)

screen.fill( (0,0,0) )
screen.blit(counting_text, counting_rect)

pygame.display.update()

clock.tick(25)

关于python - 我如何添加一个显示在 pygame 屏幕上的正在运行的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20359845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com