import random
import time
import pygame
import sys
from pygame.locals import *
SCREEN_SIZE = WIDHT, HEIGHT = 640, 480
if __name__ == "__main__":
pygame.init() # 初始化程序
screen = pygame.display.set_mode(SCREEN_SIZE) # 创建主窗口界面
pygame.display.set_caption('Demo') # 创建窗口名称
# 图像填充
background_image = pygame.image.load('/Users/peak/Documents/game/image/Background.png') # 导入图像
font = pygame.font.Font('/Users/peak/Library/Fonts/华康黑体_W9.TTF', 20)
# 显示进入信息
print('* ' * 30)
print(f'{"* " * 11}欢迎进入武侠江湖 {" *" * 11}')
print('* ' * 30)
input(f'{"* " * 11}回车确定开始游戏 {" *" * 11}')
print('* ' * 30)
# 身份注册信息
time.sleep(0.5)
print('请大侠起个名称: ')
name = input('>>> ')
print('* ' * 30)
print(f'欢迎{name}大侠进入《武侠江湖》...')
print('* ' * 30)
# NPC初始化属性
class Npc():
def __init__(self, name, life, attack):
self.name = name
self.life = life
self.attack = attack
def npc(self):
self.life += 50 + (random.randint(50, 100))
self.attack += 5 + (random.randint(5, 10))
wmans = ['钟灵', '任盈盈', '曲非烟', '苗若兰', '王语嫣', '小龙女']
wman_result = random.choice(wmans)
n = Npc(wman_result, random.randint(600, 1200), random.randint(20, 40))
# 玩家初始化属性
class Player(Npc):
def __init__(self, name, life, attack):
self.name = name
self.life = life
self.attack = attack
def player(self):
self.life += 50 + (random.randint(50, 100))
self.attack += 5 + (random.randint(5, 10))
print(f'{p.name}升级了!'
f'\t生命增加{self.life}'
f'\t攻击增加{self.attack}')
print('* ' * 30)
n.npc()
p = Player(name, random.randint(400, 800), random.randint(15, 30))
# 招式
class Martial(Player, Npc):
def __init__(self, p_martial, n_martial):
self.p_martial = p_martial
self.n_martial = n_martial
def p_battle(self):
p.life -= n.attack + random.randint(2, 5)
print(
f'【{p.name}】出手一招 {random.choice(m.p_martial)} 攻击了【{n.name}】受到了{p.attack + random.randint(2, 5)}点伤害!')
def n_battle(self):
n.life -= p.attack + random.randint(2, 5)
print(
f'【{n.name}】反击一招 {random.choice(m.n_martial)} 攻击了【{p.name}】受到了{n.attack + random.randint(2, 5)}点伤害!')
time.sleep(0.5)
player_martial = [' [大日如来] ', ' [猴子偷桃] ', ' [偷天换日] ', ' [倒挂金钟] ', ' [一指探穴] ']
npc_martial = [' [黑虎掏心] ', ' [雪媚横生] ', ' [午夜聊骚] ', ' [官人来搞] ', ' [环龙抱月] ']
m = Martial(player_martial, npc_martial)
# 装备
class Arms():
def __init__(self, weapons):
self.weapons = weapons
def equip(self):
print(f'装备: 1.木剑\t2.铁剑\t3.青铜剑')
eq = input('>>> ')
if eq == '1':
p.attack += 5
print(f'你装备上了 {self.weapons[0]}, 攻击力 +{5}')
print('* ' * 30)
elif eq == '2':
p.attack += 10
print(f'你装备上了 {self.weapons[1]}, 攻击力 +{10}')
print('* ' * 30)
elif eq == '3':
p.attack += 20
print(f'你装备上了 {self.weapons[2]}, 攻击力 +{20}')
print('* ' * 30)
arm_weapons = ['木剑', '铁剑', '青铜剑']
arms = Arms(arm_weapons)
# 物品掉落
goods = []
class Drop(Arms):
def __init__(self):
self.goods = goods
def arms(self):
if random.randint(1, 5) == 1:
d.goods.append(arms.weapons[0])
print(f'你获取一把 {arms.weapons[0]}')
print('* ' * 30)
return
elif random.randint(1, 10) == 1:
d.goods.append(arms.weapons[1])
print(f'你获取一把 {arms.weapons[1]}')
print('* ' * 30)
return
elif random.randint(1, 20) == 1:
d.goods.append(arms.weapons[2])
print(f'你获取一把 {arms.weapons[2]}')
print('* ' * 30)
return
d = Drop()
font_height = font.get_linesize() # 获取字体高度
list_texts = [] # 创建一个文字列表
while True:
event = pygame.event.wait() # 如果没有事件会滞停
list_texts.append(str(event)) # 把接收的日志转成字符串,存入列表中
event_text = list_texts[int(-SCREEN_SIZE[1] / font_height): 0: 1] # 将列表中的字符串切片
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(background_image, (0, 0)) # 图像填充到背景(背景变量,(位置元组))
y = SCREEN_SIZE[1] - font_height # 窗口高度 - 字体高度
for text in reversed(list_texts):
screen.blit(font.render(text, True, (0, 0, 50)), (10, y))
y -= font_height
pygame.display.update() # 刷新界面
while 0 < p.life and 0 < n.life:
# 显示游戏选项
print(f'\t[玩家]生命值: {p.life}\t攻击力: {p.attack}')
print(f'\t[电脑]生命值: {n.life}\t攻击力: {n.attack}')
print(f'{name}大侠, 请选择进行的操作:\n\t1.练级\n\t2.打BOSS\n\t3.装备\n\t4.退出游戏')
game_choose = input('>>> ')
print('* ' * 30)
# 玩家升级操作
if game_choose == '1':
p.player() # 玩家升级
d.arms() # 物品掉落
# 双方战斗信息
elif game_choose == '2':
while 0 < p.life and 0 < n.life:
m.p_battle()
m.n_battle()
# 判断NPC是否死亡
if n.life <= 0:
print(f'【{p.name}】一招 {random.choice(m.p_martial)} 戳中【{n.name}】,重伤不治死了!')
print(f'【{p.name}】大声说道: 来生再见吧!')
break
# 判断玩家是否死亡
if p.life <= 0:
print(f'【{n.name}】一招 {random.choice(m.n_martial)} 把【{p.name}】勒死了!')
print(f'【{n.name}】轻视一眼, 大声喊道: 你给我下地狱吧!')
break
# 背包
elif game_choose == '3':
arms.equip()
# 逃跑退出游戏
elif game_choose == '4':
print(f'【{p.name}】一扭头,撒腿就跑!')
print(f'【{n.name}】喊道:小样就那么不经打,看你还敢不敢来!')
print(f'【{n.name}】轻视一眼: 有缘江湖再见!')
break
else:
print('您的输入有误,请重新输入!')
print('* ' * 30)