python最简单的游戏代码(python自制小游戏)

这是一个python打地鼠的小游戏代码,耐玩性比较高,而且代码相对简单。之所以选择这个,是因为它虽然相对于最简单的小游戏来说很复杂,但是耐玩,不至于几分钟就不想玩了,而且更适合初学者学习。

游戏截图

python打地鼠小游戏

游戏源代码下载

下载地址:https://github.com/CharlesPikachu/Games/tree/master/cpgames/core/games/whacamole

游戏部分代码

游戏主循环

# 游戏主循环
while True:
  # --游戏时间为60s
  time_remain = round((61000 - pygame.time.get_ticks()) / 1000.)
  # --游戏时间减少, 地鼠变位置速度变快
  if time_remain == 40:
    pygame.time.set_timer(change_hole_event, 650)
  elif time_remain == 20:
    pygame.time.set_timer(change_hole_event, 500)
  # --倒计时音效
  if time_remain == 10:
    audios['count_down'].play()
  # --游戏结束
  if time_remain < 0: break
  count_down_text = font.render('Time: '+str(time_remain), True, cfg.WHITE)
  # --按键检测
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()
    elif event.type == pygame.MOUSEMOTION:
      hammer.setPosition(pygame.mouse.get_pos())
    elif event.type == pygame.MOUSEBUTTONDOWN:
      if event.button == 1:
        hammer.setHammering()
    elif event.type == change_hole_event:
      hole_pos = random.choice(cfg.HOLE_POSITIONS)
      mole.reset()
      mole.setPosition(hole_pos)
  # --碰撞检测
  if hammer.is_hammering and not mole.is_hammer:
    is_hammer = pygame.sprite.collide_mask(hammer, mole)
    if is_hammer:
      audios['hammering'].play()
      mole.setBeHammered()
      your_score += 10
  # --分数
  your_score_text = font.render('Score: '+str(your_score), True, cfg.BROWN)
  # --绑定必要的游戏元素到屏幕(注意顺序)
  screen.blit(bg_img, (0, 0))
  screen.blit(count_down_text, (875, 8))
  screen.blit(your_score_text, (800, 430))
  mole.draw(screen)
  hammer.draw(screen)
  # --更新
  pygame.display.flip()
  clock.tick(60)

游戏开始界面

'''游戏开始界面'''
def startInterface(screen, begin_image_paths):
    begin_images = [pygame.image.load(begin_image_paths[0]), pygame.image.load(begin_image_paths[1])]
    begin_image = begin_images[0]
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                mouse_pos = pygame.mouse.get_pos()
                if mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)):
                    begin_image = begin_images[1]
                else:
                    begin_image = begin_images[0]
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1 and mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)):
                    return True
        screen.blit(begin_image, (0, 0))
        pygame.display.update()


'''结束界面'''
def endInterface(screen, end_image_path, again_image_paths, score_info, font_path, font_colors, screensize):
    end_image = pygame.image.load(end_image_path)
    again_images = [pygame.image.load(again_image_paths[0]), pygame.image.load(again_image_paths[1])]
    again_image = again_images[0]
    font = pygame.font.Font(font_path, 50)
    your_score_text = font.render('Your Score: %s' % score_info['your_score'], True, font_colors[0])
    your_score_rect = your_score_text.get_rect()
    your_score_rect.left, your_score_rect.top = (screensize[0] - your_score_rect.width) / 2, 215
    best_score_text = font.render('Best Score: %s' % score_info['best_score'], True, font_colors[1])
    best_score_rect = best_score_text.get_rect()
    best_score_rect.left, best_score_rect.top = (screensize[0] - best_score_rect.width) / 2, 275
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                mouse_pos = pygame.mouse.get_pos()
                if mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)):
                    again_image = again_images[1]
                else:
                    again_image = again_images[0]
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1 and mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)):
                    return True
        screen.blit(end_image, (0, 0))
        screen.blit(again_image, (416, 370))
        screen.blit(your_score_text, your_score_rect)
        screen.blit(best_score_text, best_score_rect)
        pygame.display.update()

游戏来源

原作者:Charles_pikachu(Charles的皮卡丘)

github主页:https://github.com/CharlesPikachu