鱼C论坛

 找回密码
 立即注册
楼主: jf050108

[作品展示] pygame 打飞机 游戏

  [复制链接]
发表于 2019-2-1 16:00:51 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-1 16:25:50 | 显示全部楼层
这么快就能自己写游戏了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-6 12:45:55 | 显示全部楼层
不错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-7 15:56:47 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-20 18:57:04 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-20 23:27:05 | 显示全部楼层
打飞机素材这里有吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-21 14:01:28 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-8 12:46:01 | 显示全部楼层
打飞机游戏
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-9 15:49:34 | 显示全部楼层
谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-10 17:33:34 From FishC Mobile | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-10 20:39:54 | 显示全部楼层
66
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-10 20:41:25 | 显示全部楼层
源代码在哪?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-10 21:04:20 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-10 21:15:42 | 显示全部楼层
厉害啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-11 21:47:13 | 显示全部楼层
nice
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-11 22:38:34 | 显示全部楼层
玩一下看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-12 10:29:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-13 19:47:20 | 显示全部楼层
哎呦,不错呦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-13 20:02:18 | 显示全部楼层
看看大佬的作品
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-13 20:16:08 | 显示全部楼层
顺便公布一下web的代码:

  1. <!doctype html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title>飞机大战</title>
  6.         </head>

  7.         <body style="background-color: black">
  8.                 <div>
  9.                         <img align="left"  src="images/hero2.png"/>
  10.                         <img align="right" src="images/hero2.png"/>
  11.                         <h1 style="text-align: center;color:blue; ">飞机大战</h1>
  12.                          <img align="left" src="images/ufo1.png">
  13.                         <img align="right" src="images/ufo2.png"/>
  14.                         <hr/>
  15.                         <br/>
  16.                         <div id="stage" style="margin: 0 auto; width: 480px; height: 650px; text-align: center; vertical-align: middle" >
  17.                         <canvas id="canvas" style="border:0px solid red;" width="480" height="650">
  18.                                 不支持画板对象
  19.                         </canvas>
  20.                 </div>
  21.                   
  22.                 </div>       
  23.                
  24.                
  25.                
  26.                
  27.                
  28.                 <script>
  29.                 //飞机大战
  30.                 /*
  31.                 调整生命                112——112
  32.                 控制生产对象         184——213  
  33.                 发射子弹(hero) 366——383   
  34.                 设置画板                10——11
  35.                 画角色                    230——244
  36.                 角色移动                216——227
  37.                 创建角色                34——105                   
  38.                  */
  39.                        
  40.                        
  41.                         var canvas = document.getElementById('canvas');
  42.                         var ctx = canvas.getContext('2d');

  43.                         var background = new Image();
  44.                         background.src = "images/background.png";

  45.                         var sky = new Sky();
  46.         //导入Enemy1角色
  47.                         var e1 = [];
  48.                         e1[0] = new Image();
  49.                         e1[0].src = "images/enemy1.png";
  50.                         e1[1] = new Image();
  51.                         e1[1].src = "images/enemy1_down1.png";
  52.                         e1[2] = new Image();
  53.                         e1[2].src = "images/enemy1_down2.png";
  54.                         e1[3] = new Image();
  55.                         e1[3].src = "images/enemy1_down3.png";
  56.                         e1[4] = new Image();
  57.                         e1[4].src = "images/enemy1_down4.png";
  58.         //导入Enemy2角色
  59.                         var e2 = [];
  60.                         e2[0] = new Image();
  61.                         e2[0].src = "images/enemy2.png";
  62.                         e2[1] = new Image();
  63.                         e2[1].src = "images/enemy2_down1.png";
  64.                         e2[2] = new Image();
  65.                         e2[2].src = "images/enemy2_down2.png";
  66.                         e2[3] = new Image();
  67.                         e2[3].src = "images/enemy2_down3.png";
  68.                         e2[4] = new Image();
  69.                         e2[4].src = "images/enemy2_down4.png";
  70.         
  71.                         var e3 = [];
  72.                         e3[0] = new Image();
  73.                         e3[0].src = "images/enemy3_n1.png";
  74.                         e3[1] = new Image();
  75.                         e3[1].src = "images/enemy3_n2.png";
  76.                         e3[2] = new Image();
  77.                         e3[2].src = "images/enemy3_down1.png";
  78.                         e3[3] = new Image();
  79.                         e3[3].src = "images/enemy3_down2.png";
  80.                         e3[4] = new Image();
  81.                         e3[4].src = "images/enemy3_down3.png";
  82.                         e3[5] = new Image();
  83.                         e3[5].src = "images/enemy3_down4.png";
  84.                         e3[6] = new Image();
  85.                         e3[6].src = "images/enemy3_down5.png";
  86.                         e3[7] = new Image();
  87.                         e3[7].src = "images/enemy3_down6.png";

  88.                         var h = [];
  89.                         h[0] = new Image();
  90.                         h[0].src = "images/hero1.png";
  91.                         h[1] = new Image();
  92.                         h[1].src = "images/hero2.png";
  93.                         h[2] = new Image();
  94.                         h[2].src = "images/hero_blowup_n1.png";
  95.                         h[3] = new Image();
  96.                         h[3].src = "images/hero_blowup_n2.png";
  97.                         h[4] = new Image();
  98.                         h[4].src = "images/hero_blowup_n3.png";
  99.                         h[5] = new Image();
  100.                         h[5].src = "images/hero_blowup_n4.png";

  101.                         var copyright = new Image();
  102.                         copyright.src = "images/shoot_copyright.png";

  103.                         var pause = new Image();
  104.                         pause.src = "images/game_pause_nor.png";

  105.                         var b = [];
  106.                         b[0] = new Image();
  107.                         b[0].src = "images/bullet1.png";
  108.                         b[1] = new Image();
  109.                         b[1].src = "images/bullet2.png";
  110.                        
  111.                         var u=[]
  112.                         u[0]=new Image();
  113.                         u[0].src="images/ufo1.png";
  114.                        
  115.                         var enemies = [];
  116.                         var hero = new Hero(0, 0, 99, 124, 1, h, 2);
  117.                         var bullets = [];
  118.                         var score= 0;
  119.                         var ufo=[];
  120.                          
  121.                         var heroes = 3;

  122.                         var START = 1;
  123.                         var RUNNING = 2;
  124.                         var PAUSE = 3;
  125.                         var GAME_OVER = 4;
  126.                         var state = START;
  127.             var s_c=true
  128.             
  129.                         setInterval(function() {
  130.                                 controlState(ctx);
  131.                         }, 10);
  132.                        
  133.                         function controlState(ctx) {
  134.                                 switch(state) {
  135.                                         case START:
  136.                                                 sky.paint(ctx);
  137.                                                 sky.step();
  138.                                                 var x = 20;
  139.                                                 var y = 130;
  140.                                                 ctx.drawImage(copyright, x, y);
  141.                                                 break;
  142.                                         case RUNNING:
  143.                                                 paintComponent(ctx);
  144.                                                 sky.step();
  145.                                                 componentStep();
  146.                                                 componentEnter();
  147.                                                 hero.shoot();
  148.                                                 checkHit();
  149.                                                 deleteComponent();
  150.                                                 break;
  151.                                         case PAUSE:
  152.                                                 paintComponent(ctx);
  153.                                                 sky.step();
  154.                                                 ctx.drawImage(pause, 0, 0);
  155.                                                 break;
  156.                                         case GAME_OVER:
  157.                                                 paintComponent(ctx);
  158.                                                 sky.step();
  159.                                                 var x = 480 / 2 - 245 / 2;
  160.                                                 var y = 852 * (1 - 0.6);
  161.                                                 ctx.font = "40px 微软雅黑";
  162.                                                 ctx.fillText("GAME OVER", x, y);
  163.                                                 if(s_c){
  164.                                                          alert("YOUSCORE:"+score);
  165.                                                          s_c=false;
  166.                                                 }
  167.                                                      
  168.                                                 break;
  169.                                 }
  170.                         }
  171.                        
  172.                         canvas.onclick = function() {
  173.                                 if (state == START) {
  174.                                         state = RUNNING;
  175.                                 }
  176.                         }
  177.                         canvas.onmouseout = function() {
  178.                                 if (state == RUNNING) {
  179.                                         state = PAUSE;
  180.                                 }
  181.                         }
  182.                         canvas.onmouseover = function() {
  183.                                 if (state == PAUSE) {
  184.                                         state = RUNNING;
  185.                                 }
  186.                         }
  187.                         function isActionTime(lastTime, interval) {
  188.                                 if (lastTime == 0) {
  189.                                         return true;
  190.                                 }
  191.                                 var currentTime = new Date().getTime();
  192.                                 return currentTime - lastTime >= interval;
  193.                         }

  194.                         var lastTime = 0;
  195.                         var interval = 400;
  196.                         function componentEnter() {
  197.                                 if (! isActionTime(lastTime, interval)) {
  198.                                         return;
  199.                                 }
  200.                                 lastTime = new Date().getTime();
  201.                                 var n = parseInt(Math.random() * 10);
  202.                                 switch(n) {
  203.                                         case 0:
  204.                                         case 1:
  205.                                         case 2:
  206.                                         case 3:
  207.                                         case 4:
  208.                                         case 5:
  209.                                         case 6:
  210.                                         case 7:
  211.                                                 enemies[enemies.length] = new Enemy(0, -51, 57, 51, 1, 1, 1, e1, 1);
  212.                                                 break;
  213.                                         case 8:
  214.                                                 enemies[enemies.length] = new Enemy(0, -95, 69, 95, 2, 3, 5, e2, 1);
  215.                                                 break;
  216.                                         case 9:
  217.                                                 if (enemies[0] == undefined || enemies[0].type != 3) {
  218.                                                         enemies.splice(0, 0, new Enemy(0, -258, 169, 258, 3, 20, 20, e3, 2));
  219.                                                 }
  220.                                 }
  221.                                 var random=parseInt(Math.random()*15);
  222.                                 if(random==1){
  223.                                         ufo[ufo.length]=new Ufo(0,0,58,88,1,u,1);
  224.                                 }
  225.                         }

  226.                         function componentStep() {
  227.                                 sky.step();
  228.                                 for (var i = 0; i < enemies.length; i++) {
  229.                                         enemies[i].step();
  230.                                 }
  231.                                 for (var i = 0; i < bullets.length; i++) {
  232.                                         bullets[i].step();
  233.                                 }
  234.                                 hero.step();
  235.                                 for(var i = 0;i < ufo.length;i++){
  236.                                         ufo[i].step();
  237.                                 }
  238.                         }

  239.                         function paintComponent(ctx) {
  240.                                 sky.paint(ctx);
  241.                                 for (var i = 0; i < enemies.length; i++) {
  242.                                         enemies[i].paint(ctx);
  243.                                 }
  244.                                 for (var i = 0; i < bullets.length; i++) {
  245.                                         bullets[i].paint(ctx);
  246.                                 }
  247.                                 hero.paint(ctx);
  248.                                 for(var i = 0;i < ufo.length;i++){
  249.                                         ufo[i].paint(ctx);
  250.                                 }
  251.                                 ctx.font = "20px 微软雅黑";
  252.                                 ctx.fillText("SCORE:" + score, 10, 20);
  253.                                 ctx.fillText("LIFE:" + heroes, 400, 20);
  254.                         }

  255.                         function Sky() {
  256.                                 this.img = background;
  257.                                 this.width = 480;
  258.                                 this.height = 852;
  259.                                 this.x1 = 0;
  260.                                 this.y1 = 0;
  261.                                 this.x2 = 0;
  262.                                 this.y2 = -this.height;
  263.                                 this.interval = 400;
  264.                                 this.lastTime = 0;
  265.                                 this.paint = function(ctx) {
  266.                                         ctx.drawImage(this.img, this.x1, this.y1);
  267.                                         ctx.drawImage(this.img, this.x2, this.y2);
  268.                                 }
  269.                                 this.step = function() {
  270.                                         if (! isActionTime(this.lastTime, this.interval)) {
  271.                                                 return;
  272.                                         }
  273.                                         this.lastTime = new Date().getTime();
  274.                                         this.y1 = this.y1 + 1;
  275.                                         this.y2 = this.y2 + 1;
  276.                                         if (this.y1 > this.height) {
  277.                                                 this.y1 = -this.height;
  278.                                         }
  279.                                         if (this.y2 > this.height) {
  280.                                                 this.y2 = -this.height;
  281.                                         }
  282.                                 }
  283.                         }

  284.                         function getPointOnCanvas(x, y) {
  285.                                 var bbox = canvas.getBoundingClientRect();
  286.                                 return {
  287.                                         x : x - bbox.left,
  288.                                         y : y - bbox.top
  289.                                 };
  290.                         }


  291.                         canvas.onmousemove = function(e) {
  292.                                 if (state == RUNNING) {
  293.                                         var mpoint = getPointOnCanvas(e.x, e.y);
  294.                                         hero.x = mpoint.x - hero.width / 2;
  295.                                         hero.y = mpoint.y - hero.height / 2;
  296.                                 }
  297.                         }
  298.                         /*飞行函数(x坐标,y坐标,宽度,长度,生命,图片帧,图片帧计数)*/
  299.                         function FlyingObject(x, y, width, height, life, frames, baseFrameCount) {
  300.                                 this.x = x;
  301.                                 this.y = y;
  302.                                 this.width = width;
  303.                                 this.height = height;
  304.                                 this.life = life;
  305.                                 this.interval = 10;
  306.                                 this.lastTime = 0;
  307.                                 this.down = false;
  308.                                 this.canDelete = false;
  309.                                 this.frames = frames;
  310.                                 this.img = this.frames[0];
  311.                                 this.frameIndex = 0;
  312.                                 this.frameCount = baseFrameCount;
  313.                                 this.paint = function(ctx) {
  314.                                         ctx.drawImage(this.img, this.x, this.y);
  315.                                 }
  316.                                 this.step = function() {
  317.                                         if (! isActionTime(this.lastTime, this.interval)) {
  318.                                                 return;
  319.                                         }
  320.                                         this.lastTime = new Date().getTime();
  321.                                         if (this.down) {
  322.                                                 if (this.frameIndex == frames.length) {
  323.                                                         this.canDelete = true;
  324.                                                 } else {
  325.                                                         this.img = this.frames[this.frameIndex];
  326.                                                         this.frameIndex++;
  327.                                                 }
  328.                                         } else {
  329.                                                 this.move();
  330.                                                 this.img = this.frames[this.frameIndex % this.frameCount];
  331.                                                 this.frameIndex++;
  332.                                         }
  333.                                 }
  334.                                 this.move = function() {
  335.                                         this.y++;
  336.                                 }
  337.                                 this.hit = function(component) {
  338.                                         var c = component;
  339.                                         return c.x > this.x - c.width && c.x < this.x + this.width && c.y > this.y - c.height && c.y < this.y + this.height;
  340.                                 }
  341.                                 this.bang = function() {
  342.                                         this.life--;
  343.                                         if (this.life == 0) {
  344.                                                 this.down = true;
  345.                                                 if (this.score) {
  346.                                                         score = score + this.score;
  347.                                                 }
  348.                                                 this.frameIndex = this.frameCount;
  349.                                         }
  350.                                 }
  351.                                 this.outOfBounds = function() {
  352.                                         return this.y >= 852;
  353.                                 }
  354.                         }

  355.                         function Enemy(x, y, width, height, type, life, score, frames, baseFrameCount) {
  356.                                 FlyingObject.call(this, x, y, width, height, life, frames, baseFrameCount);
  357.                                 this.x = Math.random() * (480 - this.width);
  358.                                 this.y = -this.height;
  359.                                 this.score = score;
  360.                                 this.type = type;
  361.                         }

  362.                         function Hero(x, y, width, height, life, frames, baseFrameCount) {
  363.                                 FlyingObject.call(this, x, y, width, height, life, frames, baseFrameCount);
  364.                                 this.x = 480 / 2 - this.width / 2;
  365.                                 this.y = 650 - this.height - 30;
  366.                                 this.shootInterval = 300;
  367.                                 this.shootLastTime = 0;
  368.                                 this.multipleFire = false;
  369.                                 this.power = 0;
  370.                                 this.shoot = function() {
  371.                                         if (!isActionTime(this.shootLastTime, this.shootInterval)) {
  372.                                                 return;
  373.                                         }
  374.                                         this.shootLastTime = new Date().getTime();
  375.                                         if(this.multipleFire&&this.power!=0){
  376.                                                
  377.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b,1, 1);
  378.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b,1, 2);
  379.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b,1, 3);
  380.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b,1, 4);
  381.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b,1, 5);
  382.                                                 this.power--;
  383.                                         }else{
  384.                                                 bullets[bullets.length] = new Bullet(this.x + 45, this.y, 9, 21,1, b, 1,3);
  385.                                         }
  386.                                        
  387.                                         }
  388.                                 this.move = function() {

  389.                                 }
  390.                         }

  391.                         function Bullet(x, y, width, height, life, frames, baseFrameCount,type) {
  392.                                 FlyingObject.call(this, x, y, width, height, life, frames, baseFrameCount);
  393.                                 this.move = function() {
  394.                                         /*子弹的发射速度(需要将减去的值+1才是真正的移动值)*/this.y -= 10;
  395.                                         /*判断子弹种类*/switch(type){
  396.                                                 case 1:
  397.                                                 /*往左偏*/
  398.                                                 this.x-=1;
  399.                                                 break;
  400.                                                 case 2:
  401.                                                 /*稍往左偏*/
  402.                                                 this.x-=0.45;
  403.                                                 break;                                               
  404.                                                 case 3:
  405.                                                 /*正中心*/                                               
  406.                                                 break;
  407.                                                 case 4:
  408.                                                 /*稍往右偏*/
  409.                                                 this.x+=0.45;
  410.                                         break;               
  411.                                                 case 5:
  412.                                                 /*往右偏*/
  413.                                                 this.x+=1;
  414.                                         }
  415.                                 }
  416.                                 this.outOfBounds = function() {
  417.                                         return this.y < -this.height;
  418.                                 }
  419.                         }
  420.                        
  421.                         function Ufo(x,y,width,height,life,frames, baseFrameCount){
  422.                                 FlyingObject.call(this,x,y,width,height,life,frames, baseFrameCount);
  423.                                 this.x=Math.random()*(480-this.width);
  424.                                 this.y=-this.height;
  425.                         }
  426.                        
  427.                         function reload(number){
  428.                                 hero.power += number;
  429.                                 if(hero.power>0){
  430.                                         hero.multipleFire = true;
  431.                                 }
  432.                         }

  433.                         function checkHit() {
  434.                                 for(var i = 0;i < ufo.length;i++){
  435.                                         if(ufo[i].hit(hero)){
  436.                                                 ufo[i].bang();
  437.                                                 reload(5);
  438.                                         }
  439.                                 }
  440.                                 for (var i = 0; i < enemies.length; i++) {
  441.                                         var enemy = enemies[i];
  442.                                         if (enemy.canDelete || enemy.down) {
  443.                                                 continue;
  444.                                         }
  445.                                         for (var j = 0; j < bullets.length; j++) {
  446.                                                 var bullet = bullets[j];
  447.                                                 if (bullet.canDelete || bullet.down) {
  448.                                                         continue;
  449.                                                 }
  450.                                                 if (enemy.hit(bullet)) {
  451.                                                         enemy.bang();
  452.                                                         bullet.bang();
  453.                                                 }
  454.                                         }
  455.                                         if (enemy.hit(hero)) {
  456.                                                 enemy.bang();
  457.                                                 hero.bang();
  458.                                         }
  459.                                 }
  460.                         }

  461.                         function deleteComponent() {
  462.                                 var ary = [];
  463.                                 var idx = 0;
  464.                                 for (var i = 0; i < enemies.length; i++) {
  465.                                         if (!(enemies[i].canDelete || enemies[i].outOfBounds())) {
  466.                                                 ary[idx] = enemies[i];
  467.                                                 idx++;
  468.                                         }
  469.                                 }
  470.                                 enemies = ary;
  471.                                 ary = [];
  472.                                 idx = 0;
  473.                                 for (var i = 0; i < bullets.length; i++) {
  474.                                         if (!(bullets[i].canDelete || bullets[i].outOfBounds())) {
  475.                                                 ary[idx] = bullets[i];
  476.                                                 idx++;
  477.                                         }
  478.                                 }
  479.                                 bullets = ary;
  480.                                 ary = [];
  481.                                 idx = 0;
  482.                                 for(var i = 0;i < ufo.length;i++){
  483.                                         if(!(ufo[i].canDelete||ufo[i].outOfBounds())){
  484.                                                 ary[idx] = ufo[i];
  485.                                                 idx++;
  486.                                         }
  487.                                 }
  488.                                 ufo = ary;
  489.                                 if (hero.canDelete) {
  490.                                         heroes--;
  491.                                         if (heroes == 0) {
  492.                                                 state = GAME_OVER;
  493.                                         } else {
  494.                                                 hero = new Hero(0, 0, 99, 124, 4, h, 2);
  495.                                         }
  496.                                 }
  497.                         }
  498.                 </script>
  499.         </body>
  500. </html>

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 22:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表