hnldyxx551 发表于 2020-4-5 12:10:19

作业要求rpg游戏,但是只code到了斗士和法师,进阶职业不知道该怎么办了

本帖最后由 hnldyxx551 于 2020-4-5 12:17 编辑

开局会给你800块买英雄,每回合买一个,每个职业的价格都不一样。
我已经把游戏的py还有队伍的py都编程好了。就是角色不知道该怎么code。麻烦大佬看一下
DEBUG_PRINT = True
def dprint(s):
if DEBUG_PRINT:
    print(s)

### CONSTANT FOR MAGE
MANA_COST = 20
MANA_RECOVERY = 30


### BASE CHARACTER
class Character():
def __init__(self):
    self.name = ''
    self.maxhp   = 800
    self.hp      = 800
    self.maxmana = 0
    self.mana    = 0
    self.str   = 0
    self.int   = 0
    self.cost    = 9999999999
    self.alive   = True

def act(self, my_team, enemy):
    return

def got_hurt(self, damage):
    if damage >= self.hp:
      self.hp = 0
      self.alive = False
      dprint(self.name + ' died!')
    else:
      self.hp -= damage
      dprint(self.name +
         f' hurt with remaining hp {self.hp}.')


### FIGHTER
class Fighter(Character):
def __init__(self):
    super().__init__()
    self.name= 'Fighter'
    self.maxhp = 1200
    self.hp    = 1200
    self.str   = 100
    self.cost= 100

def act(self, my_team, enemy):
    target = rand_alive(enemy)
    dprint(f'Hurt enemy {target} by damage {self.str}.')
    enemy.got_hurt(self.str)


### MAGE
class Mage(Character):
def __init__(self):
    super().__init__()
    self.name    = 'Mage'
    self.maxmana = 50
    self.mana    = 50
    self.cost    = 200
    self.int   = 400

def cast(self, my_team, enemy):
    self.mana -= MANA_COST
    target = rand_alive(enemy)
    dprint(f'Strike enemy {target} with spell')
    enemy.got_hurt(self.int)
   
def act(self, my_team, enemy):
    if self.mana < MANA_COST:
      self.mana += MANA_RECOVERY
      dprint(f'Mana recover to {self.mana}.')
    else:
      self.cast(my_team, enemy)
### BERSERKER


### ARCHMAGE


### NECROMANCER

狂战士的要求是A Berserker is a Fighter but he costs 200 gold.If his hp is lower than or equal to half of his maximum hp, he will enter the “berserk mode”.This doubles his strength to 200.
秘术师的要求是An ArchMage is a Mage but he costs 600 gold.If he is the only one alive in his own team, he will cast the special spell KABOOM and it inflicts EVERY enemy alive with a damage of double of his intelligence.This KABOOM spell also requires 20 mana to cast.
通灵师的要求是An Necromancer is a Mage but he costs 400 gold.If there are some dead members in his own team, he can cast a spell RAISE DEAD costing 20 mana to revive a random dead team member and recover half of the revived member maximum hp rounded down instead of hurting his enemy this turn.

hnldyxx551 发表于 2020-4-5 12:13:10

编程完之后差不多就是这样子。麻烦大佬可以帮一下忙
Your enemy will be:
Members:   |       Mage|    Fighter|    Fighter|       Mage|Berserker
Hitpoints: |      800|       1200|       1200|      800|       1200
Strength:|         |      100|      100|         |      100
Max. Mana: |         50|         |         |         50|         
Currnet M: |         50|         |         |         50|         

Your current team:
(Currently no member in the team now)

You have 800 gold currently
Choices:
1: Fighter (cost: 100)
2: Mage (cost: 200)
3: Berserker (cost: 200)
4: ArchMage (cost: 600)
5: Necromancer (cost: 400)
Input a choice from 1 to 5:
Your current team:
Members:   |    Fighter
Hitpoints: |       1200
Strength:|      100
Max. Mana: |         
Currnet M: |         

You have 700 gold currently
Choices:
1: Fighter (cost: 100)
2: Mage (cost: 200)
3: Berserker (cost: 200)
4: ArchMage (cost: 600)
5: Necromancer (cost: 400)
Input a choice from 1 to 5:
Your current team:
Members:   |    Fighter|       Mage
Hitpoints: |       1200|      800
Strength:|      100|         
Max. Mana: |         |         50
Currnet M: |         |         50

You have 500 gold currently
Choices:
1: Fighter (cost: 100)
2: Mage (cost: 200)
3: Berserker (cost: 200)
4: ArchMage (cost: 600)
5: Necromancer (cost: 400)
Input a choice from 1 to 5:
Your current team:
Members:   |    Fighter|       Mage|Necromancer
Hitpoints: |       1200|      800|      800
Strength:|      100|         |         
Max. Mana: |         |         50|         50
Currnet M: |         |         50|         50

You have 100 gold currently
Choices:
1: Fighter (cost: 100)
2: Mage (cost: 200)
3: Berserker (cost: 200)
4: ArchMage (cost: 600)
5: Necromancer (cost: 400)
Input a choice from 1 to 5:
Your current team:
Members:   |    Fighter|       Mage|Necromancer
Hitpoints: |       1200|      800|      800
Strength:|      100|         |         
Max. Mana: |         |         50|         50
Currnet M: |         |         50|         50

You have 100 gold currently
Choices:
1: Fighter (cost: 100)
2: Mage (cost: 200)
3: Berserker (cost: 200)
4: ArchMage (cost: 600)
5: Necromancer (cost: 400)
Input a choice from 1 to 5:
THE BATTLE STARTS!!!!!

Round 1
Team A:
Members:   |    Fighter|       Mage|Necromancer|    Fighter
Hitpoints: |       1200|      800|      800|       1200
Strength:|      100|         |         |      100
Max. Mana: |         |         50|         50|         
Currnet M: |         |         50|         50|         

Team B:
Members:   |       Mage|    Fighter|    Fighter|       Mage|Berserker
Hitpoints: |      800|       1200|       1200|      800|       1200
Strength:|         |      100|      100|         |      100
Max. Mana: |         50|         |         |         50|         
Currnet M: |         50|         |         |         50|         

Team A member 3 Fighter acts
Hurt enemy 2 by damage 100.
Fighter hurt with remaining hp 1100.

Team A:
Members:   |    Fighter|       Mage|Necromancer|    Fighter
Hitpoints: |       1200|      800|      800|       1200
Strength:|      100|         |         |      100
Max. Mana: |         |         50|         50|         
Currnet M: |         |         50|         50|         

Team B:
Members:   |       Mage|    Fighter|    Fighter|       Mage|Berserker
Hitpoints: |      800|       1200|       1100|      800|       1200
Strength:|         |      100|      100|         |      100
Max. Mana: |         50|         |         |         50|         
Currnet M: |         50|         |         |         50|         

Team B member 4 Berserker acts
Hurt enemy 0 by damage 100.
Fighter hurt with remaining hp 1100.

Round 2
Team A:
Members:   |    Fighter|       Mage|Necromancer|    Fighter
Hitpoints: |       1100|      800|      800|       1200
Strength:|      100|         |         |      100
Max. Mana: |         |         50|         50|         
Currnet M: |         |         50|         50|         

Team B:
Members:   |       Mage|    Fighter|    Fighter|       Mage|Berserker
Hitpoints: |      800|       1200|       1100|      800|       1200
Strength:|         |      100|      100|         |      100
Max. Mana: |         50|         |         |         50|         
Currnet M: |         50|         |         |         50|         

Team A member 0 Fighter acts
Hurt enemy 0 by damage 100.
Mage hurt with remaining hp 700.

Team A:
Members:   |    Fighter|       Mage|Necromancer|    Fighter
Hitpoints: |       1100|      800|      800|       1200
Strength:|      100|         |         |      100
Max. Mana: |         |         50|         50|         
Currnet M: |         |         50|         50|         

Team B:
Members:   |       Mage|    Fighter|    Fighter|       Mage|Berserker
Hitpoints: |      700|       1200|       1100|      800|       1200
Strength:|         |      100|      100|         |      100
Max. Mana: |         50|         |         |         50|         
Currnet M: |         50|         |         |         50|         

Team B member 1 Fighter acts
Hurt enemy 0 by damage 100.
Fighter hurt with remaining hp 1000.

hnldyxx551 发表于 2020-4-5 12:16:14

这个是整个游戏的py文件
from characters import *
from random import randint
from team import *

# You increase nCharType one at a time
NUM_CHAR = 2    # no. of types of characters you can choose
GOLD   = 200# Gold for you to recruit characters
MIN_COST = 100# A hack for user_choose_team()

'''
Type:
1: Fighter (Given)
2: Mage (Given)
3: Berserker
4: ArchMage
5: Necromancer
'''

def create_char(i):
if i == 1:
    return Fighter()
elif i == 2:
    return Mage()
# UNCOMMENT ONCE YOU HAVE IMPLEMENTED THE NECESSARY CLASS
#   elif i == 3:
#   return Berserker()
#   elif i == 4:
#   return ArchMage()
#   elif i == 5:
#   return Necromancer()


def create_rand_team(gold):
team = []
while gold > 0:
    tempChar = create_char(randint(1, NUM_CHAR))
    if gold >= tempChar.cost:
      gold -= tempChar.cost
      team.append(tempChar)   
return team

def user_choose_team(gold):
team = []
choice = -1
while (gold >= MIN_COST) and (gold != 0) :
    print("\nYour current team:")
    print_stat(team)
    print(f'\nYou have {gold} gold currently')
    choice = -1
    while choice < 1 or choice > NUM_CHAR:
      print("Choices:")
      print(f"1: Fighter (cost: {Fighter().cost})")
      print(f"2: Mage (cost: {Mage().cost})")
    # UNCOMMENT ONCE YOU HAVE IMPLEMENTED THE NECESSARY CLASS
    #   print(f"3: Berserker (cost: {Berserker().cost})")
    #   print(f"4: ArchMage (cost: {ArchMage().cost})")
    #   print(f"5: Necromancer (cost: {Necromancer().cost})")
      choice = int(input(f'Input a choice from 1 to {NUM_CHAR}:'))
      if choice < 1 or choice > NUM_CHAR:
      print(f"Your choice {choice} is not valid. Please choose again")
    if choice > 0 and choice <= NUM_CHAR:
      temp_char = create_char(choice)
      if gold >= temp_char.cost:
      gold -= temp_char.cost
      team.append(temp_char)   
return team

def run_battle(team_a, team_b, pause = True):
# Return 0 when Team A win and 1 otherwise.
# There will be no tie

rd = 0
a_turn = True
dprint("")
dprint("THE BATTLE STARTS!!!!!")

while (not all_dead(team_a)) and not all_dead(team_b):
    dprint('')
   
    if a_turn:
      attacker_team = team_a
      defender_team = team_b
      rd += 1
      dprint(f'Round {rd}')
    else:
      attacker_team = team_b
      defender_team = team_a

    if DEBUG_PRINT:
      print("Team A:")
      print_stat(team_a)
      print()
      print("Team B:")
      print_stat(team_b)

    dprint('')
   
    attacker = rand_alive(attacker_team)
    team_s = 'Team A' if a_turn else 'Team B'
      
    dprint(team_s + f' member {attacker} {attacker_team.name} acts')
    attacker_team.act(attacker_team, defender_team)
    a_turn = not a_turn
    if pause:
      input("Press Enter to continue....")
   
if all_dead(team_b):
    dprint("First team won!")
    return 0
else:
    dprint("Second team won!")
    return 1


def game_start(gold,pause = True):
enemy = create_rand_team(gold)
print("Your enemy will be:")
print_stat(enemy)
my_team = user_choose_team(gold)
if run_battle(my_team, enemy, pause) == 0:
    print("Congratz! You won!")
else:
    print("Sorry, you lose")


game_start(GOLD,False) # The second parameter is to wait for each turn

hnldyxx551 发表于 2020-4-5 12:16:45

这个是队伍的py文件
from random import randint
from characters import *

def rand_alive(team):
n = len(team)
r = randint(0,n-1)
return r if team.alive else rand_alive(team)

def rand_death(team):
n = len(team)
r = randint(0,n-1)
return r if not team.alive else rand_death(team)

def count_alive(team):
res = 0
for i in team:
    if i.alive:
      res += 1
return res

def count_dead(team):
res = 0
for i in team:
    if not i.alive:
      res += 1
return res

def all_dead(team):
for i in team:
    if i.alive:
      return False
return True

def all_alive(team):
for i in team:
    if not i.alive:
      return False
return True


LONGEST_NAME = 11

def print_stat(team):

if team == []:
    print("(Currently no member in the team now)")
    return
names = 'Members:   '
hp ='Hitpoints: '
sth='Strength:'
maxm ='Max. Mana: '
mana ='Currnet M: '
for i in team:
    nspace = LONGEST_NAME - len(i.name)
    names += '|' + ' '*nspace   +i.name
    nspace = LONGEST_NAME - len(str(i.hp))
    hp += '|' + ' '*nspace+ str(i.hp)
    if i.str:
      nspace = LONGEST_NAME - len(str(i.str))
      sth += '|' + ' '*nspace+ str(i.str)
    else:
      sth += '|' + ' '*LONGEST_NAME
    if i.maxmana:
      nspace = LONGEST_NAME - len(str(i.maxmana))
      maxm += '|' + ' '*nspace+ str(i.maxmana)
    else:
      maxm += '|' + ' '*LONGEST_NAME
      
    if i.mana:
      nspace = LONGEST_NAME - len(str(i.mana))
      mana += '|' + ' '*nspace+ str(i.mana)
    else:
      mana += '|' + ' '*LONGEST_NAME      
print(names)
print(hp)
print(sth)
print(maxm)
print(mana)

昕颖 发表于 2020-4-5 14:16:45

厉害我都看不懂我才刚刚安装好pygame

hnldyxx551 发表于 2020-4-5 16:44:42

请问有没有人可以帮忙看一下呢

hnldyxx551 发表于 2020-4-5 19:27:43

请问有人可以帮一下忙吗

Twilight6 发表于 2020-4-5 21:59:27

哈哈啾啾

hnldyxx551 发表于 2020-4-6 09:38:51

额,请问有没有大佬帮忙啊

乘号 发表于 2020-4-6 16:53:31

@一个账号 @qiuyouzhi

hnldyxx551 发表于 2020-4-6 20:35:43

请大佬帮帮忙啊

hnldyxx551 发表于 2020-4-7 03:09:45

@一个账号 @qiuyouzhi 请大佬帮帮忙

hnldyxx551 发表于 2020-4-7 13:23:49

我需要大佬帮帮忙啊

hnldyxx551 发表于 2020-4-8 23:02:55

请问有没有人可以帮忙看一下啊

hnldyxx551 发表于 2020-4-10 17:51:49

所以没有大佬会做是吗

boonpython 发表于 2021-11-8 01:45:09

楼主知道答案了吗?能否分享一下?

傻眼貓咪 发表于 2021-11-8 18:33:53

兄弟,激将法没有用的。
很多大佬不是不会做,只是题目代码太长,如果没有针对性的问题,很难回答的。

什么是针对性问题,如:
请问什么意思是内存地址?
请问什么是局部变量?
请问什么是栈溢出?
请问为什么我的代码报错: xxxxxxxxxxxx?

如果不是针对性问题,如:
请帮我做习题
又或代码全部我看不懂(全部?)
又或者发文几百行几千行代码,让别人一行行看(注解都没有,想说别人很闲?)
页: [1]
查看完整版本: 作业要求rpg游戏,但是只code到了斗士和法师,进阶职业不知道该怎么办了