Python game development, pyGame module, python implementation of Xiaole games

Chu_ Yang 2021-09-15 04:28:31
python game development pygame module

Preface :

hello , Long time no see . Vanishing population online updates a wave that will come out every month python Make small game series articles . In this issue, we will make a Xiaole game , Let's start happily ~

Effect display


development tool

Python edition : 3.6.4

Related modules :

pygame modular ;

As well as some Python Built in modules .

Environment building

install Python And add to environment variable ,pip Install the relevant modules required .

Introduction of the principle

The rules of the game :

Players exchange adjacent puzzles with the mouse , If the level after exchange / There are three consecutive identical puzzles in the vertical direction , Then these puzzles disappear , Players score , At the same time, a new puzzle is generated to supplement the missing part , otherwise , The exchange failed , Players don't score .

Players need to score as high as possible within the specified time .

Step by step :

Step1****: initialization

Game initialization , And load the necessary game materials :


Step2****: Define the game sprite class

Collision detection between mouse and puzzle block is needed in the game , So as to operate the puzzle block to move , Therefore, we define a puzzle game wizard class to facilitate the implementation of these functions :


Step3****: Realize the main cycle of the game

Logic is very simple , Is to constantly detect whether a mouse click event occurs , If there is , Then judge whether the position when the mouse clicks is within the position area of a puzzle block , If in , Then select the puzzle block , Otherwise, don't select .

When a second puzzle piece is selected , Then judge whether the two puzzle blocks meet the conditions of puzzle exchange , If meet , Then exchange puzzle pieces , And get a reward , Otherwise, do not swap and deselect the selected state of the two puzzle blocks .

The source code is shown in the figure below :


Step4****: Game ending screen

After the game countdown , Enter the game end interface , The interface displays the user's current score . meanwhile , If the user types R Key to restart the game , type ESC Key to exit the game .

The source code implementation is as follows :


This is the end of the article , Thanks for watching ,Python24 A series of small games , Share the next article 24 A little game

Don't indulge in games , We should combine work with rest

I wish you all a happy study

本文为[Chu_ Yang]所创,转载请带上原文链接,感谢

  1. 程序員是這樣學習【中藥學】知識的,先用python采集分析一波
  2. Python - pydantic(2)嵌套模型
  3. Python - 面向对象编程 - __dict__
  4. Python - 面向对象编程 - 反射 hasattr、getattr、getattr、delattr
  5. Python - 面向对象编程 - 实战(6)
  6. Python - 面向对象编程 - 魔术方法(双下划线方法)
  7. Python - 面向对象编程 - __new__() 和单例模式
  8. Python - 面向对象编程 - __call__()
  9. Python - repr()、str() 的区别
  10. Python - 面向对象编程 - __repr__()
  11. Python - eval()
  12. 用 Django REST framework 来实现一次性验证码(OTP)
  13. Python code checking tool (static code review)
  14. Python QT GUI Design: overview of UI interface visualization components and properties (Fundamentals - 3)
  15. 【算法学习】1863. 找出所有子集的异或总和再求和(java / c / c++ / python / go / rust)
  16. [apprentissage de l'algorithme] 1863. Trouver la somme Xor de tous les sous - ensembles (Java / C / C + + / python / go / Rust)
  17. Python code checking tool (static code review)
  18. Python 万能代码模版:爬虫代码篇
  19. Modèle de code universel Python: Code crawler
  20. 收藏 | Python 之 Pandas系统性总结!
  21. Take you to learn more about nginx basic login authentication: generating passwords using Python
  22. 超硬核Python避坑学习方案奉上!入门到就业一篇就搞定!
  23. Talk about how JMeter executes Python scripts concurrently
  24. Talk about how JMeter executes Python scripts concurrently
  25. Talk about how JMeter executes Python scripts concurrently
  26. python内置函数通过字符串的方式来执行函数代码块,类似java的反射机制相当强大!
  27. python内置函数通过字符串的方式来执行函数代码块,类似java的反射机制相当强大!
  28. python內置函數通過字符串的方式來執行函數代碼塊,類似java的反射機制相當强大!
  29. Les fonctions intégrées Python exécutent des blocs de code de fonction à travers des chaînes, et les mécanismes de réflexion comme Java sont assez puissants!
  30. Python module 1
  31. Python tip: use namedtuple instead of manually created classes
  32. Python - poetry(3)配置项详解
  33. Python - poetry(3)配置项详解
  34. Python - poetry(3)配置項詳解
  35. Python - poetry(3)配置項詳解
  36. Python - détails de l'élément de configuration Poetry (3)
  37. Python - détails de l'élément de configuration Poetry (3)
  38. Python案例实战,pygame模块,Python实现字母代码雨
  39. Python calculation vector angle code
  40. Python基础面试题解读|《Python面试100层》|第1层
  41. 面对小白的pandas命令手册+练习题【三万字详解】
  42. 面對小白的pandas命令手册+練習題【三萬字詳解】
  43. Face au Manuel de commande pandas de Xiaobai + question d'exercice [30 000 mots pour plus de détails]
  44. Interprétation des questions d'entrevue de base Python | 100 couches d'entrevue Python | couche 1
  45. Python data structure and algorithm (17) -- merge sort
  46. Les fonctions intégrées Python exécutent des blocs de code de fonction à travers des chaînes, et les mécanismes de réflexion comme Java sont assez puissants!
  47. Python笔记-uiautomator2截图点击,OpenCV找图
  48. Python文档阅读笔记-OpenCV中Template Matching
  49. Python笔记-利用OpenCV的matchTemplate屏幕找图并使用pyautogui点击
  50. Python筆記-利用OpenCV的matchTemplate屏幕找圖並使用pyautogui點擊
  51. Notes python - utilisez l'écran matchtemplate d'OpenCV pour trouver des images et cliquez sur
  52. Notes de lecture de documents python - Matching de modèles dans OpenCV
  53. Notes python - capture d'écran de l'automate 2 Cliquez pour ouvrir la vue
  54. python链接云服务器的mysql8
  55. python鏈接雲服務器的mysql8
  56. Mysql8 pour les serveurs Cloud liés Python
  57. Python资源大集合,要的话可以拿走!
  58. ️万字【Python基础】保姆式教学️,小白快速入门Python!
  59. ️萬字【Python基礎】保姆式教學️,小白快速入門Python!
  60. Wanzi [base Python] Baby - sitting Teaching, Little White Quick Start Python!