Simple Python game making

Programming Lao yuan 2021-11-25 18:48:10
simple python game making

What about today , Let's show you Python Interesting little place , Show you , My little game , Are there any memories of your childhood . Let's not talk more nonsense , Show you ! It's ok if the interested partners don't understand , You can download one PyCharm, Knock along with my picture code !

First, we choose the development tools Python edition :3.6.4, Related template selection pygame Templates , As well as some Python Built in modules . The environment construction is also very simple , Just install Python And add to environment variable ,pip Install the required relevant templates .

Step by step :

Step1: Define the game sprite class

Because the game involves collision testing , So let's first define some game spirit classes , Including the pusher 、 The box 、 Wall and target location indicator .

First of all, let's define the elves who push boxes :

He needs to have the ability to move , Here is a choice to simulate movement , It is to judge whether it can go on by simulating movement 、 Next 、 Left 、 Move right . Because of the nature and type of other things on the map , So we define them as the same elf class ( In fact, they are all similar in nature , But it's necessary to distinguish people from things ):

The box has the ability to move , Others cannot be moved . The function of simulating mobile selection is similar to that before .

Step2: Define game map class

Here we define a game map class , The goal is to use this class to create arbitrary game maps . therefore , This class should be able to add and save game elements ( people 、 wall 、 Boxes, etc ), And draw the map on the screen . At the same time, a method should also be brought to judge whether the boxes on this map have been sent to the specified location ( This makes it easy to change levels ):

Step3: Define game interface classes

Game interface class is responsible for parsing levels Folder under the game level map file , And use game map class to create and display game map :

And because of the game map area > Game window interface , Therefore, this class needs to add the function of scrolling game map according to the position of characters :

Step4: Define a level of the game's main loop

The main loop is responsible for instantiating game interface classes , And according to the results of key detection, the game interface class is operated :

The logic of character movement is : The target of character movement is an empty white space , Then people move ; If you hit a box , The box can move one space in the same direction as the person , Then the person and the box move ; In other cases, people and boxes cannot be moved .

Step5: Define the beginning of the game 、 Switch and end the interface

It's a little crude , Just take the source code .

Start interface :

Switch interface :

End the screen :

Step6: Realize the main function of the game

Just string all the interfaces together :

Finally, I added a background music , It shouldn't be a big problem . Students who understand try to do , Students who don't understand can just follow the code and knock it out , After running, as shown in the figure :

If no one understands, please comment and leave a message ! Or a private letter, I can , Comments must be answered , Private letters must be read , Don't miss any students who study hard !!!

本文为[Programming Lao yuan]所创,转载请带上原文链接,感谢

  1. python3.7 FileNotFoundError: [WinError 2] 系统找不到指定的文件。
  2. 最全面的Python重点知识汇总
  3. python中 我想爬取一个网页,我在cmd中pip list中已经把requests 以及bs4 弄好了 但是他还是提示我没有那个模块
  4. You can learn Python articles without reading online classes (the first day)
  5. 在Python和Go项目之后使用Rust的经验分享 - scaleway
  6. 使用PyO3从Python调用 Rust:加速Python
  7. Python 3.7 filenotfounderror: [winerror 2] le système n'a pas pu trouver le fichier spécifié.
  8. Python 一切皆对象
  9. 【Pandas学习笔记01】强大的分析结构化数据的工具集
  10. 求各位亲们可以解答一下嘛,为什么跑不出来呀,大学Python
  11. 【Pandas学习笔记01】强大的分析结构化数据的工具集
  12. python飞机大战如何让飞机始终在界面中,不能跑出界面
  13. Python代码阅读(第63篇):数字奇偶性
  14. python建立⼀個圖形介⾯應⽤(GUI app)
  15. python接口自动化的html格式报告以邮件形式发送展示的原文很丑怎么处理
  16. 针对初学者,我建议你学这 3 个 Python AutoEDA 工具包
  17. #yyds干货盘点#Python-爬虫_urlib一个类型和六个方法
  18. Pour les débutants, je vous recommande d'apprendre ces 3 kits autoeda Python
  19. 我想问python怎么入门嘞?求各路人士
  20. Summary of today's Django lessons: data reverse query triggers error field 'score_ jieshoufankui‘ expected a number but got ‘‘-20211109
  21. 关于#python_while循环的写法#的问题,如何解决?
  22. Python异常处理中异常的种类有哪些?你知道几个?
  23. Python异常處理中异常的種類有哪些?你知道幾個?
  24. Quels sont les types d'exceptions dans la gestion des exceptions python? Combien en savez - vous?
  25. À propos de # Python Comment résoudre le problème de l'écriture de la Boucle while?
  26. Python如何操作数据库
  27. python数字游戏,让你欲罢不能
  28. Python中的可迭代对象、迭代器、For循环工作机制、生成器
  29. 一个从没接触过编程的人,如何自学进入Python行业?
  30. 一份超级实用的 Python ”技巧“清单
  31. Python 程序员给上路初学者的3点忠告
  32. 3 conseils pour les débutants sur la route par les programmeurs Python
  33. Une liste de conseils Python super pratiques
  34. Comment quelqu'un qui n'a jamais été en contact avec la programmation peut - il apprendre à entrer dans l'industrie python?
  35. Objets itérables, itérateurs, pour le mécanisme de travail circulaire, générateurs en python
  36. Les Jeux de chiffres Python vous font vous arrêter
  37. Comment Python fonctionne avec la base de données
  38. Python之html与markdown互相转换
  39. Python之html與markdown互相轉換
  40. Conversion HTML et markdown de Python
  41. Python生成的随机数,要怎么设定成随机数a小于随机数b啊
  42. Python生成的隨機數,要怎麼設定成隨機數a小於隨機數b啊
  43. Le nombre aléatoire généré par Python, comment définir le nombre aléatoire a est inférieur au nombre aléatoire B
  44. Python爬虫能做什么
  45. Python老技师给上路初学者的3点忠告
  46. 3 conseils aux débutants sur la route de l'ancien technicien Python
  47. Que peuvent faire les reptiles Python
  48. The code NPM cloned from git will report a python error after it is installed!
  49. 数据分析从零开始实战,Pandas读写CSV数据
  50. Python基础(十二):类与对象
  51. Python Foundation (12): classes et objets
  52. Python基礎(十二):類與對象
  53. L'analyse des données commence à zéro et pandas lit et écrit les données CSV
  54. Python Qt GUI设计:QSlider滑动条类(基础篇—16)
  55. #yyds干货盘点#数据分析从零开始实战,Pandas读写CSV数据
  56. This paper designs an examination system for automatically setting questions and judging papers by using Python standard library language to investigate the ability of users to calculate four integers within 100.
  57. Yyds Dry Inventory # Data Analysis started from zero, pandas read and write CSV Data
  58. python数据结构:数组、列表、栈、队列及实现
  59. Python编程真的是未来人工智能的主流吗?
  60. Python编写通讯录,支持模糊查询,利用数据库存储