0%

用C#写个小游戏:决斗吧大魔王

最近应付C#作业,写了个网页小游戏,写着写着发现还是蛮有意思的,灵感来源于知乎问答“如何设计一款世界上最无趣的游戏?”下的一个答案,嗯,现在看起来确实有够无聊的,但是我打算后面如果有时间的话把它用Java写一下然后开发多一点功能,技能系统什么的,甚至有想写个安卓客户端的冲动,如果还有余力的话

步骤如下

1.打开VS2015,新建网站
建立文件default.aspx如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" 
Inherits="_default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>小游戏</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
</style>
</head>
<body style="background-image:url(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491491962000&di=a101e2033883077242ea21ad64dc3444&imgtype=0&src=http%3A%2F%2Fh7.86.cc%2Fwalls%2F20160729%2F1920x1200_88f5b02af13e733.jpg)" >
<div class="container">
<div class="col-md-6 column">
<div class="jumbotron">
<form class="form-horizontal" id="form1" runat="server">
<h1>拯救地球<br />消灭大魔王</h1>
<p>
某一天,你在上课的路上遇到一只扬言要毁灭世界的大魔王
</p>
<asp:Button class="btn btn-primary btn-large" ID="Button1" runat="server" OnClick="Button1_Click1" Text="开始" />
<br />
<!-- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> -->
<asp:Button class="btn btn-primary btn-large" ID="Button2" runat="server" OnClick="Button2_Click1" Text="攻击" />
<asp:HiddenField ID="you" runat="server" />
<asp:HiddenField ID="dmw" runat="server" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</div>
</div>
</div>
</body>
</html>

这是基本的界面,然后写后台逻辑

建立default.aspx.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = "请开始你的表演";

you.Value = "100";
dmw.Value = "100";
}
protected void Button2_Click1(object sender, EventArgs e)
{
Random rd = new Random();
dmw.Value = (Convert.ToInt32(dmw.Value) - rd.Next(1, 30))+"";
you.Value = (Convert.ToInt32(you.Value) - rd.Next(1, 30)) + "";
if (Convert.ToInt32(you.Value) > 0 && Convert.ToInt32(dmw.Value) > 0)
{
Label1.Text ="你冲上去就是一拳,大魔王剩下血量为" + dmw.Value + " 愤怒的大魔王反击,你剩下血量为" + you.Value;
}
else if (Convert.ToInt32(you.Value) < 0 && Convert.ToInt32(dmw.Value) > 0)
{
Label1.Text = "你输了,大魔王毁灭了地球";
}
else if (Convert.ToInt32(you.Value) > 0 && Convert.ToInt32(dmw.Value) < 0)
{
Label1.Text = "你赢了,你拯救了地球";
}
else if (Convert.ToInt32(you.Value) < 0 && Convert.ToInt32(dmw.Value) < 0)
{
Label1.Text = "你和大魔王同归于尽了";
}

}
}

逻辑很简单,但是有些地方和Java还是不太一样的,比如asp.net没有全局变量可以用这一点,确实有点不方便。

2.在项目上右键选择生成,这就建成了一个非常简单非常智障的.net网站啦

3.接着在Windows的控制面板里开启IIS功能,设置IP地址和文件夹地址,然后在项目上右键发布,配置发布的配置文件,文件夹地址与上面设置的一致就可以了。

4.如果要让外网耶可以访问,我使用了一位好心人的服务器用ngrok做内网映射,感谢好心人