Scala天天酷跑小游戏
需求分析:
- 实现《天天酷跑》的核心逻辑,该游戏具有登录、注册、玩家操作、结束等功能。
- 注册模块:需要玩家身份信息为:用户名 密码 昵称 邮箱
- 登录模块:需要玩家信息为:用户名 和 密码
- 玩家(Person)功能:跳跃up(y坐标的改变),右移功能(right),左移功能left(修 改x的坐 标),判断玩家是否越界的功能。玩家具有得分、跑酷距离、生命值属性。
初始位置坐标为(x,y),补充自由下落功能(注意y坐标的临界点),login方法,注册方法,(建议可以通过在控制台内输入w a d 或者 2 6 8来控制玩家的坐标移动)- 障碍物:障碍物从右向左自动进场,y坐标固定,x坐标左移,个数随机, 间隔 某个时间段 会创建一个障碍物,障碍物的越界问题
- 逻辑层代码:实现玩家和障碍物碰撞的逻辑,结束的逻辑
代码结构
class Person(){val width=2 //2*2的矩阵val height=2var locationX=9 //初始位置(9,1)var locationY=1var jumpCount=0 // 最多允许二段跳var HP=5var score:Int=0var speedX=0 //单位char/svar speedY=0def commandP():Unit={} //读入w,a,d移动指令def move():Unit={} //计算位移def crashed(o:obstruct):Int={} //判断撞击障碍物def crashMoney(m:money):Int={} //判断撞击金币
}
class obstruct(){var obstructList=ListBuffer[ListBuffer[Int]]()// [0:“x”,1:"y",2:"speedX",3:"speedY",4:"weight",5:"height"],障碍物列表def move(): Unit ={}def appendObject(): Unit ={}def deleteObject(): Unit ={}
}
class money(){val length:Int=1val weight:Int=1val point=20000 //一个金币两万分var moneyList=ListBuffer[ListBuffer[Int]]()// [0:“x”,1:"y",2:"speedX",3:"speedY"] //金币列表def move(): Unit ={}def appendObject(): Unit ={}def deleteObject(): Unit ={}
}
class background() {val height:Int=30 //100*30字符位组成的游戏界面val width:Int=100def draw(o:obstruct,p:Person,m:money): Unit = {} //ui界面函数
}
class gameThread extends Thread {var p=new Person()var o=new obstruct()var m=new money()var bg=new background()var timecount:Float=0 //单位:svar gamemode=1 //游戏开始override def run() {}
}
class account(){var currentName=""var account=ListBuffer[Map[String,String]](Map("用户名"->"root","密码"->"000","昵称"->"god","邮箱"->"8848@qq.com"))def register(){}def search(username:String,password:String) :Boolean ={}def login(): Boolean ={}
}
object game { //主类def main(args:Array[String]):Unit = {}
}
详细代码
package _4_14实验三
import scala.io.StdIn
import scala.collection.mutable.Map
import scala.collection.mutable.ListBuffer
class Person(){val width=2 //2*2的矩阵val height=2var locationX=9 //初始位置(9,1)var locationY=1var jumpCount=0 // 最多允许二段跳var HP=5var score:Int=0var speedX=0 //单位char/svar speedY=0def commandP(): Unit ={val command = StdIn.readLine()command match {case "w" => {if(jumpCount<2){speedY=15 //向上腾起jumpCount+=1}}case "a" => {speedX=(-15)}case "d" => {speedX=15}case _ =>{//无操作}}}def move(): Unit ={locationX=locationX+(speedX.toFloat*0.5).toInt //det(x)=速度v*det(t),小区间内近似看作匀速运动locationY=locationY+(speedY.toFloat*0.5).toIntprintln("person(x,y):"+locationX,locationY)if(locationY<0){//println("越界了")locationY=0speedY=0jumpCount=0} //越界处理if(locationX>100){locationX=100}if(locationX<0){locationX=0}//区间结束后对加速度做出改变speedY=speedY+((-10.0)*0.5).toInt //det(v)=加速度a*det(t)speedX=0}def crashed(o:obstruct): Int ={println("剩余血量: ",HP)for(item <- o.obstructList){val centerDistanceX=((locationX+width/2)-(item(0)+item(4)/2)).absval centerDistanceY=((locationY+height/2)-(item(1)+item(5)/2)).absif( (centerDistanceX<(width/2+item(4)/2)) && (centerDistanceY<(height/2+item(5)/2)) ){//两个中心点间的x,y方向距离都小于AB边长和的一半 相交println("发生碰撞")HP-=1if(HP<=0){println("碰撞五次,游戏结束!")return 0} //结束游戏else {return 1}}}return 1}def crashMoney(m:money): Int ={for(item <- m.moneyList){if(item(0)>=locationX && item(0)<locationX+width && item(1)>=locationY && item(1)<locationY+height){println("吃到金币")return m.point}}return 0}
}
class obstruct(){var obstructList=ListBuffer[ListBuffer[Int]]()// [0:“x”,1:"y",2:"speedX",3:"speedY",4:"weight",5:"height"]def move(): Unit ={var i=0while(i<obstructList.length){obstructList(i)(0)+=(obstructList(i)(2)*0.5).toInt //det(x)=速度v*det(t),小区间内近似看作匀速运动 障碍物只做横向运动i+=1}}def appendObject(): Unit ={if(scala.util.Random.nextInt(10)>=7){ //每0.5秒有30%概率生成一个障碍物val randomY=scala.util.Random.nextInt(30)//val randomSpeed= -(scala.util.Random.nextInt(10)+10)val randomWidth=scala.util.Random.nextInt(5)+4 //宽度随机1-3val randomHeight=scala.util.Random.nextInt(10)+5 //高度随机2-4obstructList.append(ListBuffer(100,randomY,-15,0,randomWidth,randomHeight))}}def deleteObject(): Unit ={var i=0while(i<obstructList.length){if(obstructList(i)(0)<=0) { //越界,删除障碍物obstructList.remove(i)}i+=1}}
}
class money(){val length:Int=1val weight:Int=1val point=20000 //一个金币两万分var moneyList=ListBuffer[ListBuffer[Int]]()// [0:“x”,1:"y",2:"speedX",3:"speedY"]def move(): Unit ={var i=0while(i<moneyList.length){moneyList(i)(0)+=(moneyList(i)(2)*0.5).toInt //det(x)=速度v*det(t),小区间内近似看作匀速运动 障碍物只做横向运动i+=1}}def appendObject(): Unit ={if(scala.util.Random.nextInt(100)>=65){ //每0.5秒有40%概率生成一个金币val randomY=scala.util.Random.nextInt(3)moneyList.append(ListBuffer(100,randomY,-15,0))}}def deleteObject(): Unit ={var i=0while(i<moneyList.length){if(moneyList(i)(0)<=0) { //越界,删除金币moneyList.remove(i)}i+=1}}
}
class background() {val height:Int=30 //100*30字符位组成的游戏界面val width:Int=100def draw(o:obstruct,p:Person,m:money): Unit = {var i = 0var j = 0var CharList=ListBuffer.fill(height)(ListBuffer.fill(width)(' '))while (i < height) { //给游戏界面,画上边界//左右侧CharList(i)(0) = '*'CharList(i)(99)= '*'i+=1}while (j < width) {//上下侧CharList(0)(j) = '*'CharList(29)(j) = '*'j+=1}for (e <- o.obstructList) { //画障碍物 obstructList[][0:“x”,1:"y",2:"speedX",3:"speedY",4:"weight",5:"height"]i = e(1)j = e(0)while (i < e(1) + e(5) && i < height) {j = e(0)while (j < e(0) + e(4) && j < width) {CharList(i)(j)='|'j += 1}i += 1}}for (e <- m.moneyList) { //画金币 moneyList[][0:“x”,1:"y",2:"speedX",3:"speedY",4:"weight",5:"height"]if(e(0)<100 && e(1)<30){CharList(e(1))(e(0))='$'}}i = p.locationYj = p.locationXwhile (i < p.locationY + p.height && i < height) { //画人j = p.locationXwhile (j < p.locationX + p.width && j < width) {CharList(i)(j) = 'P'j += 1}i += 1}i=0while (i < height) {j=0while (j < width){printf("%c",CharList(29-i)(j))j+=1}printf("\n")i+=1}}
}class gameThread extends Thread {var p=new Person()var o=new obstruct()var m=new money()var bg=new background()var timecount:Float=0 //单位:svar gamemode=1 //游戏开始override def run() {while(gamemode==1) {timecount=(timecount+0.5).toFloatbg.draw(o,p,m)//绘画ui界面println("计时: "+timecount+"s")p.score +=500 //每0.5秒500分p.score +=p.crashMoney(m)println("得分: "+p.score+" 分")p.commandP()p.move()o.appendObject()o.move()o.deleteObject()m.appendObject()m.move()m.deleteObject()gamemode=p.crashed(o)//Thread.sleep(500)}}
}
class account(){var currentName=""var account=ListBuffer[Map[String,String]](Map("用户名"->"root","密码"->"000","昵称"->"god","邮箱"->"8848@qq.com"))def register(){println("请输入你的用户名:")val username =StdIn.readLine()println("请输入你的密码")val password=StdIn.readLine()println("请输入你的昵称:")val nickname =StdIn.readLine()println("请输入你的邮箱")val email=StdIn.readLine()account.append(Map("用户名"->username,"密码"->password,"昵称"->nickname,"邮箱"->email))println("用户"+username+"注册成功!请重新登录")}def search(username:String,password:String) :Boolean ={for (e <- account){if (e.get("用户名").getOrElse("Null") == username && e.get("密码").getOrElse("Null") == password) {return true}}return false //登陆失败}def login(): Boolean ={println("请输入你的用户名:")val username =StdIn.readLine()currentName=usernameprintln("请输入你的密码")val password=StdIn.readLine()if(search(username,password)==false){println("密码错误,请重新登录")return false}else{println("欢迎 "+username+" 进入游戏!")return true}}
}
object game {def main(args:Array[String]):Unit = {val account = new account()var gamemode = 0 //游戏运行状态var loginmode = 1 //登录状态while (loginmode == 1) {println("------------------------------------------------\n" +" 欢迎来到天天酷跑 \n" +" 输入(1)进行登录 \n" +" 输入(2)进行注册 \n" +" 输入(#)退出游戏 \n" +"------------------------------------------------\n")StdIn.readLine() match {case "1" => {if (account.login()==false) {loginmode=1gamemode=0}else{println(account.currentName)loginmode=0 //跳出登录循环界面gamemode=1 //运行游戏}}case "2" => {account.register()loginmode=1}case "#" => {println("正在关闭天天酷跑...")return}case _ =>{println("输入非法,请检查后重新输入")loginmode=1}}if(gamemode == 1) {val Timigame=new gameThread()Timigame.start()}}}
}
效果图
Scala天天酷跑小游戏相关推荐
- 天天酷跑小游戏,Python帮你实现
天天酷跑小游戏 首先安装PYcharm软件,python3.7解释器 先展示效果图如 分享代码: 首先导入库: #库定义: import pygame,sys import random #游戏配置 ...
- 天天酷跑php源码_Java实现天天酷跑小游戏完整代码(附源码)
首先,写一个需求文档: 一.项目名称:<天天酷跑>(RunDay) 二.功能介绍: 闯关类游戏,玩家登录后,选择进入游戏,通过键盘控制玩家的上下左右移动,来躲避 障碍物和吃金币,玩家躲避的 ...
- C语言仿天天酷跑小游戏
前言:全文代码是模仿程序员rock开发的游戏代码思路,如果大家想根据视频一步步进行制作可以直接去b站进行搜索.全文代码主要分为两个部分,一个是完整版的开发代码(博主根据视频进行一步一步制作而成,素材也 ...
- 天天酷跑电脑版代码C语言,Java实现天天酷跑小游戏完整代码(附源码).pdf
Java实实现现天天天天酷酷跑跑小小游游戏戏完完整整代代码码(附附源源码码) 首首先先,,写写一一个个需需求求文文档档:: 一.项目名称: <天天酷跑> (RunDay ) 二.功能介绍: ...
- 牛逼,两百行Python代码带你打造一款《天天酷跑》游戏!
公众号关注 "菜鸟学Python" 第431篇原创,设为 "星标",带你一起学编程! 最近一段时间,小编发现已经好久没有给大家带来趣味游戏的案例展示了.刚好小编 ...
- 天天酷跑Python
天天酷跑小游戏 (Python) 目 录 一.选题背景----------------1 二.设计流程----------------2 2.1算法思想------------------------ ...
- 【C语言项目】——天天酷跑
C语言项目--天天酷跑 文章目录 C语言项目--天天酷跑 前言 注意事项 源代码分享 效果展示 总结 前言 自学编程最有效的就是通过一些自己感兴趣的项目去学习,如果只是纯粹的听取知识点很难坚持,在项目 ...
- 【原创】IOS游戏辅助--天天酷跑助手的实现
标 题: [原创]IOS游戏辅助--天天酷跑助手的实现 作 者: coltor 时 间: 2014-04-16,20:20:44 链 接: http://bbs.pediy.com/showthrea ...
- Android版xx助手之天天酷跑外挂详细分析
Android版xx助手之天天酷跑外挂详细分析 图/文 莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也开 ...
最新文章
- 阮一峰在 GitHub 又一开源力作!
- AI基础:特征工程-数字特征处理
- bashrc文件实例
- 信息学奥赛一本通(1237:求排列的逆序数)
- 西双版纳真的适合养老吗?
- vue.js 添加 fastclick的支持
- 【leetcode 简单】 第六十六题 用栈实现队列
- VC6保姆级图文教程
- 怎么把ide改成ahci_无需重装操作系统,IDE模式轻松改成AHCI模式
- Jesd204b中的参数M
- The server time zone value 'XXXXXXXXX' is unrecognized or represents more tha
- 安消一体智能分析终端服务器
- Day082 数据分析案例
- Axure RP Chrome插件安装
- 网站压力测试工具was
- Java中有序与无序
- 【渝粤教育】21秋期末考试国际私法10216k1
- pos机骗局收取押金如何投诉-真实案列解答
- python里range什么意思_python里range什么意思
- Xshell无法启动问题解决