Python Fun: Battle of the Knights

Battle of the Knights GitHub

Introduction

This program is a command line interface game that allows the user to play the computer in a virtual dual to the death, a battle of the knights. I also implemented two player but have not yet pursued finding out how best to play this mode as right now the second player can see what choice the first player makes.

The gameplay is simple. The user may choose one of three options: Attack, Defend, or Rest. Attack deals damage, Defend deflects damage, and Rest adds health. The game also includes the rule where the user is not allowed to perform an action more than twice in a row. This makes it interesting because a player cannot just perform any one action every time during the game. For example, if your opponent defends twice in a row, you know they must rest or attack on the next turn. And your opponent knows you know. And you know your opponent knows you know. So things get interesting.


The Code

The code uses a gameloop to perform the necessary game operations on two instances of the Knight class. Get Action -> Display Scene -> Handle Action -> Display Health -> Check End Game

The Knight class has the following attributes and methods:

  • Attributes: name, health, action, is_player, last_actions
  • Methods: get_valid_input, get_action_choice, get_defense, get_attack, apply_damage, apply_rest

Aside from that, the game uses a main file to run the program and a constants file to store several constant values including a list of names to help me generate a knightly name for the players.

Inspiration

The inspiration for this program came from 101Computing.Net. It seemed like a fun challenge and I just kept adding on and I feel I have made something small but cool.

Later Improvements

There are several improvements I can make in the future:

  • additional choices other than Attack, Defend, Rest
  • classes / archetypes for player to choose from using class inheritance
  • one player mode: CPU strategies / difficulties…
  • fix two player mode to use socket programming or something else so you cannot see the other players input

Gameplay Example

Welcome to Battle of the Knights!
Please enter one of the following modes in order to play: ['one', 'two']:one
You are playing single player. You're name is Sir Percival the Negotiator. You're opponent's name is Voss Mock the Contradictory.
You can Attack, Defend, or Rest on each turn.
You cannot perform the same action more than twice in a row.
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 89
Voss Mock the Contradictory Health = 93
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 70
Voss Mock the Contradictory Health = 68
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: D
 O      | O 
-+-     |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 80
Voss Mock the Contradictory Health = 68
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 80
Voss Mock the Contradictory Health = 58
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: D
 O  /   | O 
-+-/    |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 70
Voss Mock the Contradictory Health = 58
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: D
 O      | O 
-+-     |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 80
Voss Mock the Contradictory Health = 58
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 64
Voss Mock the Contradictory Health = 50
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 64
Voss Mock the Contradictory Health = 40
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: R
 O        O 
-+-      -+-
/ \      / \
Sir Percival the Negotiator Health = 74
Voss Mock the Contradictory Health = 50
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 58
Voss Mock the Contradictory Health = 27
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 41
Voss Mock the Contradictory Health = 8
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: d
Knight1: D | Knight2: R
 O |      O 
-+-|     -+-
/ \|     / \
Sir Percival the Negotiator Health = 41
Voss Mock the Contradictory Health = 18
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 18
Voss Mock the Contradictory Health = 11
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: D
 O  /   | O 
-+-/    |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 8
Voss Mock the Contradictory Health = 11
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: R
 O        O 
-+-      -+-
/ \      / \
Sir Percival the Negotiator Health = 18
Voss Mock the Contradictory Health = 21
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 18
Voss Mock the Contradictory Health = 13
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 18
Voss Mock the Contradictory Health = 11
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: D
 O      | O 
-+-     |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 28
Voss Mock the Contradictory Health = 11
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 28
Voss Mock the Contradictory Health = 1
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: R
 O  /     O 
-+-/     -+-
/ \      / \
Sir Percival the Negotiator Health = 28
Voss Mock the Contradictory Health = 3
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: R
 O        O 
-+-      -+-
/ \      / \
Sir Percival the Negotiator Health = 38
Voss Mock the Contradictory Health = 13
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: D
 O  /   | O 
-+-/    |-+-
/ \     |/ \
Sir Percival the Negotiator Health = 32
Voss Mock the Contradictory Health = 13
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 16
Voss Mock the Contradictory Health = 8
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: r
Knight1: R | Knight2: R
 O        O 
-+-      -+-
/ \      / \
Sir Percival the Negotiator Health = 26
Voss Mock the Contradictory Health = 18
------------------------------
Player Turn: Sir Percival the Negotiator
Choose 'a' to Attack, 'd' to Defend, or 'r' to Rest: a
Knight1: A | Knight2: A
 O  /  \  O 
-+-/    \-+-
/ \      / \
Sir Percival the Negotiator Health = 7
Voss Mock the Contradictory Health = -6
------------------------------
Voss Mock the Contradictory dies. Sir Percival the Negotiator wins the game!
\O/         
 +          
/ \    /-+-O