用python编写一个卡牌战斗小游戏,附上代码
时间: 2023-09-06 08:11:29 浏览: 127
好的,下面是一个简单的卡牌战斗小游戏的 Python 代码,供您参考:
```python
import random
# 定义卡牌类
class Card:
def __init__(self, name, attack, defense, hp):
self.name = name
self.attack = attack
self.defense = defense
self.hp = hp
def __str__(self):
return f"{self.name} (Attack={self.attack}, Defense={self.defense}, HP={self.hp})"
# 定义玩家类
class Player:
def __init__(self, name, deck):
self.name = name
self.deck = deck
self.hand = []
self.discard_pile = []
self.field = []
# 抽一张牌
def draw(self):
if len(self.deck) == 0:
self.deck = self.discard_pile
self.discard_pile = []
random.shuffle(self.deck)
card = self.deck.pop()
self.hand.append(card)
return card
# 打出一张牌
def play_card(self, card_index):
if card_index < 0 or card_index >= len(self.hand):
return None
card = self.hand.pop(card_index)
self.field.append(card)
return card
# 攻击
def attack_player(self, target):
if len(self.field) == 0:
return False
attacking_card = random.choice(self.field)
damage = attacking_card.attack - target.field_defense()
if damage > 0:
target.hp -= damage
target.destroy_dead_cards()
attacking_card.hp -= target.field_attack()
if attacking_card.hp <= 0:
self.field.remove(attacking_card)
self.discard_pile.append(attacking_card)
return True
# 获取场上所有卡牌的攻击力之和
def field_attack(self):
return sum([card.attack for card in self.field])
# 获取场上所有卡牌的防御力之和
def field_defense(self):
return sum([card.defense for card in self.field])
# 消灭场上死亡的卡牌
def destroy_dead_cards(self):
for card in self.field:
if card.hp <= 0:
self.field.remove(card)
self.discard_pile.append(card)
# 判断是否输了
def has_lost(self):
return self.hp <= 0
def __str__(self):
return f"{self.name} (HP={self.hp})"
# 定义游戏类
class Game:
def __init__(self, player1, player2):
self.current_turn = 1
self.players = [player1, player2]
# 开始游戏
def start(self):
print("Game starts!")
while True:
print(f"\nTurn {self.current_turn}:")
for i, player in enumerate(self.players):
print(f"\nPlayer {i+1}'s turn:")
player.hp = max(player.hp, 0) # 防止血量小于0
print(player)
self.print_field()
self.print_hand(player.hand)
choice = input("Enter your choice (1=draw a card, 2=play a card, 3=attack player): ")
if choice == "1":
card = player.draw()
print(f"{player.name} draws {card}")
elif choice == "2":
card_index = int(input("Enter the index of the card you want to play: "))
card = player.play_card(card_index)
if card:
print(f"{player.name} plays {card}")
elif choice == "3":
target = self.players[(i+1)%2]
success = player.attack_player(target)
if success:
print(f"{player.name} attacks {target.name}")
else:
print(f"{player.name} cannot attack")
else:
print("Invalid choice")
continue
player.destroy_dead_cards()
if target.has_lost():
print(f"{target.name} has lost the game!")
return
self.current_turn += 1
# 打印场上情况
def print_field(self):
for player in self.players:
print(f"{player.name}'s field: {[str(card) for card in player.field]}")
# 打印手牌
def print_hand(self, hand):
print("Hand:")
for i, card in enumerate(hand):
print(f"{i}: {card}")
# 创建卡牌
cards = [
Card("Card 1", 3, 2, 5),
Card("Card 2", 2, 3, 5),
Card("Card 3", 4, 1, 4),
Card("Card 4", 1, 4, 4),
Card("Card 5", 2, 2, 6),
Card("Card 6", 5, 0, 3),
Card("Card 7", 0, 5, 3),
Card("Card 8", 2, 1, 7),
Card("Card 9", 1, 2, 7),
Card("Card 10", 3, 3, 3)
]
# 创建玩家并分配卡牌
deck1 = cards.copy()
random.shuffle(deck1)
deck2 = cards.copy()
random.shuffle(deck2)
player1 = Player("Player 1", deck1[:5])
player2 = Player("Player 2", deck2[:5])
# 开始游戏
game = Game(player1, player2)
game.start()
```
希望这个代码对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)