html飞机向上发射子弹,HTML5飞机大战

飞机大战

你的浏览器不支持canvas画布元素,请更新浏览器获得演示

飞机大战
分数:0分

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

document.addEventListener("keydown",onkeydown);

var Plan = function (image,x,y,n) {

this.image = image;

this.x = x;

this.y = y;

this.originX = x;

this.originY = y;

this.width = image.height;

this.isCaught = false;

this.frm = 0;

this.dis = 0;

this.n = n;

};

Plan.prototype.getCaught = function(bool){

this.isCaught = bool;

if (bool == false){

this.orignx = 0;

this.origny = this.y;

}

};

Plan.prototype.testPoint = function(x,y){

var betweenX = (x >= this.x) && (x <= this.x + this.width);

var betweenY = (y >= this.y) && (y <= this.y + this.height);

return betweenX && betweenY;

};

Plan.prototype.move = function(dx,dy){

this.x += dx;

this.y += dy;

};

Plan.prototype.Y = function(){

return this.y;

};

//不断下移飞机

Plan.prototype.draw = function(ctx){

ctx.save();

ctx.translate(this.x,this.y);

ctx.drawImage(this.image,this.frm*this.width,0,this.width,this.height,0,0,this.width,this.height);

ctx.restore();

this.y++;

this.x = this.orignx + 20 * Math.sin(Math.PI / 100*this.y);

this.dis++;

if(this.dis >= 3){//3帧换图

this.dis = 0;

this.frm++;

if(this.frm >= this.n) this.frm = 0;

}

};

//原地不动画飞机

Plan.prototype.draw2 = function(ctx){

ctx.save();

ctx.translate(this.x,this.y);

ctx.drawImage(this.image,this.frm*this.width,0,this.width,this.height,0,0,this.width,this.height);

ctx.restore();

this.dis++;

if(this.dis >= 3){//3帧换图

this.dis = 0;

this.frm++;

if(this.frm >= this.n) this.frm=0;

}

};

//检测飞机碰撞

//将所有子弹对象的矩形区域与敌机对象的矩形区域逐一检测,如果重叠则说明子弹与敌机的碰撞

Plan.prototype.hitTestObject=function(planobj){

if(iscolliding(this.x,this.y,this.width,this.height,

planobj.x,planobj.y,planobj.width,planobj.height))

//发生碰撞

return true;

else

return false;

}

function isColliding(ax,ay,aw,ah,bx,by,bw,bh)

{

if(ay > by + bh || by > ay + ah

|| ax > bx + bw || bx > ax + aw)

return false;

else

return true;

}

//子弹类

var Bullet = function(image,x,y){

this.image=image;

this.x = x;

this.y = y;

this.orignX = x;

this.orignY = y;

this.width = image.width/4;

this.height = image.height;

this.isCaught = false;

this.frm = 0;

this.dis = 0;

}

Bullet.prototype.testPoint = function(x,y){

var betweenX = (x >= this.x) && (x <= this.x + this.width);//

var betweenY = (y >= this.y) && (y <= this.y + this.height);

return betweenX && betweenY;

};

Bullet.prototype.move=function(dx,dy){

this.x += dx;

this.y += dy;

};

Bullet.prototype.Y = function(){

return this.y;

};

Bullet.prototype.draw = function(ctx){

ctx.save();

ctx.translate(this.x,this.y);

ctx.drawImage(this.image,this.frm*this.width,0,this.width,this.height,0,0,this.width,this.height);

ctx.restore();

this.y--;

this.dis++;

if(this.dis >= 10){//10帧换图

this.dis = 0;

this.frm++;

if(this.frm >= 4) this.frm = 0;

}

};

Bullet.prototype.hitTestObject = function(planobj){

if(isColliding(this.x,this.y,this.width,this.height,

planobj.x,planobj.y,planobj.width,planobj.height))//发生碰撞

return true;

else

return false;

}

//爆炸动画

var Bomb=function(image,x,y){

this.image = image;

this.x = x;

this.y = y;

this.width = image.width/6;

this.height = image.height;

this.frm = 0;

this.dis = 0;

};

Bomb.prototype.draw2 = function(ctx){

ctx.save();

ctx.translate(this.x,this.y);

if(this.frm >= 6) return ;

ctx.drawImage(this.image,this.frm*this.width,0,this.width,this.height,0,0,this.width,this.height);

ctx.restore();

this.dis++;

if(this.dis >= 10){//10帧换图

this.dis = 0;

this.frm++;

}

};

var plan1,plan2,plan3,plan4,caughtplan = null;

var isClick = false;

var mouseX,mouseY,preX,preY;

var plans=[];

var bullets=[];

var bombs=[];

var score=0;

var overflag=false;//游戏是否结束,true为结束

var myplane;//己方飞机

var image = new Image();

var image2 = new Image();

var image3 = new Image();

var image4 = new Image();

var image5 = new Image();

var bakground = new Image();

bakground.src='map_0.png';

image.src='plan.png';//自己飞机图片

image.οnlοad=function(){

};

image2.src='bomb.png';//爆炸图片

image2.οnlοad=function(){

}

image3.src='enemy.png';//敌机图片

image3.οnlοad=function(){

myplane=new Plan(image,300*Math.random(),400,6);

plan_interval=setInterval(function(){

plans.push(new Plan(image3,300*Math.random(),20*Math.random(),2));

},3000);//3秒产生1架敌机

setInterval(function(){

context.clearRect(0,0,320,480);

context.drawImage(bakground,0,0);

//画自己方飞机

if(!overflag)//游戏没有结束

myplane.draw2(context);//原地不动

//画敌人飞机

for(var i=plans.length - 1; i >= 0; i--){

if (plans[i].Y() > 400)

plans.splice(i,1);//删除敌机

else

plans[i].draw(context);

}

//画子弹

for (var i = bullets.length - 1; i >= 0; i--){

if (bullets[i].Y() < 100)

bullets.splice(i,1);//删除子弹

else

bullets[i].draw(context);

}

//碰撞检测

//判断敌机碰到玩家自己飞机

for (var i = plans.length - 1; i >= 0; i--){

e1 = plans[i];

if(e1!=null && myplane!=null && myplane.hitTestObject(e1))

{

clearInterval(plan_interval);//清除定时器,不再产生敌机

plans.splice(i,1);//删除敌机

bombs.push(new Bomb(image2, myplane.x, myplane.y));

message_txt.innerHTML='敌机碰到玩家自己飞机,游戏结束';

overflag=true;

}

}

//判断子弹碰到敌机

for(var j = bullets.length - 1; j >= 0; j--){

var b1=bullets[j];

for(var i = plans.length - 1;i >= 0; i--){

e1 = plans[i];

if (e1!=null && b1!=null && b1.hitTestObject(e1))//击中敌机

{

plans.splice(i,1);//删除敌机

bullets.splice(i,1);//删除此颗子弹

bombs.push(new Bomb(image2,b1.x,b1.y-36));

message_txt.innerHTML='敌机被击中,加20分';

score+=20;

score_txt.innerHTML='分数:'+score+'分';

}

}

}

//画炸弹

for (var i = bombs.length - 1;i >= 0; i--){

if (bombs[i].frm >= 6)

bombs.splice(i,1);//删除子弹

else

bombs[i].draw2(context);

}

},1000/60);

};

image4.src='bullet.png';//子弹图片

image4.οnlοad=function(){

};

function onKeydown(e){

if(e.keyCode==32){//空格

//发射子弹

bullets.push(new Bullet(image4,myplane.x,myplane.y-36));

}else if(e.keyCode==37){//向左

myplane.move(-10,0);

}else if(e.keyCode==39){//向右

myplane.move(10,0);

}else if(e.keyCode==38){//向上

myplane.move(0,-10);

}else if(e.keyCode==40){//向下

myplane.move(0,10);

}

}

```

来源:oschina

链接:https://my.oschina.net/u/4460892/blog/3209467

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Python 中编写飞机大战游戏时,可以使用 Pygame 库来实现自动发射子弹的功能。具体实现步骤如下: 1. 定义一个子弹类,包括子弹的位置、速度、图片等属性。 2. 在主循环中,检测玩家是否按下了发射子弹的键,如果是,则创建一个新的子弹对象,并将其添加到子弹列表中。 3. 在主循环中,遍历子弹列表,将每个子弹对象的位置向上移动一定距离,并且判断子弹是否超出了屏幕范围,如果是,则从子弹列表中删除该子弹对象。 下面是一个简单的示例代码,可以帮助你更好地理解如何实现自动发射子弹的功能: ```python import pygame import random # 定义子弹类 class Bullet(pygame.sprite.Sprite): def __init__(self, position): super().__init__() self.image = pygame.image.load('bullet.png') self.rect = self.image.get_rect() self.rect.center = position self.speed = -10 def update(self): self.rect.move_ip(0, self.speed) if self.rect.bottom < 0: self.kill() # 初始化 Pygame pygame.init() # 设置游戏窗口 screen_width, screen_height = 600, 800 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('飞机大战') # 加载玩家飞机图片 player_image = pygame.image.load('player.png') player_rect = player_image.get_rect() player_rect.centerx = screen_width // 2 player_rect.bottom = screen_height - 10 # 定义子弹列表 bullet_list = pygame.sprite.Group() # 设置游戏帧率 clock = pygame.time.Clock() FPS = 60 # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: # 创建新的子弹对象,并添加到子弹列表中 bullet = Bullet(player_rect.midtop) bullet_list.add(bullet) # 移动玩家飞机 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_rect.move_ip(-5, 0) if keys[pygame.K_RIGHT]: player_rect.move_ip(5, 0) if keys[pygame.K_UP]: player_rect.move_ip(0, -5) if keys[pygame.K_DOWN]: player_rect.move_ip(0, 5) # 绘制游戏场景 screen.fill((255, 255, 255)) screen.blit(player_image, player_rect) # 更新子弹位置 bullet_list.update() # 绘制子弹 bullet_list.draw(screen) # 刷新屏幕 pygame.display.update() # 控制游戏帧率 clock.tick(FPS) ``` 在这个示例代码中,我们定义了 `Bullet` 类来表示子弹,包括子弹的图片、位置、速度等属性。在主循环中,我们检测玩家是否按下了空格键,如果是,则创建一个新的子弹对象,并将其添加到子弹列表中。每个子弹对象都会自动向上移动一定距离,并且超出屏幕范围时会被从子弹列表中删除。最后,我们使用 Pygame 提供的 `update()` 和 `draw()` 方法来更新和绘制子弹的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值