Python 游戏时钟不准确
我正试图在弗雷迪的“粉丝游戏”中度过五个晚上,但时钟部分不起作用。我有一个名为“time”的函数,设置为每帧运行一次。它计算程序正在运行的帧数,当它等于40时(我让它以40 fps的速度运行),它会在变量“seconds”中加1,当seconds等于某个量时,小时会改变。有三个问题: 当相机向上移动(将鼠标移到屏幕底部)时,新的时间图像会在旧的时间图像上闪烁,但一旦您将相机放回原位(只需再次将鼠标移到屏幕底部),它就会恢复正常 时间不准确。就像我前面提到的,我有它,所以每10秒时间会改变一次。问题是,当我计时时,小时与小时之间的时间不一致(即,将15秒更改为1:00,然后18秒更改为2:00,然后17秒更改为3:00,依此类推) 最后一个问题是6:00的图像永远不会显示。该程序不使用6:00图像替换5:00图像,而是简单地通过blitts no图像来显示5:00后的一小时 我的代码如下:Python 游戏时钟不准确,python,pygame,Python,Pygame,我正试图在弗雷迪的“粉丝游戏”中度过五个晚上,但时钟部分不起作用。我有一个名为“time”的函数,设置为每帧运行一次。它计算程序正在运行的帧数,当它等于40时(我让它以40 fps的速度运行),它会在变量“seconds”中加1,当seconds等于某个量时,小时会改变。有三个问题: 当相机向上移动(将鼠标移到屏幕底部)时,新的时间图像会在旧的时间图像上闪烁,但一旦您将相机放回原位(只需再次将鼠标移到屏幕底部),它就会恢复正常 时间不准确。就像我前面提到的,我有它,所以每10秒时间会改变一次。问
import pygame, random
pygame.init()
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN, 32)
pygame.display.toggle_fullscreen()
clock = pygame.time.Clock()
twelve = pygame.image.load('images/12.png')
one = pygame.image.load('images/1.png')
two = pygame.image.load('images/2.png')
three = pygame.image.load('images/3.png')
four = pygame.image.load('images/4.png')
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')
office_pic = pygame.image.load('images/office.png')
stage = pygame.image.load('images/stage.jpg')
building_map = pygame.image.load('images/map.png')
building_map.convert_alpha()
breathing = pygame.mixer.Sound('sounds/breathing.wav')
screen_width = 1280
screen_height = 800
x = 0
x_move = 0
camera_up = False
v = 0
camera = stage
fps_tick = 0
seconds = 0
hour = 1
def time():
global fps_tick, seconds, hour
fps_tick += 1
if fps_tick == 40:
seconds += 1
fps_tick = 0
if seconds == 10:
hour += 1
seconds = 0
#blit the hour
if hour == 1:
screen.blit(twelve, ((screen_width-150), 30))
elif hour == 2:
screen.blit(one, ((screen_width-150), 30))
elif hour == 3:
screen.blit(two, ((screen_width-150), 30))
elif hour == 4:
screen.blit(three, ((screen_width-150), 30))
elif hour == 5:
screen.blit(four, ((screen_width-150), 30))
elif hour == 6:
screen.blit(five, ((screen_width-150), 30))
elif hour == 7:
screen.blit(six, ((screen_width-150), 30))
def office(mouse_x, click):
global x, x_move
if camera_up == False:
if mouse_x >= (screen_width/2) + 200 and mouse_x <= (screen_width/2) + 400:
x_move = 6
elif mouse_x <= (screen_width/2) - 200 and mouse_x >= (screen_width/2) - 400:
x_move = -6
elif mouse_x > (screen_width/2) + 400:
x_move = 12
elif mouse_x < (screen_width/2) - 400:
x_move = -12
else:
x_move = 0
x -= x_move
if camera_up == False:
screen.blit(office_pic, (x,0))
def cameras(mouse_x, mouse_y, click):
global camera_up, v
if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False and v == 0:
camera_up = True
if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True:
v = 1
if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True and v == 1:
screen.fill((0,0,0))
camera_up = False
if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False:
v = 0
if camera_up == True:
screen.blit(camera, (0,0))
screen.blit(building_map, (screen_width-650, screen_height-426))
def main():
global x, x_move, camera_up, v, click
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
mouse_xy = pygame.mouse.get_pos()
mouse_x = mouse_xy[0]
mouse_y = mouse_xy[1]
click = pygame.mouse.get_pressed()
office(mouse_x, click)
cameras(mouse_x, mouse_y, click)
time()
pygame.display.update()
clock.tick(40)
main()
导入pygame,随机
pygame.init()
screen=pygame.display.set_模式((0,0),pygame.FULLSCREEN,32)
pygame.display.toggle_fullscreen()
clock=pygame.time.clock()
12=pygame.image.load('images/12.png')
one=pygame.image.load('images/1.png')
two=pygame.image.load('images/2.png')
three=pygame.image.load('images/3.png')
four=pygame.image.load('images/4.png')
five=pygame.image.load('images/5.png')
six=pygame.image.load('images/5.png')
office\u pic=pygame.image.load('images/office.png')
stage=pygame.image.load('images/stage.jpg')
building_map=pygame.image.load('images/map.png')
构建映射。转换α()
呼吸=pygame.mixer.Sound('sounds/breathing.wav'))
屏幕宽度=1280
屏幕高度=800
x=0
x_move=0
摄像头向上=错误
v=0
摄像机=舞台
fps_tick=0
秒=0
小时=1
定义时间():
全局fps_刻度,秒,小时
fps_tick+=1
如果fps_tick==40:
秒+=1
fps_tick=0
如果秒=10:
小时+=1
秒=0
#分秒必争
如果小时=1:
屏幕。blit(12,((屏幕宽度-150),30))
elif小时==2:
筛网布利特(一个(筛网宽度150),30))
elif小时==3:
筛网布利特(两个(筛网宽度-150),30))
elif小时==4:
屏幕。blit(三个,((屏幕宽度-150),30))
elif小时==5:
屏幕。blit(四个(屏幕宽度150),30))
elif小时==6:
屏幕布利特(五个(屏幕宽度150),30))
elif小时==7:
屏幕布利特(六个(屏幕宽度150),30))
def办公室(鼠标单击):
全局x,x_移动
如果摄像头向上==假:
如果鼠标x>=(屏幕宽度/2)+200和鼠标x(屏幕宽度/2)+400:
x_move=12
elif鼠标x<(屏幕宽度/2)-400:
x_move=-12
其他:
x_move=0
x-=x_移动
如果摄像头向上==假:
屏幕显示(办公室图片,(x,0))
def摄像头(鼠标x,鼠标y,单击):
全球摄像机向上,v
如果鼠标_y>=(屏幕高度-(屏幕高度/6))和摄像头_向上==False和v==0:
照相机向上=正确
如果不是鼠标(y>=(屏幕高度-(屏幕高度/6))和摄像头(向上==真):
v=1
如果鼠标y>=(屏幕高度-(屏幕高度/6))和摄像头向上==真且v==1:
屏幕填充((0,0,0))
摄像头向上=错误
如果不是鼠标(y>=(屏幕高度-(屏幕高度/6))和摄像头(向上)=False:
v=0
如果摄影机_up==真:
屏幕光点(摄像机,(0,0))
屏幕。blit(建筑地图,(屏幕宽度-650,屏幕高度-426))
def main():
全局x,x_移动,摄影机向上,v,单击
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
pygame.quit()
退出
mouse_xy=pygame.mouse.get_pos()
鼠标_x=鼠标_xy[0]
鼠标_y=鼠标_xy[1]
click=pygame.mouse.get_pressed()
办公室(鼠标点击)
照相机(鼠标x、鼠标y、单击)
时间()
pygame.display.update()
时钟滴答(40)
main()
如果你想每10秒做一件事,最好使用pygame的事件系统,只需使用pygame.time.set\u timer
计划一个用户事件即可
比如说,
pygame.time.set_timer(pygame.USEREVENT, 10000)
将每隔10秒触发pygame.USEREVENT
事件。您可以像聆听主循环中的每个事件一样聆听它:
...
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.type == pygame.USEREVENT:
hour += 1
...
如果仔细观察,您会发现为什么图像在five
和six
之间没有变化:您使用相同的图像
...
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')
...
如果你想每10秒做一件事,最好使用pygame的事件系统,简单地用
pygame.time.set\u timer
安排一个用户事件
比如说,
pygame.time.set_timer(pygame.USEREVENT, 10000)
将每隔10秒触发pygame.USEREVENT
事件。您可以像聆听主循环中的每个事件一样聆听它:
...
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.type == pygame.USEREVENT:
hour += 1
...
如果仔细观察,您会发现为什么图像在five
和six
之间没有变化:您使用相同的图像
...
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')
...
如果你想每10秒做一件事,最好使用pygame的事件系统,简单地用
pygame.time.set\u timer
安排一个用户事件
比如说,
pygame.time.set_timer(pygame.USEREVENT, 10000)
将每隔10秒触发pygame.USEREVENT
事件。您可以像聆听主循环中的每个事件一样聆听它:
...
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.type == pygame.USEREVENT:
hour += 1
...
如果仔细观察,您会发现为什么图像在five
和six
之间没有变化:您使用相同的图像
...
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')
...
如果你想每10秒做一件事,最好使用pygame的事件系统,简单地用
pygame.time.set\u timer
安排一个用户事件
比如说,
pygame.time.set_timer(pygame.USEREVENT, 10000)
将每隔10秒触发pygame.USEREVENT
事件。您可以像聆听主循环中的每个事件一样聆听它:
...
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.type == pygame.USEREVENT:
hour += 1
...
<