跳过RPG游戏中的Python

时间:2019-10-30 09:16:44

标签: python

我正在用Python构建一个简单的游戏,其中2个Pokemon相互对抗,特别是RBY游戏中的2个Tauros,因为RBY到目前为止拥有最简单的规则。我是初学者。

我已经按照自己的意愿正确运行了游戏(速度平局,伤害公式,致命一击,未击中等都可以正常工作),我唯一缺少的就是Hyper Beam机械师:如果移动不成功杀死目标后,口袋妖怪下一回合不会移动,因为它需要补充能量。

我完全不知道该如何实现。我的想法是走一圈。只要这两个玩家不必充电,游戏就只会调用损坏功能并继续进行,然后不会根据充电状态调用损坏功能。

我不知道如何在不中断整个循环的情况下更改充电状态。我尝试从损伤函数返回补给值,但后来我不确定如何使用它。

这是整个程序的代码,我使用的是Python 3.7.4,我尽了最大的评论:

import math
import random

#creating move class

class Move:
    def __init__(self, name, power, accuracy, typing, recharge):
        self.name = name
        self.power = power
        self.accuracy = accuracy
        self.typing = typing
        self.recharge = recharge
#creating pokemon class

class Pokemon:
    def __init__(self, name, hp, attack, defense, special, speed, typing):
        self.name = name
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.special = special 
        self.speed = speed
        self.typing = typing

#creating 2 objects from the pokemon class

tauros_1 = Pokemon('tauros', 353, 298, 288, 238, 318, 'normal')
tauros_2 = Pokemon('tauros', 353, 298, 288, 238, 318, 'normal')

#creating 4 objects from the move class

body_slam = Move('body slam', 85, 100, 'normal', False)
earthquake = Move('earthquake', 100, 100, 'ground', False)
blizzard = Move('blizzard', 120, 90, 'ice', False)
hyper_beam = Move('hyper beam', 150, 90, 'normal', True)

#inserting the move objects in a list for easier calling

moveList = [body_slam, hyper_beam, blizzard, earthquake]

#player selects a move

def select_move():
    while True:
        move = input('Select a move:\n1) Body Slam\n2) Hyper Beam\n3) Blizzard\n4) Earthquake\n\n')
        if move == '1' or move == '2' or move == '3' or move == '4':
            break
        else:
            print('Invalid value, try again')
    moveNumber = int(move)
    selected_move = moveList[moveNumber]
    return selected_move

#the enemy randoms a move (TEMPORARY)

def enemy_move():
    move = random.randrange(3)
    return move

#critical hits

def critical_hit():
    critical_chance = round(random.uniform(0, 100), 2)
    if critical_chance <= 21.48:
        return 2
    else:
        return 1

#damage formula

def damage(attacker, defender, move):
    miss_chance = random.randrange(99)
    #critical hits deal double damage
    a = critical_hit()
    if a == 2:
        print('A critical hit!')
    #missing equal to dealing 0 damage
    if miss_chance >= move.accuracy:
        accuracy = 0
    else:
        accuracy = 1
    #STAB is a boost to the move used if its of the same typing as the attacker
    if move.typing == attacker.typing:
        stab_modifier = 1.5
    else:
        stab_modifier = 1
    #simplifying the pokemon damage formula ends up with this, it's pretty accurate although not 100%, but I don't care at the moment
    damage = math.floor(0.84 * random.uniform(0.85, 1.00) * move.power * stab_modifier * (attacker.attack / defender.defense)) * a *  accuracy
    return damage

#calculate the damage dealt by the player
def player_damage():
    move_used = select_move()
    player_damage = damage(tauros_1, tauros_2, move_used)
    tauros_2.hp -= player_damage
    enemy_damage_message = f'The enemy Tauros took {player_damage} damage from {move_used.name.title()} and is at {tauros_2.hp} HP\n'
    if player_damage != 0:
        print(enemy_damage_message)
    else:
        player_miss_message = f'Your Tauros missed their {move_used.name.title()}, oh no!\n'
        print(player_miss_message)
    return move_used.recharge

#calculate the damage dealt by the enemy
def enemy_damage():
    enemy_move_selector = enemy_move()
    enemy_damage = damage(tauros_1, tauros_2, moveList[enemy_move_selector])
    tauros_1.hp -= enemy_damage
    if enemy_damage != 0:
        player_damage_message = f'Your Tauros took {enemy_damage} damage from {moveList[enemy_move_selector].name.title()} and is at {tauros_1.hp} HP\n'
        print(player_damage_message)
    else:
        enemy_miss_message = f'The enemy Tauros missed their {moveList[enemy_move_selector].name.title()}, bop!\n'
        print(enemy_miss_message)

def run_game(pokemon_1, pokemon_2):
    #the loop runs when both mons are alive, the following conditions check whether only one of the two died, and the final condition randoms the loser
    #(since they have the same speed)
    while pokemon_1.hp > 0 and pokemon_2.hp > 0:
        game_loop()
        if pokemon_1.hp <= 0 and pokemon_2.hp <= 0:
            death = random.randrange(2)
            if death == 0:
                print('You won because you moved first. Lucky and bad')
            else:
                print('You moved second and lost. Sad!')
        elif pokemon_1.hp <= 0 and pokemon_2.hp > 0:
            print('You lost')
        elif pokemon_1.hp > 0 and pokemon_2.hp <= 0:
            print('You won')

def game_loop():
    #here is where the magic should happen I think
    #if skip_turn is false for both:
    player_damage()
    enemy_damage()
    #elif skip turn is false only for the player:
    player(damage)
    print('Enemy Tauros recharging')
    #elif skip turn is false only for the enemy:
    enemy_damage()
    print('Your Tauros is recharging')
    #else
    print('Your Tauros is recharging')
    print('The enemy Tauros is recharging')

#runs the game
run_game(tauros_1, tauros_2)

0 个答案:

没有答案