current position:Home>Python: simple single player strange game (text)
Python: simple single player strange game (text)
2022-02-01 13:32:57 【Xu Hongda】
The game is very simple , It is very suitable for newcomers like me to learn . We can learn about Python Classes and objects in the foundation . Steps are as follows .
1. Definition “ class ”
class Creature():# The first letter should be capitalized , The colon cannot be missing
2. initialization
Initialization class Creature, Add attributes to it , Here we add hp This attribute , You can also add more attributes , Add as needed .
def __init__(self,hp):
self.hp=hp
Copy code
3. Defined function
When writing a program, we will use the same piece of code repeatedly , By defining functions, we can greatly reduce the amount of code and our time . This defines the character's attack , Blood volume , A function of whether you're alive or not and how much blood you show .
def attack(self):
attack_value=random.randint(0,10)
return attack_value
def not_dead(self):
if self.hp<=0:
return False
else:
return True
def nowhp(self,attack_value):
self.hp=self.hp-attack_value
def show_stastic(self):
print('its hp is {}'.format(self.hp))
Copy code
4. Create instance object
A class definition is not an object , Just a blueprint . Now come to the real “ Build a house ”. ad locum , I create two objects “pleyer”,“enemy”, And gave them hp The value of this property . Class and object can be compared to class as a species , Like a dog , And the object is different kinds of dogs . We can change the properties of the object by modifying the class , Modifying a single object cannot change other objects .
pleyer=Creature(100)
enemy=Creature(120)
Copy code
5. Realize the small game
Here , I defined that players can attack (A) And defense (D) Two options , The two options will cause different reactions .
while pleyer.not_dead() and enemy.not_dead():
pleyer.show_stastic()
enemy.show_stastic()
choose=input('A or D')
if choose=='A':
pleyer.attack_value=pleyer.attack()
enemy.attack_value=enemy.attack()
pleyer.nowhp(pleyer.attack_value)
enemy.nowhp(pleyer.attack_value)
elif choose=='D':
enemy.attack_value = enemy.attack()*0.1
pleyer.nowhp(enemy.attack_value)
if pleyer.not_dead():
print('you win!')
else:
print('you lose!')
Copy code
Conclusion
For simplicity , Here I only set HP And attack defense , interested , I can go ahead and add MP And various skills , Make the game more interesting .
copyright notice
author[Xu Hongda],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011332545748.html
The sidebar is recommended
- Python logging log error and exception exception callback method
- Learn Python quickly and take a shortcut~
- Python from 0 to 1 (day 15) - Python conditional judgment 2
- Python crawler actual combat, requests module, python to capture headlines and take beautiful pictures
- The whole activity collected 8 proxy IP sites to pave the way for the python proxy pool, and the 15th of 120 crawlers
- Why can't list be used as dictionary key value in Python
- Python from 0 to 1 (day 16) - Python conditional judgment 3
- What is the python programming language?
- Python crawler reverse webpack, a real estate management platform login password parameter encryption logic
- Python crawler reverse, a college entrance examination volunteer filling platform encrypts the parameter signsafe and decrypts the returned results
guess what you like
-
Python simulated Login, selenium module, python identification graphic verification code to realize automatic login
-
Python -- datetime (timedelta class)
-
Python's five strange skills will bring you a sense of enrichment in mastering efficient programming skills
-
[Python] comparison of dictionary dict, defaultdict and orderdict
-
Test driven development using Django
-
Face recognition practice: face recognition using Python opencv and deep learning
-
leetcode 1610. Maximum Number of Visible Points(python)
-
Python thread 03 thread synchronization
-
Introduction and internal principles of Python's widely used concurrent processing Library Futures
-
Python - progress bar artifact tqdm usage
Random recommended
- Python learning notes - the fifth bullet * class & object oriented
- Python learning notes - the fourth bullet IO operation
- Python crawler actual combat: crawl all the pictures in the answer
- Quick reference manual of common regular expressions, necessary for Python text processing
- [Python] the characteristics of dictionaries and collections and the hash table behind them
- Python crawler - fund information storage
- Python crawler actual combat, pyteseract module, python realizes the visualization of boos direct employment & hook post data
- Pit filling summary: Python memory leak troubleshooting tips
- Python code reading (Chapter 61): delaying function calls
- Through the for loop, compare the differences between Python and Ruby Programming ideas
- leetcode 1606. Find Servers That Handled Most Number of Requests(python)
- leetcode 1611. Minimum One Bit Operations to Make Integers Zero(python)
- 06python learning notes - reading external text data
- [Python] functions, higher-order functions, anonymous functions and function attributes
- Python Networkx practice social network visualization
- Data analysis starts from scratch, and pandas reads and writes CSV data
- Python review (format string)
- [pandas learning notes 01] powerful tool set for analyzing structured data
- leetcode 147. Insertion Sort List(python)
- apache2. 4 + windows deployment Django (multi site)
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
- Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
- Python series -- web crawler
- Plotly + pandas + sklearn: shoot the first shot of kaggle
- How to learn Python systematically?
- Analysis on several implementations of Python crawler data De duplication
- leetcode 1616. Split Two Strings to Make Palindrome (python)
- Python Matplotlib drawing violin diagram
- Python crawls a large number of beautiful pictures with 10 lines of code
- [tool] integrated use of firebase push function in Python project
- How to use Python to statistically analyze access logs?