# Yue Chuang take you live coding games

Posted on Dec. 2, 2022, 7:39 p.m. by Guy Jones-James
Category: The back-end

Hello, I'm Yue Chuang. Today I'm going to share a little hands-on project in a one-to-one teaching of Python. - The protagonist beat the monster - word game

### Goal:

1. Game name: the main character dozen strange - word game
2. One player, one enemy
3. Enemies and players attack each other
4. The player can choose to attack or defend, the enemy will definitely attack, and the defense will be attacked: 1 in 10
5. The enemy's attack value is random
``````# -*- coding: utf-8 -*-
# @Author: clela
# @Date: 2020-03-31 14:13:35
import random
# 1
class Creature() :
def attack(self) :
# I want to get a random attack number
attack_value = random.randint(0.50)
return attack_value
Copy the code``````

With our class created above, I need to instantiate it:

``````player = Creature()
enemy = Creature()
Copy the code``````

In the context of a game, we usually have to determine the state of the user and the enemy, that is, alive or dead (player or enemy), that is, constantly judging, what is needed at this point? Obviously you need a while loop here. We define a not_dead() function to determine

``````while player.not_dead() and enemy.not_dead():
pass
Copy the code``````

This requires setting an initial value (initial health) for both the player and the enemy, which is passed in during class instantiation, such as: Player health: 100. Enemy: 80. Now that we're adding the initial value, we need to write the class initialization.

``````player = Creature(100)
enemy = Creature(80)
Copy the code``````

Class, we then use HP to store the user-initialized health. (Use self. HP to store user and enemy health)

``````class Creature() :
def __init__(self, hp) :
self.hp = hp
Copy the code``````

At this point, our game has health, and the next step is to determine the game state of the player and the enemy:

``````# 1:
if self.hp = 0:
return False
else:
return True
# write two
if self.hp = 0:
return False
return True
Copy the code``````

Not_dead () returns True, and the game continues.

And because our game is user input to get action, we need to add user input to the while loop:

###### Here I write a simple point, do not judge whether the user input is in line with the rules, we here first default user input is correct, after class to add!
``````while player.not_dead() and enemy.not_dead():
user_input = input("Attack or Defence(A/D):")

if user_input == "A":
player_attack_value = player.attack()
enemy_attack_value = enemy.attack()
Copy the code``````

So now that we have the player's attack value and the enemy's attack value, we need to reduce health.

So, let's write a regulation for being_attack() health reduction:

``````enemy.being_attack(player_attack_value)
player.being_attack(enemy_attack_value)
Copy the code``````

At this point we need to add an attack function:

``````def being_attack(self, attack_value) :
self.hp = self.hp - attack_value
Copy the code``````

Next, we will write that when the user enters defense, and when the player chooses defense, only the attack value of the enemy is left, and the attack value of the enemy is reduced by one tenth:

``````elif user_input == "D":
enemy_attack_value = enemy.attack()*0.1
player.being_attack(enemy_attack_value)
Copy the code``````

Do you feel something is missing? That's the game state, and where should I put the game state?

That is to see player and enemy health in real time before the player chooses:

``````player.show_status()
enemy.show_status()
Copy the code``````

Write the required functions:

``````def show_status(self) :
print(self.hp)
Copy the code``````

Now we need to know which enemy the user is, so we need to add a custom user name in the initialization place:

``````player = Creature(100."AI yue chong")
enemy = Creature(80."Enemy")
Copy the code``````

Then we need to write the initialization part of the class:

``````def __init__(self, hp, name) :
self.hp = hp
self.name = name
Copy the code``````

Show_status (); show_status(); show_status();

``````def show_status(self) :
print("{}' hp is {}.".format(self.name, self.hp))
Copy the code``````

Now we're done writing the main program. We also have to decide who won and who lost:

``````if player.not_dead():
print("You Win!")
else:
print("You Lose")
Copy the code``````

Program source code can pay attention to the public number: AI Yue Chuang, background reply: word game can be obtained!

Search