pygame time.clock.tick

使用

这个函数的参数就是每秒调用n次tick函数,一般设置在循环中,限制循环每秒的循环次数。从而达到设置页面刷新率的效果。

其他

一秒多少帧,就是看页面一秒刷新多少次,每刷新一次,就调用一次tick函数,就相当于是每帧调用一次tick函数。

做了两个黑洞,进入一个黑洞会传送到另一个黑洞,但是timer.tick(30)帧太快导致无法从传送,改成timer.tick(2)就可以,这是为什么,应该怎么做?主程序代码如下: ``` 主程序开始 pygame.init() img_size =40 img_group = list() screen_size = 0 #pygame.FULLSCREEN screen = pygame.display.set_mode((24*img_size,18*img_size),screen_size) pygame.display.set_caption("嗷大喵贪吃蛇") face = pygame.image.load("face.png") load_pic() game_init() game_over = False last_time = 0 bgm = None auto_play = False step_time = 400 waiting =True audio_init() rePlay = True play_sound(bgm) while True: timer.tick(2) ticks = pygame.time.get_ticks() current_time = time.clock() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONUP: if waiting == True: waiting = False game_over = False last_time = 0 auto_play = False step_time = 400 game_init() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() elif keys[K_UP] or keys[K_w]: snake.velocity = Point(0,-1) elif keys[K_DOWN] or keys[K_s]: snake.velocity = Point(0,1) elif keys[K_LEFT] or keys[K_a]: snake.velocity = Point(-1,0) elif keys[K_RIGHT] or keys[K_d]: snake.velocity = Point(1,0) elif keys[K_f]: if screen_size == pygame.FULLSCREEN: screen_size =0 else: screen_size =pygame.FULLSCREEN screen = pygame.display.set_mode((24*img_size,18*img_size),screen_size) pygame.display.set_caption("嗷大喵贪吃蛇") elif keys[K_PLUS] or keys[K_KP_PLUS]: step_time -= 10 if step_time <50: step_time=50 elif keys[K_MINUS] or keys[K_KP_MINUS]: step_time += 10 if step_time >400: step_time = 400 elif keys[K_SPACE]: if auto_play: auto_play = False step_time = 400 else: auto_play = True step_time = 100 if waiting: screen.blit(face,(0,0)) else: if not game_over: hole_group.update(ticks) snake.update(ticks) food_group.update(ticks) #检测是否捡起食物,并增加身体长度 hit_list = pygame.sprite.groupcollide(snake.segments, \ food_group, False, True) if len(hit_list) > 0: food_group.add(Food()) snake.add_segment() #检测是否碰到了自己的身体 for n in range(1, len(snake.segments)): if pygame.sprite.collide_rect(snake.segments[0], snake.segments[n]): game_over = True #检查屏幕边界 head_x = snake.segments[0].X//img_size head_y = snake.segments[0].Y//img_size if head_x < 0: snake.segments[0].X = 23 * img_size elif head_x > 24: snake.segments[0].X = 0 * img_size if head_y < 0: snake.segments[0].Y = 17 * img_size elif head_y > 18: snake.segments[0].Y = 0 * img_size #检测是否进入黑洞,并从另一个黑洞出来 holex = snake.segments[0].X holey = snake.segments[0].Y if holex == hole1.X and holey == hole1.Y: snake.segments[0].X = hole2.X snake.segments[0].Y = hole2.Y elif holex == hole2.X and holey == hole2.Y: snake.segments[0].X = hole1.X snake.segments[0].Y = hole1.Y #执行自动寻路代码 if auto_play: auto_move() backbuffer.fill((20,50,20)) snake.draw(backbuffer) food_group.draw(backbuffer) hole_group.draw(backbuffer) screen.blit(backbuffer, (0,0)) if not game_over: print_text(font, 0, 0, "Length " + str(len(snake.segments))) print_text(font, 0, 20, "Position " + str(snake.segments[0].X//img_size) + \ "," + str(snake.segments[0].Y//img_size)) else: print_text(font, 0, 0, "GAME OVER") waiting = True game_over = False #显示自动字样 if auto_play: print_text(font, 600, 0, "AUTO") #循环播放背景音乐 if int(current_time)%200 ==0 and rePlay: play_sound(bgm) rePlay = False if int(current_time)%200 == 1: rePlay = True pygame.display.update() ```
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页