从零开始制作一款游戏

从零开始制作一款游戏

1.开发环境

一个编辑器(Visual Studio Code),Powershell和Python。
我花了一些时间来纠结使用C++还是Python,最后我选择了Python,毕竟Python更简短。
我到https://www.python.org/downloads/release/python-381/下载最新版的Python,然后安装(别忘了把它添加进PATH路径里面去)
接下来,我去https://code.visualstudio.com/下载Visual Studio Code。
完事之后我的界面是这样的:
图1.1

2.基本架构

我打算把我的游戏取名为DarkRoom,在这个游戏里,玩家需要用自己的智慧闯过一个又一个房间,最后逃出这个地方。
我想象中的房间有这些:
MainRoom:主屋,玩家出生在这里
BearRoom:玩家在这里会遇到一只熊
BoxRoom:有很多箱子,玩家需要猜出那个箱子里有宝贝(猜不出来就会有一支箭射到玩家身上)
ForestRoom:这里有很多蛇,玩家需要爬到一棵没有蛇的树上
DarkRoom:所有危机大集合
LastRoom:玩家会从这里逃出去
这也只是基本的架构,以后还会加一些东西。

3.代码

首先创建一个Rooms.py文件,用来把各个类和函数放在里面。

#Rooms.py
from sys import exit
from random import randint
def death():
    ways = ["You have nothing to eat!\n",
            "Your joke is less funny than your father's!\n",
            "BOOOOOOOOOOOOOOOOM!\n",
            "You die because you are too poor.\n",
            "Your Mum kill you because you haven't do your homework yet.\n"]
    print(ways[int(randint(0,4))])
    exit(0)

我首先写的就是这个奇怪的Death场景,使用randint随机输出一个死亡原因。
然后是各个房间的代码。

#Rooms.py
class MainRoom(object):
    def begin():
        print("Welcome to the DarkRoom-I!\nIt's dark all around you")
        print("You: Aaaah! Where am I? Wait, I maybe in the…")
        print("GAME START")
        print("""Now you are in the MainRoom.
You can [go] to the next room or [sleep].
Input 'go' or 'sleep' to choose.\n""")
        answer = input("Player:\n")
        if answer == "go":
            print("PASS")
            BearRoom.begin()
        elif answer == "sleep":
            death()

首先是这个初始的房间,玩家会在这里出生。
接下来就是其他房间的了。

class BearRoom(MainRoom):
    def begin():
        print("""Now a bear is in the middle of the room.
It walks to you slowly.\tSuddenly, it runs to you.
You can [catch] it or [run] away.\n""")
        answer = input("Player:\n")
        if answer == "catch":
            print("PASS")
            BearRoom.next()
        elif answer == "run":
            death()
    def next():
        print("""You kill the bear.\tGood!
Now there are two doors. One is made with gold. One is made with wood.
Would you like to open the [gold] door or the [wood] door.\tBe careful!\n""")
        answer = input("Player:\n")
        if answer == "gold":
            death()
        elif answer == "wood":
            print("PASS")
……

运行结果

我在玩一个小的冒险游戏,我玩的不怎么样

Welcome to the DarkRoom-I!
It's dark all around you
You: Aaaah! Where am I? Wait, I maybe in the…
GAME START
Now you are in the MainRoom.
You can [go] to the next room or [sleep].
Input 'go' or 'sleep' to choose.

Player:
go
PASS
Now a bear is in the middle of the room.
It walks to you slowly. Suddenly, it runs to you.
You can [catch] it or [run] away.

Player:
catch
PASS
You kill the bear.      Good!
Now there are two doors. One is made with gold. One is made with wood.
Would you like to open the [gold] door or the [wood] door.      Be careful!

Player:
gold

YOU LOSE!
You have nothing to eat!

剩下的代码大家可以去我的GitHub上看。
网址在这里:https://github.com/ccboy522/DarkRoom-I

就写到这里,次饭去喽
发布了4 篇原创文章 · 获赞 2 · 访问量 332

猜你喜欢

转载自blog.csdn.net/ccboy522/article/details/103841724
今日推荐