一个简单的rpg游戏(由Python实现)

这篇文章介绍了如何用Python的Tkinter库开发一个简单的RPG游戏,包括创建角色、地图,随机生成敌人,以及战斗和角色升级机制。玩家可以控制角色移动并与敌人互动。
摘要由CSDN通过智能技术生成

import random
import tkinter as tk

player = {
    "name": "Player",
    "level": 1,
    "health": 100,
    "max_health": 100,
    "attack": 20,
    "magic": 50,
    "max_magic": 50,
    "defense": 10,
    "experience": 0,
    "position": [0, 0]  # 玩家位置坐标
}

monster = {
    "name": "Monster",
    "health": 50,
    "attack": 10,
    "defense": 5,
    "experience": 20
}

elite_monster = {
    "name": "Elite Monster",
    "health": monster["health"] * 2,
    "attack": monster["attack"] * 2,
    "defense": monster["defense"] * 2,
    "experience": monster["experience"] * 2
}

final_boss = {
    "name": "Final Boss",
    "health": monster["health"] * 5,
    "attack": monster["attack"] * 5,
    "defense": monster["defense"] * 5,
    "experience": monster["experience"] * 10
}

map_size = 10  # 地图大小

def create_map(size):
    map_data = []
    for i in range(size):
        row = []
        for j in range(size):
            row.append(None)
        map_data.append(row)
    return map_data

def place_monsters(map_data, num_monsters):
    for _ in range(num_monsters):
        x = random.randint(0, map_size - 1)
        y = random.randint(0, map_size - 1)
        if map_data[x][y] is None:
            if random.random() < 0.01:  # 1%的概率放置最终BOSS
                map_data[x][y] = final_boss.copy()
            elif random.random() < 0.1:  # 10%的概率放置精英怪物
                map_data[x][y] = elite_monster.copy()
            else:
                map_data[x][y] = monster.copy()

def move_player(dx, dy):
    player["position"][0] += dx
    player["position"][1] += dy
    encounter_chance = random.random()
    if encounter_chance < 0.2:  # 20%的概率遭遇战斗
        battle()
    else:
        result_text.insert(tk.END, "You moved to position ({}, {})\n".format(player["position"][0], player["position"][1]))

def attack_animation():
    canvas.move(player_image, 100, 0)
    window.update()
    canvas.after(500)  # 500毫秒后恢复原位
    canvas.move(player_image, -100, 0)
    window.update()

def level_up():
    player["level"] += 1
    player["max_health"] += 10
    player["health"] = player["max_health"]
    player["max_magic"] += 10
    player["magic"] = player["max_magic"]
    player["attack"] += 5

def check_level_up():
    if player["experience"] >= 100 and player["level"] < 50:
        level_up()
        result_text.insert(tk.END, "Congratulations! You leveled up to level " + str(player["level"]) + "!\n")

def battle():
    result_text.insert(tk.END, "You encountered a " + monster["name"] + "!\n")
    if monster == elite_monster:
        result_text.insert(tk.END, "It's an elite monster!\n")
    elif monster == final_boss:
        result_text.insert(tk.END, "It's the final boss!\n")
    player_damage = player["attack"] - monster["defense"]
    if player_damage < 0:
        player_damage = 0
    monster["health"] -= player_damage
    result_text.insert(tk.END, "You attacked " + monster["name"] + " for " + str(player_damage) + " damage.\n")
    
    # Check if monster is defeated
    if monster["health"] <= 0:
        result_text.insert(tk.END, "You defeated " + monster["name"] + " and gained " + str(monster["experience"]) + " experience.\n")
        player["experience"] += monster["experience"]
        check_level_up()
        monster["health"] = 50
    
    # Monster's turn
    monster_damage = monster["attack"] - player["defense"]
    if monster_damage < 0:
        monster_damage = 0
    player["health"] -= monster_damage
    result_text.insert(tk.END, monster["name"] + " attacked you for " + str(monster_damage) + " damage.\n")
    
    # Check if player is defeated
    if player["health"] <= 0:
        result_text.insert(tk.END, "You were defeated by " + monster["name"] + "\n")

# Create the main window
window = tk.Tk()
window.title("RPG Game")

# Create the UI elements
welcome_label = tk.Label(window, text="Welcome to the RPG game!")
welcome_label.pack()

name_label = tk.Label(window, text="Enter your name:")
name_label.pack()

name_entry = tk.Entry(window)
name_entry.pack()

move_button = tk.Button(window, text="Move Up", command=lambda: move_player(0, -1))
move_button.pack()

move_button = tk.Button(window, text="Move Down", command=lambda: move_player(0, 1))
move_button.pack()

move_button = tk.Button(window, text="Move Left", command=lambda: move_player(-1, 0))
move_button.pack()

move_button = tk.Button(window, text="Move Right", command=lambda: move_player(1, 0))
move_button.pack()

result_text = tk.Text(window, height=10, width=50)
result_text.pack()

# Create the canvas for animation
canvas = tk.Canvas(window, width=400, height=200)
canvas.pack()

# Load player image
player_image = canvas.create_rectangle(50, 50, 100, 100, fill="blue")

# Create the map
map_data = create_map(map_size)
place_monsters(map_data, 10)  # 在地图上随机放置10个怪物

# Start the game loop
window.mainloop()

  • 29
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值