VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网站开发 > ASPnet >
  • asp.net教程之用ASP实现聊天室中的在线答题游戏

主要功能为:在聊天室中,每隔5分钟,向显示对话页面,发送一道题,所有用户都可答题。 
答对者加相应的分值。 
具体实现:在聊天室框架中加入一隐含帧, 
此帧每5分钟刷新一次。 
隐含帧内的asp页内容如下: 
<%@ language=vbscript%> 
<%response.expires=0%> 
<% 
dim conn,rs 
application.lock 
application("canswername")=application("answername") '保存答对题用户的名字 
application("answername")="" 
application("tanswername")="" '清除答错题用户的名子 
set conn=server.createobject("adodb.connection") 
accessdb="db1.mdb" '题库的连接 
mydsn="driver={microsoft access driver (*.mdb)};" 
mydsn=mydsn & "dbq=" &server.mappath(accessdb) 
conn.open mydsn 
set rs = server.createobject("adodb.recordset") 
rs.cursorlocation=3 
rs.open "select * from question ",conn 
randomize '随机取得一道题的id 
r=rs.recordcount 
id=int((r-1)*rnd)+1 
rs.close 
set rs=nothing 
set rs = server.createobject("adodb.recordset") 
rs.open "select * from question where questionid="&id,conn 
application("questionname")=rs("questionname") '题目 
application("questionid")=rs("questionid") '题的id 
application("questioncent")=rs("questioncent") '题的分数 
application("questionclass")=rs("questionnotes")'题的说明 
rs.close 
set rs = nothing 
set ra = server.createobject("adodb.recordset") 
ra.open "select * from answer where questionid="&id,conn 
application("answera")=ra("answera") '题的选项 
application("answerb")=ra("answerb") 
application("answerc")=ra("answerc") 
application("answerd")=ra("answerd") 
application("answertrue")=ra("answertrue") '题的正确答案 
ra.close 
set ra=nothing 
conn.close 
set conn=nothing 
application("questiontime")=now() 
application.unlock 
%> 
<html> 
<head> 
<meta http-equiv='content-type' content='text/html; charset=gb2312'> 
<link rel='stylesheet' href='../style.css' type='text/css'> 
<meta http-equiv="refresh" content="300;url=question.asp"> 
<title><%=application("v")%></title> 
<script language="javascript"> 
parent.showsays.document.write("******系统消息:****** 
"); //向聊天室显示对话窗口写入问题 
parent.showsays.document.write("**上题答对者为:<%=application"canswername")%> 
"); 
parent.showsays.document.write("问题::<%=application("questionname")%>"); 
parent.showsays.document.write(" <%=application("questioncent")%>分(1分钟之内,回答输入‘#答案’)<%=time 
()%> 
"); 
parent.showsays.document.write("a::<%=application("answera")%> 
"); 
parent.showsays.document.write("b::<%=application("answerb")%> 
"); 
parent.showsays.document.write("c::<%=application("answerc")%> 
"); 
parent.showsays.document.write("d::<%=application("answerd")%> 
"); 
</script> 
</head> 
<body bgcolor='#ffffff'> 
</body> 
</html> 
答案处理部分,放在处理发送对话信息的程序中即可。下面是答案处理的代码部分: 

if mid(fstr,i,1)="#" then 'fstr是保存对话信息的字符串变量 
tfstr=mid(fstr,i+1,1) 
if cint(datediff("n",application("questiontime"),now()))<=1 then '判断时间1分钟内答题有效 
if lcase(tfstr)=lcase(application("answertrue")) then '判断答题是否正确 
if instr(1,application("answername"),session("user_username"))>0 then '判断用户是否已经答过题了 
tfstr=session("user_username")&"已经答过这道了!!!" 
else 
tfstr=session("user_username")&"答题正确!" 
session("user_exp") =session("user_exp")+application("questioncent") '给答对题的用户加分
application("answername")=application("answername")&session("user_username")&" " '保存答对题用 
户的名字 
end if 
else 
if instr(1,application("tanswername"),session("user_username"))>0 then ''判断用户是否已经答过题 
了 

tfstr=session("user_username")&"已经答过这道了!!!" 
else 
application("tanswername")=application("tanswername")&session("user_username")&" " '保存答错题用 
户的名字 
end if 
end if 
end if

相关教程