java魂斗罗_java 魂斗罗
1.課設課題
組長: 沈宇濤
組員: 唐洪俊, 蔡豐駿
1.1 基于java swing開發的魂斗羅
可實現的功能: 人物的移動射擊以及跳躍
人物武器的切換
人物的死亡與重生
敵人的行為設定
1.2 UML設計圖
1.3 程序運行展示
uploading-image-813493.png
2.代碼展示
2.1 重點代碼展示
難度選擇模塊
獨立一個窗口控制難度, 通過線程傳參啟動frame窗口
該部分代碼運行結果如下:
圖像繪制模塊
繪制相關圖片
鍵盤監聽模塊
根據玩家操控角色的操作, 判斷是否做出相應的指令
角色的移動
private void heroMove()
{
if ((this.hero.state != 4) && (this.hero.state != 5))
if ((!this.hero.jumping) && (!this.jumpDown) && (!this.leftDown) && (!this.rightDown) && (!this.upDown) && (!this.downDown))
{
this.hero.state = 0;
if (this.hero.towardsLeft)
{
this.hero.direction = 6;
}
else if (this.hero.towardsRight)
{
this.hero.direction = 2;
}
}
else if (this.hero.jumping)
{
if (this.upDown)
{
this.hero.direction = 0;
this.hero.state = 2;
if ((this.leftDown) && (!this.showBossScene))
{
this.hero.direction = 7;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
else if ((this.rightDown) && (!this.showBossScene)) {
this.hero.direction = 1;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
}
else if (this.downDown)
{
this.hero.direction = 4;
this.hero.state = 2;
if (this.leftDown)
{
this.hero.direction = 5;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
else if ((this.rightDown) && (!this.showBossScene))
{
this.hero.direction = 3;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
}
else if ((this.leftDown) && (!this.showBossScene))
{
this.hero.direction = 6;
this.hero.state = 2;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
else if ((this.rightDown) && (!this.showBossScene)) {
this.hero.direction = 2;
this.hero.state = 2;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
}
else if ((this.leftDown) && (!this.showBossScene))
{
if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F)))
{
this.hero.direction = 6;
this.hero.state = 2;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
this.hero.jumping = true;
this.hero.jumpFinished = false;
}
else if (this.upDown) {
this.hero.state = 1;
this.hero.direction = 7;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
else if (this.downDown) {
this.hero.direction = 5;
this.hero.state = 1;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
else
{
this.hero.state = 1;
this.hero.direction = 6;
this.hero.towardsLeft = true;
this.hero.towardsRight = false;
if (this.hero.position.x > 8.0F + this.mapPosition)
this.hero.position.x -= this.heroSpeed;
}
}
else if ((this.rightDown) && (!this.showBossScene))
{
if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
this.hero.direction = 2;
this.hero.state = 2;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
this.hero.jumping = true;
this.hero.jumpFinished = false;
}
else if (this.upDown) {
this.hero.direction = 1;
this.hero.state = 1;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
else if (this.downDown)
{
this.hero.direction = 3;
this.hero.state = 1;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
else
{
this.hero.direction = 2;
this.hero.state = 1;
this.hero.towardsLeft = false;
this.hero.towardsRight = true;
moveForwardToRight();
}
}
else if (this.upDown) {
if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
this.hero.direction = 0;
this.hero.state = 2;
this.hero.jumpFinished = false;
this.hero.jumping = true;
}
else
{
this.hero.state = 0;
this.hero.direction = 0;
}
}
else if (this.downDown)
{
if (this.jumpDown)
{
this.hero.state = 1;
}
else
{
this.hero.state = 3;
if (this.hero.direction == 3) {
this.hero.direction = 2;
}
else if (this.hero.direction == 5) {
this.hero.direction = 6;
}
}
}
else if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F)))
{
this.hero.state = 2;
this.hero.jumping = true;
this.hero.jumpFinished = false;
}
}
通過對角色當前狀態的屬性進行判定, 以此為依據來控制角色的移動. 以及角色是否處于可站立的位置
地圖移動模塊(只可向右移動)
判定角色在屏幕中所處的位置, 當角色處于屏幕中央位置及向右移動時, 地圖隨之向右移動.
角色死亡后的出生點判斷
角色死亡后將會出生在屏幕最左端的可站立的方塊上
需要對角色位置進行判定的是是否通過移動方塊, 若未通過, 佳能復活在移動方塊左端的方塊之上
角色的死亡判定(碰撞檢測)
private void heroDeathCheck()
{
if ((this.hero.state != 4) && (this.hero.state != 5))
{
for (int i = 0; i < this.enemyBullets.size(); i++)
{
Bullet b = (Bullet)this.enemyBullets.get(i);
if ((b.position.x > this.hero.position.x - this.hero.width / 2 / 3) && (b.position.x < this.hero.position.x + this.hero.width / 2 / 3) &&
(b.position.y > this.hero.position.y - this.hero.height / 3) && (b.position.y < this.hero.position.y) &&
(this.hero.visible))
{
this.hero.state = 4;
this.hero.deathEventType = 0;
this.enemyBullets.remove(i);
}
}
for (int i = 0; i < this.enemys.size(); i++)
{
SimpleEnemy e = (SimpleEnemy)this.enemys.get(i);
if ((e.state != 4) && (e.state != 5) &&
(this.hero.position.x > e.position.x - this.hero.width / 2 / 3 - 11.0F) && (this.hero.position.x < e.position.x + 11.0F + this.hero.width / 2 / 3) &&
(this.hero.position.y > e.position.y - 26.0F) && (this.hero.position.y < e.position.y + this.hero.height / 3)) {
if (this.hero.visible) {
this.hero.state = 4;
this.hero.deathEventType = 2;
}
else {
e.state = 4;
}
}
}
for (int i = 0; i < this.bossChildren.size(); i++)
{
BossChild e = (BossChild)this.bossChildren.get(i);
if ((e.state != 4) && (e.state != 5) &&
(this.hero.position.x > e.position.x - this.hero.width / 2 / 3 - 15.0F) && (this.hero.position.x < e.position.x + 15.0F + this.hero.width / 2 / 3) &&
(this.hero.position.y > e.position.y - 10.0F) && (this.hero.position.y < e.position.y + this.hero.height / 3) &&
(this.hero.visible)) {
this.hero.state = 4;
this.hero.deathEventType = 2;
}
}
}
}
判斷角色是否與子彈或敵人相撞, 或判定角色是否出界
可站立方塊的設定
private void initGrassLands()
{
FloatPoint[] points =
{
new FloatPoint(1.0F, 110.0F), new FloatPoint(5.0F, 142.0F), new FloatPoint(8.0F, 174.0F),
new FloatPoint(9.0F, 206.0F), new FloatPoint(11.0F, 174.0F), new FloatPoint(13.0F, 142.0F), new FloatPoint(18.0F, 206.0F),
new FloatPoint(19.0F, 158.0F), new FloatPoint(27.0F, 110.0F), new FloatPoint(36.0F, 110.0F), new FloatPoint(42.0F, 78.0F),
new FloatPoint(43.0F, 206.0F), new FloatPoint(46.0F, 160.0F), new FloatPoint(49.0F, 142.0F), new FloatPoint(53.0F, 206.0F),
new FloatPoint(57.0F, 110.0F), new FloatPoint(59.0F, 174.0F), new FloatPoint(62.0F, 174.0F), new FloatPoint(63.0F, 78.0F),
new FloatPoint(65.0F, 158.0F), new FloatPoint(67.0F, 142.0F), new FloatPoint(69.0F, 110.0F), new FloatPoint(72.0F, 142.0F),
new FloatPoint(72.0F, 206.0F), new FloatPoint(73.0F, 174.0F), new FloatPoint(76.0F, 110.0F), new FloatPoint(77.0F, 78.0F),
new FloatPoint(77.0F, 206.0F), new FloatPoint(78.0F, 158.0F), new FloatPoint(80.0F, 110.0F), new FloatPoint(81.0F, 142.0F),
new FloatPoint(84.0F, 206.0F), new FloatPoint(88.0F, 174.0F), new FloatPoint(91.0F, 142.0F), new FloatPoint(93.0F, 110.0F),
new FloatPoint(93.0F, 206.0F), new FloatPoint(94.0F, 158.0F), new FloatPoint(98.0F, 142.0F), new FloatPoint(99.0F, 174.0F),
new FloatPoint(24.0F, 110.0F), new FloatPoint(33.0F, 110.0F)
};
int[] lengths = { 22, 3, 1, 2, 1, 2, 2, 3, 5, 8, 16, 3, 2, 7, 6, 7, 2, 2, 5, 1, 3, 2, 2, 1, 3, 2, 2, 1, 1, 2, 5, 3, 2, 2, 5, 22, 4, 1, 1, 1, 1 };
for (int i = 0; i < points.length - 2; i++) {
GrassLand gl = new GrassLand(points[i], lengths[i]);
this.grassLands[i] = gl;
}
GrassLand movingGrassLand1 = new GrassLand(new FloatPoint(24.0F, 110.0F), 1);
GrassLand movingGrassLand2 = new GrassLand(new FloatPoint(33.0F, 110.0F), 1);
movingGrassLand1.movingDirection = 3;
movingGrassLand2.movingDirection = 3;
this.grassLands[(this.grassLands.length - 2)] = movingGrassLand1;
this.grassLands[(this.grassLands.length - 1)] = movingGrassLand2;
}
private void drawNPC(Graphics2D g)
{
this.grassLands[(this.grassLands.length - 2)].drawMovingGrassLand(this.hero, 23.4F, 25.799999F, (int)this.mapPosition, this.backgroundSizeOfWidth, this, g);
this.grassLands[(this.grassLands.length - 1)].drawMovingGrassLand(this.hero, 32.400002F, 34.799999F, (int)this.mapPosition, this.backgroundSizeOfWidth, this, g);
}
private boolean isGrassLand(float x, float y) {
if (this.hero.towardsRight) {
for (int i = 0; i < this.grassLands.length; i++) {
if ((x > this.grassLands[i].position.x * 32.0F - 7.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F + 3.0F) &&
(y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
return true;
}
}
else if (this.hero.towardsLeft) {
for (int i = 0; i < this.grassLands.length; i++) {
if ((x > this.grassLands[i].position.x * 32.0F - 7.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F + 1.0F) &&
(y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
return true;
}
}
else if ((!this.hero.towardsLeft) && (!this.hero.towardsRight)) {
for (int i = 0; i < this.grassLands.length; i++) {
if ((x > this.grassLands[i].position.x * 32.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F) &&
(y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
return true;
}
}
return false;
}
規定角色可以站立而不死亡的位置
3.代碼改進及任務的分配
3.1
在學習了借鑒的代碼之后, 我們對其進行了一定程度的優化, 增加了一些原來沒有的功能
1.增加了難度的設置, 讓玩家有了更多的選擇
2.使用了緩存的技術, 解決了閃屏的問題
3.增加了角色的死亡位置判定, 使其能在移動方塊左方出生
3.2小組任務分配
沈宇濤: Frame窗口, 繪制動畫, 地圖設定, weapon類
唐洪俊: 人物類設計, 難度調整, 解決Java運行時的閃屏問題
蔡豐駿: Bulet類, 鍵盤監聽, 素材繪制
4.課設總結
Java代碼的團隊合作不同于之前的c, 需要大家的配合, 規范的命名以及寫法, 讓我受益匪淺. 在這次課設中我鍛煉了我自己, 也知道了自己的很多不足, 在編寫的過程之中我們也遇到了很多困難, 在很多我不會的地方, 其他組員也對我伸出了援手. 在共同的努力之下完成了這次的課程設計.
總結
以上是生活随笔為你收集整理的java魂斗罗_java 魂斗罗的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python程序设计案例课堂第二篇_Py
- 下一篇: java 中的vector_详解Java