基于Python+Pygame如何实现变异狗大战游戏

其他教程   发布日期:2023年08月02日   浏览次数:426

这篇文章主要介绍了基于Python+Pygame如何实现变异狗大战游戏的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于Python+Pygame如何实现变异狗大战游戏文章都会有所收获,下面我们一起来看看吧。

一、准备环境

1)环境安装

本文用到的环境如下——

Python3、Pycharm社区版,pygame其他自带的库只要安装完 Python就可以直接使用了

一般安装:pip install +模块名

镜像源安装:pip install -i pypi.douban.com/simple/+模块名…

二、代码展示

1)导入库

  1. import pygame, sys
  2. from pygame.locals import *

2)主程序

  1. def pygame_run():
  2. pygame.init()
  3. _display_surf = pygame.display.set_mode((480, 320))
  4. pygame.display.set_caption('py梦')
  5. _font_type = pygame.font.match_font('Microsoft YaHei')
  6. # 敌方精灵状态,文字显示
  7. _ord_pym_rect = pygame.Rect(-260, 0, 220, 50)
  8. # 敌方精灵名字,文字显示设置
  9. _ord_pym_name = pygame.font.Font(_font_type, 16)
  10. _ord_pym_name_surf_obj = _ord_pym_name.render("lv10:它狗", True, BLACK, None)
  11. _ord_pym_name_rect = _ord_pym_name_surf_obj.get_rect()
  12. _ord_pym_name_rect.left = -200
  13. _ord_pym_name_rect.top = 0
  14. # 敌方精灵血量,文字显示设置
  15. _ord_pym_blood = pygame.font.Font(_font_type, 16)
  16. _ord_pym_blood_surf_obj = _ord_pym_blood.render("血量:----------[69/69]", True, BLACK, None)
  17. _ord_pym_blood_rect = _ord_pym_blood_surf_obj.get_rect()
  18. _ord_pym_blood_rect.left = -200
  19. _ord_pym_blood_rect.top = 20
  20. # 敌方精灵贴图显示设置
  21. _ord_pym_img = pygame.image.load('dog1.png')
  22. _ord_pym_img_top = 20
  23. _ord_pym_img_left = 320+220
  24. # 我方精灵状态,文字显示设置
  25. _my_pym_rect = pygame.Rect(260, 170, 220, 50)
  26. # 我方精灵名字,文字显示设置
  27. _my_pym_name = pygame.font.Font(_font_type, 16)
  28. _my_pym_name_surf_obj = _my_pym_name.render("lv18:我狗", True, BLACK, None)
  29. _my_pym_name_rect = _my_pym_name_surf_obj.get_rect()
  30. _my_pym_name_rect.left = 480
  31. _my_pym_name_rect.top = 170
  32. # 我方精灵血量,文字显示设置
  33. _my_pym_blood = pygame.font.Font(_font_type, 16)
  34. _my_pym_blood_surf_obj = _my_pym_blood.render("血量:----------[99/99]", True, BLACK, None)
  35. _my_pym_blood_rect = _my_pym_blood_surf_obj.get_rect()
  36. _my_pym_blood_rect.left = 480
  37. _my_pym_blood_rect.top = 190
  38. # 我方精灵贴图显示设置
  39. _my_pym_img = pygame.image.load('dog2.png')
  40. _my_pym_img_top = 80
  41. _my_pym_img_left = 20-220
  42. # 对战面板,显示设置
  43. _select_rect = pygame.Rect(480, 220, 220, 95)
  44. # 战斗,文字显示设置
  45. _select_font_1 = pygame.font.Font(_font_type, 30)
  46. _select_font_1_surf_obj = _select_font_1.render("战斗", True, BLACK, None)
  47. _select_font_1_rect = _select_font_1_surf_obj.get_rect()
  48. _select_font_1_rect.left = 480
  49. _select_font_1_rect.top = 220
  50. # 道具,文字显示设置
  51. _select_font_2 = pygame.font.Font(_font_type, 30)
  52. _select_font_2_surf_obj = _select_font_2.render("道具", True, BLACK, None)
  53. _select_font_2_rect = _select_font_2_surf_obj.get_rect()
  54. _select_font_2_rect.left = 580
  55. _select_font_2_rect.top = 220
  56. # 精灵,文字显示设置
  57. _select_font_3 = pygame.font.Font(_font_type, 30)
  58. _select_font_3_surf_obj = _select_font_3.render("精灵", True, BLACK, None)
  59. _select_font_3_rect = _select_font_3_surf_obj.get_rect()
  60. _select_font_3_rect.left = 480
  61. _select_font_3_rect.top = 270
  62. # 逃跑,文字显示设置
  63. _select_font_4 = pygame.font.Font(_font_type, 30)
  64. _select_font_4_surf_obj = _select_font_4.render("逃跑", True, BLACK, None)
  65. _select_font_4_rect = _select_font_4_surf_obj.get_rect()
  66. _select_font_4_rect.left = 580
  67. _select_font_4_rect.top = 270
  68. while True:
  69. _display_surf.fill(WHITE)
  70. pygame.draw.rect(_display_surf, BLACK, _select_rect, 1)
  71. pygame.draw.rect(_display_surf, WHITE, _my_pym_rect, 0)
  72. _display_surf.blit(_ord_pym_img, (_ord_pym_img_left, _ord_pym_img_top))
  73. _display_surf.blit(_my_pym_img, (_my_pym_img_left, _my_pym_img_top))
  74. _display_surf.blit(_ord_pym_name_surf_obj, _ord_pym_name_rect)
  75. _display_surf.blit(_ord_pym_blood_surf_obj, _ord_pym_blood_rect)
  76. _display_surf.blit(_my_pym_name_surf_obj, _my_pym_name_rect)
  77. _display_surf.blit(_my_pym_blood_surf_obj, _my_pym_blood_rect)
  78. _display_surf.blit(_select_font_1_surf_obj, _select_font_1_rect)
  79. _display_surf.blit(_select_font_2_surf_obj, _select_font_2_rect)
  80. _display_surf.blit(_select_font_3_surf_obj, _select_font_3_rect)
  81. _display_surf.blit(_select_font_4_surf_obj, _select_font_4_rect)
  82. if _select_rect.left != 260:
  83. _select_rect.left = _select_rect.left - 5
  84. _select_font_1_rect.left = _select_font_1_rect.left - 5
  85. _select_font_2_rect.left = _select_font_2_rect.left - 5
  86. _select_font_3_rect.left = _select_font_3_rect.left - 5
  87. _select_font_4_rect.left = _select_font_4_rect.left - 5
  88. _my_pym_name_rect.left = _my_pym_name_rect.left - 5
  89. _my_pym_blood_rect.left = _my_pym_blood_rect.left - 5
  90. _ord_pym_name_rect.left = _ord_pym_name_rect.left + 5
  91. _ord_pym_blood_rect.left = _ord_pym_blood_rect.left + 5
  92. _ord_pym_img_left = _ord_pym_img_left - 5
  93. _my_pym_img_left = _my_pym_img_left + 5
  94. for _event in pygame.event.get():
  95. if _event.type == QUIT:
  96. pygame.quit()
  97. sys.exit()
  98. pygame.display.update()
  99. _fps_Clock.tick(FPS)
  100. if __name__ == '__main__':
  101. pygame_run()

以上就是基于Python+Pygame如何实现变异狗大战游戏的详细内容,更多关于基于Python+Pygame如何实现变异狗大战游戏的资料请关注九品源码其它相关文章!