当前位置:网站首页>飞机躲子弹小游戏案例
飞机躲子弹小游戏案例
2022-07-21 17:52:00 【weixin_50179990】
飞机躲子弹小游戏案例
如今的网络世界,各种各样的小游戏层出不穷,但随着游戏行业的发展,有的小游戏淡出了玩家们的视野,有的小游戏则被程序员们按照现在玩家的喜好更改为其他的形式,继续陪伴着玩家。虽然我对这些小游戏关注少了,但我童年的玩游记忆并没有淡出。关于这个案例,我记得我在小时候也玩过这样类似的飞机游戏,和我写的不同的是我的游戏里没有敌对飞机,只有子弹。子弹不会消失会在接触窗口最边缘后反弹,玩家必须操作飞机进行移动躲避,直到躲不了了,飞机会被消灭,代码会记住你得游玩时间,在你输了后弹出飞机的存活时间。
用JFrame窗口实现游戏,放上背景图片、飞机图片,再生成炮弹
通过线程睡眠、重画,产生动画。
对飞机的操作
public void drawSelf(Graphics g) {
if(live) {
g.drawImage(img, (int)x, (int)y, null);
if(left) {
x -=speed;
}
if(right) {
x +=speed;
}
if(top) {
y -=speed;
}
if(down) {
y +=speed;
}
}else {
}
}
修改坐标值,进行重画的时候,飞机位置会发生改变。
炮弹飞行角度,和触碰到尽头时炮弹返回的效果
double degree;
public shell() {
x = 200;
y = 200;
width = 10;
height =10;
speed = 3;
degree =Math.random()Math.PI2;
System.out.println(“圆桌率”+degree);
}
public void draw(Graphics g) {
// TODO Auto-generated method stub
Color c =g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, width, height);
x +=speed*Math.cos(degree);
y +=speed*Math.sin(degree);
if(x<0||x>Constant.GAME_WIDTH-width) {
degree = Math.PI-degree;
}
if(y<30||y>Constant.GAME_HIGHT-height) {
degree =-degree;
}
}
炮弹接触飞机使得飞机爆炸
Intersects 会判断两个物体的范围是否重合,重合后返回true,返回飞机的位置,判断为true后,遍历事先准备好的图片,在飞机的位置替换掉飞机的图片,产生飞机爆炸的效果。
代码大致是这样子,我并不是代码的第一手。
边栏推荐
猜你喜欢
随机推荐
Thinking of data storage scheme based on sub database and sub table
Municipalités locales - importations, exportations et balance commerciale (2000 - 2020)
Redis learning notes
Tool classes encapsulated based on redistemplate
What's the use of regular ^ $
SerializationException: Could not read JSON: Unrecognized token “xxx“
conda 常用功能记录
go Redis 管道(pipeline)运用
LeetCode83 && LeetCode82: 删除排序链表中的重复元素
Go zero micro service practical series (III. API definition and table structure design)
获取 (对象数组 / 数组) 的(最小 / 最大值)
MySQL学习之MVCC多版本并发控制
The request was rejected because the URL contained a potentially malicious String “//“
推荐系统-协同过滤在Spark中的实现
MongoDB的使用MongoTemplate操作增删改查,分页,排序,聚合(含内嵌数据),文件上传下载
前辈的前后台分离介绍
Leetcode-12: integer to Roman numeral
SerializationException: Could not read JSON: Unrecognized token “xxx“
OpenGL: dynamically modify vbo/ebo
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过